ADC with 16F877a to UART

Status
Not open for further replies.

varunme

Advanced Member level 3
Joined
Aug 10, 2011
Messages
741
Helped
17
Reputation
36
Reaction score
17
Trophy points
1,318
Activity points
5,764
My code for updating UART with ADC not working,

Code:
// char text[12];
long tlong;
int i;
unsigned int adc_rd;
char *text = "000.0000";

  void main(){
   CMCON  |= 0x07;            // turn off comparators
  ADCON1 |= 0x0C;            // Set AN2 channel pin as analog
  TRISA2_bit = 1;            //   input

  UART1_Init(9600);               // Initialize UART1 module at 9600 bps
  Delay_ms(100);                  // Wait for UART 1module to stabilize
  UART1_Write_Text("Start");
  UART1_Write(13);
  UART1_Write(10);
  
  while(1)
  {
           adc_rd  = ADC_read(2);                 // get ADC value from 2nd channel
           tlong = (long)adc_rd * 5000;           // covert adc reading to milivolts
           tlong = tlong / 1023;                  // 0..1023 -> 0-5000mV

 //UART1_Write_Text(tlong); //delay_ms(500);

                 longtostr(tlong,text);
                 for (i=0;i<strlen(text);i++)
                 UART1_Write(text[i]);
                 delay_ms(500);


}

}
 

two thing-

1) First check the ADC input voltage is proper or not
2) Check your ADC read function that you are setting proper ADCON0 Value like ADCON0 = 0b10001001;

Good Luck
 

yes,
now value is changing
 

Good see that

make sure that you set proper ADCON0 value.....As I was using clock frequecy of 20Mhz and FOSC/4 you can have different configurations also.....

Good Luck
 

I think there is a mistake in choosing the value of ADCON1 register...

You have used
Code:
ADCON1 = ADCON1 | 0x0C;

But according to Datasheet of PIC

ADCON1 = 0x0C;
AN2 is Vref- Pin..

Are u getting what i want to say..
Try some other value of ADCON1 register


Hope this Helps You...

---------- Post added at 21:42 ---------- Previous post was at 21:40 ----------

Try ADCON1 = 0x00; Value..

And Report your result back to this thread..
 
yes its already working, I gave

ADCON1 = 0x0C; // Set AN2 channel pin as analog
ADCON1 = 0b10001001;
 

I mean to Say that..
You can't select Channel AN2 as Analog Input Channel by
doing this
ADCON1 = 0x0C;
Because in this Setting AN2 is used as Vref- PIN
Do you Understand What i want to Say

AN0 - Analog
AN1 - Analog
AN2 - Vref-
AN3 - Vref+
AN4 -Analog
AN5 - Digital
AN6 - Digital
AN7 - Digital


Change You Code as Follow:-

Code:
char *text = "000.0000";

  void main(){
   CMCON  |= 0x07;            // turn off comparators
[B][I][I][I][U]   ADCON1 =  0x89;[/U][/I][/I][/I][/B]
  TRISA2_bit = 1;            //   input

  UART1_Init(9600);               // Initialize UART1 module at 9600 bps
  Delay_ms(100);                  // Wait for UART 1module to stabilize
  UART1_Write_Text("Start");
  UART1_Write(13);
  UART1_Write(10);
  
  while(1)
  {
           adc_rd  = ADC_read(2);                 // get ADC value from 2nd channel
           tlong = (long)adc_rd * 5000;           // covert adc reading to milivolts
           tlong = tlong / 1023;                  // 0..1023 -> 0-5000mV

 //UART1_Write_Text(tlong); //delay_ms(500);

                 longtostr(tlong,text);
                 for (i=0;i<strlen(text);i++)
                 UART1_Write(text[i]);
                 delay_ms(500);


}

}
 
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…