Hi, I'm new to PIC programming.I have easyPICv7 and MikroC. I have two current censors,one is connected to RA0 and second one is connected to RA1 of the PIC.I would like to read the current from both sensors.I'm really struggling to configure the ports.I found one example for current measurement works fine with one sensor when ADC_Value = ADC_Read(0); as soon I connect second one to PIC and use ADC_Value_1 = ADC_Read(1); measurement is completely incorrect.Please help!Thank you in advance
void Display(unsigned int num){
char temp[] = "I = 0.00 Amp";
temp[4] = num/1000 + 48;
temp[6] = (num/100)%10 + 48;
temp[7] = (num/10)%10 + 48;
LCD_Out(2, 3, temp);
}
void Display1(unsigned int num){
char temp[] = "I = 0.00 Amp";
temp[4] = num/1000 + 48;
temp[6] = (num/100)%10 + 48;
temp[7] = (num/10)%10 + 48;
LCD_Out(4, 4, temp);
}
char message[] = "Current reading";
unsigned int ADC_Value, ADC_Value_1, Factor;
unsigned long temp;
main(){
ANSELA = 0x03;
TRISA.RA0 = 1;
TRISA.RA2 = 1;
CM1CON0=0;
CM2CON0=0;
do{
// Read multiple samples for better accuracy
ADC_Value = ADC_Read(0);
ADC_Value = ADC_Value + ADC_Read(0);
ADC_Value = ADC_Value + ADC_Read(0);
ADC_Value = ADC_Value/3;
temp = (ADC_Value-512)*Factor ;
ADC_Value = temp/10;
Display(ADC_Value);
ADC_Value_1 = ADC_Read(1);
ADC_Value_1 = ADC_Value_1 + ADC_Read(1);
ADC_Value_1 = ADC_Value_1 + ADC_Read(1);
ADC_Value_1 = ADC_Value_1/3;
temp = (ADC_Value-512)*Factor ;
ADC_Value_1 = temp/10;
LCD_Out(3, 3, temp);
Display1(ADC_Value_1);
Delay_ms(1000); } while(1);
}
ADC PORTS
- petar.timotijevic
- mikroElektronika team
- Posts: 1739
- Joined: 19 Feb 2014 13:46
- Location: Serbia
- Contact:
Re: ADC PORTS
Hi Shaltera and welcome to the MikroE forum,
For posting the source code please use code tags, this will keep code format and will make better view on the code.
Please zip and attach project files.
Also you can find many existing ADC examples on our web site Libstock at www.libstock.com
mikroC Pro - ADC Library
http://www.mikroe.com/download/eng/docu ... ibrary.htm
Best regards,
Peter
For posting the source code please use code tags, this will keep code format and will make better view on the code.
Code: Select all
...Your code here...
Also you can find many existing ADC examples on our web site Libstock at www.libstock.com
mikroC Pro - ADC Library
http://www.mikroe.com/download/eng/docu ... ibrary.htm
Best regards,
Peter
Re: ADC PORTS
Peter, thanks for your reply.I have already looked at http://www.libstock.com/, and could not find any relevant example.I need to interface two analog sensors (ACS712).In http://www.libstock.com/ there is an example for ACS712 which is connected to RA0, working fine, but when I connected second sensor to RA1 did not work as you can see from the code I attached.
- petar.timotijevic
- mikroElektronika team
- Posts: 1739
- Joined: 19 Feb 2014 13:46
- Location: Serbia
- Contact:
Re: ADC PORTS
Hi,
Please check in your code:
With 0x03 set RA0 and RA1 as analog 0x03 is 0b00000011, in code later you read channel 0 and channel 2 instead channel 1.
Please which MCU you use? Zip and attach project files.
Best regards,
Peter
Please check in your code:
Code: Select all
ANSELA = 0x03;
TRISA.RA0 = 1;
TRISA.RA2 = 1;
Please which MCU you use? Zip and attach project files.
Best regards,
Peter
Re: ADC PORTS
I have managed to do it.However interfacing a relay to the design does not work.I have followed this example, but for some reason does not work in my design.Can someone help please
Code: Select all
void main() {
TRISB.F0 = 0; // set RB0 as output
while (1) // endless loop
{
PORTB.F0 = 1; //Turns ON relay
Delay_ms(5000); // 5S delay
PORTB.F0 = 0; //Turns OFF relay
Delay_ms(5000); //5S delay
}
}
- petar.timotijevic
- mikroElektronika team
- Posts: 1739
- Joined: 19 Feb 2014 13:46
- Location: Serbia
- Contact:
Re: ADC PORTS
Hi Shaltera,
Which MCU you use in this project?
Best regards,
Peter
Which MCU you use in this project?
Best regards,
Peter
- petar.timotijevic
- mikroElektronika team
- Posts: 1739
- Joined: 19 Feb 2014 13:46
- Location: Serbia
- Contact:
Re: ADC PORTS
Hi,
For relay output please use LATB instead PORTB.
Project use button/taster on RA5?
Best regards,
Peter
For relay output please use LATB instead PORTB.
Project use button/taster on RA5?
Best regards,
Peter