forked from szczys/tiva-c-launchpad-hardware-pwm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
96 lines (82 loc) · 2.97 KB
/
main.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
/*################################################
# Hardware PWM proof of concept using
# the Tiva C Launchpad
#
# Started with example code by
# lawrence_jeff found here:
# http://forum.stellarisiti.com/topic/707-using-hardware-pwm-on-tiva-launchpad/
#
# Altered to use code found on section
# 22.3 of the TivaWare Peripheral Driver
# Library User's Guide found here:
# http://www.ti.com/lit/ug/spmu298a/spmu298a.pdf
#
#
# This example pulses three on-board LEDs
#
#################################################*/
#include "driverlib/pin_map.h"
#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_gpio.h"
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "driverlib/sysctl.h"
#include "driverlib/pin_map.h"
#include "driverlib/gpio.h"
#include "driverlib/pwm.h"
void delayMS(int ms) {
SysCtlDelay( (SysCtlClockGet()/(3*1000))*ms ) ;
}
int
main(void)
{
//Set the clock
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);
//Configure PWM Clock to match system
SysCtlPWMClockSet(SYSCTL_PWMDIV_1);
// Enable the peripherals used by this program.
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1); //The Tiva Launchpad has two modules (0 and 1). Module 1 covers the LED pins
//Configure PF1,PF2,PF3 Pins as PWM
GPIOPinConfigure(GPIO_PF1_M1PWM5);
GPIOPinConfigure(GPIO_PF2_M1PWM6);
GPIOPinConfigure(GPIO_PF3_M1PWM7);
GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3);
//Configure PWM Options
//PWM_GEN_2 Covers M1PWM4 and M1PWM5
//PWM_GEN_3 Covers M1PWM6 and M1PWM7 See page 207 4/11/13 DriverLib doc
PWMGenConfigure(PWM1_BASE, PWM_GEN_2, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);
PWMGenConfigure(PWM1_BASE, PWM_GEN_3, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);
//Set the Period (expressed in clock ticks)
PWMGenPeriodSet(PWM1_BASE, PWM_GEN_2, 320);
PWMGenPeriodSet(PWM1_BASE, PWM_GEN_3, 320);
//Set PWM duty-50% (Period /2)
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_5,100);
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_6,100);
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_7,100);
// Enable the PWM generator
PWMGenEnable(PWM1_BASE, PWM_GEN_2);
PWMGenEnable(PWM1_BASE, PWM_GEN_3);
// Turn on the Output pins
PWMOutputState(PWM1_BASE, PWM_OUT_5_BIT | PWM_OUT_6_BIT | PWM_OUT_7_BIT, true);
//Fade
bool fadeUp = true;
unsigned long increment = 10;
unsigned long pwmNow = 100;
while(1)
{
delayMS(20);
if (fadeUp) {
pwmNow += increment;
if (pwmNow >= 320) { fadeUp = false; }
}
else {
pwmNow -= increment;
if (pwmNow <= 10) { fadeUp = true; }
}
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_5,pwmNow);
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_6,pwmNow);
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_7,pwmNow);
}
}