คำตอบที่ 70
/*------------------------------------------------------------------------------------------------------------------------*/
// Program : LCD display 01
// Description : Show Message on LCD display
// Filename : l1001.c
// C compiler : RIDE 51 V6.1
/*------------------------------------------------------------------------------------------------------------------------*/
#include // Header file register for P89V51RD2BN
#include
#include
#include
#include
char m = 0;
char flag = 0;
unsigned int pluse_w = 0;
unsigned int pluse_w1= 0;
unsigned int cap1 = 0;
unsigned int cap2 = 0;
void PCA_ISR()interrupt 6 using 1
{
do
{
}
while(CCF0==0);
CCF0=0;
if(flag==0)
{
cap1=CCAP0L | (CCAP0H << 8);
CCAPM0=0x11;
flag=1;
}
do
{
}
while(CCF0==0);
CCF0=0;
if(flag==1)
{
cap2=CCAP0L | (CCAP0H << 8);
pluse_w=cap2-cap1;
pluse_w=pluse_w+126;
CCAPM0=0x21;
flag=0;
}
do
{
}
while(CCF2==0);
CCF2=0;
if(flag==0)
{
cap1=CCAP2L | (CCAP2H << 8);
CCAPM2=0x11;
flag=1;
}
do
{
}
while(CCF2==0);
CCF2=0;
if(flag==1)
{
cap2=CCAP2L | (CCAP2H << 8);
pluse_w1=cap2-cap1;
pluse_w1=pluse_w1+126;
m++;
if(m==30)
{
if(pluse_w > 256)
{
pluse_w = 0;
pluse_w = 0;
}
CCAP1L = pluse_w;
CCAP1H = pluse_w;
CCAP3L = pluse_w1;
CCAP3H = pluse_w1;
m=0;
}
CCAPM2=0x21;
flag=0;
}
}
/********************************************************************************************************/
/************************************* Main loop *******************************************************/
/********************************************************************************************************/
void main(void)
{
TMOD = 0x12;
TH1 = 0x98;
TL1 = 0xF7;
TH0 = 215; //185; // 256-240, 78.125usec Auto reload (20msec/PWM)
TL0 = TH0;
TI = 1;
TF0 = 0;
TF1 = 0;
TR0 = 1;
CMOD = 0x04; // Set CMOD PCA count pulse as (osc/12)
CCAPM0 = 0x21;
CCAPM1 = 0x42; // Set CCAP Module 2 as 8 bit PWM
CCAPM2 = 0x21; // Set CCAP Module 2 as 8 bit PWM
CCAPM3 = 0x42; // Set CCAP Module 3 as 8 bit PWM
CCAPM4 = 0x42; // Set CCAP Module 4 as 8 bit PWM
CCAP1L = 225;
CCAP1H = 225;
CCAP3L = 225; // Set CCAP3L reload at 50% duty cycle
CCAP3H = 225; // Set CCAP3L reload at 50% duty cycle
CCAP4L = 225; // Set CCAP4L reload at 80% duty cycle
CCAP4H = 225; // Set CCAP4L reload at 80% duty cycle
CCON = 0x40; // Set PCA counter running
flag=0;
IE=0xC0;
CR=1;
EA=1;
while(1)
{
}
} //end