|
#include "STC12c5A.h"
#define Left 0 //o궨òå×ó×a
#define Right 1 //o궨òåóò×a
sbit ControlPort = P2^0; //¶æ»úDÅoŶ˿ú
unsigned char TimeOutCounter = 0,LeftOrRight = 0;
//TimeOutCounter:¶¨ê±Æ÷òç3ö¼Æêy
// LeftOrRight: ×ó×aóò×a±êÖ¾
void InitialTimer ( void )
{
TMOD=0x10; //¶¨ê±¼ÆêyÆ÷11¤×÷óú·½ê½1
TH1 = ( 65535 - 500*12 ) / 256; //0.25ms
TL1 = ( 65535 - 500*12 ) % 256;
EA=1; //¿a×üÖD¶Ï
ET1=1; //ÔêDí¶¨ê±¼ÆêyÆ÷1ÖD¶Ï
TR1=1; //Æô¶ˉ¶¨ê±¼ÆêyÆ÷1ÖD¶Ï
}
void delay_ms(unsigned int _countMS)
{
unsigned int i;
unsigned char j;
for(i=_countMS*12;i>0;i--)
for(j=120;j>0;j--);
}
void ControlLeftOrRight ( void ) //¿ØÖƶæ»úoˉêy
{
LeftOrRight = Left;
delay_ms(2000);
LeftOrRight = Right;
delay_ms(2000);
}
void main ( void ) //Ö÷oˉêy
{
InitialTimer();
for(;;)
{
ControlLeftOrRight();
}
}
void Timer1 ( void ) interrupt 3 //¶¨ê±Æ÷ÖD¶Ïoˉêy
{
TH1 = ( 65535 - 500*12 ) / 256;
TL1 = ( 65535 - 500*12 ) % 256;
TimeOutCounter ++;
switch ( LeftOrRight )
{
case 0 : //Îa0ê±£¬¶æ»ú×ó×a£¬1ms
{
if( TimeOutCounter <= 2 )
{
ControlPort = 1;
}
else
{
ControlPort = 0;
}
break;
}
case 1 : //Îa1ê±£¬¶æ»úóò×a2ms
{
if( TimeOutCounter <= 10 )
{
ControlPort = 1;
}
else
{
ControlPort = 0;
}
break;
}
default : break;
}
if( TimeOutCounter == 80 ) //20ms,2»òaDT¸Ä
{
TimeOutCounter = 0;
}
}
|
|