Interfacing Tiva Board With Example Codes
Interfacing Tiva Board With Example Codes
-
PWM+GPIO
#include "TM4C123GH6PM.h"
int main(void)
{
SYSCTL->RCGCPWM |= 2; // Enable clock to PWM1 module
SYSCTL->RCGCGPIO |= 0x20; // Enable clock for Port F
SYSCTL->RCC |= 0x00100000 | 0x00080000; // Enable PWM divider and set divider
to 8 (16MHz / 8 = 2MHz)
GPIOF->AFSEL |= (1 << 2); // Enable alternate function on PF2
GPIOF->PCTL &= ~0x00000F00; // Clear previous function settings
on PF2
GPIOF->PCTL |= 0x00000500; // Set PF2 to M1PWM6 function
GPIOF->DEN |= (1 << 2); // Enable digital function on PF2
PWM1->_3_CTL &= ~(1 << 0); // Disable Generator 3 while setting
it
PWM1->_3_CTL &= ~(1 << 1); // Select down-count mode
PWM1->_3_GENA = 0x0000008C; // Set high at LOAD, low at CMPA (in
down count)
PWM1->_3_LOAD = 2000 - 1; // Set period to 1ms
PWM1->_3_CMPA = 1600 - 1; // Set duty to 20% (ON for 0.2ms)
PWM1->_3_CTL |= (1 << 0); // Enable Generator 3
PWM1->ENABLE |= (1 << 6); // Enable PWM output on M1PWM6 (PF2)
while (1)
{
// PWM keeps running on PF2
}
}