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

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

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

1,166 Views
admin
Specialist II

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
Labels (1)
0 Kudos
Reply
1 Reply

327 Views
admin
Specialist II
I've got it working already. The error was due to some extra code in the for(;; ) loop, which I didn't include here. Thx
0 Kudos
Reply