久久人妻精品人妻视频五月天一区二区三区,,自慰午夜喷水久久久av国产成人a视频高在线,,欧美日韩免费专区在线97精品国产手机

  • <cite id="cwywg"></cite>
    <button id="cwywg"></button>
  • <button id="cwywg"><source id="cwywg"></source></button>
    <button id="cwywg"><tbody id="cwywg"></tbody></button>
  • <button id="cwywg"></button>
  • 
    
  • logo logo
    • BANNER
    • BANNER
    • BANNER
    您的位置 : 首頁 > 新聞資訊 > 技術(shù)支持
    STM32單片機(6) PWM輸出實驗
    發(fā)布者:江蘇瑞易通電子科技有限公司 人氣:640 發(fā)布日期:2021-04-23 10:10:40
    1. /******************************************************************************* 

    2. *    

    3. * 軟件功能:  PWM輸出實驗 

    4. *  

    5. *******************************************************************************/  

    6. #include "stm32f10x.h"  

    7. #include "delay.h"  

    8.   

    9.   

    10. void RCC_Configuration(void);  

    11. void GPIO_Configuration(void);  

    12. void NVIC_Configuration(void);  

    13. void TIM3_Configuration(u16 arr,u16 psc);  

    14.   

    15. /************************************************* 

    16. 函數(shù): int main(void) 

    17. 功能: main主函數(shù) 

    18. 參數(shù): 無 

    19. 返回: 無 

    20. **************************************************/  

    21. int main(void)  

    22. {  

    23.   u8 led_direction=1;  

    24.   u16 led_brightness=0;  

    25.   

    26.   RCC_Configuration();  

    27.   GPIO_Configuration();  

    28.   TIM3_Configuration(499,71);   

    29.   delay_init(72);  

    30.   GPIO_ResetBits(GPIOB,GPIO_Pin_5);   

    31.   while(1)  

    32.   {  

    33.       delay_ms(10);  

    34.       if(1==led_direction) led_brightness++;  

    35.       else  led_brightness--;  

    36.   

    37.       if(led_brightness>499)  led_direction=0;  

    38.       if(led_brightness==0)   led_direction=1;  

    39.   

    40.       TIM_SetCompare2(TIM3, led_brightness);  

    41.   }     

    42. }  

    43.   

    44.   

    45. /************************************************* 

    46. 函數(shù): void RCC_Configuration(void) 

    47. 功能: 復(fù)位和時鐘控制 配置 

    48. 參數(shù): 無 

    49. 返回: 無 

    50. **************************************************/  

    51. void RCC_Configuration(void)  

    52. {  

    53.   ErrorStatus HSEStartUpStatus;                    //定義外部高速晶體啟動狀態(tài)枚舉變量  

    54.   RCC_DeInit();                                    //復(fù)位RCC外部設(shè)備寄存器到默認值  

    55.   RCC_HSEConfig(RCC_HSE_ON);                       //打開外部高速晶振  

    56.   HSEStartUpStatus = RCC_WaitForHSEStartUp();      //等待外部高速時鐘準備好  

    57.   if(HSEStartUpStatus == SUCCESS)                  //外部高速時鐘已經(jīng)準別好  

    58.   {  

    59.     FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //開啟FLASH預(yù)讀緩沖功能,加速FLASH的讀取。所有程序中必須的用法.位置:RCC初始化子函數(shù)里面,時鐘起振之后  

    60.     FLASH_SetLatency(FLASH_Latency_2);                    //flash操作的延時  

    61.           

    62.     RCC_HCLKConfig(RCC_SYSCLK_Div1);               //配置AHB(HCLK)時鐘等于==SYSCLK  

    63.     RCC_PCLK2Config(RCC_HCLK_Div1);                //配置APB2(PCLK2)鐘==AHB時鐘  

    64.     RCC_PCLK1Config(RCC_HCLK_Div2);                //配置APB1(PCLK1)鐘==AHB1/2時鐘  

    65.            

    66.     RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);  //配置PLL時鐘 == 外部高速晶體時鐘 * 9 = 72MHz  

    67.     RCC_PLLCmd(ENABLE);                                   //使能PLL時鐘  

    68.      

    69.     while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)    //等待PLL時鐘就緒  

    70.     {  

    71.     }  

    72.     RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);            //配置系統(tǒng)時鐘 = PLL時鐘  

    73.     while(RCC_GetSYSCLKSource() != 0x08)                  //檢查PLL時鐘是否作為系統(tǒng)時鐘  

    74.     {  

    75.     }  

    76.   }  

    77.     

    78.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);  //允許 GPIOB、AFIO端口復(fù)用時鐘  

    79.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //時鐘使能  

    80. }  

    81.   

    82. /************************************************* 

    83. 函數(shù): void GPIO_Configuration(void) 

    84. 功能: GPIO配置 

    85. 參數(shù): 無 

    86. 返回: 無 

    87. **************************************************/  

    88. void GPIO_Configuration(void)  

    89. {  

    90.   GPIO_InitTypeDef GPIO_InitStructure;        //定義GPIO初始化結(jié)構(gòu)體  

    91.   

    92.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;   

    93.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   

    94.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//復(fù)用輸出   

    95.   GPIO_Init(GPIOB, &GPIO_InitStructure);       //PB用于輸出控制LED燈  

    96.   

    97. }  

    98.   

    99.   

    100. void TIM3_Configuration(u16 arr,u16 psc)      //TIM3定時器配置     

    101. {  

    102.     TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;  

    103.     TIM_OCInitTypeDef TIM_OCInitStructure;  

    104.   

    105.     GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE);//端口映射,參考STM32中文手冊P119和庫函數(shù)手冊P132  PB4  

    106.   

    107.     TIM_TimeBaseStructure.TIM_Period = arr; //設(shè)置在下一個更新事件裝入活動的自動重裝載寄存器周期的值     

    108.     TIM_TimeBaseStructure.TIM_Prescaler =psc; //設(shè)置用來作為TIMx時鐘頻率除數(shù)的預(yù)分頻值  

    109.     TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //設(shè)置時鐘分割:TDTS = Tck_tim  

    110.     TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上計數(shù)模式  

    111.     TIM_TimeBaseInit(TIM3, & TIM_TimeBaseStructure); //根據(jù)指定的參數(shù)初始化TIMx的時間基數(shù)單位  

    112.     //((1+71[TIM_Prescaler] )/72M)*(1+499[TIM_Period] )=500us   

    113.   

    114.   

    115.     /* Configures the TIM3 in PWM Mode */  

    116.     TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;  

    117.     TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  

    118.     TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;  

    119.   

    120.     TIM_OC2Init(TIM3, & TIM_OCInitStructure);       //此處用比較 2 寄存器,對應(yīng)main函數(shù) ,也可設(shè)置為    比較 1 寄存器  

    121.   

    122.     TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);   //預(yù)裝載器使能  

    123.   

    124.     //ARR預(yù)裝載緩沖器使能    

    125.     TIM_ARRPreloadConfig(TIM3, ENABLE);  

    126.     TIM_Cmd(TIM3, ENABLE);  //使能TIMx  

    127. }  


    返回新聞列表