#include "STC15F2K60S2.H"
|
|
void CCP_PWMInit(void) //³õʼ»¯PCA£¨Programmable Counter Array£©Ä£¿é×÷ΪPWMÊä³ö
|
{
|
CCON = 0; //PCA¿ØÖƼĴæÆ÷³õʼ»¯(xx-- -xxx£¬0000 0000£º¶¼ÊÇÇåÁã¾ÍÍêÊÂÁË)
|
CMOD = 0x02; //PCA¹¤×÷ģʽ¼Ä´æÆ÷(x--- xxxx£¬0000 0010£ºÏµÍ³Ê±ÖÓ£¬SYSclk/2)
|
CL = 0; //PCA¼ÆÊýÆ÷µÍ8λÇåÁã
|
CH = 0; //PCA¼ÆÊýÆ÷¸ß8λÇåÁã
|
|
AUXR1 = 0x00; //¸¨Öú¼Ä´æÆ÷1ÇåÁ㣬³õʼ»¯
|
PCA_PWM0 = 0x00; //PCAµÄPWM¼Ä´æÆ÷(xx-- --xx£¬0000 0000£º¹¤×÷ÓÚ8 bit PWM)
|
CCAP0H = 0x80; //±È½Ï/²¶»ñ¼Ä´æÆ÷0¸ß8λÉèÖÃΪ 0x80£¬±íʾPWM1µÄÖØ×°ÔØÖµµÄ¸ß8λ
|
CCAP0L = 0x80; //±È½Ï/²¶»ñ¼Ä´æÆ÷0µÍ8λÉèÖÃΪ 0x80£¬±íʾPWM1µÄÖØ×°ÔØÖµµÄµÍ8λ
|
CCAPM0 = 0x42; //±È½Ï/²¶»ñ¹¤×÷ģʽ¼Ä´æÆ÷0(-xxx xxxx£¬0100 0010£º±È½ÏÆ÷´ò¿ª£¬CCP0½ÅÓÃ×÷PWMÊä³ö)
|
|
AUXR1 = 0x00;
|
PCA_PWM1 = 0x00;
|
CCAP1H = 0x80; //256-x/256 = pulse width
|
CCAP1L = 0x80;
|
CCAPM1 = 0x42; //±È½ÏÆ÷´ò¿ª£¬CCP1½ÅÓÃ×÷PWMÊä³ö
|
CR = 1; //Æô¶¯PCA¼ÆÊýÆ÷
|
}
|
|
void PWMInit(void)
|
{
|
//¸ÃоƬÔöÇ¿ÐÍ PWM Êä³ö¶Ë¿Ú£º1.6 1.7 2.1 2.2 2.3 3.7
|
P1M0 &= ~0xc0; //½« 0011 1111 Óë P1M0 ½øÐÐλÓëÔËË㣬ÇåÁã P1M0 µÄ¸ß2룬½áºÏP1M1£¬P1.6 P1.7Òý½ÅÅäÖøü¸ÄΪ׼˫Ïò¿Ú£¬ÆäËû½Å¿ªÂ©
|
//P1M0 &= 0x3f
|
P1M1 &= ~0xc0; //P1M1_P1M0״̬(ͬһλÖÃÁ½¸ö¼Ä´æÆ÷µÄÊýÖµ×éºÏ)£º00£º×¼Ë«Ïò£»01£ºÍÆÍ죻10£º¸ß×裻11£º¿ªÂ©
|
P1 &= ~0xc0; //resetP1.6/.P1.7
|
P2M0 &= ~0x0e; //1111 0001
|
P2M1 &= ~0x0e;
|
P2 &= ~0x0e;
|
P3M0 &= ~0x80; //0111 1111
|
P3M1 &= ~0x80;
|
P3 &= ~0x80;
|
|
P_SW2 |= 0x80; //ʹÄÜ·ÃÎÊXSFR
|
PWMCFG = 0x3f; //ÅäÖÃPWMµÄ³õʼµçƽΪ high
|
PWMCKS = 0x00; //Ñ¡ÔñPWMµÄʱÖÓFosc/(0+1) 22118400/1024=21.6k
|
//PWMCKS = 0x07; //Ñ¡ÔñPWMµÄʱÖÓFosc/(8) 22118400/1000/8=2.76k
|
PWMC = CYCLE; //ÉèÖÃPWMµÄÖÜÆÚ
|
|
//-----PWM2--------------------------------
|
PWM2T1 = 0x0001; //ÉèÖÃPWMµÚ1´Î·×ªµÄPWM¼ÆÊý
|
PWM2T2 = CYCLE * 2L / 100; //ÉèÖÃPWMµÚ2´Î·×ªµÄPWM¼ÆÊý
|
//Õ¼¿Õ±È(PWM2T2-PWM2T1)/PWMC
|
PWM2CR = 0x00; //Ñ¡ÔñPWM2Êä³öµ½P3.7,²»Ê¹ÄÜPWM2ÖжÏ
|
//-------PWM3-----------------------------
|
PWM3T1 = 0x0001; //ÉèÖÃPWMµÚ1´Î·×ªµÄPWM¼ÆÊý
|
PWM3T2 = CYCLE * 2L / 100; //ÉèÖÃPWMµÚ2´Î·×ªµÄPWM¼ÆÊý
|
//Õ¼¿Õ±È(PWM2T2-PWM2T1)/PWMC
|
PWM3CR = 0x00; //Ñ¡ÔñPWM2Êä³öµ½P2.1,²»Ê¹ÄÜPWM2ÖжÏ
|
//--------pwm4----------------------------------
|
PWM4T1 = 0x0001; //ÉèÖÃPWMµÚ1´Î·×ªµÄPWM¼ÆÊý
|
PWM4T2 = CYCLE * 2L / 100; //ÉèÖÃPWMµÚ2´Î·×ªµÄPWM¼ÆÊý
|
//Õ¼¿Õ±È(PWM2T2-PWM2T1)/PWMC
|
PWM4CR = 0x00; //Ñ¡ÔñPWM2Êä³öµ½P2.2,²»Ê¹ÄÜPWM2ÖжÏ
|
//--------pwm5----------------------------------
|
PWM5T1 = 0x0001; //ÉèÖÃPWMµÚ1´Î·×ªµÄPWM¼ÆÊý
|
PWM5T2 = CYCLE * 2L / 100; //ÉèÖÃPWMµÚ2´Î·×ªµÄPWM¼ÆÊý
|
//Õ¼¿Õ±È(PWM2T2-PWM2T1)/PWMC
|
PWM5CR = 0x00; //Ñ¡ÔñPWM2Êä³öµ½P2.3,²»Ê¹ÄÜPWM2ÖжÏ
|
//--------pwm6----------------------------------
|
PWM6T1 = 0x0001; //ÉèÖÃPWMµÚ1´Î·×ªµÄPWM¼ÆÊý
|
PWM6T2 = CYCLE * 2L / 100; //ÉèÖÃPWMµÚ2´Î·×ªµÄPWM¼ÆÊý
|
//Õ¼¿Õ±È(PWM2T2-PWM2T1)/PWMC
|
PWM6CR = 0x00; //Ñ¡ÔñPWM2Êä³öµ½P1.6,²»Ê¹ÄÜPWM2ÖжÏ
|
|
//--------pwm7----------------------------------
|
PWM7T1 = 0x0001; //ÉèÖÃPWMµÚ1´Î·×ªµÄPWM¼ÆÊý
|
PWM7T2 = CYCLE * 2L / 100; //ÉèÖÃPWMµÚ2´Î·×ªµÄPWM¼ÆÊý
|
//Õ¼¿Õ±È(PWM2T2-PWM2T1)/PWMC
|
PWM7CR = 0x00; //Ñ¡ÔñPWM2Êä³öµ½P1.7,²»Ê¹ÄÜPWM2ÖжÏ
|
|
PWMCR = 0x3f; //ʹÄÜPWM2.3.4.5.6.7,6·ÐźÅÊä³ö
|
PWMCR |= 0x80; //ʹÄÜPWMÄ£¿é
|
//PWMCR |= 0x40; //ʹÄÜPWM¹éÁãÖжÏ
|
P_SW2 &= ~0x80;
|
}
|
//----------pwm2/3/4/5-------------------------------------------------------------------
|
void pwm2( unsigned short Wide) //PWM2
|
{
|
if((Wide==0)||(Wide==1))
|
{
|
P_SW2 |= 0x80; //ÉèÖø¨Öú¼Ä´æÆ÷2µÄλ7Ϊ1£¬½«PWM2Òý½ÅÇл»µ½P1.3
|
PWMCR &=~0x01; //PWM¿ØÖƼĴæÆ÷µÄλ0ÇåÁ㣬¼´½ûÓÃPWM2
|
PWM2=1; //PWM¿ØÖƼĴæÆ÷µÄλ0ÇåÁ㣬¼´½ûÓÃPWM2
|
P_SW2 &=~0x80; //Çå³ý¸¨Öú¼Ä´æÆ÷2µÄλ7£¬½«PWM2Òý½ÅÇл»»ØÔÀ´µÄλÖÃ
|
}
|
else if (Wide==CYCLE)
|
{
|
PWMCR &=~0x01; //PWM¿ØÖƼĴæÆ÷µÄλ0ÇåÁ㣬¼´½ûÓÃPWM2
|
PWM2=0; //PWM2Òý½ÅÖÃΪµÍµçƽ
|
}
|
else
|
{
|
P_SW2 |= 0x80; //ÉèÖø¨Öú¼Ä´æÆ÷2µÄλ7Ϊ1£¬½«PWM2Òý½ÅÇл»µ½P1.3
|
PWM2T1 = 0x01; //PWM2¶¨Ê±Æ÷1¼Ä´æÆ÷µÄÖµÉèÖÃΪ0x01
|
PWM2T2 = Wide; //PWM2¶¨Ê±Æ÷2¼Ä´æÆ÷µÄÖµÉèÖÃΪ Wide
|
PWMCR |= 0x01; //PWM¿ØÖƼĴæÆ÷µÄλ0ÖÃΪ1£¬¼´ÆôÓÃPWM2
|
P_SW2 &=~0x80; //Çå³ý¸¨Öú¼Ä´æÆ÷2µÄλ7£¬½«PWM2Òý½ÅÇл»»ØÔÀ´µÄλÖÃ
|
|
}
|
|
}
|
void pwm3(unsigned short Wide) //PWM3
|
{
|
if((Wide==0)||(Wide==1))
|
{
|
P_SW2 |= 0x80;
|
PWMCR &=~0x02;
|
PWM3=1;
|
P_SW2 &=~0x80;
|
|
}
|
else if(Wide==CYCLE)
|
{
|
PWMCR &=~0x02;
|
PWM3=0;
|
}
|
else
|
{
|
P_SW2 |= 0x80;
|
PWMCFG = 0x3f; //ÅäÖÃPWMµÄ³õʼµçƽΪ high
|
PWM3T1 =0x01 ;
|
PWM3T2 =Wide;
|
P_SW2 &=~0x80;
|
PWMCR |= 0x02;
|
|
}
|
}
|
void pwm4(unsigned short Wide) //PWM4
|
{
|
if((Wide==0)||(Wide==1))
|
{
|
P_SW2 |= 0x80;
|
PWMCR &=~0x04;
|
PWM4=1;
|
P_SW2 &=~0x80;
|
}
|
else if (Wide==CYCLE)
|
{
|
PWMCR &=~0x04;
|
PWM4=0;
|
}
|
else
|
{
|
P_SW2 |= 0x80;
|
PWM4T1 = 0x01;
|
PWM4T2 = Wide;
|
P_SW2 &=~0x80;
|
PWMCR |= 0x04;
|
|
}
|
}
|
void pwm5(unsigned short Wide) //PWM5
|
{
|
if((Wide==0)||(Wide==1))
|
{
|
P_SW2 |= 0x80;
|
PWMCR &=~0x08;
|
PWM5=1;
|
P_SW2 &=~0x80;
|
|
}
|
else if (Wide==CYCLE)
|
{
|
PWMCR &=~0x08;
|
PWM5=0;
|
}
|
else
|
{
|
P_SW2 |= 0x80;
|
PWM5T1 = 0x01;
|
PWM5T2 = Wide;
|
P_SW2 &=~0x80;
|
PWMCR |= 0x08;
|
|
}
|
}
|