Вы находитесь на странице: 1из 17

DC MOTOR

PWM

• Pulse Width Modulation, or PWM, is a technique for getting analog results with

digital means. How ??


Vmax
Vout

Vout

Vmin
Vout = Amp * Duty Cycle
Amp = Vmax – Vmin

Vout Range from Vmin to Vmax


PWM
PWM Port Pin Functions Associated Corresponding
Match Register
Channel Pin PINSEL Register
PWM_1 P2.0 0-GPIO, 1-PWM1[1], 2-TXD1, 3- 0,1 bits of PINSEL4 MR1
PWM_2 P2.1 0-GPIO, 1-PWM1[2], 2-RXD1, 3- 2,3 bits of PINSEL4 MR2
PWM_3 P2.2 0-GPIO, 1-PWM1[3], 2-CTS1, 3- 4,5 bits of PINSEL4 MR3
TRACEDATA[3]
PWM_4 P2.3 0-GPIO, 1-PWM1[4], 2-DCD1, 3- 6,7 bits of PINSEL4 MR4
TRACEDATA[2]
PWM_5 P2.4 0-GPIO, 1-PWM1[5], 2-DSR1, 3- 8,9 bits of PINSEL4 MR5
TRACEDATA[1]
PWM_6 P2.5 0-GPIO, 1-PWM1[6], 2-DTR1, 3- 10,11 bits of MR6
TRACEDATA[0] PINSEL4
#include <lpc17xx.h>
while(1)
void delay(unsigned int d)
{
{
for(dc=0; dc<4000; dc++)
unsigned int i,j;
{
for(i=0;i<d;i++)
LPC_PWM1->MR1 = dc;
for(j=0;j<500;j++);
LPC_PWM1->MR4 = 0;
}
PC_PWM1->LER = 0x31;
delay(5);
int main(void)
}
{
for(dc=0; dc<4000; dc++)
int dc;
{
LPC_PINCON->PINSEL4 = 0x41;
LPC_PWM1->MR4 =dc;
LPC_PWM1->MR0 = 4000;
LPC_PWM1->MR1=0;
LPC_PWM1->MCR = 2;
LPC_PWM1->LER=0x31;
LPC_PWM1->PCR = (1<<9)|(1<<12); //or =0x1200
delay(5);
LPC_PWM1->TCR = 2;
}
LPC_PWM1->TCR = 9;
}
}
LPC_PINCON->PINSEL4 = 0X41;
• PINCON is Pin Connect Block
PINSEL4 REGISTER

31:8 7 6 5 4 3 2 1 0
000….0 0 1 0 0 0 0 0 1
Function PWM1.4 PWM1.1
LPC_PWM1->MR0 = 4000;
MATCH REGISTER 0 [TON + TOFF]

31:16 15:12 11:8 7:4 3:0


Hex 00…..00 0 F A 0
Binary 00…..00 0000 1111 1010 0000
LPC_PWM1->MCR = 2;
LPC_PWM1->PCR = (1<<9)|(1<<12);
//OR =0X1200
To enable the PWM1 and PWM4 output
PCR REGISTER

31:8 15:12 11:8 7:4 3:0


15 14 13 12 11 10 9 8

000….0 1 2 0 0
0001 0010 0000 0000
LPC_PWM1->TCR = 2;
LPC_PWM1->TCR = 9;
When TCR is 2:
Reset the counter
When TCR is 9:
Enable PWM
Enable Counter

31:4 3 2 1 0
PWM COUNTER COUNTER
Reserved Reserved
ENABLE RESET ENABLE
00…..000 0 0 1 0
00…..000 1 0 0 1
while(1)
{
for(dc=0; dc<4000; dc++)
{
LPC_PWM1->MR1 = dc;
LPC_PWM1->MR4 = 0;
PC_PWM1->LER = 0x31;
delay(5);
}
for(dc=0; dc<4000; dc++)
{
LPC_PWM1->MR4 =dc;
LPC_PWM1->MR1=0;
LPC_PWM1->LER=0x31;
delay(5);
}
}
}
LPC_PWM1->LER=0X31;
PCR REGISTER

31:7 6 5 4 3 2 1 0
Enable Enable Enable Enable Enable Enable Enable
Reserved PWM PWM PWM PWM PWM PWM PWM
Match 6 Match 5 Match 4 Match 3 Match 2 Match 1 Match 0

00….0 0 1 1 0 0 0 1

3 1
DC MOTOR
Use FRC 1 to interface

Вам также может понравиться