Vaisakhan Ku
Junior Member level 1
- Joined
- Jan 19, 2014
- Messages
- 16
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 259
Hi all;
Please find a simple problem that I have here. Am trying to make an ADC library for PIC16F88. While reading the ADC value, I cant get the value of ADRESH, while the other register ADRESL gets fill over after 255 count and does like this for 4 times over the entire scale of 0 to 5V.
this is my main code
and this is the function which gets called from the main.
What I had observed is that if am not clearing the ADRSH registre inside the calling function am getting a value above 255 but erratic. I tried to read two different variable to avoid any speed reading problem. But in both cases am getting some random values to the lcd and to the variables. I think the ADRESH register is not getting updated once the ADRESL gets full and dont know the reason. If anyone come across this problem pls provide some lead!!!!
Please find a simple problem that I have here. Am trying to make an ADC library for PIC16F88. While reading the ADC value, I cant get the value of ADRESH, while the other register ADRESL gets fill over after 255 count and does like this for 4 times over the entire scale of 0 to 5V.
this is my main code
Code:
#include "ADC_Library for PIC16f88.h"
int adc_v=0;
char temp[7]=" ";
int chnl_sel,clk_sel;
// LCD Initialization
sbit LCD_D4 at Rb0_bit;
sbit LCD_D5 at Rb2_bit;
sbit LCD_D6 at Rb3_bit;
sbit LCD_D7 at Rb5_bit;
sbit LCD_RS at Rb6_bit;
sbit LCD_EN at Rb7_bit;
// Pin direction
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB2_bit;
sbit LCD_D6_Direction at TRISB3_bit;
sbit LCD_D7_Direction at TRISB5_bit;
sbit LCD_RS_Direction at TRISB6_bit;
sbit LCD_EN_Direction at TRISB7_bit;
void main()
{
lcd_init();
lcd_cmd(_lcd_cursor_off);
while(1)
{
adc_v=Adc_read_PIC16F88(0);
//ltrim(adc_v);
inttostr(adc_v,temp);
lcd_out(1,1,temp);
adc_v=Adc_read_PIC16F88(1);
//ltrim(adc_v);
inttostr(adc_v,temp);
lcd_out(2,1,temp);
delay_ms(500);
}
}
and this is the function which gets called from the main.
Code:
int Adc_read_PIC16F88(int chanal)
{
unsigned int adc_value=0,temp_adc=0;
int adcon_old;
ansel=0b00000011;
pie1.adie=0; //ADC interrupt enabled.
pir1.ADIF=0; //Cleared the ADC interrupt.
adresh=adresl=0; //Clear the ADC values
delay_ms(10);
chanal=chanal<<3; //channal selection
adcon_old=0b10000001;
adcon_old=adcon_old|chanal;
adcon0=adcon_old; //chanal selected wrt the variable chanal.
delay_us(20); //for getting enough Tad for 20Mhz
adcon1=0b11000000;
adcon0.GO_DONE=1;
while(pir1.adif==0|| adcon0.GO_done==1) //polling for adc to complet
temp_adc=adresh;
adc_value=adresl;
temp_adc=temp_adc<<8;
adc_value= adc_value|temp_adc;
pir1.ADIF=0;
return(adc_value);
}
What I had observed is that if am not clearing the ADRSH registre inside the calling function am getting a value above 255 but erratic. I tried to read two different variable to avoid any speed reading problem. But in both cases am getting some random values to the lcd and to the variables. I think the ADRESH register is not getting updated once the ADRESL gets full and dont know the reason. If anyone come across this problem pls provide some lead!!!!