/* * Software PWM by abcdabcd987 2011-1-2 * There is something to write into your header:extern unsigned int _T;extern const char _maxpwm;extern char _pwm[_tol];extern char _sta[_tol];extern void PWM_Init(const char, const char);extern void PWM_interrupt();extern void PWM_SetPWM(const char, const char);extern void PWM_Stop(); * Use PWM_init(t2con, pr2) to start the software PWM. * Use PWM_SetPWM(index, value) to set the value of each PWM channel. * Add a usage of PWM_interrupt() to your interrupt. * You can modify _tol, _prot, _tris(header.h), _maxpwm. Others DO NOT modify. */#include "header.h"constchar_maxpwm=10;constchar_out[8]={0x1,0x2,0x4,0x8,0x10,0x20,0x40,0x80};constchar_trisseting[8]={0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x0};unsignedint_T;char_pwm[_tol];char_sta[_tol];voidPWM_Init(constchar,constchar);voidPWM_SetPWM(constchar,constchar);voidPWM_interrupt();voidPWM_Stop();voidPWM_Init(constchart2con,constcharpr2){for(char_i=0;_i<_tol;++_i)_sta[_i]=0;_tris&=_trisseting[_tol-1];_port=0;T2CON=t2con;PR2=pr2;TMR2IE=1;PEIE=1;GIE=1;_T=0;}void/*interrupt */PWM_interrupt(){if(TMR2IF){TMR2IF=0;char_pt=_port;for(chari=0;i<_tol;++i){_sta[i]=_sta[i]+1;if(_sta[i]>=_maxpwm){_pt=_pt|_out[i];_sta[i]=0;}if(_sta[i]==_pwm[i]&&_pwm[i]!=_maxpwm){_pt=_pt&(~_out[i]);}}if(_sta[0]==0)if(_T==50000)_T=0;else_T=_T+1;_port=_pt;}}voidPWM_SetPWM(constcharindex,constcharvalue){_pwm[index]=value;}voidPWM_Stop(){T2CON=0;TMR2IE=0;}