ADC PORTS

General discussion on mikroC.
Post Reply
Author
Message
shaltera
Posts: 7
Joined: 23 Feb 2015 13:34

ADC PORTS

#1 Post by shaltera » 04 Mar 2015 00:57

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);
}

User avatar
petar.timotijevic
mikroElektronika team
Posts: 1739
Joined: 19 Feb 2014 13:46
Location: Serbia
Contact:

Re: ADC PORTS

#2 Post by petar.timotijevic » 04 Mar 2015 15:19

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.

Code: Select all

...Your code here...
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

shaltera
Posts: 7
Joined: 23 Feb 2015 13:34

Re: ADC PORTS

#3 Post by shaltera » 04 Mar 2015 16:57

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.

User avatar
petar.timotijevic
mikroElektronika team
Posts: 1739
Joined: 19 Feb 2014 13:46
Location: Serbia
Contact:

Re: ADC PORTS

#4 Post by petar.timotijevic » 04 Mar 2015 17:20

Hi,

Please check in your code:

Code: Select all

ANSELA = 0x03;
TRISA.RA0 = 1;
TRISA.RA2 = 1;
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

shaltera
Posts: 7
Joined: 23 Feb 2015 13:34

Re: ADC PORTS

#5 Post by shaltera » 05 Mar 2015 09:34

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
}
}


User avatar
petar.timotijevic
mikroElektronika team
Posts: 1739
Joined: 19 Feb 2014 13:46
Location: Serbia
Contact:

Re: ADC PORTS

#6 Post by petar.timotijevic » 05 Mar 2015 10:54

Hi Shaltera,

Which MCU you use in this project?


Best regards,
Peter

shaltera
Posts: 7
Joined: 23 Feb 2015 13:34

Re: ADC PORTS

#7 Post by shaltera » 05 Mar 2015 11:04

Sorted,thanks
Last edited by shaltera on 05 Mar 2015 11:39, edited 1 time in total.

User avatar
petar.timotijevic
mikroElektronika team
Posts: 1739
Joined: 19 Feb 2014 13:46
Location: Serbia
Contact:

Re: ADC PORTS

#8 Post by petar.timotijevic » 05 Mar 2015 11:29

Hi,

For relay output please use LATB instead PORTB.

Project use button/taster on RA5?


Best regards,
Peter

Post Reply

Return to “mikroC General”