C语言 如何在stm32 h7上设置编码器模式?

pkwftd7m  于 2023-10-16  发布在  其他
关注(0)|答案(1)|浏览(129)

我试图让编码器数据到我的stm32 h7和显示计数器值通过uart。
我可以看到计数器值0通过我的uart终端,但计数器值从来没有上升.
我用的是编码器模式。求你了,帮帮我

static void Encoder_init(void)
{
    RCC->APB2RSTR &= ~(1 <<  1);
    RCC->APB2ENR |= (1 << 1);   //TIM8 clk enable
    TIM8->CR1 &= ~(1 << 0);     //tim8 disable
    TIM8->SMCR |= (0x03 << 0);  //SMS set
    TIM8->CCER &= ~(1 << 1);    //CC1P
    TIM8->CCER &= ~(1 << 5);    //CC2P
    TIM8->CCER &= ~(1<<3);      //CC1NP LOW
    TIM8->CCER &= ~(1<<7);      //CC2NP LOW
    TIM8->CCMR1 |= (1<<0);      //CC1S
    TIM8->CCMR1 |= (1<<8);      //CC2S
    TIM8->PSC = 0;              //prescaler zero
    TIM8->ARR = 0xFFFF;
    TIM8->CR1 |= (1 << 0);      //tim8 enable}
}

void tEncoder(void *pvParameters)
{
    uint8_t encoder_data;
    char buf[4];
    char val;

    RCC->AHB4RSTR &= ~(1 << 2);
    RCC->AHB4ENR |= (1 << 2);
    GPIOC->MODER &= ~(0x03 << 12);
    GPIOC->MODER &= ~(0x03 << 14);
    GPIOC->PUPDR |= (1<<6) | (1<<7);

    Encoder_init();

    while(1) {
        vTaskDelay(1000);
        if(encoder_data!=TIM8->CNT) {
            encoder_data = TIM8->CNT;
            int2str(buf, encoder_data);
            uart7_buffer_putstr(buf);
            SystemPrint("tEncoder counting\n\r");
        }
    }
}
gopyfrb3

gopyfrb31#

您清除了MODER寄存器,但从未设置Alternate功能模式。根据reference manual第578页

GPIOC->MODER |= (GPIO_MODER_MODE6_1 | GPIO_MODER_MODE7_1); // Set mode to Alternate Function 2<<12; 2<<14;

而且您从未清除和设置备用功能低电平寄存器(引脚0-7)reference manual第582页。AF 3应根据STM32 H747 XI的datasheet第95页使用。

GPIOC->AFR[0] &= ~((0xFUL << GPIO_AFRL_AFSEL6_Pos) | (0xFUL << GPIO_AFRL_AFSEL7_Pos)); // Clear 15<<24; 15<<28
 GPIOC->AFR[0] |= (0x3UL << GPIO_AFRL_AFSEL6_Pos) | (0x3UL << GPIO_AFRL_AFSEL7_Pos); // Set AF3 for PC6 and PC7 3<<24; 3<<28

相关问题