//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`~~~~~~~~~~~~~~~~~~~~~~~~~~~
void initpwm( void ){
//````````````````````````````
CCP2M3 = 1; // ccp2 pwm 模式 RC1
CCP2M2 = 1;
CCP2M1 = 1;
CCP2M0 = 1;
CCPR2L = 0X80;
CCP2X = 0;
CCP2Y = 0;
//````````````````````````````
T2CKPS1 = 0; //16分频
T2CKPS0 = 0;
TOUTPS3 = 0;
TOUTPS2 = 0;
TOUTPS1 = 0;
TOUTPS0 = 0;
TMR2 = 0; //定时器值 0
TMR2ON = 1; //定时器开启
PR2 = 0XFE; //时基
}
//----------------------------------------------
//===========================================================================
void Delays( unsigned int s ){
unsigned int i;
for( i = s; i > 0 ; i-- ){
asm("nop");
asm("nop");
asm("nop");
asm("nop");
}
}
//===========================================================
void pwm_hl_add( unsigned int v,unsigned int ds){
unsigned int buffer_ds;
unsigned int buffer_pwm;
buffer_pwm = v ;
buffer_ds = ds;
while( buffer_pwm > CCPR2L ){
CCPR2L++;
Delays( buffer_ds );
}
}
//===========================================================
//===========================================================
void pwm_hl_uadd( unsigned int v,unsigned int ds){
unsigned int buffer_ds;
unsigned int buffer_pwm;
buffer_pwm = v;
buffer_ds = ds;
while( buffer_pwm < CCPR2L ){
CCPR2L--;
Delays( buffer_ds );
}
}
//===========================================================
void init(void){
CCPR1L=0X00;
CCPR1H=0X00;
INTCON=0X00;
PIR1=0X00;
// PIR2=0X00;
PIE1=0X00;
// PIE2=0X00;
}
//------------------------------------------------
void prot(void){
TRISC=0X04; //RC2 CPP捕捉方式输入
}
//------------------------------------------------
void int_TIME_INTCON(void){
T1CON=0X00; //0分频1:1 fsoc/4 // ccp1 捕捉 模式 RC2
CCP1IE=0B1; //CCP1 中断使能
PEIE=0B1;
GIE=0B1;
CCP1CON=0X05; //捕捉上升沿
TMR1ON=0B1;
}
//-----------------------------------------------
void delays(void){
#asm
MOVLW 0x02
MOVWF 70H
LP00 MOVLW 0Xff
MOVWF 71H
LP10 NOP
NOP
CLRWDT
DECFSZ 71H,F
GOTO LP10
DECFSZ 70H,F
GOTO LP00
#endasm
}
//---------------------------------------------------
void interrupt cpp1(void){ //中断服务函数
CCP1IF=0B0;
if(!CCP1M0){ //busy进入低电平 a/d转换已经完毕
flag = 1;
new_data_l = CCPR1L;
new_data_h = CCPR1H;
// CCP1IF=0B0;
CCP1M0=0B1; //上升沿有效
}
else{
TMR1L=0X00; //定时器开始计时 busy进入高电平
TMR1H=0X00;
// CCP1IF=0B0;
CCP1M0=0B0; //下降沿有效
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void main(void)
{
// unsigned char gsum=1;
unsigned int in_v,in_ds=1000;
init();
prot();
initpwm();
int_TIME_INTCON();
while(1){
if( flag == 1){
GIE = 0;
flag = 0;
in_v = new_data_h; //+ new_data_l; ------------> > > > > > > > > > > > > 取不到 new_data_h 值.
if( in_v > in_vref ){
in_vref = in_v;
pwm_hl_add( in_v,in_ds );
}
if( in_v < in_vref ){
in_vref = in_v;
pwm_hl_uadd( in_v,in_ds );
}
CCP1IF=0B0;
GIE = 1;
}
// asm("clrdwt");
}
}
new_data_h 在isis 仿真的时候一直取不到值.. pwm 输出脉宽一直就不变了.呵呵.希望高手帮忙看一下程序.指点一二.