今日头条
一,打开文件(可以随文下载放置在文档中打开)。(如下图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)
uint32_t gt_fig(u8 sts){
if(1== sts) return TIM_GetCapture1(TIM4);
if(2== sts) return TIM_GetCapture2(TIM4);
if(3== sts) return TIM_GetCapture3(TIM4);
if(4== sts) return TIM_GetCapture4(TIM4);
}
void st_fig(u8 sts, uint32_t gt){
if(1== sts) TIM_SetCompare1(TIM4, gt);
if(2== sts) TIM_SetCompare2(TIM4, gt);
if(3== sts) TIM_SetCompare3(TIM4, gt);
if(4== sts) TIM_SetCompare4(TIM4, gt);
}
int main(void)
{
u16 psc=840, per=600, idc[4]= {500, 200, 300, 400};
u16 umd, pwmval=0;
u8 status= 0, dir= 1;
int gtln= 0;
char *wd, *wds, lnslt[5]={'_', 'A', 'B', 'C', 'D'};
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2
delay_init(168); //初始化延时函数
uart_init(9600);//初始化串口波特率为115200
LED_Init();
KEY_Init();
uprint("大家好!!!\r\n");
uprint("大家来造呼吸机-----。\r\n");
LED0= LED1= LED2= 1;
Pwm_Init(psc, per);
TIM_SetCompare1(TIM4, idc[0]);
TIM_SetCompare2(TIM4, idc[1]);
TIM_SetCompare3(TIM4, idc[2]);
TIM_SetCompare4(TIM4, idc[3]);
uprint("控制芯片STM32F407ZGT6_008_PWM仿真开始了。\r\n");
delay_ms(100);
while(1)
{
wd[0]=lnslt[status];
wd[1]= 0;
uprint("Arm运行时......占空比");
uprint(wd);
uprint("线路可调整状态:\r\n");
if(1==ReadKey(GPIOC, 0x0080)) {
status++;
delay_ms(20);
if(status> 4) status= 0;
wd[0]=lnslt[status];
wd[1]= 0;
uprint("线路调整状态......占空比");
uprint(wd);
uprint("线路可调整状态:\r\n");
}
if(1==ReadKey(GPIOC, 0x0010)) {
LED0= 1;
pwmval= gt_fig(status);
gtln= nmtoa(wds, 10, pwmval);
if(status>0){
if(per< (pwmval+ 50)) {
uprint("线路调整, 占空值到达最高值 ......\r\n");
st_fig(status, (per+ 1));
}
else{
LED0= 1;
pwmval= gt_fig(status);
gtln= nmtoa(wds, 10, pwmval);
umd= pwmval +50;
uprint("线路调整, 占空值由 ");
uprint(wds);
uprint(" 增加50");
uprint(" ...\r\n ");
st_fig(status, umd);
}
}
}
if(1==ReadKey(GPIOC, 0x0020)) {
LED1= 1;
delay_ms(20);
pwmval= gt_fig(status);
gtln= nmtoa(wds, 10, pwmval);
uprint("Arm运行时......占空比");
uprint(wd);
uprint("线路调整, 由 ");
uprint(wds);
uprint(" 清零 ");
uprint(" ...\r\n ");
st_fig(status, 0);
}
if(1==ReadKey(GPIOC, 0x0040)) {
LED2= 1;
pwmval= gt_fig(status);
gtln= nmtoa(wds, 10, pwmval);
if(pwmval> 50) umd= pwmval -50;
else umd= 1;
uprint("线路调整, 占空值由 ");
uprint(wds);
uprint(" 减少50");
uprint(" ...\r\n ");
st_fig(status, umd);
}
LED0= LED1= LED2= 0;
delay_ms(100);
}
}
timer.c代码:
#include "mfuncs.h"
#include "timer.h"
#include "led.h"
void PWM_GPIO(void){
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_TIM3);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_TIM4);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_TIM4);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_TIM4);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_TIM4);
}
// PWM 定时器配置
void Pwm_Init(u16 TimerPrescaler, u32 TimerPeriod)
{
PWM_GPIO();
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4 , ENABLE);
TIM_TimeBaseStructure.TIM_Prescaler = TimerPrescaler-1; // 时钟预分频数
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
TIM_TimeBaseStructure.TIM_Period = TimerPeriod-1; // 自动重装载寄存器的值
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; //重复寄存器,用于自动更新pwm占空比
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //使能该通道输出
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //设置互补端输出极性
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //使能互补端输出
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; //死区后输出状态??
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; //死区后互补端输出状态??
/* TIM1 counter enable */
TIM_OCInitStructure.TIM_Pulse = 0; //设置占空比时间
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = 0; //设置占空比时间
TIM_OC2Init(TIM4, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = 0; //设置占空比时间
TIM_OC3Init(TIM4, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = 0; //设置占空比时间
TIM_OC4Init(TIM4, &TIM_OCInitStructure);
TIM_Cmd(TIM4, ENABLE);
TIM_CtrlPWMOutputs(TIM4, ENABLE);
}
timer.h代码
#ifndef _TIMER_H
#define _TIMER_H
#include "sys.h"
void PWM_GPIO(void);
void Pwm_Init(u16 TimerPrescaler, u32 TimerPeriod);
#endif
key.c,key.h, led.c led.h之前的代码都可借鉴,不在多列,以后不再一一罗列,只列main.c 与相关功能的.c, .h代码。
六,点击构建工程按钮,编译工程。(如下图7所示)
图7
七,点击窗口左下方仿真按钮,可见红,黄,绿灯点亮后一会灭,仿真开始,按下SELECT按钮,选择要调整的线路,按下KEY_ADD占空比增加;按下KEY_MINUS按钮,占空比减少;按下KEY_ZERO按钮,占空比清零。虚拟串口,示波器将的状态变化显示在屏。 (如下图8,9,10所示)
图8
图9
图10
八,选择release,点击构建工程按钮,编译工程生成Hex文件。(如下图11所示)
图11
lw
全部0条评论
快来发表一下你的评论吧 !