c - 尝试在 TM4C123 微控制器上的 PB4 上初始化 PWM
问题描述
正如标题所述,我正在尝试使用 Keil 作为 IDE 在 TM4C123 微控制器上为 PB4 初始化 PWM。到目前为止,我已经能够初始化 PB6 和 PB7,但我不确定为什么 PB4 不工作。你们知道我做错了什么吗?在此先感谢您的帮助。我已经发布了用于初始化端口 B6 的工作代码以及不适用于端口 B4 的工作代码。
// PWM clock rate = processor clock rate/SYSCTL_RCC_PWMDIV
// = BusClock/16
// = 80 MHz/16 = 5 MHz
// Output on PB7/M0PWM1
void PWM0B4_Init(uint16_t period, uint16_t duty)
{
volatile unsigned long delay;
SYSCTL_RCGCPWM_R |= 0x01; // 1) activate PWM0
SYSCTL_RCGCGPIO_R |= 0x02; // 2) activate port B
// delay = SYSCTL_RCGCGPIO_R; // allow time to finish activating
while((SYSCTL_PRGPIO_R&0x02) == 0){};
GPIO_PORTB_AFSEL_R |= 0x10; // enable alt funct on PB4
GPIO_PORTB_PCTL_R &= ~0x000F000; // configure PB4 as M0PWM2
GPIO_PORTB_PCTL_R |= 0x0004000;
GPIO_PORTB_AMSEL_R &= ~0x10; // disable analog functionality on PB4
GPIO_PORTB_DEN_R |= 0x10; // enable digital I/O on PB4 ** double check **
SYSCTL_RCC_R = 0x00100000 | // 3) use PWM divider
(SYSCTL_RCC_R & (~0x000C0000)); // configure for /16 divider
PWM0_1_CTL_R = 0; // 4) re-loading down-counting mode
PWM0_1_GENB_R = (PWM_1_GENB_ACTCMPBD_ONE|PWM_1_GENB_ACTLOAD_ZERO);
// PB4 goes low on LOAD
// PB4 goes high on CMPB down
PWM0_1_LOAD_R = period - 1; // 5) cycles needed to count down to 0
PWM0_1_CMPB_R = duty - 1; // 6) count value when output rises
PWM0_1_CTL_R |= 0x00000001; // 7) start PWM0
PWM0_ENABLE_R |= 0x00000004; // enable PB4/M0PWM2 **** check
}
// change duty cycle of PB4
// duty is number of PWM clock cycles output is high (2<=duty<=period-1)
void PWM0B4_Duty(uint16_t duty)
{
PWM0_1_CMPB_R = duty - 1; // 6) count value when output rises
}
void PWM0B6_Init(uint16_t period, uint16_t duty)
{
SYSCTL_RCGCPWM_R |=0x01; // 1) activate PWM0
SYSCTL_RCGCGPIO_R|=0x02; // 2) activate portB
while ((SYSCTL_PRGPIO_R&0x02) == 0) {};
GPIO_PORTB_AFSEL_R |= 0x40; // enable alt funct on PB6
GPIO_PORTB_PCTL_R &= ~0x0F000000; // configure PB6 as PWM0
GPIO_PORTB_PCTL_R |= 0x04000000;
GPIO_PORTB_AMSEL_R &= ~0x40; // disable analog functionality on PB6
GPIO_PORTB_DEN_R |= 0x40; // enable digital I/O on PB6
SYSCTL_RCC_R = 0x00100000 | // 3) use PWM divider
(SYSCTL_RCC_R & (~0x000C0000)); // configure for /16 divider to C
PWM0_0_CTL_R = 0; // 4) re-loading down-counting mode
PWM0_0_GENA_R = 0xC8; // low on LOAD, high on CMPA down
// PB6 goes low on LOAD
// PB6 goes high on CMPA down
PWM0_0_LOAD_R = period - 1; // 5) cycles needed to count down to 0
PWM0_0_CMPA_R = duty - 1; // 6) count value when output rises
PWM0_0_CTL_R |= 0x00000001; // 7) start PWM0
PWM0_ENABLE_R |= 0x00000001; // enable PB6/M0PWM0
}
// change duty cycle of PB6
// duty is number of PWM clock cycles output is high (2<=duty<=period-1)
void PWM0B6_Duty(uint16_t duty)
{
PWM0_0_CMPA_R = duty - 1; // 6) count value when output rises
}
解决方案
推荐阅读
- php - 使用 PHP 的简单 HTML dom 解析器删除一行
- flutter - 颤振中的 CircularProgressIndicator 不断旋转
- prometheus - windows_exporter 服务在服务器重启后断开与 prometheus 的连接
- big-o - 算法anaylisys:是否存在函数f,g使得f = O(g)也不f = Ω(g)?
- java - 如何在运动流中找到平均记录大小?
- c++ - 在这个链接列表中,为什么它不允许我再次运行并创建另一个节点我的代码中的错误是什么?
- angular - Angular : NavigationEnd is not getting triggered
- angular - How can I get values from dynamically created ngx-star-rating in Angular 11?
- reactjs - Convert Element or SVGSVGElement to string and keep mouseover, mouseout events
- python - 使用python从couchbase集合中提取文档