Help on PWM control using accelerometer

Hi
I wanted to control the servo motor according to the accelerometer value ,i developed the code using Arm wizard tool ,the code works well in Proteus tool but in hardware it fails , i checked the PWM output using the Oscilloscope can you help me to rectify the problem in the code

Code:

#include <LPC213x.h>


void input();
void adc_ini();
void adc_data();

void pwm();
void motor();
void motorzero();


unsigned long val,adc_val;

int main(void)
{
                input();
                pwm();


                        while(1)
                                {
                                                        adc_ini();
                                                        adc_data();

                                                        motor();
                                }

}




void input(void)
{

    PINSEL0 = 0x00028000;
    IO0DIR = 0x80000000;
    PINSEL1 = 0x01000000;
    PINSEL2 = 0x00000000;
    IO1DIR = 0x00000000;
}


void adc_ini(void)
{

    PCONP = (PCONP & 0x001817BE) | (1UL<<12);
    AD0INTEN = 0x00000000;
    AD0CR = 0x01200202;

}



void adc_data(void)
{
    while((AD0DR & 0x80000000)==0); //until bit 31 is high (DONE)
    val = AD0DR; //read a/d data register
    adc_val = ((val >> 6) & 0x3FF);    // Extract AD result
 }








void pwm(void)
 {

    PCONP = (PCONP & 0x001817BE) | (1UL<<5);
    PWMTC = 0x00000000;
    PWMPR = 0x0000003B;
    PWMMCR = 0x00000002;
    PWMMR0 = 0x000003E8;
    PWMMR2 = 0x0000004B;
    PWMMR4 = 0x0000004B;
    PWMPCR = 0x00001400;
    PWMTCR = 0x09;
}



void motor(void)
{
      if ((adc_val>=0x0000020E) && (adc_val<0x00000224))
          {
                        PWMMR2 = 0x0000004C;
                        PWMMR4 = 0x0000004A;
                        PWMLER = 0x00000014;
    }
                else if ((adc_val>=0x00000224) && (adc_val<0x00000238))
          {
                        PWMMR2 = 0x0000004D;
                        PWMMR4 = 0x00000049;
                        PWMLER = 0x00000014;
    }
                else if ((adc_val>=0x00000238) && (adc_val<0x0000024C))
          {
                        PWMMR2 = 0x0000004E;
                        PWMMR4 = 0x00000048;
                        PWMLER = 0x00000014;
    }
                else if ((adc_val>=0x0000024C) && (adc_val<0x00000260))
          {
                        PWMMR2 = 0x0000004F;
                        PWMMR4 = 0x00000047;
                        PWMLER = 0x00000014;
    }


                /*for negative angle*/

                else if ((adc_val<=0x000001B8) && (adc_val>0x000001A3))
          {
                        PWMMR2 = 0x0000004A;
                        PWMMR4 = 0x0000004C;
                        PWMLER = 0x00000014;
    }
                else if ((adc_val<=0x000001A3) && (adc_val>0x0000018F))
          {
                        PWMMR2 = 0x00000049;
                        PWMMR4 = 0x0000004D;
                        PWMLER = 0x00000014;
    }
                else if ((adc_val<=0x0000018F) && (adc_val>0x0000017A))
          {
                        PWMMR2 = 0x00000048;
                        PWMMR4 = 0x0000004E;
                        PWMLER = 0x00000014;
    }
                else if ((adc_val<=0x0000017A) && (adc_val>0x00000167))
          {
                        PWMMR2 = 0x00000047;
                        PWMMR4 = 0x0000004F;
                        PWMLER  = 0x00000014;
                }

                else
                {
                        motorzero();
                }
}



void motorzero(void)
        {
                        PWMMR2 = 0x0000004B;
                        PWMMR4 = 0x0000004B;
                        PWMLER =  0x00000014;
        }



thanks in advance

More questions in this forum