-
Notifications
You must be signed in to change notification settings - Fork 0
/
pwmControl.c
executable file
·157 lines (129 loc) · 4.8 KB
/
pwmControl.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* pwmControl.c
*
* Created on: Mar 18, 2018
* Author: maxtalley
*/
/**********************************************************
*
* pwmGen.c - Example code which generates a single PWM
* output on J4-05 (M0PWM7) with duty cycle fixed and
* the frequency controlled by UP and DOWN buttons in
* the range 50 Hz to 400 Hz.
* 2017: Modified for Tiva and using straightforward, polled
* button debouncing implemented in 'buttons4' module.
*
* P.J. Bones UCECE
* Last modified: 7.2.2018
**********************************************************/
#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/pin_map.h" //Needed for pin configure
#include "driverlib/debug.h"
#include "driverlib/gpio.h"
#include "driverlib/pwm.h"
#include "driverlib/sysctl.h"
#include "pwmControl.h"
/**********************************************************
* Constants
**********************************************************/
// PWM configuration
#define PWM_DIVIDER_CODE SYSCTL_PWMDIV_4
#define PWM_DIVIDER 4
#define PWM_CONST_FREQ 200
// Duty Cycle Configuration
#define DUTY_CYCLE_MAX 100
#define DUTY_CYCLE_START 0
// PWM Hardware Details M0PWM7 (gen 3)
// ---Main Rotor PWM: PC5, J4-05
#define PWM_MAIN_BASE PWM0_BASE
#define PWM_MAIN_GEN PWM_GEN_3
#define PWM_MAIN_OUTNUM PWM_OUT_7
#define PWM_MAIN_OUTBIT PWM_OUT_7_BIT
#define PWM_MAIN_PERIPH_PWM SYSCTL_PERIPH_PWM0
#define PWM_MAIN_PERIPH_GPIO SYSCTL_PERIPH_GPIOC
#define PWM_MAIN_GPIO_BASE GPIO_PORTC_BASE
#define PWM_MAIN_GPIO_CONFIG GPIO_PC5_M0PWM7
#define PWM_MAIN_GPIO_PIN GPIO_PIN_5
// PWM Hardware Details M1PWM5
// --Tail Rotor PWM: PF1, J3-10
#define PWM_TAIL_BASE PWM1_BASE
#define PWM_TAIL_GEN PWM_GEN_2
#define PWM_TAIL_OUTNUM PWM_OUT_5
#define PWM_TAIL_OUTBIT PWM_OUT_5_BIT
#define PWM_TAIL_PERIPH_PWM SYSCTL_PERIPH_PWM1
#define PWM_TAIL_PERIPH_GPIO SYSCTL_PERIPH_GPIOF
#define PWM_TAIL_GPIO_BASE GPIO_PORTF_BASE
#define PWM_TAIL_GPIO_CONFIG GPIO_PF1_M1PWM5
#define PWM_TAIL_GPIO_PIN GPIO_PIN_1
/*********************************************************
* initialisePWM
* M0PWM7 (J4-05, PC5) is used for the main rotor motor
*********************************************************/
void
initialiseMainPWM (void)
{
SysCtlPeripheralEnable(PWM_MAIN_PERIPH_PWM);
SysCtlPeripheralEnable(PWM_MAIN_PERIPH_GPIO);
GPIOPinConfigure(PWM_MAIN_GPIO_CONFIG);
GPIOPinTypePWM(PWM_MAIN_GPIO_BASE, PWM_MAIN_GPIO_PIN);
PWMGenConfigure(PWM_MAIN_BASE, PWM_MAIN_GEN,
PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC);
// Set the initial PWM parameters
setMainPWM (DUTY_CYCLE_START);
PWMGenEnable(PWM_MAIN_BASE, PWM_MAIN_GEN);
// Disable the output. Repeat this call with 'true' to turn O/P on.
PWMOutputState(PWM_MAIN_BASE, PWM_MAIN_OUTBIT, false);
}
void
initialiseTailPWM (void)
{
SysCtlPeripheralEnable(PWM_TAIL_PERIPH_PWM);
SysCtlPeripheralEnable(PWM_TAIL_PERIPH_GPIO);
GPIOPinConfigure(PWM_TAIL_GPIO_CONFIG);
GPIOPinTypePWM(PWM_TAIL_GPIO_BASE, PWM_TAIL_GPIO_PIN);
PWMGenConfigure(PWM_TAIL_BASE, PWM_TAIL_GEN,
PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC);
// Set the initial PWM parameters
setTailPWM (DUTY_CYCLE_START);
PWMGenEnable(PWM_TAIL_BASE, PWM_TAIL_GEN);
// Disable the output. Repeat this call with 'true' to turn O/P on.
PWMOutputState(PWM_TAIL_BASE, PWM_TAIL_OUTBIT, false);
}
// Turn on/off the main PWM generator
void setMainPWMOutput(uint16_t main_output)
{
// Disable the output. Repeat this call with 'true' to turn O/P on.
PWMOutputState(PWM_MAIN_BASE, PWM_MAIN_OUTBIT, main_output);
}
// turn on/off the tail PWM generator
void setTailPWMOutput(uint16_t tail_output)
{
// Disable the output. Repeat this call with 'true' to turn O/P on.
PWMOutputState(PWM_TAIL_BASE, PWM_TAIL_OUTBIT, tail_output);
}
/********************************************************
* Function to set the freq, duty cycle of M0PWM7
********************************************************/
void
setMainPWM (uint16_t ui16Duty)
{
// Calculate the PWM period corresponding to the freq.
uint32_t ui32Period =
SysCtlClockGet() / PWM_DIVIDER / PWM_CONST_FREQ;
PWMGenPeriodSet(PWM_MAIN_BASE, PWM_MAIN_GEN, ui32Period);
PWMPulseWidthSet(PWM_MAIN_BASE, PWM_MAIN_OUTNUM,
ui32Period * ui16Duty / 100);
}
void
setTailPWM (uint16_t ui16Duty)
{
// Calculate the PWM period corresponding to the freq.
uint32_t ui32Period =
SysCtlClockGet() / PWM_DIVIDER / PWM_CONST_FREQ;
PWMGenPeriodSet(PWM_TAIL_BASE, PWM_TAIL_GEN, ui32Period);
PWMPulseWidthSet(PWM_TAIL_BASE, PWM_TAIL_OUTNUM,
ui32Period * ui16Duty / 100);
}