|
資源全部原創(chuàng),有任何問(wèn)題歡迎聯(lián)系
基于stm32f103r6的pwm輸出,再經(jīng)過(guò)RC低通濾波實(shí)現(xiàn)正弦波輸出,使用protues仿真實(shí)現(xiàn)
仿真圖
仿真文件
示例代碼
- #include "pbdata.h"
- u16 fre;
- void RCC_Configuration(void);
- void GPIO_Configuration(void);
- void TIM3_Configuration();
- void Delay (uint32_t nCount)
- {
- for(; nCount != 0; nCount--);
- }
- int main(void)
- {
- u16 arr=42000;
- u16 led_dt = arr/2;
- RCC_Configuration(); //系統(tǒng)時(shí)鐘初始化
- GPIO_Configuration();//端口初始化
- TIM3_Configuration(arr);//定時(shí)器和pwm配置
-
- while(1)
- {
- TIM_SetCompare2(TIM3,led_dt); //用的是TIM3的通道2,輸出PWM 送到相應(yīng)的寄存器中 //滿占空比為900
-
- GPIO_SetBits(GPIOB,GPIO_Pin_5); //LED 發(fā)光
- Delay(0x2ffff);
- GPIO_ResetBits(GPIOB,GPIO_Pin_5);//LED 熄滅
- Delay(0x2ffff);
-
-
- // if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_7)== Bit_RESET)
- // {
- // //LED 發(fā)光
- // GPIO_SetBits(GPIOB,GPIO_Pin_5);
- // }
- // else
- // {
- // //LED 熄滅
- // GPIO_ResetBits(GPIOB,GPIO_Pin_5);
- // }
- }
- }
- void RCC_Configuration(void)
- {
- SystemInit();
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);//這個(gè)是必須的,仿真軟件必須的
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//端口復(fù)用,一定在APB2的時(shí)鐘線
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
- }
- void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- //PWM
- GPIO_InitStructure.GPIO_Pin= GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //通過(guò)PWM控制,端口為復(fù)用方式輸出
- GPIO_Init(GPIOA,&GPIO_InitStructure);
-
- //LED
- GPIO_InitStructure.GPIO_Pin= GPIO_Pin_5;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
- GPIO_Init(GPIOB,&GPIO_InitStructure);
-
- //BUTTON
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPD;
- GPIO_Init(GPIOC,&GPIO_InitStructure);
- }
- void TIM3_Configuration(arr)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;
- TIM_OCInitTypeDef TIM_OCInitStructure; //PWM的結(jié)構(gòu)體
- GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE);//TIM3 復(fù)用功能部分映射,可以此找到對(duì)應(yīng)的管腳
- //關(guān)于部分映射可以參考“STM32參考手冊(cè)”119面
- //定時(shí)器初始化
-
- TIM_TimeBaseStruct.TIM_Period=arr;//初值
- TIM_TimeBaseStruct.TIM_Prescaler=2;//預(yù)分頻
- //不分頻,在晶振為72MHz的情況下,定時(shí)器執(zhí)行到899后即會(huì)溢出,表示計(jì)數(shù)滿
- TIM_TimeBaseStruct.TIM_ClockDivision=0;
- TIM_TimeBaseStruct.TIM_CounterMode=TIM_CounterMode_Up;//向上
- TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStruct);
-
- //pwm初始化
- TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1; //使用模式1
- TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable; //使能位
- TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High; //設(shè)置輸出極性,一定注意
-
- TIM_OC2Init(TIM3,&TIM_OCInitStructure);
- TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable); //與裝載使能,不會(huì)說(shuō)執(zhí)行一次后就不執(zhí)行了
- TIM_Cmd(TIM3,ENABLE);
-
- }
- /*PWM不是中斷,所以不需要設(shè)置中斷優(yōu)先級(jí) */
復(fù)制代碼
Keil代碼與Proteus8.8仿真下載(注意要是用Proteus8.8這個(gè)版本,其他版本可能會(huì)出現(xiàn)異常):
protues仿真stm32f103輸出PWM.7z
(241.15 KB, 下載次數(shù): 889)
2023-6-9 22:25 上傳
點(diǎn)擊文件名下載附件
|
評(píng)分
-
查看全部評(píng)分
|