標(biāo)題: protues仿真stm32f103輸出PWM 附程序 [打印本頁]

作者: rnaker    時(shí)間: 2019-6-29 09:07
標(biāo)題: protues仿真stm32f103輸出PWM 附程序
1、注意protues版本應(yīng)當(dāng)在8.7版本以上,8.6版本聯(lián)想后可能會(huì)出現(xiàn)IO讀入不能讓那個(gè)實(shí)現(xiàn)問題。
2、注意設(shè)置好電網(wǎng),把初始的VSS和VDD添加進(jìn)組。
3、注意設(shè)置好芯片界面下的默認(rèn)晶振值,外接電路晶振沒有作用。

仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)


單片機(jī)源程序如下:
  1. #include "pbdata.h"

  2. u16 fre;
  3. void RCC_Configuration(void);
  4. void GPIO_Configuration(void);
  5. void TIM3_Configuration();

  6. void  Delay (uint32_t nCount)
  7. {
  8.   for(; nCount != 0; nCount--);
  9. }

  10. int main(void)
  11. {
  12.      u16 arr=42000;
  13.      u16 led_dt = arr/2;

  14.                  RCC_Configuration();        //系統(tǒng)時(shí)鐘初始化
  15.                  GPIO_Configuration();//端口初始化
  16.                  TIM3_Configuration(arr);//定時(shí)器和pwm配置
  17.         
  18.                  while(1)
  19.                  {        
  20.                                 TIM_SetCompare2(TIM3,led_dt); //用的是TIM3的通道2,輸出PWM         送到相應(yīng)的寄存器中   //滿占空比為900               
  21.                         

  22.                                                 GPIO_SetBits(GPIOB,GPIO_Pin_5);        //LED 發(fā)光
  23.                                                         Delay(0x2ffff);
  24.                                                 GPIO_ResetBits(GPIOB,GPIO_Pin_5);//LED 熄滅
  25.                                                         Delay(0x2ffff);
  26.                                                 
  27.                               
  28. //                                        if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_7)== Bit_RESET)
  29. //                                                        {
  30. //                                                                //LED 發(fā)光
  31. //                                                                GPIO_SetBits(GPIOB,GPIO_Pin_5);
  32. //                                                         }
  33. //                                        else
  34. //                                                        {
  35. //                                                                //LED 熄滅                                 
  36. //                                                                GPIO_ResetBits(GPIOB,GPIO_Pin_5);
  37. //                                                         }
  38.                  }        
  39. }

  40. void RCC_Configuration(void)
  41. {
  42.   SystemInit();

  43.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);//這個(gè)是必須的,仿真軟件必須的
  44.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
  45.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//端口復(fù)用,一定在APB2的時(shí)鐘線
  46.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  47.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
  48.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
  49. }

  50. void GPIO_Configuration(void)
  51. {
  52.   GPIO_InitTypeDef GPIO_InitStructure;
  53.          
  54. //PWM
  55.         GPIO_InitStructure.GPIO_Pin= GPIO_Pin_7;
  56.         GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  57.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;      //通過PWM控制,端口為復(fù)用方式輸出
  58.         GPIO_Init(GPIOA,&GPIO_InitStructure);
  59.         
  60. //LED
  61.         GPIO_InitStructure.GPIO_Pin= GPIO_Pin_5;
  62.         GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  63.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;      
  64.         GPIO_Init(GPIOB,&GPIO_InitStructure);
  65.         
  66. //BUTTON
  67.         GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7;
  68.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPD;
  69.         GPIO_Init(GPIOC,&GPIO_InitStructure);
  70. }

  71. void TIM3_Configuration(arr)
  72. {
  73.         TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;
  74.         TIM_OCInitTypeDef TIM_OCInitStructure;             //PWM的結(jié)構(gòu)體

  75.         GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE);//TIM3 復(fù)用功能部分映射,可以此找到對(duì)應(yīng)的管腳
  76.         //關(guān)于部分映射可以參考“STM32參考手冊(cè)”119面
  77.         //定時(shí)器初始化
  78.         
  79.         TIM_TimeBaseStruct.TIM_Period=arr;//初值
  80.         TIM_TimeBaseStruct.TIM_Prescaler=2;//預(yù)分頻
  81.         //不分頻,在晶振為72MHz的情況下,定時(shí)器執(zhí)行到899后即會(huì)溢出,表示計(jì)數(shù)滿
  82.         TIM_TimeBaseStruct.TIM_ClockDivision=0;
  83.         TIM_TimeBaseStruct.TIM_CounterMode=TIM_CounterMode_Up;//向上
  84.         TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStruct);
  85.         

  86.         //pwm初始化
  87.         TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;             //使用模式1
  88.         TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable; //使能位
  89.         TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;     //設(shè)置輸出極性,一定注意

  90.         
  91.         TIM_OC2Init(TIM3,&TIM_OCInitStructure);
  92.   TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable);            //與裝載使能,不會(huì)說執(zhí)行一次后就不執(zhí)行了
  93.         TIM_Cmd(TIM3,ENABLE);
  94.                  
  95. }
  96. /*PWM不是中斷,所以不需要設(shè)置中斷優(yōu)先級(jí) */
復(fù)制代碼

所有資料51hei提供下載:
protues仿真stm32f103輸出PWM.7z (241.09 KB, 下載次數(shù): 453)


作者: 簡單的就好    時(shí)間: 2019-7-1 16:59
正需要學(xué)習(xí)
作者: 回來哥啦    時(shí)間: 2019-11-7 22:21
為什么重映射到的是PB5,但是輸出pwm的是PA7?
作者: zhangdaoyong    時(shí)間: 2019-11-26 19:47
回來哥啦 發(fā)表于 2019-11-7 22:21
為什么重映射到的是PB5,但是輸出pwm的是PA7?

我為什么仿真不了,下面的時(shí)間走的太慢了
作者: 撼地神牛雨    時(shí)間: 2019-12-26 10:12
需要學(xué)習(xí)中
作者: hsiaoshi    時(shí)間: 2019-12-26 11:40
非常
需要學(xué)習(xí)
作者: PYY2    時(shí)間: 2020-4-15 16:24
感謝大佬
作者: 紫蘇2020    時(shí)間: 2020-5-14 10:55
請(qǐng)問這是stm32f103r6嗎
作者: 紫蘇2020    時(shí)間: 2020-5-15 12:49
由于資料無法正常解壓所以請(qǐng)問pbdata.h中包含什么內(nèi)容?
作者: Longan.Wang    時(shí)間: 2021-11-30 16:08
下載看看有沒有驚喜
作者: xuefei    時(shí)間: 2022-11-27 17:55
正在學(xué)習(xí)PROTUES
作者: sjh7366    時(shí)間: 2023-5-26 17:18
直接翻譯的的程序吧,這注釋




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