/*
** DC motor interfaced at CN7
** P0.6 -> GPIO
** P0.7 -> PWM2
*/
#include <lpc214x.h>
#define CLOCKWISE 1
#define ANTICLOCKWISE 2
unsigned int period;
void PWM_init(void)
{
period = 10000;
PINSEL0 &= 0xFFFF0FFF; /* P0.6 as GPIO */
PINSEL0 |= 0x00008000; /* P0.7 as PWM2 */
IODIR0 |= 0x00000040; /* P0.6 as output */
IOCLR0 = 0x00000040; /* P0.6 = 1 */
PWMPR = 0x16;
PWMMCR = 0x00000002;
PWMPCR = 0x00000400;
PWMMR0 = period;
PWMMR2 = period/2;
PWMLER = 0x05;
PWMTCR = 0x00000009; /* Enable PWM and TC */
}
void PWM_SetDutyCycle(unsigned char dir, unsigned int dc)
{
switch(dir)
{
case CLOCKWISE: IOCLR0 = 0x00000040;
PWMMR2 = (period/100)*dc;
PWMLER = 0x05;
break;
case ANTICLOCKWISE: IOSET0 = 0x00000040;
PWMMR2 = period - (period/100)*dc;
PWMLER = 0x05;
break;
}
}
void delay(unsigned int time)
{
unsigned int i,j;
for(i=0;i<time;i++)
for(j=0;j<5000;j++);
}
int main()
{
unsigned int i;
PWM_init();
while(1)
{
for(i=0;i<=100;i++)
{
PWM_SetDutyCycle(CLOCKWISE,i);
delay(100);
}
for(i=100;i>0;i--)
{
PWM_SetDutyCycle(CLOCKWISE,i);
delay(100);
}
for(i=0;i<=100;i++)
{
PWM_SetDutyCycle(ANTICLOCKWISE,i);
delay(100);
}
for(i=100;i>0;i--)
{
PWM_SetDutyCycle(ANTICLOCKWISE,i);
delay(100);
}
}
}