-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPWM.c
73 lines (58 loc) · 1.54 KB
/
PWM.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <stdint.h>
#include "tm4c123gh6pm.h"
void PWM_Init()
{
SYSCTL_RCGCPWM_R |= 0x01;
SYSCTL_RCC_R &= ~(1 << 20);
GPIO_PORTB_DIR_R |= 0xF0;
GPIO_PORTE_DIR_R |= 0x30;
GPIO_PORTB_AFSEL_R |= 0xF0;
GPIO_PORTE_AFSEL_R |= 0x30;
GPIO_PORTB_PCTL_R = (GPIO_PORTF_PCTL_R & 0x0000FFFF) | (0x44440000);
GPIO_PORTE_PCTL_R = (GPIO_PORTE_PCTL_R & 0xFF00FFFF) | (0x00440000);
GPIO_PORTB_DEN_R |= 0xF0;
GPIO_PORTE_DEN_R |= 0x30;
PWM0_0_CTL_R &= ~0x03;
PWM0_1_CTL_R &= ~0x03;
PWM0_2_CTL_R &= ~0x03;
PWM0_0_GENA_R = 0x8C;
PWM0_0_GENB_R = 0x80C;
PWM0_1_GENA_R = 0x8C;
PWM0_1_GENB_R = 0x80C;
PWM0_2_GENA_R = 0x8C;
PWM0_2_GENB_R = 0x80C;
PWM0_0_LOAD_R = 8000 - 1;
PWM0_1_LOAD_R = 8000 - 1;
PWM0_2_LOAD_R = 8000 - 1;
PWM0_0_CMPA_R = 8000 - 1;
PWM0_0_CMPB_R = 8000 - 2;
PWM0_1_CMPA_R = 8000 - 1;
PWM0_1_CMPB_R = 8000 - 2;
PWM0_2_CMPA_R = 8000 - 1;
PWM0_2_CMPB_R = 8000 - 2;
PWM0_0_CTL_R |= 0x01;
PWM0_1_CTL_R |= 0x01;
PWM0_2_CTL_R |= 0x01;
PWM0_ENABLE_R = 0x00;
}
void SetDutyCycle(int duty_cycle)
{
if(duty_cycle >= 100)
{
PWM0_0_CMPB_R = 8000 - 1;
PWM0_1_CMPB_R = 8000 - 1;
PWM0_2_CMPB_R = 8000 - 1;
}
else if(duty_cycle <= 0)
{
PWM0_0_CMPB_R = 8000 - 2;
PWM0_1_CMPB_R = 8000 - 2;
PWM0_2_CMPB_R = 8000 - 2;
}
else
{
PWM0_0_CMPB_R = (uint16_t)((8000 - ((8000 * duty_cycle) / 100)) - 1);
PWM0_1_CMPB_R = (uint16_t)((8000 - ((8000 * duty_cycle) / 100)) - 1);
PWM0_2_CMPB_R = (uint16_t)((8000 - ((8000 * duty_cycle) / 100)) - 1);
}
}