|
個人實測已經(jīng)通過,大家可以借鑒一下
單片機源程序如下:
- #include "config.h"
- #include "GPIO.h"
- #include "PCA.h"
- #include "delay.h"
- /************* 功能說明 ***************
- 本例程基于STC8G1K08-20PIN進(jìn)行編寫測試,STC8G系列芯片可通用參考.
- 輸出3路變化的PWM信號, 類似"呼吸燈"的驅(qū)動.
- PWM0 為8位PWM.
- PWM1 為7位PWM.
- PWM2 為10位PWM.
- 下載時, 選擇時鐘 24MHz (用戶可在"config.h"修改頻率).
- ******************************************/
- /************* 本地常量聲明 **************/
- /************* 本地變量聲明 **************/
- u16 pwm0,pwm1,pwm2;
- bit B_PWM0_Dir,B_PWM1_Dir,B_PWM2_Dir; //方向, 0為+, 1為-.
- /************* 本地函數(shù)聲明 **************/
- /************* 外部函數(shù)和變量聲明 *****************/
- /******************** IO口配置 ********************/
- void GPIO_config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure; //結(jié)構(gòu)定義
- 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_P1,&GPIO_InitStructure); //初始化
- GPIO_InitStructure.Pin = GPIO_Pin_7; //指定要初始化的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_P3,&GPIO_InitStructure); //初始化
- }
- /******************** PCA配置 ********************/
- void PCA_config(void)
- {
- PCA_InitTypeDef PCA_InitStructure;
- PCA_InitStructure.PCA_Clock = PCA_Clock_1T; //PCA_Clock_1T, PCA_Clock_2T, PCA_Clock_4T, PCA_Clock_6T, PCA_Clock_8T, PCA_Clock_12T, PCA_Clock_Timer0_OF, PCA_Clock_ECI
- PCA_InitStructure.PCA_IoUse = PCA_P12_P11_P10_P37; //PCA_P12_P11_P10_P37, PCA_P34_P35_P36_P37, PCA_P24_P25_P26_P27
- PCA_InitStructure.PCA_Interrupt_Mode = DISABLE; //ENABLE, DISABLE
- PCA_InitStructure.PCA_Priority = Priority_0; //指定中斷優(yōu)先級(低到高) Priority_0,Priority_1,Priority_2,Priority_3
- PCA_InitStructure.PCA_RUN = DISABLE; //ENABLE, DISABLE
- PCA_Init(PCA_Counter,&PCA_InitStructure); //設(shè)置公用Counter
- PCA_InitStructure.PCA_Mode = PCA_Mode_PWM; //PCA_Mode_PWM, PCA_Mode_Capture, PCA_Mode_SoftTimer, PCA_Mode_HighPulseOutput
- PCA_InitStructure.PCA_PWM_Wide = PCA_PWM_8bit; //PCA_PWM_8bit, PCA_PWM_7bit, PCA_PWM_6bit, PCA_PWM_10bit
- PCA_InitStructure.PCA_Interrupt_Mode = DISABLE; //PCA_Rise_Active, PCA_Fall_Active, ENABLE, DISABLE
- PCA_InitStructure.PCA_Value = 128 << 8; //對于PWM,高8位為PWM占空比
- PCA_Init(PCA0,&PCA_InitStructure);
- PCA_InitStructure.PCA_Mode = PCA_Mode_PWM; //PCA_Mode_PWM, PCA_Mode_Capture, PCA_Mode_SoftTimer, PCA_Mode_HighPulseOutput
- PCA_InitStructure.PCA_PWM_Wide = PCA_PWM_7bit; //PCA_PWM_8bit, PCA_PWM_7bit, PCA_PWM_6bit, PCA_PWM_10bit
- PCA_InitStructure.PCA_Interrupt_Mode = DISABLE; //PCA_Rise_Active, PCA_Fall_Active, ENABLE, DISABLE
- PCA_InitStructure.PCA_Value = 64 << 8; //對于PWM,高8位為PWM占空比
- PCA_Init(PCA1,&PCA_InitStructure);
- PCA_InitStructure.PCA_Mode = PCA_Mode_PWM; //PCA_Mode_PWM, PCA_Mode_Capture, PCA_Mode_SoftTimer, PCA_Mode_HighPulseOutput
- PCA_InitStructure.PCA_PWM_Wide = PCA_PWM_10bit; //PCA_PWM_8bit, PCA_PWM_7bit, PCA_PWM_6bit, PCA_PWM_10bit
- PCA_InitStructure.PCA_Interrupt_Mode = DISABLE; //PCA_Rise_Active, PCA_Fall_Active, ENABLE, DISABLE
- PCA_InitStructure.PCA_Value = 32 << 8; //對于PWM,高8位為PWM占空比
- PCA_Init(PCA2,&PCA_InitStructure);
- CR = 1; //啟動PCA
- }
- /******************** task A **************************/
- void main(void)
- {
- GPIO_config();
- PCA_config();
- pwm0 = 128;
- pwm1 = 64;
- pwm2 = 512;
- B_PWM0_Dir = 0;
- B_PWM1_Dir = 0;
- B_PWM2_Dir = 0;
- UpdatePwm(PCA0,pwm0);
- UpdatePwm(PCA1,pwm1);
- UpdatePwm(PCA2,pwm2);
- // EA = 1;
-
- while (1)
- {
- delay_ms(20);
- if(B_PWM0_Dir)
- {
- if(--pwm0 <= 16) B_PWM0_Dir = 0; //8位PWM
- }
- else if(++pwm0 >= 240) B_PWM0_Dir = 1; //8位PWM
- UpdatePwm(PCA0,pwm0);
- if(B_PWM1_Dir)
- {
- if(--pwm1 <= 8) B_PWM1_Dir = 0; //7位PWM
- }
- else if(++pwm1 >= 120) B_PWM1_Dir = 1; //7位PWM
- UpdatePwm(PCA1,pwm1);
- if(B_PWM2_Dir)
- {
- if(--pwm2 <= 24) B_PWM2_Dir = 0; //10位PWM
- }
- else if(++pwm2 >= 1000) B_PWM2_Dir = 1; //10位PWM
- UpdatePwm(PCA2,pwm2);
- }
- }
復(fù)制代碼
Keil代碼下載:
3路硬件PWM模式驅(qū)動電機-STC8G.zip
(250.5 KB, 下載次數(shù): 67)
2022-5-11 13:36 上傳
點擊文件名下載附件
|
評分
-
查看全部評分
|