?一,打開文件(可以隨文下載放置在文檔中打開)。(如下圖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);
}
void new_Pwm(u16 *tmbuf, u16 * mdc) {
?Pwm_Init(*(tmbuf+0), *(tmbuf+1));
?TIM_SetCompare1(TIM4, mdc[0]);
?TIM_SetCompare2(TIM4, mdc[1]);
?TIM_SetCompare3(TIM4, mdc[2]);
?Pwm_Init3(*(tmbuf+2), *(tmbuf+3));
?TIM_SetCompare2(TIM3, mdc[1]);
}
int main(void)
{
?u16 psc=840, per=600, idc[4]= {500, 200, 300, 400};
?u16 umd, pwmval=0, tm43[2][2]= {840, 600, 1680, 1200};???
?u8 status= 0, dir= 1, chgFry[1]= {0, 0};
?int gtln= 0;
?char *wd, *wds, *lnslt[7]={"未選", "A線路", "B線路", "C線路", "D線路", "變頻1-A,B,C倍頻或初始狀態", "變頻2-D四倍頻或初始狀態"};
?NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//設置系統中斷優先級分組2
?delay_init(168);? //初始化延時函數
?uart_init(9600);//初始化串口波特率為115200
??? LED_Init();
??? KEY_Init();
?uprint("大家好!!!\r\n");
?uprint("大家來造呼吸機-----。\r\n");
???gtln= nmtoa(wd, 10, psc);
???uprint(wd); uprint("\r\n");
???gtln= nmtoa(wd, 10, (psc+10000));
???uprint(wd); uprint("\r\n");
?LED0= LED1= LED2= 1;
?chgFry[0]= 0;?
?chgFry[1]= 4;
?new_Pwm(tm43, idc);?
?
?uprint("控制芯片STM32F407ZGT6_008_PWM仿真開始了。\r\n");
??delay_ms(100);?
?? while(1)
?{
???wd= lnslt[status]; ;
??uprint("Arm運行時......占空比:\r\n");?
??uprint(wd);
??uprint("線路可調整狀態:\r\n");
??if(1==ReadKey(GPIOC, 0x0080)) {
???status++;?
????? delay_ms(20);
???if(status> 6) status= 0;
???wd= lnslt[status]; ;
???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(numtoa(10, umd));
?????uprint(" ...\r\n ");
?????st_fig(status, umd);
????}
???}
??}
??if(1==ReadKey(GPIOC, 0x0020)) {
???LED1= 1;?
???if(status==5) {
????if(chgFry[0]==0) {
?????chgFry[0]= 2;
?????tm43[0][0]= 1680;
?????new_Pwm(tm43, idc);?
?????uprint("線路A,B,C頻率調整到倍頻...\r\n");
?????delay_ms(200);
????}
????else{
?????chgFry[0]= 0;
?????tm43[0][0]= 840;
?????new_Pwm(tm43, idc);?
?????uprint("線路A,B,C頻率調整到初始狀態...\r\n");
?????delay_ms(200);
????}
???}
???if(status==6) {
????if(chgFry[1]==0) {
?????chgFry[1]= 4;
?????tm43[1][0]= 1680; tm43[1][1]= 1200;
?????new_Pwm(tm43, idc);?
?????uprint("線路D頻率調整到四倍頻...\r\n");
?????delay_ms(200);
????}
????else{
?????chgFry[1]= 0;
?????tm43[1][0]= 840; tm43[1][1]= 600;
?????new_Pwm(tm43, idc);?
?????uprint("線路D頻率調整到初始狀態...\r\n");
?????delay_ms(200);
????}
???}
?????? 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;
???uprint("線路調整, 占空值由 ");
???uprint(wds);
???uprint(" 減少50 至 ");
???uprint(numtoa(10, umd));
???uprint(" ...\r\n ");
???st_fig(status, umd);
???}
???else {
????umd= 1;
????uprint("線路調整, 占空值到達最低值 ......\r\n");
????st_fig(status, 1);
???}
??}
??LED0= LED1= LED2= 0;
???delay_ms(100);?
?}
}
?
key.c,key.h, led.c led.h,time.c,time.h之前的代碼都可借鑒,不在多列,以后不再一一羅列,只列main.c 與相關功能的.c, .h代碼。
六,點擊構建工程按鈕,編譯工程。(如下圖7所示)
?
圖7
七,點擊窗口左下方仿真按鈕,可見紅,黃,綠燈點亮后一會滅,仿真開始,按下SELECT按鈕,選擇"變頻1-A,B,C倍頻或初始狀態", "變頻2-D四倍頻或初始狀態",按下按下KEY_ZERO按鈕,倍頻, 四倍頻或初始狀態。占空比的調試,參見:博主的Proteus8.9 仿真STM32407ZGT6系列009_PWM多路可調占空比輸出文。虛擬串口,示波器將的狀態變化顯示在屏。 (如下圖8,9,10,11所示)
?圖8
?
圖9
?
圖10
圖11
八,選擇release,點擊構建工程按鈕,編譯工程生成Hex文件。(如下圖12所示)
?圖12
lw
評論
查看更多