金牌会员
- 积分
- 1547
- 金钱
- 1547
- 注册时间
- 2014-7-11
- 在线时间
- 256 小时
|
2金钱
原子哥,我用STM32F407寄存器版本输出PWNM,也输不出PWM波形
#define PWMDAC_GPIO_PORT GPIOA
#define PWMDAC_GPIO_PIN1 SYS_GPIO_PIN6
#define PWMDAC_GPIO_AF 1 // AF1|ÄüÑ¡Ôñ
#define PWMDAC_GPIO_CLK_ENABLE1() do{ RCC->AHB1ENR |= 1 << 0; }while(0) // PA¿úê±Öóê1Äü
#define PWMDAC_TIMX TIM3
#define PWMDAC_TIMX_CHY1 1 // í¨μàY, 1<= Y <=4
#define PWMDAC_TIMX_CCRX1 PWMDAC_TIMX->CCR1 // í¨μàYμÄêä3ö±è½Ï¼Ä′æÆ÷
#define PWMDAC_TIMX_CLK_ENABLE1() do{ RCC->APB1ENR |= 1 << 0; }while(0) // TIM2 ê±Öóê1Äü
#define PWMDAC_GPIO_PORT GPIOA
#define PWMDAC_GPIO_PIN2 SYS_GPIO_PIN7
#define PWMDAC_GPIO_AF 1 // AF1|ÄüÑ¡Ôñ
#define PWMDAC_GPIO_CLK_ENABLE2() do{ RCC->AHB1ENR |= 1 << 0; }while(0) // PA¿úê±Öóê1Äü
#define PWMDAC_TIMX TIM3
#define PWMDAC_TIMX_CHY2 2 // í¨μàY, 1<= Y <=4
#define PWMDAC_TIMX_CCRX2 PWMDAC_TIMX->CCR2 // í¨μàYμÄêä3ö±è½Ï¼Ä′æÆ÷
#define PWMDAC_TIMX_CLK_ENABLE2() do{ RCC->APB1ENR |= 1 << 0; }while(0) // TIM2 ê±Öóê1Äü
void pwmdac_init(uint16_t arr, uint16_t psc)
{
uint8_t chy = PWMDAC_TIMX_CHY1;
PWMDAC_GPIO_CLK_ENABLE1(); /* PWM DAC GPIO ê±Öóê1Äü */
PWMDAC_TIMX_CLK_ENABLE1(); /* PWM DAC ¶¨ê±Æ÷ê±Öóê1Äü */
PWMDAC_GPIO_CLK_ENABLE2(); /* PWM DAC GPIO ê±Öóê1Äü */
PWMDAC_TIMX_CLK_ENABLE2(); /* PWM DAC ¶¨ê±Æ÷ê±Öóê1Äü */
sys_gpio_set(PWMDAC_GPIO_PORT, PWMDAC_GPIO_PIN1,
SYS_GPIO_MODE_AF, SYS_GPIO_OTYPE_PP, SYS_GPIO_SPEED_HIGH, SYS_GPIO_PUPD_PU); /* TIMX PWM CHY òy½ÅÄ£ê½éèÖà */
sys_gpio_af_set(PWMDAC_GPIO_PORT, PWMDAC_GPIO_PIN1, PWMDAC_GPIO_AF); /* IO¿ú¸′óÃ1|ÄüÑ¡Ôñ ±ØDëéèÖöÔ!! */
sys_gpio_set(PWMDAC_GPIO_PORT, PWMDAC_GPIO_PIN2,
SYS_GPIO_MODE_AF, SYS_GPIO_OTYPE_PP, SYS_GPIO_SPEED_HIGH, SYS_GPIO_PUPD_PU); /* TIMX PWM CHY òy½ÅÄ£ê½éèÖà */
sys_gpio_af_set(PWMDAC_GPIO_PORT, PWMDAC_GPIO_PIN2, PWMDAC_GPIO_AF); /* IO¿ú¸′óÃ1|ÄüÑ¡Ôñ ±ØDëéèÖöÔ!! */
PWMDAC_TIMX->ARR = arr; /* é趨¼ÆêyÆ÷×Ô¶ˉÖØ×°Öμ */
PWMDAC_TIMX->SC = psc; /* éèÖÃÔ¤·ÖÆμÆ÷ */
PWMDAC_TIMX->BDTR |= 1 << 15; /* ê1ÄüMOEλ(½öTIM1/8 óD′˼Ä′æÆ÷,±ØDëéèÖÃMOE2ÅÄüêä3öWM), ÆäËûí¨óö¨ê±Æ÷, Õa¸ö
* ¼Ä′æÆ÷êÇÎTD§μÄ, ËùòÔéèÖÃ/2»éèÖÃ2¢2»ó°Ïì½á1û, Îaá˼æèYÕaàïí3ò»¸Ä3ééèÖÃMOEλ
*/
if (chy <= 2)
{
PWMDAC_TIMX->CCMR1 |= 6 << (4 + 8 * (chy - 1)); /* CH1/2 PWMÄ£ê½1 */
PWMDAC_TIMX->CCMR1 |= 1 << (3 + 8 * (chy - 1)); /* CH1/2 ԤװÔØê1Äü */
}
else if (chy <= 4)
{
PWMDAC_TIMX->CCMR2 |= 6 << (4 + 8 * (chy - 3)); /* CH3/4 PWMÄ£ê½1 */
PWMDAC_TIMX->CCMR2 |= 1 << (3 + 8 * (chy - 3)); /* CH3/4 ԤװÔØê1Äü */
}
PWMDAC_TIMX->CCER |= 1 << (4 * (chy - 1)); /* OCy êä3öê1Äü */
PWMDAC_TIMX->CCER |= 0 << (1 + 4 * (chy - 1)); /* OCy ¸ßμçƽóDD§ */
PWMDAC_TIMX->CR1 |= 1 << 7; /* ARPEê1Äü */
PWMDAC_TIMX->CR1 |= 1 << 0; /* ê1Äü¶¨ê±Æ÷TIMX */
}
void pwmdac_set_voltage1(uint16_t vol)
{
double temp = vol;
temp /= 100; /* ËõD¡100±¶, μÃμ½êμ¼êμçÑ1Öμ */
temp = temp * 256 / 3.3; /* ½«μçÑ1×a»»3éPWMÕ¼¿Õ±è */
PWMDAC_TIMX_CCRX1 = temp; /* éèÖÃDÂμÄÕ¼¿Õ±è */
}
void pwmdac_set_voltage2(uint16_t vol)
{
double temp = vol;
temp /= 100; /* ËõD¡100±¶, μÃμ½êμ¼êμçÑ1Öμ */
temp = temp * 256 / 3.3; /* ½«μçÑ1×a»»3éPWMÕ¼¿Õ±è */
PWMDAC_TIMX_CCRX2 = temp; /* éèÖÃDÂμÄÕ¼¿Õ±è */
}
这程序哪里设置的不对,就是数不出PWM波形
|
|