Hello,
There seem to be a number of problems with your code -
- You have enabled the ADC interrupt, but then proceed to poll the COCO flag within the main loop. However, the COCO bit will always read as zero when the interrupt is enabled. Initially, I would suggest that you disable the ADC interrupt, and poll conversion complete flag.
- You have selected the ADC clock to be Bus_clk/16. Assuming you are using the internal oscillator (Bus_clk = 3.2MHz), the divider value will need to be Bus_clk/4, for the ADC to operate correctly.
- Since you have selected single conversion mode, the ADSCR register will need to be written to start each conversion - your code only writes the register once.
I assume that the analog signal is present at the PTA5 (AN3) pin, and that you are actually using a 'QY4 device. This device is practically obsolete, and has been replaced by the 'QY4A device. The later device has a different ADC module, and its setup may differ from the earlier one. The following code assumes the 'QY4.
void main(void) {
byte result;
inicializacion();
ADICLK = 0x40; // Assumes internal oscillator
for( ; ; ) {
ADSCR = 0x03; // Start conversion - AN3 channel
while (!ADSCR_COCO);
result = ADR;
PTB = result;
}
}
Regards,
Mac