#include "config.h"
u8 EIFlag=0;
u16 counter;
u16 countertatle;
u8 PWM16flag;
#define EIFUCTION 1
#define TIMERFUCTION 1
//u8 senddata[RECEIVEBUFLEN];
//u8 receivedata[RECEIVEBUFLEN];
//u8 i;
void PCA_USE_EI_TEST(void)
{
u8 senddata[RECEIVEBUFLEN];
u8 i;
for(i=0;i<RECEIVEBUFLEN;i++)
{
senddata[i]=i*8;
}
PCA_EI_Init();
while(1)
{
if(EIFlag)
{
SendDataBytes(senddata, RECEIVEBUFLEN);
EIFlag=0;
}
mdelay(10);
}
}
void PCA_USE_Timer_TEST(void)
{
//u8 senddata[RECEIVEBUFLEN];
u8 i=0;
//PCA_Timer_Init();
//for(i=0;i<RECEIVEBUFLEN;i++)
//{
// senddata[i]=i*8;
//}
//SendOneDataByte(0XA5);
//while(1)
{
PWMJudge();
//SendOneDataByte(CCAP0H);
//SendOneDataByte(CCAP0L);
//SendOneDataByte(counter>>8);
//SendOneDataByte(counter);
//mdelay(10);
//if(EIFlag>60)
{
//SendOneDataByte(CCAP0H);
//SendOneDataByte(CCAP0L);
//i++;
//SendOneDataByte(i);
//SendOneDataByte(CCAPM0);
//SendDataBytes(senddata, 4);
//EIFlag=0;
//CL=0;//清PCA
//CH=0;
//CCAP0H=0X00;
//CCAP0L=0X60;//预设CCAP0,计数时CH,CL=CCAP0时,中断
}
//if(EIFlag==2)
{
//SendOneDataByte(0x99);
//EIFlag=0;
}
WDT_CONTR=0X3F;
mdelay(100);
}
}
void PCA_USE_PWM_TEST(void)
{
u8 receivedata[RECEIVEBUFLEN];
u8 senddata[RECEIVEBUFLEN];
u8 i;
PCA_PWM_Init();
for(i=0;i<RECEIVEBUFLEN;i++)
{
senddata[i]=i*8;
}
while(1)
{
if(UART1Receive.ReceiveDataLen>3)
if(GetDataFromUART1Buffer(receivedata,UART1Receive.ReceiveDataLen))
{
if((receivedata[0]==0x5a)&&(receivedata[3]==0xa5))
{
i=receivedata[0]+receivedata[1];
//SendDataBytes(receivedata,4);
if(receivedata[2]==i)
{
CCAP0H=receivedata[1];
SendDataBytes(senddata, 4);
}
else
{
SendOneDataByte(receivedata[0]+receivedata[1]);
}
}
}
//mdelay(10);
}
}
//PCA用于外部中断初始化
void PCA_EI_Init(void)
{
CMOD=0X82;//PCA空闲不计数,用时钟源F/1,(0x80)为F/12
CCON=0x00;//因为用PCA做外部中断,因此这里不用定时器,标志位都清0
//应该是:用到PCA功能都要开启PCA定时器,虽然这里用于外部中断
//PWM用不着匹配/捕获(同一个CCFx),定时器要匹配,外部中断用捕获
//CCAPM0=0X11;//设置CCAPM0下降沿触发(捕获输入,看外围电路平时为高中,断为低),
//CCAPM0.0=1使能CCON.0(对应CCF0)中断,当外部触发时,硬件能使CCF0值1
//CCAPM0=p3.7 ,CCAPM1=P3.5, CCAPM2=P2.0, CCAPM3=P2.4
//CCAPMx配置为几个固定用法,可参考相关资料表格
CCAPM1=0x11;//pca1启动外部中断捕获
CL=0;
CH=0;//清除PCA计数器,外部中断不用
EA=1;//总中断开启
EPCA_LVD=1;//开启PCA_LVD中断,包括PCA定时器,PCA外部中断,和PWM
CR=1;//开启PCA时钟。ccon.6
}
void PCA_Timer_Init(void)
{
P3M0&=0x7f;
P3M1|=0x80;
CMOD=0X80;
CCON=0x00;
CCAPM0=0x49;
PWM16flag=0;
CL=0;//清PCA
CH=0;
countertatle=PRECOUNTERTOTLE;
counter=PRECOUNTER;
CCAP0H=(counter>>8);//预定时0x0080
CCAP0L=counter;//预设CCAP0,计数时CH,CL=CCAP0时,中断
//SendOneDataByte(CCAP0H);
//SendOneDataByte(CCAP0L);
EA=1;//总中断开启
EPCA_LVD=1;//开启PCA_LVD中断,包括PCA定时器,PCA外部中断,和PWM
CR=1;//开启PCA时钟。ccon.6
}
//
void PCA_PWM_Init(void)
{
CMOD=0X02;//空闲计数,时钟源为f/2
//CCON=0x00;//PWM用不着匹配/捕获(同一个CCFx),定时器要匹配,外部中断用捕获
CCAPM0=0x42;
CL=0;//清PCA
CH=0;
CCAP0H=0X7f;
CCAP0L=0X7f;//预设CCAP0,CL:0~CCAP0L,OUTPUT LOW; CL:CCAP0L~FF,OUTPUT HIGH;
//when CL=oxff,hard will do:CCAP0L=CCAP0H
//EA=1;//总中断开启
//EPCA_LVD=1;//开启PCA_LVD中断,包括PCA定时器,PCA外部中断,和PWM
CR=1;//开启PCA时钟。ccon.6
}
//void PCA_Interrupt()interrupt 6
void PCA_Interrupt()interrupt 6
{
///////////////////////////////////////////////////////////////////////////////////////
//PCA用于外部中断
///////////////////////////////////////////////////////////////////////////////////////
/*
if(CCF0)
{
EIFlag=1;
CCF0=0;
}
if(CCF1)
{
EIFlag=1;
CCF1=0;
}
*/
///////////////////////////////////////////////////////////////////////////////////////
//PCA用于定时器
///////////////////////////////////////////////////////////////////////////////////////
if(CCF0)
{
//EIFlag++;
//counter+=PRECOUNTER;
if(PWM16flag==0)
{
CEX0=0;
PWM16flag=1;
CCAP0H=(PRECOUNTERTOTLE>>8);//预定时0x0080
CCAP0L=PRECOUNTERTOTLE;//预设CCAP0,计数时CH,CL=CCAP0时,中断
CCAPM0=0x49;//不知道为什么,经过中断之后CCAPM0变成了0X09,这里特意定义为0X49
CCF0=0;
}
else if(PWM16flag==1)
{
CEX0=1;
PWM16flag=0;
CCAP0H=(counter>>8);//预定时0x0080
CCAP0L=counter;//预设CCAP0,计数时CH,CL=CCAP0时,中断
CL=0;//清PCA
CH=0;
CCAPM0=0x49;//不知道为什么,经过中断之后CCAPM0变成了0X09,这里特意定义为0X49
CCF0=0;
}
}
//if(CF)
//{
// EIFlag=2;
// CF=0;
//}
//if(CCF1)
//{
// EIFlag=1;
// counter+=PRECOUNTER;
// CCAP0H=(counter>>8);//预定时0x0080
// CCAP0L=counter;//预设CCAP0,计数时CH,CL=CCAP0时,中断
// CCF1=0;
//}
/**/
}
void PWMJudge(void)
{
u8 receivebuf[32]={0x11,0x55,0x2f,0x08,0x02,0x05,0x10,0x05,0x00,0x00,0x00};//={0x00, 0x55, 0xBA, 0x02, 0x02, 0x00, 0x00, 0x00};
u16 temp;
if(UART1Receive.ReceiveDataLen>=8)
{
GetDataFromUART1Buffer(receivebuf, 1);
if(receivebuf[0]==0xaa)
{
GetDataFromUART1Buffer(receivebuf+1, 7);
temp=CRC16(receivebuf,6);
if((receivebuf[6]==(u8)(temp>>8))&&(receivebuf[7]==(u8)(temp)))
{
CCAPM0&=(~0x40);
counter=((u16)(receivebuf[1])<<8)+(u16)receivebuf[2];
SendDataBytes(receivebuf,10);
mdelay(4);
CCAPM0|=0x40;
}
}
}
}
评论1