chimera786
Member level 2
hello. I am running adc on PIC 16f687. Now, I known I can use the adc read command from mikro.. However, I want to use my own. This is the command I have:
As you can see, I am reading from ADRESH register, which is the upper 8 bits. This means that I have to quit the last two bits which are stored in the ADSEL register. This in turn means that I have only 8 bits of resolution.
Now, lets say I am okay with 8 bits of resolution. Accoding to my code, I can read ADRESH which is 8 bits long, equate that to 'test' and read it . However, when ever I use the 'if' condition that 'if' is greater than 240, it does not work. If I use, 'if (test >100)', then the program works. Why is this.
Also, what is I am trying to use the complete 10 bits. How am I to store the complete 10 bits.
Thank you. I do appreciate each and every reply.
Code:
void init(){
ADCON0=0x00; // pin A0 for analog read, left justified, Vref=VDD, ADO off
ADCON1=0x20; // Aquisition time is Fosc/32 @ 8Mhz
}
unsigned int Read(){
ADON_bit=1; // turn on ADC
GO_DONE_bit=1; // start conversion
while (GO_DONE_bit); // wait for conversion to get done
ADON_bit=0; // turn of ADC
return ADRESH; // read only the ADRESH register.
}
unsigned int test;
void main() {
ANSEL=0x01; // Pin A0 for analog read
TRISA=0xFF; // PORTA is all input
TRISB=0x00; // Port B is all OUTPUT
CM1CON0.B7o=0; // comparators off
CM2CON0.B7=0;
init();
while (1){
PORTB.B6=0; // make port b pin off
test=Read(); // get the result from ADRESH register--8 bit resolution
if (test>240){ // if it is close to Vdd turn on PIN B6
PORTB.B6=1;}
}
As you can see, I am reading from ADRESH register, which is the upper 8 bits. This means that I have to quit the last two bits which are stored in the ADSEL register. This in turn means that I have only 8 bits of resolution.
Now, lets say I am okay with 8 bits of resolution. Accoding to my code, I can read ADRESH which is 8 bits long, equate that to 'test' and read it . However, when ever I use the 'if' condition that 'if' is greater than 240, it does not work. If I use, 'if (test >100)', then the program works. Why is this.
Also, what is I am trying to use the complete 10 bits. How am I to store the complete 10 bits.
Thank you. I do appreciate each and every reply.
Last edited by a moderator: