问题描述
作为标题,我试图使用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
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)