一,打開文件(可以隨文下載放置在文檔中打開)。(如下圖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
評論
查看更多