#include "pwm.h" #include "../clib/type.h" #include "../clib/bit.h" #include "../cpu/c51_macro.h" #include "../bsp/bsp_config.h" /********** sfr P_SW2 = 0xba; sfr PWMCFG = 0xf1; sfr PWMIF = 0xf6; sfr PWMFDCR = 0xf7; sfr PWMCR = 0xfe; #define PWMC (*(unsigned int volatile xdata *)0xfff0) #define PWMCKS (*(unsigned char volatile xdata *)0xfff2) #define TADCP (*(unsigned int volatile xdata *)0xfff3) #define PWM0T1 (*(unsigned int volatile xdata *)0xff00) #define PWM0T2 (*(unsigned int volatile xdata *)0xff02) #define PWM0CR (*(unsigned char volatile xdata *)0xff04) #define PWM0HLD (*(unsigned char volatile xdata *)0xff05) #define PWM1T1 (*(unsigned int volatile xdata *)0xff10) #define PWM1T2 (*(unsigned int volatile xdata *)0xff12) #define PWM1CR (*(unsigned char volatile xdata *)0xff14) #define PWM1HLD (*(unsigned char volatile xdata *)0xff15) #define PWM2T1 (*(unsigned int volatile xdata *)0xff20) #define PWM2T2 (*(unsigned int volatile xdata *)0xff22) #define PWM2CR (*(unsigned char volatile xdata *)0xff24) #define PWM2HLD (*(unsigned char volatile xdata *)0xff25) #define PWM3T1 (*(unsigned int volatile xdata *)0xff30) #define PWM3T2 (*(unsigned int volatile xdata *)0xff32) #define PWM3CR (*(unsigned char volatile xdata *)0xff34) #define PWM3HLD (*(unsigned char volatile xdata *)0xff35) #define PWM4T1 (*(unsigned int volatile xdata *)0xff40) #define PWM4T2 (*(unsigned int volatile xdata *)0xff42) #define PWM4CR (*(unsigned char volatile xdata *)0xff44) #define PWM4HLD (*(unsigned char volatile xdata *)0xff45) #define PWM5T1 (*(unsigned int volatile xdata *)0xff50) #define PWM5T2 (*(unsigned int volatile xdata *)0xff52) #define PWM5CR (*(unsigned char volatile xdata *)0xff54) #define PWM5HLD (*(unsigned char volatile xdata *)0xff55) #define PWM6T1 (*(unsigned int volatile xdata *)0xff60) #define PWM6T2 (*(unsigned int volatile xdata *)0xff62) #define PWM6CR (*(unsigned char volatile xdata *)0xff64) #define PWM6HLD (*(unsigned char volatile xdata *)0xff65) #define PWM7T1 (*(unsigned int volatile xdata *)0xff70) #define PWM7T2 (*(unsigned int volatile xdata *)0xff72) #define PWM7CR (*(unsigned char volatile xdata *)0xff74) #define PWM7HLD (*(unsigned char volatile xdata *)0xff75) #define PWM_CONFIG_START P_SW2 = 0x80; #define PWM_CONFIG_END P_SW2 = 0x00; */ void pwm0_change(PWM0_PIN pin) { switch(pin){ case PWM0_20: PWM0CR = 0x00; //PWM0/P2.0 break; case PWM0_10: PWM0CR = 0x08; //PWM0_2/P1.0 break; case PWM0_60: PWM0CR = 0x10; //PWM0_3/P6.0 break; default: //assert(TRUE==FALSE); break; } } void pwm1_change(PWM0_PIN pin) { switch(pin){ case PWM1_21: PWM1CR = 0x00; //PWM1/P2.1 break; case PWM1_11: PWM1CR = 0x08; //PWM1_2/P1.1 break; case PWM1_61: PWM1CR = 0x10; //PWM1_3/P6.1 break; default: //assert(TRUE==FALSE); break; } } void pwm2_change(PWM0_PIN pin) { switch(pin){ case PWM2_22: PWM2CR = 0x00; //PWM2/P2.2 break; case PWM2_12: PWM2CR = 0x08; //PWM2_2/P1.2 break; case PWM2_62: PWM2CR = 0x10; //PWM2_3/P6.2 break; default: //assert(TRUE==FALSE); break; } } void pwm3_change(PWM0_PIN pin) { switch(pin){ case PWM3_23: PWM3CR = 0x00; //PWM3/P2.3 break; case PWM3_13: PWM3CR = 0x08; //PWM3_2/P1.3 break; case PWM3_63: PWM3CR = 0x10; //PWM3_3/P6.3 break; default: //assert(TRUE==FALSE); break; } } void pwm4_change(PWM0_PIN pin) { switch(pin){ case PWM4_24: PWM4CR = 0x00; //PWM4/P2.4 break; case PWM4_14: PWM4CR = 0x08; //PWM4_2/P1.4 break; case PWM4_64: PWM4CR = 0x10; //PWM4_3/P6.4 break; default: //assert(TRUE==FALSE); break; } } void pwm5_change(PWM0_PIN pin) { switch(pin){ case PWM5_25: PWM5CR = 0x00; //PWM5/P2.5 break; case PWM5_15: PWM5CR = 0x08; //PWM5_2/P1.5 break; case PWM5_65: PWM5CR = 0x10; //PWM5_3/P6.5 break; default: //assert(TRUE==FALSE); break; } } void pwm6_change(PWM0_PIN pin) { switch(pin){ case PWM6_26: PWM6CR = 0x00; //PWM6/P2.6 break; case PWM6_16: PWM6CR = 0x08; //PWM6_2/P1.6 break; case PWM6_66: PWM6CR = 0x10; //PWM6_3/P6.6 break; default: //assert(TRUE==FALSE); break; } } void pwm7_change(PWM0_PIN pin) { switch(pin){ case PWM7_27: PWM7CR = 0x00; //PWM7/P2.7 break; case PWM7_17: PWM7CR = 0x08; //PWM7_2/P1.7 break; case PWM7_67: PWM7CR = 0x10; //PWM7_3/P6.7 break; default: //assert(TRUE==FALSE); break; } } //n的范围0-15 void pwm_set_clk_div(U8 n) { if(n<0 || n>15) n = 15; PWMCKS = n + 1; } void pwm_set_val(U16 val_H,U16 val_L) { PWMC = val_H + val_L; } void L1_pwm0_init(void) //PWM0/P2.0 { P_SW2 |= 0x80; //千万用|哇,直接=0x80,会把串口4干掉 PWMCKS = 0x00; // PWM时钟为系统时钟/1 PWMC = 291; //设置PWM周期为18个PWM时钟 PWM0CR = 0x00 ; //并且选择为p2.0 PWM0T1 = 0; //0开始输出低电平 PWM0T2 = 146; //8开始输出高电平 P_SW2 &= ~(0x80); D_P20_OFF(); D_IO_normal_P2(0); } void L1_P20_init(void) //PWM0/P2.0 { P_SW2 |= 0x80; BITN_0(PWM0CR, BITN7);//设置关闭PWM0,并且释放p2.0 P_SW2 &= ~(0x80); D_P20_ON(); D_IO_HighOut_P2(0); } void L1_pwm1_init(void) //PWM0/P2.0 { P_SW2 |= 0x80; //千万用|哇,直接=0x80,会把串口4干掉 PWMCKS = 0x00; // PWM时钟为系统时钟/1 PWMC = 291; //设置PWM周期为18个PWM时钟 PWM1CR = 0x00 ; //并且选择为p2.0 ///PWM1CR = 0x00 | 0x80; 设置开启PWM0, PWM1T1 = 0; //0开始输出低电平 PWM1T2 = 146; //8开始输出高电平 P_SW2 &= ~(0x80); } /******************* void L1_pwm_on(U8 ch,U8 onf) { P_SW2 |= 0x80; //千万用|哇,直接=0x80,会把串口4干掉 PWMCKS = 0x00; // PWM时钟为系统时钟/1 PWMC = 291; //设置PWM周期为18个PWM时钟 P_SW2 &= ~(0x80); switch(ch) { case 0: $end$ break; } P_SW2 &= ~(0x80); PWMCR |= 0x80; //启动PWM模块 } **********/ void L1_pwm_on(void) { PWMCR |= 0x80; //启动PWM模块 } void L1_pwm_off(void) { PWMCR &= ~(0x80); //关闭PWM模块 }