一, 打开文件(可以随文下载放置在文档中打开)。(如下图1所示)
图1
二,调整虚拟器件,另存工程文件。(如下图2,3,4所示)
图2
图3
图4
三,点击Source Code标签。(如下图5所示)
图5
四,编辑main.c,timer.c,timer.h, key.c,key.h, led.c,led.h 代码如Proteus8.9 仿真STM32407ZGT6系列001(如下图6所示)
图6
五,Main.c 代码:
/* Main.c file generated by New Project wizard
* Author: Ziegler Yin
* Created: 周四 一月 16 2020
* Processor: STM32F407ZGT6ZGT6
* Compiler: GCC for ARM
*/
#include "mfuncs.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "key.h"
#include "timer.h"
#define RT(A , B) (A> 300)? (B= 0): (B=1)
int main(void)
{
u16 pwmval=0;
u8 dir=1;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2
delay_init(168); //初始化延时函数
uart_init(9600);//初始化串口波特率为115200
LED_Init();
KEY_Init();
uprint("大家好!!!\r\n");
uprint("大家来造呼吸机-----。\r\n");
uprint("控制芯片STM32F407ZGT6_008_PWM仿真开始了。\r\n");
LED0= LED1= LED2= 1;
TIM4_PWM_Init(500-1,840-1); //84M/84=1Mhz的计数频率,重装载值500,所以PWM频率为 1M/500=2Khz.
TIM_SetCompare1(TIM4, pwmval);
while(1) //实现比较值从0-300递增,到300后从300-0递减,循环
{
uprint("Arm运行时......\r\n");
if(1==ReadKey(GPIOC, 0x0010)) {
LED0= 1;
uprint("占空比增加......\r\n");
if(dir) pwmval+=10;
RT(pwmval , dir);
}if(1==ReadKey(GPIOC, 0x0020)) {
LED1= 1;
uprint("占空清零......\r\n");
if(dir) pwmval= 0;
RT(pwmval , dir);
}if(1==ReadKey(GPIOC, 0x0040)) {
LED2= 1;
uprint("占空比减少......\r\n");
if(dir) pwmval-=10;
RT(pwmval , dir);
}
TIM_SetCompare1(TIM4, pwmval);
delay_ms(100);
LED0= LED1= LED2= 0;
}
}
timer.c代码:
#include "mfuncs.h"
#include "timer.h"
#include "led.h"
void TIM4_PWM_Init(u32 arr,u32 psc)
{
//此部分需手动修改IO口设置
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //TIM4时钟使能
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); //使能PORTB时钟
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_TIM4); //GPIOB6, 7, 8复用为定时器4
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_TIM4); //GPIOB6, 7, 8复用为定时器4
GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_TIM4); //GPIOB6, 7, 8复用为定时器4
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6| GPIO_Pin_7| GPIO_Pin_8; //GPIOB6, 7, 8
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //速度100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化PB
TIM_TimeBaseStructure.TIM_Prescaler= psc; //定时器分频
TIM_TimeBaseStructure.TIM_CounterMode= TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseStructure.TIM_Period= arr/ 5; //自动重装载值
TIM_TimeBaseStructure.TIM_ClockDivision= TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);//初始化定时器4
//初始化TIM4 Channel1 PWM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性:TIM输出比较极性低
TIM_OC1Init(TIM4, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM14
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable); //使能TIM4在CCR1上的预装载寄存器
TIM_TimeBaseStructure.TIM_Prescaler= psc; //定时器分频
TIM_TimeBaseStructure.TIM_CounterMode= TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseStructure.TIM_Period= arr/ 2; //自动重装载值
TIM_TimeBaseStructure.TIM_ClockDivision= TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);//初始化定时器4
//初始化TIM4 Channel1 PWM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性:TIM输出比较极性低
TIM_OC1Init(TIM4, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM1
TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable); //使能TIM4在CCR1上的预装载寄存器
TIM_TimeBaseStructure.TIM_Prescaler= psc; //定时器分频
TIM_TimeBaseStructure.TIM_CounterMode= TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseStructure.TIM_Period= arr; //自动重装载值
TIM_TimeBaseStructure.TIM_ClockDivision= TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);//初始化定时器14
//初始化TIM4 Channel1 PWM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性:TIM输出比较极性低
TIM_OC1Init(TIM4, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM1 4OC1
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable); //使能TIM4在CCR1上的预装载寄存器
TIM_ARRPreloadConfig(TIM4, ENABLE); //ARPE使能
TIM_Cmd(TIM4, ENABLE); //使能TIM4
}
timer.h代码
#ifndef _TIMER_H
#define _TIMER_H
#include "sys.h"
void TIM4_PWM_Init(u32 arr,u32 psc);
#endif
key.c,key.h, led.c led.h之前的代码都可借鉴,不在多列,以后不再一一罗列,只列main.c 与相关功能的.c, .h代码。
六,点击构建工程按钮,编译工程。(如下图7所示)
图7
七,点击窗口左下方仿真按钮,可见红,黄,绿灯点亮后一会灭,仿真开始,按下KEY_ADD按钮,占空比增加;按下KEY_MINUS按钮,占空比减少;按下KEY_ZERO按钮,占空比清零。虚拟串口,示波器将的状态变化显示在屏。 (如下图8,9,10所示)
图8
图9
图10
八,选择release,点击构建工程按钮,编译工程生成Hex文件。(如下图11所示)
图11
lw
评论
查看更多