STM32實現呼吸燈

具體目標

利用延時函數模擬PWM輸出,變佔空比實現呼吸燈

源碼

#define DeyTim  5000
void LED()
{
    int i=1;
    GPIO_InitTypeDef GPIO_InitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE |RCC_APB2Periph_GPIOB, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(GPIOE, &GPIO_InitStructure);


    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    while(1)
    {       
        for(i=0;i<DeyTim;i+=10)
        {
        GPIO_WriteBit(GPIOE, GPIO_Pin_2, Bit_SET);
        GPIO_WriteBit(GPIOE, GPIO_Pin_3, Bit_RESET);
        delay_us(i);
        GPIO_WriteBit(GPIOE, GPIO_Pin_2, Bit_RESET);
        GPIO_WriteBit(GPIOE, GPIO_Pin_3, Bit_SET);
        delay_us(DeyTim-i);     //此處注意一定要是 DeyTim - i 而不能是DeyTim 保證燈亮滅時間之和保持不變 從而實現變佔空比
        }


    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章