tm4c123gh6pm 输出pwm
时间: 2023-05-14 13:06:48 浏览: 163
可以使用以下代码实现tm4c123gh6pm输出PWM:
```c
#include <stdint.h>
#include "tm4c123gh6pm.h"
void PWM_Init(void)
{
SYSCTL_RCGCPWM_R |= 0x02; // Enable PWM1 module clock
SYSCTL_RCGCGPIO_R |= 0x02; // Enable GPIOB module clock
while((SYSCTL_PRGPIO_R & 0x02) == 0){}; // Wait for GPIOB module to be ready
GPIO_PORTB_AFSEL_R |= 0x40; // Enable alternate function for PB6
GPIO_PORTB_PCTL_R |= 0x04000000; // Configure PB6 as PWM1 output
GPIO_PORTB_DEN_R |= 0x40; // Enable digital function for PB6
PWM1_3_CTL_R = 0; // Disable PWM1 generator 3
PWM1_3_GENA_R = 0x0000008C; // Configure PWM1 generator 3, output A
PWM1_3_LOAD_R = 320; // Set PWM period to 320 cycles (1 kHz)
PWM1_3_CMPA_R = 160; // Set PWM duty cycle to 50%
PWM1_3_CTL_R |= 0x00000001; // Enable PWM1 generator 3
PWM1_ENABLE_R |= 0x00000040; // Enable PWM1 output 6
}
```
这段代码使用了PWM1模块的生成器3,将PB6配置为PWM1输出,并设置PWM周期为320个时钟周期(1 kHz),占空比为50%。可以根据需要修改这些参数。
阅读全文