Help with adc QY4

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Help with adc QY4

2,110 Views
scientist
Contributor I
hi, I 'm sorry to trouble you, but I can't make that work the adc of QY4, I'm new in the microcontroller freescale, but I start and I need some help., my program is this

Code:
#include <hidef.h> /* for EnableInterrupts macro */#include <MC68HC908QY4.h> /* include peripheral declarations */#include <startup.h>unsigned char result;void interrupt 16 ADC_ISR(void){  result=ADR;  }void inicializacion(void);void main(void) {  inicializacion();    ADICLK=0x80;  ADSCR = 0x43;     for(;;){         while (!ADSCR_COCO);        result=ADR;        PTB= result;      }           /* loop forever */  /* please make sure that you never leave this function */}void inicializacion(){   CONFIG1=0x09; CONFIG2=0x80; OSCTRIM=0x80;  DDRB=0xFF;   }

 please, could you put an easy example of adc in c

thanks, and I'm sorry for my english, I promise to learn speak english

Labels (1)
0 Kudos
5 Replies

516 Views
bigmac
Specialist III
Hello,
 
There seem to be a number of problems with your code -
  1. 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.
  2. 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.
  3. 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
 
0 Kudos

516 Views
scientist
Contributor I
thanks bigmac, its just what I needed, thank you very much, I goind to see it's works :smileyvery-happy:
0 Kudos

516 Views
scientist
Contributor I
doesn't work, I don't know why, I put this program

Code:
#include <hidef.h> /* for EnableInterrupts macro */#include "derivative.h" /* include peripheral declarations */#include <startup.h>  void inicializacion(void);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;  }}   /* loop forever */  /* please make sure that you never leave main */void inicializacion(){   CONFIG1=0x09; CONFIG2=0x00; OSCTRIM=0x80;  DDRB=0xFF;   }

 and I can't see nothing in the port B, what's wrong whit my code?

thanks again


0 Kudos

516 Views
bigmac
Specialist III
Hello,
 
A few questions -
  1. Have you checked that the device you are using is actually a 'QY4, and not a 'QY4A type?
  2. Have you programmed the device with the code, and are running in stand-alone manner, (i.e. you are not attempting to use full chip simulation within the debugger)?
  3. What analog voltage level is being applied to the PTA5 (AN3) pin?
  4. How are you monitoring the state of port B, perhaps using LEDs.  If so, a substantial delay between each reading may help.

Regards,

Mac

 

 
 
0 Kudos

516 Views
scientist
Contributor I
Hi, i´m sorry, I check my circuit in protoboard, and the potenciometro (I don´t know how I say to english) doesn't work right, so I change the potenciometro and work good the micro, thanks, and como decimos en mi tierra muchas gracias, saludos
0 Kudos