Need help on using ADC interrupt for a simple operation - MC9S08AW60

Discussion created by Guest on Mar 20, 2009
Latest reply on Mar 23, 2009 by Guest

Hi. I'm using MC9S08AW60 MCU and I've just started using ADC. So I'm starting out by doing a simple operation. It involves an infrared sensor which is connected to the ADC, a card with a good reflection surface, and a motor. The sensor has a numerical range of 0-255 (00-FF in hex) based on the distance between the sensor and the card.

 

My desired operation is: When I place my card near the sensor, the sensor will change its integer value and start the motor. And when the card is far from the sensor, the sensor value will increase and stop the motor. Here's my "half-working" code:

 

void ADC_configuration (void)

{
  ADC1SC1 = 0x20;        /* Interrupt disable. Continuous conversion mode and channel 0 active */
  ADC1SC2 = 0x20;        /* Compare enabled and triggers when input < compared value */
  ADCCFG = 0x30;        /* Input clock/2. Long Sample time configuration. 8-bit conversion */

  ADC1CVL = 40;     /* set compared value to 40 */
  ADC1SC1_COCO = 0;

 

void main(void)
{
  init_Motor();  /* Function that initializes the motor */
  ADC_configuration();  /* Function that initializes the ADC module */             
  EnableInterrupts; /* enable interrupts */
  ADC1SC1_AIEN = 1; /* Enable ADC interrupt */
  APCTL1_ADPC0 = 1; /* Select channel 0 for ADC input */    
  for(;; )
  {
    __RESET_WATCHDOG();
  } 
}

 

interrupt 23 void ADC_input(void)
{              
  UINT8 temp;
  temp = ~ADC1RL;
  Motor_move();  
}

 

Here's the problem: The program only works one time. Which means, if the card is close to the sensor when I start running the program, the motor will move. But if I remove the card from the sensor while the program is still running, the motor does not stop. And vice versa, where if the card is far from the sensor when I start running the program, the motor won't move. And if I place the card close to the sensor while the program is still running, the motor still won't move. Overall, it means that the sensor only reads once at the instantaneous point when the program starts running, and doesn't read at all after that.

 

Help is appreciated. Thx

Message Edited by Cryptical on 2009-03-20 09:38 AM
 Added p/n to subject.
Message Edited by NLFSJ on 2009-03-20 09:21 AM

Outcomes