ito ang code na nakita koCode: [Select]voidadc_read_hummidity(void){int humidity_h, humidity_l;ADCON1=128;HUMIDITY = 1;adc_read(3); // read channel 3humidity_h = ADRESH;humidity_l = ADRESL;HUMIDITY = 0;ADCON1=7; // Set all the input/output to digital}meron naman ADRESH & ADRESL
voidadc_read_hummidity(void){int humidity_h, humidity_l;ADCON1=128;HUMIDITY = 1;adc_read(3); // read channel 3humidity_h = ADRESH;humidity_l = ADRESL;HUMIDITY = 0;ADCON1=7; // Set all the input/output to digital}
sir bobot,i think, aside pa dun sa dalawang register (ADRESL and ADRESH),kelangan mo rin tingnan yung ADFM(: A/D Result Format Select bit) ng ADCON1 (or ADCON2).
CCS named this as 16-bit or 10-bit results.
what do you mean sir? is that the declaration at the preprocessor - "adc=10"?
looking at the "adc_read_hummidity" function, I can see that "humidity_l" receives "ADRESL" and humidity_h" receives "ADRESH". However at the the main(), only the "humidity_l" is being used. I am greatly confused with that fact. I see that the 2-msb is not being used.
We are in tropical country, relative humidity is always above 50%. Maybe that device was developed in a dryer climate.
Secondly, there is no preprocessor directive in the source code. Instead there is a line in code, ADCON1 = 128. This is equivalent to ADFM = 1 (right justified result).
ito example from mikroc, for 10 bit adcCode: [Select]unsigned int temp_res;void main() { ADCON1 = 0x80; // Configure analog inputs and Vref TRISA = 0xFF; // PORTA is input TRISB = 0x3F; // Pins RB7, RB6 are outputs TRISD = 0; // PORTD is output do { temp_res = ADC_Read(2); // Get results of AD conversion PORTD = temp_res; // Send lower 8 bits to PORTD PORTB = temp_res >> 2; // Send 2 most significant bits to RB7, RB6 } while(1);}ADC_Read() is a built-in function.If you dont want to use the built-in function, you can make your own function for reading the 10-bit adcCode: [Select]unsigned int read_ADC(void){ unsigned int adc_temp; //this variable will hold the 16-bit adc value ADCON0 |= 0x04; //start conversion while(ADCON0 & 0x04) //wait while conversion is no finished ; adc_temp = ADRESL; //read the lower 8-bit adc_temp += (ADRESH << 8); //read the higher 8-bit //and form a 16-bit adc value return(adc_temp); //return}
unsigned int temp_res;void main() { ADCON1 = 0x80; // Configure analog inputs and Vref TRISA = 0xFF; // PORTA is input TRISB = 0x3F; // Pins RB7, RB6 are outputs TRISD = 0; // PORTD is output do { temp_res = ADC_Read(2); // Get results of AD conversion PORTD = temp_res; // Send lower 8 bits to PORTD PORTB = temp_res >> 2; // Send 2 most significant bits to RB7, RB6 } while(1);}
unsigned int read_ADC(void){ unsigned int adc_temp; //this variable will hold the 16-bit adc value ADCON0 |= 0x04; //start conversion while(ADCON0 & 0x04) //wait while conversion is no finished ; adc_temp = ADRESL; //read the lower 8-bit adc_temp += (ADRESH << 8); //read the higher 8-bit //and form a 16-bit adc value return(adc_temp); //return}
patulong a din po sa pagset ng ADC clock medyo nalilito po ako dito.sa software din po ba ito sineset? ang gamit ko po ay PIC16F877A at my 20MHz ako na Xtal at 22nF.ito po ang error ko na nakukuha.ADC conversion clock period(1e-07) is invalid for device frequencymy sleep feature din daw po itong IC na ito, paano po ito ginagamit?
//ADCON0 constants#define RA0_ANA0 0x00#define FREQ_DIV_32 0x80//ADCON1 constants#define RIGHT_JUSTIFIED 0x80#define AN0 0x0Evoid init_ADC(void){ ADCON0 = FREQ_DIV_32 | RA0_ANA0; ADCON1 = RIGHT_JUSTIFIED | AN0; TRISA |= 0x01; //RA0 is input ADRESL = 0x00; //ADC Result Low Register is 0 ADRESH = 0x00; //ADC Result High Register is 0 INTCON |= 0xC0; //enable peripheral interrupt ADCON0 |= 0x01; //turn on ADC}
for more discussion on pic adc, refer to this manual from the manufacturerhttp://ww1.microchip.com/downloads/en/DeviceDoc/31023a.pdf
Same codes din po ba ito sa MikroC?Pasensya na po begginer lang po sa PIC. hehehehe
ADC conversion clock period(1e-07) is invalid for device frequency -> you are using Fosc/2 for the adc clock, which is the default value. This is very high if you are using 20Mhz xtal, since valid adc clock should not be less than 1.6 us (1e-07 s < 1.6 us). For a 20mhz xtal oscillator, you must use adc clock = Fosc/32. This can be set in software (during initialization) by setting the ADCS1 and ADCS0 bits of the ADCON0 register.here is an initialization code.Code: [Select]//ADCON0 constants#define RA0_ANA0 0x00#define FREQ_DIV_32 0x80//ADCON1 constants#define RIGHT_JUSTIFIED 0x80#define AN0 0x0Evoid init_ADC(void){ ADCON0 = FREQ_DIV_32 | RA0_ANA0; ADCON1 = RIGHT_JUSTIFIED | AN0; TRISA |= 0x01; //RA0 is input ADRESL = 0x00; //ADC Result Low Register is 0 ADRESH = 0x00; //ADC Result High Register is 0 INTCON |= 0xC0; //enable peripheral interrupt ADCON0 |= 0x01; //turn on ADC}
ADC conversion started before 'wait' time has expired following previous conversion or channel change.
/* return a 10-bit result */unsigned int read_adc(unsigned char channel){ channel&=0x07; // truncate channel to 3 bits ADCON0&=0xC5; // clear current channel select ADCON0|=(channel<<3); // apply the new channel select ADGO=1; // initiate conversion on the selected channel while(ADGO)continue; return(((ADRESH&0x03)<<8)+ADRESL); // return the 10-bit result }