This repository has been archived by the owner on Jan 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pwmgen.c
120 lines (103 loc) · 3.15 KB
/
pwmgen.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
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/debug.h"
#include "driverlib/gpio.h"
#include "driverlib/pwm.h"
#include "driverlib/sysctl.h"
#include "drivers/rit128x96x4.h"
//*****************************************************************************
//
//! \addtogroup example_list
//! <h1>PWM (pwmgen)</h1>
//!
//! This example application utilizes the PWM peripheral to output a 25% duty
//! cycle PWM signal and a 75% duty cycle PWM signal, both at 440 Hz. Once
//! configured, the application enters an infinite loop, doing nothing while
//! the PWM peripheral continues to output its signals.
//
//*****************************************************************************
//*****************************************************************************
//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
#ifdef DEBUG
void
__error__(char *pcFilename, unsigned long ulLine)
{
}
#endif
//*****************************************************************************
//
// This example demonstrates how to setup the PWM block to generate signals.
//
//*****************************************************************************
int
main(void)
{
unsigned long ulPeriod;
//
// Set the clocking to run directly from the crystal.
//
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
SYSCTL_XTAL_8MHZ);
SysCtlPWMClockSet(SYSCTL_PWMDIV_1);
//
// Initialize the OLED display.
//
RIT128x96x4Init(1000000);
//
// Clear the screen and tell the user what is happening.
//
RIT128x96x4StringDraw("Generating PWM", 18, 24, 15);
RIT128x96x4StringDraw("on PF0 and PG1", 18, 32, 15);
//
// Enable the peripherals used by this example.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG);
//
// Set GPIO F0 and G1 as PWM pins. They are used to output the PWM0 and
// PWM1 signals.
//
GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_0);
GPIOPinTypePWM(GPIO_PORTG_BASE, GPIO_PIN_1);
//
// Compute the PWM period based on the system clock.
//
ulPeriod = SysCtlClockGet() / 1200;
//
// Set the PWM period to 440 (A) Hz.
//
PWMGenConfigure(PWM0_BASE, PWM_GEN_0,
PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC);
PWMGenPeriodSet(PWM0_BASE, PWM_GEN_0, ulPeriod);
//
// Set PWM0 to a duty cycle of 25% and PWM1 to a duty cycle of 75%.
//
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0, ulPeriod / 4);
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_1, ulPeriod * 3 / 4);
//
// Enable the PWM0 and PWM1 output signals.
//
PWMOutputState(PWM0_BASE, PWM_OUT_0_BIT | PWM_OUT_1_BIT, true);
//
// Enable the PWM generator.
//
PWMGenEnable(PWM0_BASE, PWM_GEN_0);
//
// Loop forever while the PWM signals are generated.
//
while(1)
{
}
}
void PWMEnable()
{
PWMGenEnable(PWM0_BASE, PWM_GEN_0);
}
void PWMDisable()
{
PWMGenDisable(PWM0_BASE, PWM_GEN_0);
}