Faruk Chaudhry

Opinion needed on this code that uses the ADC to modify the PWM duty cycle

Discussion created by Faruk Chaudhry on Jun 21, 2006
Latest reply on Jul 28, 2006 by Faruk Chaudhry
Hello,
 
This code reads the ADC (hardware trigger), calculates the duty cycle by scaling the adc reading by 100, and looks up the timer channel value register by table in rom.
 
volatile unsigned char duty_cycle;
const unsigned char pwm_table[100][2] = {   {0x00, 0x00},
                                                                           .
                                                                           .
                                                                         {0xFF, 0xFF} };
__interrupt void isrVadc(void)
{
 
  adc_reading = ADCRL;                        // clear COCO flag
 
  asm
  {
    lda adc_reading
    ldx #100
    mul
    pshx
    pulh
    ldx #100
    div
    sta  duty_cycle
  }
 
  TPMC0VH = pwm_table[duty_cycle][0];
  TPMC0VL = pwm_table[duty_cycle][1];
 
Any comments?

Outcomes