+/EE445L

embedded systems

git clone https://git.julia.blue/+/EE445L

Joseph RyanModify rendering and fix tick-tock3d310ca

master
1.6 KiB49 linesraw
1#include "timer.h"
2#include "tm4c123gh6pm.h"
3
4void PWM0A_Init(uint16_t period, uint16_t duty) {
5    SYSCTL_RCGCPWM_R |= 0x01;  // 1) activate PWM0
6    SYSCTL_RCGCGPIO_R |= 0x02; // 2) activate port B
7    while ((SYSCTL_PRGPIO_R & 0x02) == 0) {};
8    GPIO_PORTB_AFSEL_R |= 0x40;       // enable alt funct on PB6
9    GPIO_PORTB_PCTL_R &= ~0x0F000000; // configure PB6 as PWM0
10    GPIO_PORTB_PCTL_R |= 0x04000000;
11    GPIO_PORTB_AMSEL_R &= ~0x40; // disable analog functionality on PB6
12    GPIO_PORTB_DEN_R |= 0x40;    // enable digital I/O on PB6
13    SYSCTL_RCC_R =
14        0x00100000 |                    // 3) use PWM divider
15        (SYSCTL_RCC_R & (~0x000E0000)); //    configure for /2 divider
16    PWM0_0_CTL_R = 0;                   // 4) re-loading down-counting mode
17    PWM0_0_GENA_R = 0xC8;               // low on LOAD, high on CMPA down
18    // PB6 goes low on LOAD
19    // PB6 goes high on CMPA down
20    PWM0_0_LOAD_R = period - 1;  // 5) cycles needed to count down to 0
21    PWM0_0_CMPA_R = duty - 1;    // 6) count value when output rises
22    PWM0_0_CTL_R |= 0x00000001;  // 7) start PWM0
23    PWM0_ENABLE_R |= 0x00000001; // enable PB6/M0PWM0
24}
25
26// change duty cycle of PB6
27// duty is number of PWM clock cycles output is high  (2<=duty<=period-1)
28void PWM0A_Duty(uint16_t duty) {
29    PWM0_0_CMPA_R = duty - 1; // 6) count value when output rises
30}
31
32void shhh(void) {
33    timer0_stop();
34    PWM0A_Init(1000000, 0);
35}
36
37void tick(void) {
38    PWM0A_Init(300000, 150000);
39    timer0_start(shhh, 25, false);
40}
41
42void tock(void) {
43    PWM0A_Init(310000, 150000);
44    timer0_start(shhh, 25, false);
45}
46
47void AHHHH(void) {
48    PWM0A_Init(250000, 1250000);
49}