我给你来个程序试试
高级PWM1-PWM2-PWM3-PWM4驱动P6口呼吸灯实验程序-STC8H
- /*---------------------------------------------------------------------*/
- /* --- STC MCU Limited ------------------------------------------------*/
- /* --- STC 1T Series MCU Demo Programme -------------------------------*/
- /* 如果要在程序中使用此代码,请在程序中注明使用了STC的资料及程序 */
- /*---------------------------------------------------------------------*/
- //11-高级PWM1-PWM2-PWM3-PWM4驱动P6口呼吸灯实验程序-STC8H
- /*********************************************************/
- //#define MAIN_Fosc 22118400L //定义主时钟
- //#define MAIN_Fosc 12000000L //定义主时钟
- //#define MAIN_Fosc 11059200L //定义主时钟
- //#define MAIN_Fosc 5529600L //定义主时钟
- #define MAIN_Fosc 24000000L //定义主时钟
- /*********************************************************/
- #include "STC8xxxx.H"
- #define GPIO_PullUp 0 //上拉准双向口
- #define GPIO_HighZ 1 //浮空输入
- #define GPIO_OUT_OD 2 //开漏输出
- #define GPIO_OUT_PP 3 //推挽输出
- #define GPIO_Pin_0 0x01 //IO引脚 Px.0
- #define GPIO_Pin_1 0x02 //IO引脚 Px.1
- #define GPIO_Pin_2 0x04 //IO引脚 Px.2
- #define GPIO_Pin_3 0x08 //IO引脚 Px.3
- #define GPIO_Pin_4 0x10 //IO引脚 Px.4
- #define GPIO_Pin_5 0x20 //IO引脚 Px.5
- #define GPIO_Pin_6 0x40 //IO引脚 Px.6
- #define GPIO_Pin_7 0x80 //IO引脚 Px.7
- #define GPIO_Pin_All 0xFF //IO所有引脚
-
- #define GPIO_P0 0 //
- #define GPIO_P1 1
- #define GPIO_P2 2
- #define GPIO_P3 3
- #define GPIO_P4 4
- #define GPIO_P5 5
- #define GPIO_P6 6
- #define GPIO_P7 7
- typedef struct
- {
- u8 Mode; //IO模式, GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
- u8 Pin; //要设置的端口
- } GPIO_InitTypeDef;
- u8 GPIO_Inilize(u8 GPIO, GPIO_InitTypeDef *GPIOx);
- #define Timer0 0
- #define Timer1 1
- #define Timer2 2
- #define Timer3 3
- #define Timer4 4
- #define TIM_16BitAutoReload 0
- #define TIM_16Bit 1
- #define TIM_8BitAutoReload 2
- #define TIM_16BitAutoReloadNoMask 3
- #define TIM_CLOCK_1T 0
- #define TIM_CLOCK_12T 1
- #define TIM_CLOCK_Ext 2
- typedef struct
- {
- u8 TIM_Mode; //工作模式, TIM_16BitAutoReload,TIM_16Bit,TIM_8BitAutoReload,TIM_16BitAutoReloadNoMask
- u8 TIM_Priority; //优先级设置 Priority_0,Priority_1,Priority_2,Priority_3
- u8 TIM_Interrupt; //中断允许 ENABLE,DISABLE
- u8 TIM_ClkSource; //时钟源 TIM_CLOCK_1T,TIM_CLOCK_12T,TIM_CLOCK_Ext
- u8 TIM_ClkOut; //可编程时钟输出, ENABLE,DISABLE
- u16 TIM_Value; //装载初值
- u8 TIM_Run; //是否运行 ENABLE,DISABLE
- } TIM_InitTypeDef;
- u8 Timer_Inilize(u8 TIM, TIM_InitTypeDef *TIMx);
- #define ENO1P 0x01
- #define ENO1N 0x02
- #define ENO2P 0x04
- #define ENO2N 0x08
- #define ENO3P 0x10
- #define ENO3N 0x20
- #define ENO4P 0x40
- #define ENO4N 0x80
- #define ENO5P 0x01
- #define ENO6P 0x04
- #define ENO7P 0x10
- #define ENO8P 0x40
- #define PWMA 1
- #define PWMB 2
- #define PWM1_SW_P10_P11 0
- #define PWM1_SW_P20_P21 1
- #define PWM1_SW_P60_P61 2
- #define PWM2_SW_P12_P13 0
- #define PWM2_SW_P22_P23 (1<<2)
- #define PWM2_SW_P62_P63 (2<<2)
- #define PWM3_SW_P14_P15 0
- #define PWM3_SW_P24_P25 (1<<4)
- #define PWM3_SW_P64_P65 (2<<4)
- #define PWM4_SW_P16_P17 0
- #define PWM4_SW_P26_P27 (1<<6)
- #define PWM4_SW_P66_P67 (2<<6)
- #define PWM4_SW_P34_P33 (3<<6)
- #define PWM5_SW_P20 0
- #define PWM5_SW_P17 1
- #define PWM5_SW_P00 2
- #define PWM5_SW_P74 3
- #define PWM6_SW_P21 0
- #define PWM6_SW_P54 (1<<2)
- #define PWM6_SW_P01 (2<<2)
- #define PWM6_SW_P75 (3<<2)
- #define PWM7_SW_P22 0
- #define PWM7_SW_P33 (1<<4)
- #define PWM7_SW_P02 (2<<4)
- #define PWM7_SW_P76 (3<<4)
- #define PWM8_SW_P23 0
- #define PWM8_SW_P34 (1<<6)
- #define PWM8_SW_P03 (2<<6)
- #define PWM8_SW_P77 (3<<6)
- typedef struct
- {
- u8 PWM1_Mode; //模式, CCMRn_FREEZE,CCMRn_MATCH_VALID,CCMRn_MATCH_INVALID,CCMRn_ROLLOVER,CCMRn_FORCE_INVALID,CCMRn_FORCE_VALID,CCMRn_PWM_MODE1,CCMRn_PWM_MODE2
- u8 PWM2_Mode; //模式, CCMRn_FREEZE,CCMRn_MATCH_VALID,CCMRn_MATCH_INVALID,CCMRn_ROLLOVER,CCMRn_FORCE_INVALID,CCMRn_FORCE_VALID,CCMRn_PWM_MODE1,CCMRn_PWM_MODE2
- u8 PWM3_Mode; //模式, CCMRn_FREEZE,CCMRn_MATCH_VALID,CCMRn_MATCH_INVALID,CCMRn_ROLLOVER,CCMRn_FORCE_INVALID,CCMRn_FORCE_VALID,CCMRn_PWM_MODE1,CCMRn_PWM_MODE2
- u8 PWM4_Mode; //模式, CCMRn_FREEZE,CCMRn_MATCH_VALID,CCMRn_MATCH_INVALID,CCMRn_ROLLOVER,CCMRn_FORCE_INVALID,CCMRn_FORCE_VALID,CCMRn_PWM_MODE1,CCMRn_PWM_MODE2
- u8 PWM5_Mode; //模式, CCMRn_FREEZE,CCMRn_MATCH_VALID,CCMRn_MATCH_INVALID,CCMRn_ROLLOVER,CCMRn_FORCE_INVALID,CCMRn_FORCE_VALID,CCMRn_PWM_MODE1,CCMRn_PWM_MODE2
- u8 PWM6_Mode; //模式, CCMRn_FREEZE,CCMRn_MATCH_VALID,CCMRn_MATCH_INVALID,CCMRn_ROLLOVER,CCMRn_FORCE_INVALID,CCMRn_FORCE_VALID,CCMRn_PWM_MODE1,CCMRn_PWM_MODE2
- u8 PWM7_Mode; //模式, CCMRn_FREEZE,CCMRn_MATCH_VALID,CCMRn_MATCH_INVALID,CCMRn_ROLLOVER,CCMRn_FORCE_INVALID,CCMRn_FORCE_VALID,CCMRn_PWM_MODE1,CCMRn_PWM_MODE2
- u8 PWM8_Mode; //模式, CCMRn_FREEZE,CCMRn_MATCH_VALID,CCMRn_MATCH_INVALID,CCMRn_ROLLOVER,CCMRn_FORCE_INVALID,CCMRn_FORCE_VALID,CCMRn_PWM_MODE1,CCMRn_PWM_MODE2
- u16 PWM_Period; //周期时间, 0~65535
- u16 PWM1_Duty; //PWM1占空比时间, 0~Period
- u16 PWM2_Duty; //PWM2占空比时间, 0~Period
- u16 PWM3_Duty; //PWM3占空比时间, 0~Period
- u16 PWM4_Duty; //PWM4占空比时间, 0~Period
- u16 PWM5_Duty; //PWM5占空比时间, 0~Period
- u16 PWM6_Duty; //PWM6占空比时间, 0~Period
- u16 PWM7_Duty; //PWM7占空比时间, 0~Period
- u16 PWM8_Duty; //PWM8占空比时间, 0~Period
- u8 PWM_DeadTime; //死区发生器设置, 0~255
- u8 PWM_CC1Enable; //开启输入捕获/比较输出, ENABLE,DISABLE
- u8 PWM_CC1NEnable; //开启输入捕获/比较输出, ENABLE,DISABLE
- u8 PWM_CC2Enable; //开启输入捕获/比较输出, ENABLE,DISABLE
- u8 PWM_CC2NEnable; //开启输入捕获/比较输出, ENABLE,DISABLE
- u8 PWM_CC3Enable; //开启输入捕获/比较输出, ENABLE,DISABLE
- u8 PWM_CC3NEnable; //开启输入捕获/比较输出, ENABLE,DISABLE
- u8 PWM_CC4Enable; //开启输入捕获/比较输出, ENABLE,DISABLE
- u8 PWM_CC4NEnable; //开启输入捕获/比较输出, ENABLE,DISABLE
- u8 PWM_CC5Enable; //开启输入捕获/比较输出, ENABLE,DISABLE
- u8 PWM_CC6Enable; //开启输入捕获/比较输出, ENABLE,DISABLE
- u8 PWM_CC7Enable; //开启输入捕获/比较输出, ENABLE,DISABLE
- u8 PWM_CC8Enable; //开启输入捕获/比较输出, ENABLE,DISABLE
- u8 PWM_Reload; //输出比较的预装载使能, ENABLE,DISABLE
- u8 PWM_Fast; //输出比较快速功能使能, ENABLE,DISABLE
- u8 PWM1_SetPriority; //设置PWM1优先级, Priority_0,Priority_1,Priority_2,Priority_3
- u8 PWM2_SetPriority; //设置PWM2优先级, Priority_0,Priority_1,Priority_2,Priority_3
- u8 PWM3_SetPriority; //设置PWM3优先级, Priority_0,Priority_1,Priority_2,Priority_3
- u8 PWM4_SetPriority; //设置PWM4优先级, Priority_0,Priority_1,Priority_2,Priority_3
- u8 PWM5_SetPriority; //设置PWM4优先级, Priority_0,Priority_1,Priority_2,Priority_3
- u8 PWM_EnoSelect; //输出通道选择, ENO1P,ENO1N,ENO2P,ENO2N,ENO3P,ENO3N,ENO4P,ENO4N / ENO5P,ENO6P,ENO7P,ENO8P
- u8 PWM_PreLoad; //预装载, ENABLE,DISABLE
- u8 PWM_PS_SW; //切换端口, PWM1_SW_P10_P11,PWM1_SW_P20_P21,PWM1_SW_P60_P61
- u8 PWM_CEN_Enable; //使能计数器, ENABLE,DISABLE
- u8 PWM_BrakeEnable; //刹车输入使能, ENABLE,DISABLE
- u8 PWM_MainOutEnable;//主输出使能, ENABLE,DISABLE
- } PWMx_InitDefine;
- typedef struct
- {
- u16 PWM1_Duty; //PWM1占空比时间, 0~Period
- u16 PWM2_Duty; //PWM2占空比时间, 0~Period
- u16 PWM3_Duty; //PWM3占空比时间, 0~Period
- u16 PWM4_Duty; //PWM4占空比时间, 0~Period
- u16 PWM5_Duty; //PWM5占空比时间, 0~Period
- u16 PWM6_Duty; //PWM6占空比时间, 0~Period
- u16 PWM7_Duty; //PWM7占空比时间, 0~Period
- u16 PWM8_Duty; //PWM8占空比时间, 0~Period
- } PWMx_Duty;
- u8 PWM_Configuration(u8 PWM, PWMx_InitDefine *PWMx);
- void UpdatePwm(u8 PWM, PWMx_Duty *PWMx);
- /*************** 功能说明 ****************
- 本文件为STC8系列的端口初始化程序,用户几乎可以不修改这个程序.
- ******************************************/
- //========================================================================
- // 函数: u8 GPIO_Inilize(u8 GPIO, GPIO_InitTypeDef *GPIOx)
- // 描述: 初始化IO口.
- // 参数: GPIOx: 结构参数,请参考timer.h里的定义.
- // 返回: 成功返回0, 空操作返回1,错误返回2.
- // 版本: V1.0, 2012-10-22
- //========================================================================
- u8 GPIO_Inilize(u8 GPIO, GPIO_InitTypeDef *GPIOx)
- {
- if(GPIO > GPIO_P7) return 1; //空操作
- if(GPIOx->Mode > GPIO_OUT_PP) return 2; //错误
- if(GPIO == GPIO_P0)
- {
- if(GPIOx->Mode == GPIO_PullUp) P0M1 &= ~GPIOx->Pin, P0M0 &= ~GPIOx->Pin; //上拉准双向口
- if(GPIOx->Mode == GPIO_HighZ) P0M1 |= GPIOx->Pin, P0M0 &= ~GPIOx->Pin; //浮空输入
- if(GPIOx->Mode == GPIO_OUT_OD) P0M1 |= GPIOx->Pin, P0M0 |= GPIOx->Pin; //开漏输出
- if(GPIOx->Mode == GPIO_OUT_PP) P0M1 &= ~GPIOx->Pin, P0M0 |= GPIOx->Pin; //推挽输出
- }
- if(GPIO == GPIO_P1)
- {
- if(GPIOx->Mode == GPIO_PullUp) P1M1 &= ~GPIOx->Pin, P1M0 &= ~GPIOx->Pin; //上拉准双向口
- if(GPIOx->Mode == GPIO_HighZ) P1M1 |= GPIOx->Pin, P1M0 &= ~GPIOx->Pin; //浮空输入
- if(GPIOx->Mode == GPIO_OUT_OD) P1M1 |= GPIOx->Pin, P1M0 |= GPIOx->Pin; //开漏输出
- if(GPIOx->Mode == GPIO_OUT_PP) P1M1 &= ~GPIOx->Pin, P1M0 |= GPIOx->Pin; //推挽输出
- }
- if(GPIO == GPIO_P2)
- {
- if(GPIOx->Mode == GPIO_PullUp) P2M1 &= ~GPIOx->Pin, P2M0 &= ~GPIOx->Pin; //上拉准双向口
- if(GPIOx->Mode == GPIO_HighZ) P2M1 |= GPIOx->Pin, P2M0 &= ~GPIOx->Pin; //浮空输入
- if(GPIOx->Mode == GPIO_OUT_OD) P2M1 |= GPIOx->Pin, P2M0 |= GPIOx->Pin; //开漏输出
- if(GPIOx->Mode == GPIO_OUT_PP) P2M1 &= ~GPIOx->Pin, P2M0 |= GPIOx->Pin; //推挽输出
- }
- if(GPIO == GPIO_P3)
- {
- if(GPIOx->Mode == GPIO_PullUp) P3M1 &= ~GPIOx->Pin, P3M0 &= ~GPIOx->Pin; //上拉准双向口
- if(GPIOx->Mode == GPIO_HighZ) P3M1 |= GPIOx->Pin, P3M0 &= ~GPIOx->Pin; //浮空输入
- if(GPIOx->Mode == GPIO_OUT_OD) P3M1 |= GPIOx->Pin, P3M0 |= GPIOx->Pin; //开漏输出
- if(GPIOx->Mode == GPIO_OUT_PP) P3M1 &= ~GPIOx->Pin, P3M0 |= GPIOx->Pin; //推挽输出
- }
- if(GPIO == GPIO_P4)
- {
- if(GPIOx->Mode == GPIO_PullUp) P4M1 &= ~GPIOx->Pin, P4M0 &= ~GPIOx->Pin; //上拉准双向口
- if(GPIOx->Mode == GPIO_HighZ) P4M1 |= GPIOx->Pin, P4M0 &= ~GPIOx->Pin; //浮空输入
- if(GPIOx->Mode == GPIO_OUT_OD) P4M1 |= GPIOx->Pin, P4M0 |= GPIOx->Pin; //开漏输出
- if(GPIOx->Mode == GPIO_OUT_PP) P4M1 &= ~GPIOx->Pin, P4M0 |= GPIOx->Pin; //推挽输出
- }
- if(GPIO == GPIO_P5)
- {
- if(GPIOx->Mode == GPIO_PullUp) P5M1 &= ~GPIOx->Pin, P5M0 &= ~GPIOx->Pin; //上拉准双向口
- if(GPIOx->Mode == GPIO_HighZ) P5M1 |= GPIOx->Pin, P5M0 &= ~GPIOx->Pin; //浮空输入
- if(GPIOx->Mode == GPIO_OUT_OD) P5M1 |= GPIOx->Pin, P5M0 |= GPIOx->Pin; //开漏输出
- if(GPIOx->Mode == GPIO_OUT_PP) P5M1 &= ~GPIOx->Pin, P5M0 |= GPIOx->Pin; //推挽输出
- }
- if(GPIO == GPIO_P6)
- {
- if(GPIOx->Mode == GPIO_PullUp) P6M1 &= ~GPIOx->Pin, P6M0 &= ~GPIOx->Pin; //上拉准双向口
- if(GPIOx->Mode == GPIO_HighZ) P6M1 |= GPIOx->Pin, P6M0 &= ~GPIOx->Pin; //浮空输入
- if(GPIOx->Mode == GPIO_OUT_OD) P6M1 |= GPIOx->Pin, P6M0 |= GPIOx->Pin; //开漏输出
- if(GPIOx->Mode == GPIO_OUT_PP) P6M1 &= ~GPIOx->Pin, P6M0 |= GPIOx->Pin; //推挽输出
- }
- if(GPIO == GPIO_P7)
- {
- if(GPIOx->Mode == GPIO_PullUp) P7M1 &= ~GPIOx->Pin, P7M0 &= ~GPIOx->Pin; //上拉准双向口
- if(GPIOx->Mode == GPIO_HighZ) P7M1 |= GPIOx->Pin, P7M0 &= ~GPIOx->Pin; //浮空输入
- if(GPIOx->Mode == GPIO_OUT_OD) P7M1 |= GPIOx->Pin, P7M0 |= GPIOx->Pin; //开漏输出
- if(GPIOx->Mode == GPIO_OUT_PP) P7M1 &= ~GPIOx->Pin, P7M0 |= GPIOx->Pin; //推挽输出
- }
- return 0; //成功
- }
- /************* 功能说明 **************
- 本文件为STC8系列的定时器初始化和中断程序,用户可以在这个文件中修改自己需要的中断程序.
- ******************************************/
- /************* 本地变量声明 **************/
- bit PWM1_Flag;
- bit PWM2_Flag;
- bit PWM3_Flag;
- bit PWM4_Flag;
- /************* 外部函数和变量声明 *****************/
- extern PWMx_Duty PWMA_Duty;
- /********************* Timer0中断函数************************/
- void timer0_int (void) interrupt TIMER0_VECTOR
- {
- if(!PWM1_Flag)
- {
- PWMA_Duty.PWM1_Duty++;
- if(PWMA_Duty.PWM1_Duty >= 2047) PWM1_Flag = 1;
- }
- else
- {
- PWMA_Duty.PWM1_Duty--;
- if(PWMA_Duty.PWM1_Duty <= 0) PWM1_Flag = 0;
- }
- if(!PWM2_Flag)
- {
- PWMA_Duty.PWM2_Duty++;
- if(PWMA_Duty.PWM2_Duty >= 2047) PWM2_Flag = 1;
- }
- else
- {
- PWMA_Duty.PWM2_Duty--;
- if(PWMA_Duty.PWM2_Duty <= 0) PWM2_Flag = 0;
- }
- if(!PWM3_Flag)
- {
- PWMA_Duty.PWM3_Duty++;
- if(PWMA_Duty.PWM3_Duty >= 2047) PWM3_Flag = 1;
- }
- else
- {
- PWMA_Duty.PWM3_Duty--;
- if(PWMA_Duty.PWM3_Duty <= 0) PWM3_Flag = 0;
- }
- if(!PWM4_Flag)
- {
- PWMA_Duty.PWM4_Duty++;
- if(PWMA_Duty.PWM4_Duty >= 2047) PWM4_Flag = 1;
- }
- else
- {
- PWMA_Duty.PWM4_Duty--;
- if(PWMA_Duty.PWM4_Duty <= 0) PWM4_Flag = 0;
- }
-
- UpdatePwm(PWMA, &PWMA_Duty);
- }
- /********************* Timer1中断函数************************/
- void timer1_int (void) interrupt TIMER1_VECTOR
- {
- // P66 = ~P66;
- }
- /********************* Timer2中断函数************************/
- void timer2_int (void) interrupt TIMER2_VECTOR
- {
- // P65 = ~P65;
- }
- /********************* Timer3中断函数************************/
- void timer3_int (void) interrupt TIMER3_VECTOR
- {
- // P64 = ~P64;
- }
- /********************* Timer4中断函数************************/
- void timer4_int (void) interrupt TIMER4_VECTOR
- {
- // P63 = ~P63;
- }
- //========================================================================
- // 函数: u8 Timer_Inilize(u8 TIM, TIM_InitTypeDef *TIMx)
- // 描述: 定时器初始化程序.
- // 参数: TIMx: 结构参数,请参考timer.h里的定义.
- // 返回: 成功返回0, 空操作返回1,错误返回2.
- // 版本: V1.0, 2012-10-22
- //========================================================================
- u8 Timer_Inilize(u8 TIM, TIM_InitTypeDef *TIMx)
- {
- if(TIM > Timer4) return 1; //空操作
- if(TIM == Timer0)
- {
- Timer0_Stop(); //停止计数
- if(TIMx->TIM_Interrupt == ENABLE) Timer0_InterruptEnable(); //允许中断
- else Timer0_InterruptDisable(); //禁止中断
- if(TIMx->TIM_Priority > Priority_3) return 2; //错误
- Timer0_Priority(TIMx->TIM_Priority); //指定中断优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
- if(TIMx->TIM_Mode >= TIM_16BitAutoReloadNoMask) return 2; //错误
- TMOD = (TMOD & ~0x30) | TIMx->TIM_Mode; //工作模式,0: 16位自动重装, 1: 16位定时/计数, 2: 8位自动重装
- if(TIMx->TIM_ClkSource == TIM_CLOCK_12T) Timer0_12T(); //12T
- if(TIMx->TIM_ClkSource == TIM_CLOCK_1T) Timer0_1T(); //1T
- if(TIMx->TIM_ClkSource == TIM_CLOCK_Ext) Timer0_AsCounter(); //对外计数或分频
- else Timer0_AsTimer(); //定时
- if(TIMx->TIM_ClkOut == ENABLE) Timer0_CLKO_Enable(); //输出时钟
- else Timer0_CLKO_Disable(); //不输出时钟
-
- T0_Load(TIMx->TIM_Value);
- if(TIMx->TIM_Run == ENABLE) Timer0_Run(); //开始运行
- return 0; //成功
- }
- if(TIM == Timer1)
- {
- Timer1_Stop(); //停止计数
- if(TIMx->TIM_Interrupt == ENABLE) Timer1_InterruptEnable(); //允许中断
- else Timer1_InterruptDisable(); //禁止中断
- if(TIMx->TIM_Priority > Priority_3) return 2; //错误
- Timer1_Priority(TIMx->TIM_Priority); //指定中断优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
- if(TIMx->TIM_Mode >= TIM_16BitAutoReloadNoMask) return 2; //错误
- TMOD = (TMOD & ~0x30) | TIMx->TIM_Mode; //工作模式,0: 16位自动重装, 1: 16位定时/计数, 2: 8位自动重装
- if(TIMx->TIM_ClkSource == TIM_CLOCK_12T) Timer1_12T(); //12T
- if(TIMx->TIM_ClkSource == TIM_CLOCK_1T) Timer1_1T(); //1T
- if(TIMx->TIM_ClkSource == TIM_CLOCK_Ext) Timer1_AsCounter(); //对外计数或分频
- else Timer1_AsTimer(); //定时
- if(TIMx->TIM_ClkOut == ENABLE) Timer1_CLKO_Enable(); //输出时钟
- else Timer1_CLKO_Disable(); //不输出时钟
-
- T1_Load(TIMx->TIM_Value);
- if(TIMx->TIM_Run == ENABLE) Timer1_Run(); //开始运行
- return 0; //成功
- }
- if(TIM == Timer2) //Timer2,固定为16位自动重装, 中断无优先级
- {
- Timer2_Stop(); //停止计数
- if(TIMx->TIM_Interrupt == ENABLE) Timer2_InterruptEnable(); //允许中断
- else Timer2_InterruptDisable(); //禁止中断
- if(TIMx->TIM_ClkSource > TIM_CLOCK_Ext) return 2;
- if(TIMx->TIM_ClkSource == TIM_CLOCK_12T) Timer2_12T(); //12T
- if(TIMx->TIM_ClkSource == TIM_CLOCK_1T) Timer2_1T(); //1T
- if(TIMx->TIM_ClkSource == TIM_CLOCK_Ext) Timer2_AsCounter(); //对外计数或分频
- else Timer2_AsTimer(); //定时
- if(TIMx->TIM_ClkOut == ENABLE) Timer2_CLKO_Enable(); //输出时钟
- else Timer2_CLKO_Disable(); //不输出时钟
- T2_Load(TIMx->TIM_Value);
- if(TIMx->TIM_Run == ENABLE) Timer2_Run(); //开始运行
- return 0; //成功
- }
- if(TIM == Timer3) //Timer3,固定为16位自动重装, 中断无优先级
- {
- Timer3_Stop(); //停止计数
- if(TIMx->TIM_Interrupt == ENABLE) Timer3_InterruptEnable(); //允许中断
- else Timer3_InterruptDisable(); //禁止中断
- if(TIMx->TIM_ClkSource > TIM_CLOCK_Ext) return 2;
- if(TIMx->TIM_ClkSource == TIM_CLOCK_12T) Timer3_12T(); //12T
- if(TIMx->TIM_ClkSource == TIM_CLOCK_1T) Timer3_1T(); //1T
- if(TIMx->TIM_ClkSource == TIM_CLOCK_Ext) Timer3_AsCounter(); //对外计数或分频
- else Timer3_AsTimer(); //定时
- if(TIMx->TIM_ClkOut == ENABLE) Timer3_CLKO_Enable(); //输出时钟
- else Timer3_CLKO_Disable(); //不输出时钟
- T3_Load(TIMx->TIM_Value);
- if(TIMx->TIM_Run == ENABLE) Timer3_Run(); //开始运行
- return 0; //成功
- }
- if(TIM == Timer4) //Timer3,固定为16位自动重装, 中断无优先级
- {
- Timer4_Stop(); //停止计数
- if(TIMx->TIM_Interrupt == ENABLE) Timer4_InterruptEnable(); //允许中断
- else Timer4_InterruptDisable(); //禁止中断
- if(TIMx->TIM_ClkSource > TIM_CLOCK_Ext) return 2;
- if(TIMx->TIM_ClkSource == TIM_CLOCK_12T) Timer4_12T(); //12T
- if(TIMx->TIM_ClkSource == TIM_CLOCK_1T) Timer4_1T(); //1T
- if(TIMx->TIM_ClkSource == TIM_CLOCK_Ext) Timer4_AsCounter(); //对外计数或分频
- else Timer4_AsTimer(); //定时
- if(TIMx->TIM_ClkOut == ENABLE) Timer4_CLKO_Enable(); //输出时钟
- else Timer4_CLKO_Disable(); //不输出时钟
- T4_Load(TIMx->TIM_Value);
- if(TIMx->TIM_Run == ENABLE) Timer4_Run(); //开始运行
- return 0; //成功
- }
- return 2; //错误
- }
- u8 PWM_Configuration(u8 PWM, PWMx_InitDefine *PWMx)
- {
- if(PWM == PWMA)
- {
- EAXSFR(); /* MOVX A,@DPTR/MOVX @DPTR,A指令的操作对象为扩展SFR(XSFR) */
-
- if(PWMx->PWM1_SetPriority <= Priority_3) PWM1_Priority(PWMx->PWM1_SetPriority); //指定中断优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
- if(PWMx->PWM2_SetPriority <= Priority_3) PWM2_Priority(PWMx->PWM2_SetPriority); //指定中断优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
- if(PWMx->PWM3_SetPriority <= Priority_3) PWM3_Priority(PWMx->PWM3_SetPriority); //指定中断优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
- if(PWMx->PWM4_SetPriority <= Priority_3) PWM4_Priority(PWMx->PWM4_SetPriority); //指定中断优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
-
- PWMA_CCER1_Disable(); //关闭所有输入捕获/比较输出
- PWMA_CCER2_Disable(); //关闭所有输入捕获/比较输出
- PWMA_OC1ModeSet(PWMx->PWM1_Mode); //设置输出比较模式
- PWMA_OC2ModeSet(PWMx->PWM2_Mode); //设置输出比较模式
- PWMA_OC3ModeSet(PWMx->PWM3_Mode); //设置输出比较模式
- PWMA_OC4ModeSet(PWMx->PWM4_Mode); //设置输出比较模式
- if(PWMx->PWM_Reload == ENABLE) PWMA_OC1_ReloadEnable(); //输出比较的预装载使能
- else PWMA_OC1_RelosdDisable(); //禁止输出比较的预装载
- if(PWMx->PWM_Fast == ENABLE) PWMA_OC1_FastEnable(); //输出比较快速功能使能
- else PWMA_OC1_FastDisable(); //禁止输出比较快速功能
-
- if(PWMx->PWM_CC1Enable == ENABLE) PWMA_CC1E_Enable(); //开启输入捕获/比较输出
- else PWMA_CC1E_Disable(); //关闭输入捕获/比较输出
- if(PWMx->PWM_CC1NEnable == ENABLE) PWMA_CC1NE_Enable(); //开启输入捕获/比较输出
- else PWMA_CC1NE_Disable(); //关闭输入捕获/比较输出
- if(PWMx->PWM_CC2Enable == ENABLE) PWMA_CC2E_Enable(); //开启输入捕获/比较输出
- else PWMA_CC2E_Disable(); //关闭输入捕获/比较输出
- if(PWMx->PWM_CC2NEnable == ENABLE) PWMA_CC2NE_Enable(); //开启输入捕获/比较输出
- else PWMA_CC2NE_Disable(); //关闭输入捕获/比较输出
- if(PWMx->PWM_CC3Enable == ENABLE) PWMA_CC3E_Enable(); //开启输入捕获/比较输出
- else PWMA_CC3E_Disable(); //关闭输入捕获/比较输出
- if(PWMx->PWM_CC3NEnable == ENABLE) PWMA_CC3NE_Enable(); //开启输入捕获/比较输出
- else PWMA_CC3NE_Disable(); //关闭输入捕获/比较输出
- if(PWMx->PWM_CC4Enable == ENABLE) PWMA_CC4E_Enable(); //开启输入捕获/比较输出
- else PWMA_CC4E_Disable(); //关闭输入捕获/比较输出
- if(PWMx->PWM_CC4NEnable == ENABLE) PWMA_CC4NE_Enable(); //开启输入捕获/比较输出
- else PWMA_CC4NE_Disable(); //关闭输入捕获/比较输出
-
- PWMA_AutoReload(PWMx->PWM_Period);
- PWMA_Duty1(PWMx->PWM1_Duty);
- PWMA_Duty2(PWMx->PWM2_Duty);
- PWMA_Duty3(PWMx->PWM3_Duty);
- PWMA_Duty4(PWMx->PWM4_Duty);
-
- PWMA_CCPCAPreloaded(PWMx->PWM_PreLoad); //捕获/比较预装载控制位(该位只对具有互补输出的通道起作用)
- PWMA_PS = PWMx->PWM_PS_SW; //切换IO
- PWMA_ENO = PWMx->PWM_EnoSelect; //输出通道选择
- PWMA_DeadTime(PWMx->PWM_DeadTime); //死区发生器设置
-
- if(PWMx->PWM_BrakeEnable == ENABLE) PWMA_BrakeEnable(); //开启刹车输入
- else PWMA_BrakeDisable(); //禁止刹车输入
- if(PWMx->PWM_MainOutEnable == ENABLE) PWMA_BrakeOutputEnable(); //主输出使能
- else PWMA_BrakeOutputDisable(); //主输出禁止
- if(PWMx->PWM_CEN_Enable == ENABLE) PWMA_CEN_Enable(); //使能计数器
- else PWMA_CEN_Disable(); //禁止计数器
-
- EAXRAM(); /* MOVX A,@DPTR/MOVX @DPTR,A指令的操作对象为扩展RAM(XRAM) */
- return 0;
- }
- if(PWM == PWMB)
- {
- EAXSFR(); /* MOVX A,@DPTR/MOVX @DPTR,A指令的操作对象为扩展SFR(XSFR) */
-
- if(PWMx->PWM5_SetPriority <= Priority_3) PWM5_Priority(PWMx->PWM5_SetPriority); //指定中断优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
-
- PWMB_CCER1_Disable(); //关闭所有输入捕获/比较输出
- PWMB_CCER2_Disable(); //关闭所有输入捕获/比较输出
- PWMB_OC5ModeSet(PWMx->PWM5_Mode); //设置输出比较模式
- PWMB_OC6ModeSet(PWMx->PWM6_Mode); //设置输出比较模式
- PWMB_OC7ModeSet(PWMx->PWM7_Mode); //设置输出比较模式
- PWMB_OC8ModeSet(PWMx->PWM8_Mode); //设置输出比较模式
- if(PWMx->PWM_Reload == ENABLE) PWMB_OC5_ReloadEnable(); //输出比较的预装载使能
- else PWMB_OC5_RelosdDisable(); //禁止输出比较的预装载
- if(PWMx->PWM_Fast == ENABLE) PWMB_OC5_FastEnable(); //输出比较快速功能使能
- else PWMB_OC5_FastDisable(); //禁止输出比较快速功能
-
- if(PWMx->PWM_CC5Enable == ENABLE) PWMB_CC5E_Enable(); //开启输入捕获/比较输出
- else PWMB_CC5E_Disable(); //关闭输入捕获/比较输出
- if(PWMx->PWM_CC6Enable == ENABLE) PWMB_CC6E_Enable(); //开启输入捕获/比较输出
- else PWMB_CC6E_Disable(); //关闭输入捕获/比较输出
- if(PWMx->PWM_CC7Enable == ENABLE) PWMB_CC7E_Enable(); //开启输入捕获/比较输出
- else PWMB_CC7E_Disable(); //关闭输入捕获/比较输出
- if(PWMx->PWM_CC8Enable == ENABLE) PWMB_CC8E_Enable(); //开启输入捕获/比较输出
- else PWMB_CC8E_Disable(); //关闭输入捕获/比较输出
-
- PWMB_AutoReload(PWMx->PWM_Period);
- PWMB_Duty5(PWMx->PWM5_Duty);
- PWMB_Duty6(PWMx->PWM6_Duty);
- PWMB_Duty7(PWMx->PWM7_Duty);
- PWMB_Duty8(PWMx->PWM8_Duty);
-
- PWMB_CCPCBPreloaded(PWMx->PWM_PreLoad); //捕获/比较预装载控制位(该位只对具有互补输出的通道起作用)
- PWMB_PS = PWMx->PWM_PS_SW; //切换IO
- PWMB_ENO = PWMx->PWM_EnoSelect; //输出通道选择
- PWMB_DeadTime(PWMx->PWM_DeadTime); //死区发生器设置
-
- if(PWMx->PWM_BrakeEnable == ENABLE) PWMB_BrakeEnable(); //开启刹车输入
- else PWMB_BrakeDisable(); //禁止刹车输入
- if(PWMx->PWM_MainOutEnable == ENABLE) PWMB_BrakeOutputEnable(); //主输出使能
- else PWMB_BrakeOutputDisable(); //主输出禁止
- if(PWMx->PWM_CEN_Enable == ENABLE) PWMB_CEN_Enable(); //使能计数器
- else PWMB_CEN_Disable(); //禁止计数器
-
- EAXRAM(); /* MOVX A,@DPTR/MOVX @DPTR,A指令的操作对象为扩展RAM(XRAM) */
- return 0;
- }
- return 2; //错误
- }
- /*********************************************************/
- /************* 功能说明 **************
- 本例程基于STC8H8K64U为主控芯片的实验箱8进行编写测试,STC8H系列芯片可通用参考.
- 高级PWM定时器 PWM1P/PWM1N,PWM2P/PWM2N,PWM3P/PWM3N,PWM4P/PWM4N 每个通道都可独立实现PWM输出,或者两两互补对称输出.
- 8个通道PWM设置对应P6的8个端口.
- 通过P6口上连接的8个LED灯,利用PWM实现呼吸灯效果.
- PWM周期和占空比可以根据需要自行设置,最高可达65535.
- 下载时, 选择时钟 24MHZ (用户可在"config.h"修改频率).
- ******************************************/
- /************* 本地常量声明 **************/
- /************* 本地变量声明 **************/
- PWMx_Duty PWMA_Duty;
- /************* 本地函数声明 **************/
- /************* 外部函数和变量声明 *****************/
- /************************ IO口配置 ****************************/
- void GPIO_config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure; //结构定义
- GPIO_InitStructure.Pin = GPIO_Pin_0; //指定要初始化的IO, GPIO_Pin_0 ~ GPIO_Pin_7, 或操作
- GPIO_InitStructure.Mode = GPIO_PullUp; //指定IO的输入或输出方式,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
- GPIO_Inilize(GPIO_P4,&GPIO_InitStructure); //初始化
- GPIO_InitStructure.Pin = GPIO_Pin_All; //指定要初始化的IO, GPIO_Pin_0 ~ GPIO_Pin_7, 或操作
- GPIO_InitStructure.Mode = GPIO_PullUp; //指定IO的输入或输出方式,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
- GPIO_Inilize(GPIO_P6,&GPIO_InitStructure); //初始化
- }
- /************************ 定时器配置 ****************************/
- void Timer_config(void)
- {
- TIM_InitTypeDef TIM_InitStructure; //结构定义
- TIM_InitStructure.TIM_Mode = TIM_16BitAutoReload; //指定工作模式, TIM_16BitAutoReload,TIM_16Bit,TIM_8BitAutoReload,TIM_16BitAutoReloadNoMask
- TIM_InitStructure.TIM_Priority = Priority_0; //指定中断优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
- TIM_InitStructure.TIM_Interrupt = ENABLE; //中断是否允许, ENABLE或DISABLE
- TIM_InitStructure.TIM_ClkSource = TIM_CLOCK_1T; //指定时钟源, TIM_CLOCK_1T,TIM_CLOCK_12T,TIM_CLOCK_Ext
- TIM_InitStructure.TIM_ClkOut = DISABLE; //是否输出高速脉冲, ENABLE或DISABLE
- TIM_InitStructure.TIM_Value = 65536UL - (MAIN_Fosc / 1000); //中断频率, 1000次/秒
- TIM_InitStructure.TIM_Run = ENABLE; //是否初始化后启动定时器, ENABLE或DISABLE
- Timer_Inilize(Timer0,&TIM_InitStructure); //初始化Timer0 Timer0,Timer1,Timer2,Timer3,Timer4
- }
- /*************** 串口初始化函数 *****************/
- void PWM_config(void)
- {
- PWMx_InitDefine PWMx_InitStructure;
-
- PWMx_InitStructure.PWM1_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.PWM2_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.PWM3_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.PWM4_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.PWM1_SetPriority = Priority_0; //指定中断优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
- PWMx_InitStructure.PWM2_SetPriority = Priority_0; //指定中断优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
- PWMx_InitStructure.PWM3_SetPriority = Priority_0; //指定中断优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
- PWMx_InitStructure.PWM4_SetPriority = Priority_0; //指定中断优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
-
- PWMx_InitStructure.PWM_Period = 2047; //周期时间, 0~65535
- PWMx_InitStructure.PWM1_Duty = PWMA_Duty.PWM1_Duty; //PWM1占空比时间, 0~Period
- PWMx_InitStructure.PWM2_Duty = PWMA_Duty.PWM2_Duty; //PWM2占空比时间, 0~Period
- PWMx_InitStructure.PWM3_Duty = PWMA_Duty.PWM3_Duty; //PWM3占空比时间, 0~Period
- PWMx_InitStructure.PWM4_Duty = PWMA_Duty.PWM4_Duty; //PWM4占空比时间, 0~Period
- PWMx_InitStructure.PWM_DeadTime = 0; //死区发生器设置, 0~255
-
- PWMx_InitStructure.PWM_EnoSelect = ENO1P | ENO1N | ENO2P | ENO2N | ENO3P | ENO3N | ENO4P | ENO4N; //输出通道选择, ENO1P,ENO1N,ENO2P,ENO2N,ENO3P,ENO3N,ENO4P,ENO4N / ENO5P,ENO6P,ENO7P,ENO8P
- PWMx_InitStructure.PWM_PS_SW = PWM1_SW_P60_P61| PWM2_SW_P62_P63 | PWM3_SW_P64_P65 | PWM4_SW_P66_P67; //切换端口, PWM1_SW_P10_P11,PWM1_SW_P20_P21,PWM1_SW_P60_P61
- // PWM2_SW_P12_P13,PWM2_SW_P22_P23,PWM2_SW_P62_P63
- // PWM3_SW_P14_P15,PWM3_SW_P24_P25,PWM3_SW_P64_P65
- // PWM4_SW_P16_P17,PWM4_SW_P26_P27,PWM4_SW_P66_P67,PWM4_SW_P34_P33
- PWMx_InitStructure.PWM_CC1Enable = ENABLE; //开启PWM1P输入捕获/比较输出, ENABLE,DISABLE
- PWMx_InitStructure.PWM_CC1NEnable = ENABLE; //开启PWM1N输入捕获/比较输出, ENABLE,DISABLE
- PWMx_InitStructure.PWM_CC2Enable = ENABLE; //开启PWM2P输入捕获/比较输出, ENABLE,DISABLE
- PWMx_InitStructure.PWM_CC2NEnable = ENABLE; //开启PWM2N输入捕获/比较输出, ENABLE,DISABLE
- PWMx_InitStructure.PWM_CC3Enable = ENABLE; //开启PWM3P输入捕获/比较输出, ENABLE,DISABLE
- PWMx_InitStructure.PWM_CC3NEnable = ENABLE; //开启PWM3N输入捕获/比较输出, ENABLE,DISABLE
- PWMx_InitStructure.PWM_CC4Enable = ENABLE; //开启PWM4P输入捕获/比较输出, ENABLE,DISABLE
- PWMx_InitStructure.PWM_CC4NEnable = ENABLE; //开启PWM4N输入捕获/比较输出, ENABLE,DISABLE
-
- PWMx_InitStructure.PWM_MainOutEnable= ENABLE; //主输出使能, ENABLE,DISABLE
- PWMx_InitStructure.PWM_CEN_Enable = ENABLE; //使能计数器, ENABLE,DISABLE
- PWM_Configuration(PWMA, &PWMx_InitStructure); //初始化PWM, PWMA,PWMB
- }
- /******************* PWM 占空比设置函数 *******************/
- void UpdatePwm(u8 PWM, PWMx_Duty *PWMx)
- {
- EAXSFR(); /* MOVX A,@DPTR/MOVX @DPTR,A指令的操作对象为扩展SFR(XSFR) */
- if(PWM == PWMA)
- {
- PWMA_Duty1(PWMx->PWM1_Duty);
- PWMA_Duty2(PWMx->PWM2_Duty);
- PWMA_Duty3(PWMx->PWM3_Duty);
- PWMA_Duty4(PWMx->PWM4_Duty);
- }
- else
- {
- PWMB_Duty5(PWMx->PWM5_Duty);
- PWMB_Duty6(PWMx->PWM6_Duty);
- PWMB_Duty7(PWMx->PWM7_Duty);
- PWMB_Duty8(PWMx->PWM8_Duty);
- }
-
- EAXRAM(); /* MOVX A,@DPTR/MOVX @DPTR,A指令的操作对象为扩展RAM(XRAM) */
- }
- /*********************************************************/
- /******************** 主函数**************************/
- void main(void)
- {
- PWMA_Duty.PWM1_Duty = 128;
- PWMA_Duty.PWM2_Duty = 256;
- PWMA_Duty.PWM3_Duty = 512;
- PWMA_Duty.PWM4_Duty = 1024;
-
- GPIO_config();
- Timer_config();
- PWM_config();
- EA = 1;
- P40 = 0; //打开LED电源
- while (1);
- }
复制代码 |