欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

標(biāo)題: STM8單片機(jī)想用按鍵來控制PWM輸出頻率,現(xiàn)在寫好后發(fā)現(xiàn)按鍵無反應(yīng),求大佬 [打印本頁]

作者: wang7128953    時間: 2024-2-18 13:58
標(biāo)題: STM8單片機(jī)想用按鍵來控制PWM輸出頻率,現(xiàn)在寫好后發(fā)現(xiàn)按鍵無反應(yīng),求大佬
#include "STM8S.h"

unsigned char  a=20;

signed long int  pwm_duty;

void delay_ms (int ms) //延時函數(shù)

{

for (int i=0; i<=ms; i++)

for (int j=0; j<120; j++); // Nop = Fosc/4

}




void KEY(void)

{

GPIO_DeInit(GPIOC);//D口默認(rèn)狀態(tài)

GPIO_Init(GPIOC, GPIO_PIN_5, GPIO_MODE_IN_FL_NO_IT);

GPIO_Init(GPIOC, GPIO_PIN_4, GPIO_MODE_IN_FL_NO_IT);


        if (GPIO_ReadInputPin(GPIOC,GPIO_PIN_5) == 0)        //加1按鍵有效
        {
            delay_ms(20);   
            //延時去抖 一般10-20ms
            if (GPIO_ReadInputPin(GPIOC,GPIO_PIN_5) == 0)

            {
                while (GPIO_ReadInputPin(GPIOC,GPIO_PIN_5) == 0);

                if (a < 41)

                  a = a+1;               
            }   
        }

        if (GPIO_ReadInputPin(GPIOC,GPIO_PIN_4) == 0)        //減1按鍵有效
        {
            delay_ms(20);

            if (GPIO_ReadInputPin(GPIOC,GPIO_PIN_4) == 0)

            {
                while (GPIO_ReadInputPin(GPIOC,GPIO_PIN_4) == 0)

                if (a > 1)

                 a =a-1;


            }

}

}

void PWM(void)

{

pwm_duty=2000000/a/30;

CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);//  有些硬件會自動八分頻 導(dǎo)致后面的計(jì)算出現(xiàn)錯誤

CLK_HSICmd(ENABLE);    //打開內(nèi)部的時鐘函數(shù)

TIM2_DeInit(); //TIM2回復(fù)默認(rèn)

GPIO_Init(GPIOD,GPIO_PIN_4,GPIO_MODE_OUT_PP_LOW_FAST);//設(shè)置 Timer2 D4口輸出PWM模式

TIM2_OC1Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE,pwm_duty/2,TIM2_OCPOLARITY_HIGH);

TIM2_TimeBaseInit(TIM2_PRESCALER_8,pwm_duty);//16M晶振

TIM2_OC2PreloadConfig(ENABLE);

TIM2_Cmd(ENABLE);//啟用Timer2定時器
}



void main(void)

{  
  PWM();

while(1)
{
  KEY();   
}

}

#ifdef USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *   where the assert_param error has occurred.
  * @param file: pointer to the source file name
  * @param line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif

/**
  * @}
  */


代碼如上,百思不得其解!求大佬幫忙看看


作者: zhuls    時間: 2024-2-20 12:02
流程不對,你按對鍵后a值沒有反饋到PWM控制中去,a值變了還要pwm_duty才行。

好比你要請客,飯菜在灶臺,老是不端上桌子,客人能吃到熱乎的嗎?




歡迎光臨 (http://www.raoushi.com/bbs/) Powered by Discuz! X3.1