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