|
- #include "Config.h"
- #include "NVIC.h"
- #include "GPIO.h"
- #include "STC8H_PWM.h"
- #include "Switch.h"
- #include "Delay.h"
- //初始化
- void GPIO_Config(){
- P0_MODE_IO_PU(GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3);
- }
- /*************** 串口初始化函数 *****************/
- #define PERIOD (MAIN_Fosc/1000) // 运行周期(运行PWM周期)
- PWMx_Duty PWMB_Duty;
- void PWM_config(void)
- {
- PWMx_InitDefine PWMx_InitStructure;
-
- // 配置PWM5
- PWMx_InitStructure.PWM_Mode = CCMRn_PWM_MODE1; //模式, CCMRn_FREEZE,CCMRn_MATCH_VALID,CCMRn_MATCH_INVALID,CCMRn_ROLLOVER,CCMRn_FORCE_INVALID,CCMRn_FORCE_VALID,CCMRn_PWM_MODE1,CCMRn_PWM_MODE2
- PWMx_InitStructure.PWM_Duty = PWMB_Duty.PWM5_Duty; //PWM占空比时间, 0~Period
- PWMx_InitStructure.PWM_EnoSelect = ENO5P; //输出通道选择, ENO1P,ENO1N,ENO2P,ENO2N,ENO3P,ENO3N,ENO4P,ENO4N / ENO5P,ENO6P,ENO7P,ENO8P
- PWM_Configuration(PWM5, &PWMx_InitStructure); //初始化PWM, PWMA,PWMB
- // 配置PWM6
- PWMx_InitStructure.PWM_Mode = CCMRn_PWM_MODE2; //模式, CCMRn_FREEZE,CCMRn_MATCH_VALID,CCMRn_MATCH_INVALID,CCMRn_ROLLOVER,CCMRn_FORCE_INVALID,CCMRn_FORCE_VALID,CCMRn_PWM_MODE1,CCMRn_PWM_MODE2
- PWMx_InitStructure.PWM_Duty = PWMB_Duty.PWM6_Duty; //PWM占空比时间, 0~Period
- PWMx_InitStructure.PWM_EnoSelect = ENO6P; //输出通道选择, ENO1P,ENO1N,ENO2P,ENO2N,ENO3P,ENO3N,ENO4P,ENO4N / ENO5P,ENO6P,ENO7P,ENO8P
- PWM_Configuration(PWM6, &PWMx_InitStructure); //初始化PWM, PWMA,PWMB
- // 配置PWM7
- PWMx_InitStructure.PWM_Mode = CCMRn_PWM_MODE1; //模式, CCMRn_FREEZE,CCMRn_MATCH_VALID,CCMRn_MATCH_INVALID,CCMRn_ROLLOVER,CCMRn_FORCE_INVALID,CCMRn_FORCE_VALID,CCMRn_PWM_MODE1,CCMRn_PWM_MODE2
- PWMx_InitStructure.PWM_Duty = PWMB_Duty.PWM7_Duty; //PWM占空比时间, 0~Period
- PWMx_InitStructure.PWM_EnoSelect = ENO7P; //输出通道选择, ENO1P,ENO1N,ENO2P,ENO2N,ENO3P,ENO3N,ENO4P,ENO4N / ENO5P,ENO6P,ENO7P,ENO8P
- PWM_Configuration(PWM7, &PWMx_InitStructure); //初始化PWM, PWMA,PWMB
- // 配置PWM8
- PWMx_InitStructure.PWM_Mode = CCMRn_PWM_MODE2; //模式, CCMRn_FREEZE,CCMRn_MATCH_VALID,CCMRn_MATCH_INVALID,CCMRn_ROLLOVER,CCMRn_FORCE_INVALID,CCMRn_FORCE_VALID,CCMRn_PWM_MODE1,CCMRn_PWM_MODE2
- PWMx_InitStructure.PWM_Duty = PWMB_Duty.PWM8_Duty; //PWM占空比时间, 0~Period
- PWMx_InitStructure.PWM_EnoSelect = ENO8P; //输出通道选择, ENO1P,ENO1N,ENO2P,ENO2N,ENO3P,ENO3N,ENO4P,ENO4N / ENO5P,ENO6P,ENO7P,ENO8P
- PWM_Configuration(PWM8, &PWMx_InitStructure); //初始化PWM, PWMA,PWMB
- // 配置PWMB
- PWMx_InitStructure.PWM_Period = PERIOD-1; //周期时间, 0~65535
- PWMx_InitStructure.PWM_DeadTime = 0; //死区发生器设置, 0~255
- PWMx_InitStructure.PWM_MainOutEnable= ENABLE; //主输出使能, ENABLE,DISABLE
- PWMx_InitStructure.PWM_CEN_Enable = ENABLE; //使能计数器, ENABLE,DISABLE
- PWM_Configuration(PWMB, &PWMx_InitStructure); //初始化PWM通用寄存器, PWMA,PWMB
- // 切换PWM通道
- PWM5_SW(PWM5_SW_P00); //PWM5_SW_P20,PWM5_SW_P17,PWM5_SW_P00,PWM5_SW_P74
- PWM6_SW(PWM6_SW_P01); //PWM6_SW_P21,PWM6_SW_P54,PWM6_SW_P01,PWM6_SW_P75
- PWM7_SW(PWM7_SW_P02); //PWM7_SW_P22,PWM7_SW_P33,PWM7_SW_P02,PWM7_SW_P76
- PWM8_SW(PWM8_SW_P03); //PWM8_SW_P23,PWM8_SW_P34,PWM8_SW_P03,PWM8_SW_P77
- // 初始化PWMB的中断
- NVIC_PWM_Init(PWMB,DISABLE,Priority_0);
- }
- void main(){
- char duty_percent=0;
- char direction=1;
- EAXSFR(); //扩展寄存器访问使能
-
-
- GPIO_Config();
-
-
- PWM_config();
-
- EA=1;
-
- P00=P01=P02=P03=0;
- delay_ms(100);
-
-
- PWMB_Duty.PWM5_Duty=PERIOD * duty_percent/100;
- PWMB_Duty.PWM6_Duty=PERIOD * duty_percent/100;
- PWMB_Duty.PWM7_Duty=PERIOD * duty_percent/100;
- PWMB_Duty.PWM8_Duty=PERIOD * duty_percent/100;
- UpdatePwm(PWMB, &PWMB_Duty);
-
- while(1){
- delay_ms(10);
- duty_percent+=direction;
- if(duty_percent>=100){
- duty_percent=100;
- direction=-1;
- }else if(duty_percent<=0){
- duty_percent=0;
- direction=1;
- }
-
- PWMB_Duty.PWM5_Duty=PERIOD * duty_percent/100;
- PWMB_Duty.PWM6_Duty=PERIOD * duty_percent/100;
- PWMB_Duty.PWM7_Duty=PERIOD * duty_percent/100;
- PWMB_Duty.PWM8_Duty=PERIOD * duty_percent/100;
- UpdatePwm(PWMB, &PWMB_Duty);
-
- }
- }
复制代码 有大佬可以帮忙看一下为什么这个代码实现不了呼吸灯啊?
|
|