Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Problem in PWM for controlling servo using accelerometer

Status
Not open for further replies.

kodi.sudar

Member level 5
Member level 5
Joined
Dec 21, 2009
Messages
92
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
india
Visit site
Activity points
1,849
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>
#include <math.h>
#include <stdio.h>


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

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

unsigned long val,adc_val;
float y,s,v,m;
float angle,b;


int main(void)
{
		input();
		pwm();
		
	
			while(1)
				{
							adc_ini();
							adc_data();
							
							motor();
				}

}
		



void input(void)
{
	
    PINSEL0 = 0x00028000;     /* binary: 00000000_00000010_10000000_00000000 */
    IO0DIR = 0x80000000;     /* binary: 10000000_00000000_00000000_00000000 */
    PINSEL1 = 0x01000000;     /* binary: 00000001_00000000_00000000_00000000 */
    PINSEL2 = 0x00000000;     /* binary: 00000000_00000000_00000000_00000000 */
    IO1DIR = 0x00000000;     /* binary: 00000000_00000000_00000000_00000000 */

}	


void adc_ini(void)
{

    PCONP = (PCONP & 0x001817BE) | (1UL<<12);      /* Enable peripheral clock for ADC0 (default is enabled) */
		AD0INTEN = 0x00000000;     /* binary: 00000000_00000000_00000000_00000000 */
    AD0CR = 0x01200202;     /* binary: 00000001_00100000_00000000_00000010 */

}



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);      /* Enable peripheral clock for PWM0 (default is enabled) */
    PWMTC = 0x00000000;     /* decimal 0 */
    PWMPR = 0x0000003B;     /* decimal 59 */
    PWMMCR = 0x00000002;     /* binary: 00000000_00000000_00000000_00000010 */
    PWMMR0 = 0x000003E8;     /* decimal 1000 */
		PWMMR2 = 0x0000004B;     /* decimal 75 */
		PWMMR4 = 0x0000004B;     /* decimal 75 */
		PWMPCR = 0x00001400;     /* binary: 00000000_00000000_00010100_00000000 */
    PWMTCR = 0x09;     /* binary: 00001001 */
}



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;
	}
	
	
	
void delay(void)
	{	
		long int i,j,c;
		for(i=0;i<400;i++)
		{
			for(j=0;j<1000;j++)
				{
						c++;
				}
		}	
	}
thanks in advance
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top