RT-AICHIP-sample
pwm.c
[詳解]
1 /* ------------------------------------------------------------ *
2 File pwm.c
3 
4 16bitタイマーを用いたPWM出力の設定
5 * ------------------------------------------------------------ */
6 #include "timer.h"
7 #include "LPC13xx.h" // LPC13xx Peripheral Registers
8 #include "pwm.h"
9 
10 /*----------------------------------------------------------------------------
11  Input void
12  Output void
13  PIO0_10/CT16B0_MAT2ピンを16bitタイマーを利用したPWM出力に設定
14  *---------------------------------------------------------------------------*/
15 void Init_PWM1 (void)
16 {
17  LPC_TMR16B0->TCR = 0; //Disable Timer0
18 
19  LPC_SYSCON->SYSAHBCLKCTRL |= (1<<7); // Timer16B0 Turn ON
20  LPC_IOCON->SWCLK_PIO0_10 = 0x3; // PIO0_10 as CT16B0_MAT2
21 
22  LPC_TMR16B0->PR = 72-1;// TC周波数決定 72Mhz/720 = 100kHz
23  LPC_TMR16B0->PWMC = 0x4;
24 
25  LPC_TMR16B0->MCR = (1<<10);
26  LPC_TMR16B0->MR3 = 5000; // PWM High duration=MR2-MR0
27  LPC_TMR16B0->MR2 = 1; // PWM Period (Max 16bit dec:65535)
28 
29  LPC_TMR16B0->TCR = 2; // TCR Reset
30  LPC_TMR16B0->TCR = 1; // TCR Start
31 
32 }
33 
34 /*----------------------------------------------------------------------------
35  Input void
36  Output void
37 
38  PIO1_9/CT16B1_MAT0ピンを16bitタイマーを利用したPWM出力に設定
39  *---------------------------------------------------------------------------*/
40 void Init_PWM2 (void)
41 {
42  LPC_TMR16B1->TCR = 0; //Disable Timer1
43 
44  LPC_SYSCON->SYSAHBCLKCTRL |=0x100; // Timer16B1 Turn ON
45  LPC_IOCON->PIO1_9 = 0x001; // PIO1_9 as CT16B1_MAT0
46 
47  LPC_TMR16B1->PR = 72-1; // TC周波数決定 72Mhz/720 = 100kHz
48  LPC_TMR16B1->PWMC = 1; // MAT0 as PWM
49 
50  LPC_TMR16B1->MCR = 0x80;
51  LPC_TMR16B1->MR0 = 0;
52  LPC_TMR16B1->MR2 = 5000;
53 
54  LPC_TMR16B1->TCR = 2; // TCR Reset
55  LPC_TMR16B1->TCR = 1; // TCR Start
56 }
57 
58 /*----------------------------------------------------------------------------
59  Input duty 0.0 to 1.0
60  Output void
61  PWMのdutyを duty*100 % に設定
62  *---------------------------------------------------------------------------*/
63 void setDutyPWM1 (float duty)
64 {
65  LPC_TMR16B0->MR2 = (uint32_t)(5000.0* duty);
66 }
67 /*----------------------------------------------------------------------------
68  Input duty 0.0 to 1.0
69  Output void
70  PWMのdutyを duty*100 % に設定
71  *---------------------------------------------------------------------------*/
72 void setDutyPWM2 (float duty)
73 {
74  LPC_TMR16B1->MR0 = (uint32_t)(5000.0* duty);
75 }
76 
77 /******************************************************************************
78 ** End Of File
79 ******************************************************************************/
void setDutyPWM2(float duty)
Definition: pwm.c:72
void Init_PWM1(void)
Definition: pwm.c:15
void Init_PWM2(void)
Definition: pwm.c:40
void setDutyPWM1(float duty)
Definition: pwm.c:63
unsigned int uint32_t
Definition: type.h:29