|
我給你來個(gè)程序試試
高級PWM1-PWM2-PWM3-PWM4驅(qū)動(dòng)P6口呼吸燈實(shí)驗(yàn)程序-STC8H
- /*---------------------------------------------------------------------*/
- /* --- STC MCU Limited ------------------------------------------------*/
- /* --- STC 1T Series MCU Demo Programme -------------------------------*/
- /* 如果要在程序中使用此代碼,請?jiān)诔绦蛑凶⒚魇褂昧薙TC的資料及程序 */
- /*---------------------------------------------------------------------*/
- //11-高級PWM1-PWM2-PWM3-PWM4驅(qū)動(dòng)P6口呼吸燈實(shí)驗(yàn)程序-STC8H
- /*********************************************************/
- //#define MAIN_Fosc 22118400L //定義主時(shí)鐘
- //#define MAIN_Fosc 12000000L //定義主時(shí)鐘
- //#define MAIN_Fosc 11059200L //定義主時(shí)鐘
- //#define MAIN_Fosc 5529600L //定義主時(shí)鐘
- #define MAIN_Fosc 24000000L //定義主時(shí)鐘
- /*********************************************************/
- #include "STC8xxxx.H"
- #define GPIO_PullUp 0 //上拉準(zhǔn)雙向口
- #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; //要設(shè)置的端口
- } 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; //優(yōu)先級設(shè)置 Priority_0,Priority_1,Priority_2,Priority_3
- u8 TIM_Interrupt; //中斷允許 ENABLE,DISABLE
- u8 TIM_ClkSource; //時(shí)鐘源 TIM_CLOCK_1T,TIM_CLOCK_12T,TIM_CLOCK_Ext
- u8 TIM_ClkOut; //可編程時(shí)鐘輸出, ENABLE,DISABLE
- u16 TIM_Value; //裝載初值
- u8 TIM_Run; //是否運(yùn)行 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; //周期時(shí)間, 0~65535
- u16 PWM1_Duty; //PWM1占空比時(shí)間, 0~Period
- u16 PWM2_Duty; //PWM2占空比時(shí)間, 0~Period
- u16 PWM3_Duty; //PWM3占空比時(shí)間, 0~Period
- u16 PWM4_Duty; //PWM4占空比時(shí)間, 0~Period
- u16 PWM5_Duty; //PWM5占空比時(shí)間, 0~Period
- u16 PWM6_Duty; //PWM6占空比時(shí)間, 0~Period
- u16 PWM7_Duty; //PWM7占空比時(shí)間, 0~Period
- u16 PWM8_Duty; //PWM8占空比時(shí)間, 0~Period
- u8 PWM_DeadTime; //死區(qū)發(fā)生器設(shè)置, 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; //輸出比較的預(yù)裝載使能, ENABLE,DISABLE
- u8 PWM_Fast; //輸出比較快速功能使能, ENABLE,DISABLE
- u8 PWM1_SetPriority; //設(shè)置PWM1優(yōu)先級, Priority_0,Priority_1,Priority_2,Priority_3
- u8 PWM2_SetPriority; //設(shè)置PWM2優(yōu)先級, Priority_0,Priority_1,Priority_2,Priority_3
- u8 PWM3_SetPriority; //設(shè)置PWM3優(yōu)先級, Priority_0,Priority_1,Priority_2,Priority_3
- u8 PWM4_SetPriority; //設(shè)置PWM4優(yōu)先級, Priority_0,Priority_1,Priority_2,Priority_3
- u8 PWM5_SetPriority; //設(shè)置PWM4優(yōu)先級, 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; //預(yù)裝載, ENABLE,DISABLE
- u8 PWM_PS_SW; //切換端口, PWM1_SW_P10_P11,PWM1_SW_P20_P21,PWM1_SW_P60_P61
- u8 PWM_CEN_Enable; //使能計(jì)數(shù)器, ENABLE,DISABLE
- u8 PWM_BrakeEnable; //剎車輸入使能, ENABLE,DISABLE
- u8 PWM_MainOutEnable;//主輸出使能, ENABLE,DISABLE
- } PWMx_InitDefine;
- typedef struct
- {
- u16 PWM1_Duty; //PWM1占空比時(shí)間, 0~Period
- u16 PWM2_Duty; //PWM2占空比時(shí)間, 0~Period
- u16 PWM3_Duty; //PWM3占空比時(shí)間, 0~Period
- u16 PWM4_Duty; //PWM4占空比時(shí)間, 0~Period
- u16 PWM5_Duty; //PWM5占空比時(shí)間, 0~Period
- u16 PWM6_Duty; //PWM6占空比時(shí)間, 0~Period
- u16 PWM7_Duty; //PWM7占空比時(shí)間, 0~Period
- u16 PWM8_Duty; //PWM8占空比時(shí)間, 0~Period
- } PWMx_Duty;
- u8 PWM_Configuration(u8 PWM, PWMx_InitDefine *PWMx);
- void UpdatePwm(u8 PWM, PWMx_Duty *PWMx);
- /*************** 功能說明 ****************
- 本文件為STC8系列的端口初始化程序,用戶幾乎可以不修改這個(gè)程序.
- ******************************************/
- //========================================================================
- // 函數(shù): u8 GPIO_Inilize(u8 GPIO, GPIO_InitTypeDef *GPIOx)
- // 描述: 初始化IO口.
- // 參數(shù): GPIOx: 結(jié)構(gòu)參數(shù),請參考timer.h里的定義.
- // 返回: 成功返回0, 空操作返回1,錯(cuò)誤返回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; //錯(cuò)誤
- if(GPIO == GPIO_P0)
- {
- if(GPIOx->Mode == GPIO_PullUp) P0M1 &= ~GPIOx->Pin, P0M0 &= ~GPIOx->Pin; //上拉準(zhǔn)雙向口
- 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; //上拉準(zhǔn)雙向口
- 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; //上拉準(zhǔn)雙向口
- 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; //上拉準(zhǔn)雙向口
- 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; //上拉準(zhǔn)雙向口
- 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; //上拉準(zhǔn)雙向口
- 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; //上拉準(zhǔn)雙向口
- 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; //上拉準(zhǔn)雙向口
- 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系列的定時(shí)器初始化和中斷程序,用戶可以在這個(gè)文件中修改自己需要的中斷程序.
- ******************************************/
- /************* 本地變量聲明 **************/
- bit PWM1_Flag;
- bit PWM2_Flag;
- bit PWM3_Flag;
- bit PWM4_Flag;
- /************* 外部函數(shù)和變量聲明 *****************/
- extern PWMx_Duty PWMA_Duty;
- /********************* Timer0中斷函數(shù)************************/
- 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中斷函數(shù)************************/
- void timer1_int (void) interrupt TIMER1_VECTOR
- {
- // P66 = ~P66;
- }
- /********************* Timer2中斷函數(shù)************************/
- void timer2_int (void) interrupt TIMER2_VECTOR
- {
- // P65 = ~P65;
- }
- /********************* Timer3中斷函數(shù)************************/
- void timer3_int (void) interrupt TIMER3_VECTOR
- {
- // P64 = ~P64;
- }
- /********************* Timer4中斷函數(shù)************************/
- void timer4_int (void) interrupt TIMER4_VECTOR
- {
- // P63 = ~P63;
- }
- //========================================================================
- // 函數(shù): u8 Timer_Inilize(u8 TIM, TIM_InitTypeDef *TIMx)
- // 描述: 定時(shí)器初始化程序.
- // 參數(shù): TIMx: 結(jié)構(gòu)參數(shù),請參考timer.h里的定義.
- // 返回: 成功返回0, 空操作返回1,錯(cuò)誤返回2.
- // 版本: V1.0, 2012-10-22
- //========================================================================
- u8 Timer_Inilize(u8 TIM, TIM_InitTypeDef *TIMx)
- {
- if(TIM > Timer4) return 1; //空操作
- if(TIM == Timer0)
- {
- Timer0_Stop(); //停止計(jì)數(shù)
- if(TIMx->TIM_Interrupt == ENABLE) Timer0_InterruptEnable(); //允許中斷
- else Timer0_InterruptDisable(); //禁止中斷
- if(TIMx->TIM_Priority > Priority_3) return 2; //錯(cuò)誤
- Timer0_Priority(TIMx->TIM_Priority); //指定中斷優(yōu)先級(低到高) Priority_0,Priority_1,Priority_2,Priority_3
- if(TIMx->TIM_Mode >= TIM_16BitAutoReloadNoMask) return 2; //錯(cuò)誤
- TMOD = (TMOD & ~0x30) | TIMx->TIM_Mode; //工作模式,0: 16位自動(dòng)重裝, 1: 16位定時(shí)/計(jì)數(shù), 2: 8位自動(dòng)重裝
- 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(); //對外計(jì)數(shù)或分頻
- else Timer0_AsTimer(); //定時(shí)
- if(TIMx->TIM_ClkOut == ENABLE) Timer0_CLKO_Enable(); //輸出時(shí)鐘
- else Timer0_CLKO_Disable(); //不輸出時(shí)鐘
-
- T0_Load(TIMx->TIM_Value);
- if(TIMx->TIM_Run == ENABLE) Timer0_Run(); //開始運(yùn)行
- return 0; //成功
- }
- if(TIM == Timer1)
- {
- Timer1_Stop(); //停止計(jì)數(shù)
- if(TIMx->TIM_Interrupt == ENABLE) Timer1_InterruptEnable(); //允許中斷
- else Timer1_InterruptDisable(); //禁止中斷
- if(TIMx->TIM_Priority > Priority_3) return 2; //錯(cuò)誤
- Timer1_Priority(TIMx->TIM_Priority); //指定中斷優(yōu)先級(低到高) Priority_0,Priority_1,Priority_2,Priority_3
- if(TIMx->TIM_Mode >= TIM_16BitAutoReloadNoMask) return 2; //錯(cuò)誤
- TMOD = (TMOD & ~0x30) | TIMx->TIM_Mode; //工作模式,0: 16位自動(dòng)重裝, 1: 16位定時(shí)/計(jì)數(shù), 2: 8位自動(dòng)重裝
- 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(); //對外計(jì)數(shù)或分頻
- else Timer1_AsTimer(); //定時(shí)
- if(TIMx->TIM_ClkOut == ENABLE) Timer1_CLKO_Enable(); //輸出時(shí)鐘
- else Timer1_CLKO_Disable(); //不輸出時(shí)鐘
-
- T1_Load(TIMx->TIM_Value);
- if(TIMx->TIM_Run == ENABLE) Timer1_Run(); //開始運(yùn)行
- return 0; //成功
- }
- if(TIM == Timer2) //Timer2,固定為16位自動(dòng)重裝, 中斷無優(yōu)先級
- {
- Timer2_Stop(); //停止計(jì)數(shù)
- 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(); //對外計(jì)數(shù)或分頻
- else Timer2_AsTimer(); //定時(shí)
- if(TIMx->TIM_ClkOut == ENABLE) Timer2_CLKO_Enable(); //輸出時(shí)鐘
- else Timer2_CLKO_Disable(); //不輸出時(shí)鐘
- T2_Load(TIMx->TIM_Value);
- if(TIMx->TIM_Run == ENABLE) Timer2_Run(); //開始運(yùn)行
- return 0; //成功
- }
- if(TIM == Timer3) //Timer3,固定為16位自動(dòng)重裝, 中斷無優(yōu)先級
- {
- Timer3_Stop(); //停止計(jì)數(shù)
- 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(); //對外計(jì)數(shù)或分頻
- else Timer3_AsTimer(); //定時(shí)
- if(TIMx->TIM_ClkOut == ENABLE) Timer3_CLKO_Enable(); //輸出時(shí)鐘
- else Timer3_CLKO_Disable(); //不輸出時(shí)鐘
- T3_Load(TIMx->TIM_Value);
- if(TIMx->TIM_Run == ENABLE) Timer3_Run(); //開始運(yùn)行
- return 0; //成功
- }
- if(TIM == Timer4) //Timer3,固定為16位自動(dòng)重裝, 中斷無優(yōu)先級
- {
- Timer4_Stop(); //停止計(jì)數(shù)
- 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(); //對外計(jì)數(shù)或分頻
- else Timer4_AsTimer(); //定時(shí)
- if(TIMx->TIM_ClkOut == ENABLE) Timer4_CLKO_Enable(); //輸出時(shí)鐘
- else Timer4_CLKO_Disable(); //不輸出時(shí)鐘
- T4_Load(TIMx->TIM_Value);
- if(TIMx->TIM_Run == ENABLE) Timer4_Run(); //開始運(yùn)行
- return 0; //成功
- }
- return 2; //錯(cuò)誤
- }
- u8 PWM_Configuration(u8 PWM, PWMx_InitDefine *PWMx)
- {
- if(PWM == PWMA)
- {
- EAXSFR(); /* MOVX A,@DPTR/MOVX @DPTR,A指令的操作對象為擴(kuò)展SFR(XSFR) */
-
- if(PWMx->PWM1_SetPriority <= Priority_3) PWM1_Priority(PWMx->PWM1_SetPriority); //指定中斷優(yōu)先級(低到高) Priority_0,Priority_1,Priority_2,Priority_3
- if(PWMx->PWM2_SetPriority <= Priority_3) PWM2_Priority(PWMx->PWM2_SetPriority); //指定中斷優(yōu)先級(低到高) Priority_0,Priority_1,Priority_2,Priority_3
- if(PWMx->PWM3_SetPriority <= Priority_3) PWM3_Priority(PWMx->PWM3_SetPriority); //指定中斷優(yōu)先級(低到高) Priority_0,Priority_1,Priority_2,Priority_3
- if(PWMx->PWM4_SetPriority <= Priority_3) PWM4_Priority(PWMx->PWM4_SetPriority); //指定中斷優(yōu)先級(低到高) Priority_0,Priority_1,Priority_2,Priority_3
-
- PWMA_CCER1_Disable(); //關(guān)閉所有輸入捕獲/比較輸出
- PWMA_CCER2_Disable(); //關(guān)閉所有輸入捕獲/比較輸出
- PWMA_OC1ModeSet(PWMx->PWM1_Mode); //設(shè)置輸出比較模式
- PWMA_OC2ModeSet(PWMx->PWM2_Mode); //設(shè)置輸出比較模式
- PWMA_OC3ModeSet(PWMx->PWM3_Mode); //設(shè)置輸出比較模式
- PWMA_OC4ModeSet(PWMx->PWM4_Mode); //設(shè)置輸出比較模式
- if(PWMx->PWM_Reload == ENABLE) PWMA_OC1_ReloadEnable(); //輸出比較的預(yù)裝載使能
- else PWMA_OC1_RelosdDisable(); //禁止輸出比較的預(yù)裝載
- if(PWMx->PWM_Fast == ENABLE) PWMA_OC1_FastEnable(); //輸出比較快速功能使能
- else PWMA_OC1_FastDisable(); //禁止輸出比較快速功能
-
- if(PWMx->PWM_CC1Enable == ENABLE) PWMA_CC1E_Enable(); //開啟輸入捕獲/比較輸出
- else PWMA_CC1E_Disable(); //關(guān)閉輸入捕獲/比較輸出
- if(PWMx->PWM_CC1NEnable == ENABLE) PWMA_CC1NE_Enable(); //開啟輸入捕獲/比較輸出
- else PWMA_CC1NE_Disable(); //關(guān)閉輸入捕獲/比較輸出
- if(PWMx->PWM_CC2Enable == ENABLE) PWMA_CC2E_Enable(); //開啟輸入捕獲/比較輸出
- else PWMA_CC2E_Disable(); //關(guān)閉輸入捕獲/比較輸出
- if(PWMx->PWM_CC2NEnable == ENABLE) PWMA_CC2NE_Enable(); //開啟輸入捕獲/比較輸出
- else PWMA_CC2NE_Disable(); //關(guān)閉輸入捕獲/比較輸出
- if(PWMx->PWM_CC3Enable == ENABLE) PWMA_CC3E_Enable(); //開啟輸入捕獲/比較輸出
- else PWMA_CC3E_Disable(); //關(guān)閉輸入捕獲/比較輸出
- if(PWMx->PWM_CC3NEnable == ENABLE) PWMA_CC3NE_Enable(); //開啟輸入捕獲/比較輸出
- else PWMA_CC3NE_Disable(); //關(guān)閉輸入捕獲/比較輸出
- if(PWMx->PWM_CC4Enable == ENABLE) PWMA_CC4E_Enable(); //開啟輸入捕獲/比較輸出
- else PWMA_CC4E_Disable(); //關(guān)閉輸入捕獲/比較輸出
- if(PWMx->PWM_CC4NEnable == ENABLE) PWMA_CC4NE_Enable(); //開啟輸入捕獲/比較輸出
- else PWMA_CC4NE_Disable(); //關(guān)閉輸入捕獲/比較輸出
-
- 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); //捕獲/比較預(yù)裝載控制位(該位只對具有互補(bǔ)輸出的通道起作用)
- PWMA_PS = PWMx->PWM_PS_SW; //切換IO
- PWMA_ENO = PWMx->PWM_EnoSelect; //輸出通道選擇
- PWMA_DeadTime(PWMx->PWM_DeadTime); //死區(qū)發(fā)生器設(shè)置
-
- 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(); //使能計(jì)數(shù)器
- else PWMA_CEN_Disable(); //禁止計(jì)數(shù)器
-
- EAXRAM(); /* MOVX A,@DPTR/MOVX @DPTR,A指令的操作對象為擴(kuò)展RAM(XRAM) */
- return 0;
- }
- if(PWM == PWMB)
- {
- EAXSFR(); /* MOVX A,@DPTR/MOVX @DPTR,A指令的操作對象為擴(kuò)展SFR(XSFR) */
-
- if(PWMx->PWM5_SetPriority <= Priority_3) PWM5_Priority(PWMx->PWM5_SetPriority); //指定中斷優(yōu)先級(低到高) Priority_0,Priority_1,Priority_2,Priority_3
-
- PWMB_CCER1_Disable(); //關(guān)閉所有輸入捕獲/比較輸出
- PWMB_CCER2_Disable(); //關(guān)閉所有輸入捕獲/比較輸出
- PWMB_OC5ModeSet(PWMx->PWM5_Mode); //設(shè)置輸出比較模式
- PWMB_OC6ModeSet(PWMx->PWM6_Mode); //設(shè)置輸出比較模式
- PWMB_OC7ModeSet(PWMx->PWM7_Mode); //設(shè)置輸出比較模式
- PWMB_OC8ModeSet(PWMx->PWM8_Mode); //設(shè)置輸出比較模式
- if(PWMx->PWM_Reload == ENABLE) PWMB_OC5_ReloadEnable(); //輸出比較的預(yù)裝載使能
- else PWMB_OC5_RelosdDisable(); //禁止輸出比較的預(yù)裝載
- if(PWMx->PWM_Fast == ENABLE) PWMB_OC5_FastEnable(); //輸出比較快速功能使能
- else PWMB_OC5_FastDisable(); //禁止輸出比較快速功能
-
- if(PWMx->PWM_CC5Enable == ENABLE) PWMB_CC5E_Enable(); //開啟輸入捕獲/比較輸出
- else PWMB_CC5E_Disable(); //關(guān)閉輸入捕獲/比較輸出
- if(PWMx->PWM_CC6Enable == ENABLE) PWMB_CC6E_Enable(); //開啟輸入捕獲/比較輸出
- else PWMB_CC6E_Disable(); //關(guān)閉輸入捕獲/比較輸出
- if(PWMx->PWM_CC7Enable == ENABLE) PWMB_CC7E_Enable(); //開啟輸入捕獲/比較輸出
- else PWMB_CC7E_Disable(); //關(guān)閉輸入捕獲/比較輸出
- if(PWMx->PWM_CC8Enable == ENABLE) PWMB_CC8E_Enable(); //開啟輸入捕獲/比較輸出
- else PWMB_CC8E_Disable(); //關(guān)閉輸入捕獲/比較輸出
-
- 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); //捕獲/比較預(yù)裝載控制位(該位只對具有互補(bǔ)輸出的通道起作用)
- PWMB_PS = PWMx->PWM_PS_SW; //切換IO
- PWMB_ENO = PWMx->PWM_EnoSelect; //輸出通道選擇
- PWMB_DeadTime(PWMx->PWM_DeadTime); //死區(qū)發(fā)生器設(shè)置
-
- 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(); //使能計(jì)數(shù)器
- else PWMB_CEN_Disable(); //禁止計(jì)數(shù)器
-
- EAXRAM(); /* MOVX A,@DPTR/MOVX @DPTR,A指令的操作對象為擴(kuò)展RAM(XRAM) */
- return 0;
- }
- return 2; //錯(cuò)誤
- }
- /*********************************************************/
- /************* 功能說明 **************
- 本例程基于STC8H8K64U為主控芯片的實(shí)驗(yàn)箱8進(jìn)行編寫測試,STC8H系列芯片可通用參考.
- 高級PWM定時(shí)器 PWM1P/PWM1N,PWM2P/PWM2N,PWM3P/PWM3N,PWM4P/PWM4N 每個(gè)通道都可獨(dú)立實(shí)現(xiàn)PWM輸出,或者兩兩互補(bǔ)對稱輸出.
- 8個(gè)通道PWM設(shè)置對應(yīng)P6的8個(gè)端口.
- 通過P6口上連接的8個(gè)LED燈,利用PWM實(shí)現(xiàn)呼吸燈效果.
- PWM周期和占空比可以根據(jù)需要自行設(shè)置,最高可達(dá)65535.
- 下載時(shí), 選擇時(shí)鐘 24MHZ (用戶可在"config.h"修改頻率).
- ******************************************/
- /************* 本地常量聲明 **************/
- /************* 本地變量聲明 **************/
- PWMx_Duty PWMA_Duty;
- /************* 本地函數(shù)聲明 **************/
- /************* 外部函數(shù)和變量聲明 *****************/
- /************************ IO口配置 ****************************/
- void GPIO_config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure; //結(jié)構(gòu)定義
- 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); //初始化
- }
- /************************ 定時(shí)器配置 ****************************/
- void Timer_config(void)
- {
- TIM_InitTypeDef TIM_InitStructure; //結(jié)構(gòu)定義
- TIM_InitStructure.TIM_Mode = TIM_16BitAutoReload; //指定工作模式, TIM_16BitAutoReload,TIM_16Bit,TIM_8BitAutoReload,TIM_16BitAutoReloadNoMask
- TIM_InitStructure.TIM_Priority = Priority_0; //指定中斷優(yōu)先級(低到高) Priority_0,Priority_1,Priority_2,Priority_3
- TIM_InitStructure.TIM_Interrupt = ENABLE; //中斷是否允許, ENABLE或DISABLE
- TIM_InitStructure.TIM_ClkSource = TIM_CLOCK_1T; //指定時(shí)鐘源, 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; //是否初始化后啟動(dòng)定時(shí)器, ENABLE或DISABLE
- Timer_Inilize(Timer0,&TIM_InitStructure); //初始化Timer0 Timer0,Timer1,Timer2,Timer3,Timer4
- }
- /*************** 串口初始化函數(shù) *****************/
- 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; //指定中斷優(yōu)先級(低到高) Priority_0,Priority_1,Priority_2,Priority_3
- PWMx_InitStructure.PWM2_SetPriority = Priority_0; //指定中斷優(yōu)先級(低到高) Priority_0,Priority_1,Priority_2,Priority_3
- PWMx_InitStructure.PWM3_SetPriority = Priority_0; //指定中斷優(yōu)先級(低到高) Priority_0,Priority_1,Priority_2,Priority_3
- PWMx_InitStructure.PWM4_SetPriority = Priority_0; //指定中斷優(yōu)先級(低到高) Priority_0,Priority_1,Priority_2,Priority_3
-
- PWMx_InitStructure.PWM_Period = 2047; //周期時(shí)間, 0~65535
- PWMx_InitStructure.PWM1_Duty = PWMA_Duty.PWM1_Duty; //PWM1占空比時(shí)間, 0~Period
- PWMx_InitStructure.PWM2_Duty = PWMA_Duty.PWM2_Duty; //PWM2占空比時(shí)間, 0~Period
- PWMx_InitStructure.PWM3_Duty = PWMA_Duty.PWM3_Duty; //PWM3占空比時(shí)間, 0~Period
- PWMx_InitStructure.PWM4_Duty = PWMA_Duty.PWM4_Duty; //PWM4占空比時(shí)間, 0~Period
- PWMx_InitStructure.PWM_DeadTime = 0; //死區(qū)發(fā)生器設(shè)置, 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; //使能計(jì)數(shù)器, ENABLE,DISABLE
- PWM_Configuration(PWMA, &PWMx_InitStructure); //初始化PWM, PWMA,PWMB
- }
- /******************* PWM 占空比設(shè)置函數(shù) *******************/
- void UpdatePwm(u8 PWM, PWMx_Duty *PWMx)
- {
- EAXSFR(); /* MOVX A,@DPTR/MOVX @DPTR,A指令的操作對象為擴(kuò)展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指令的操作對象為擴(kuò)展RAM(XRAM) */
- }
- /*********************************************************/
- /******************** 主函數(shù)**************************/
- 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);
- }
復(fù)制代碼 |
|