#include "STC8G.h"
#include <intrins.h>
#include <string.h>
sbit TURN_UP = P1^1;
sbit TURN_DOWN = P1^0;
sbit TURN_LEFT = P1^6;
sbit TURN_RIGHT = P1^7;
sbit Flag_UP = P3^1; //上轉限位
sbit Flag_DOWN = P3^0; //下轉限位
sbit Flag_LEFT = P3^3; //左轉限位
sbit Flag_RIGHT = P3^4; //右轉限位
sbit led = P3^5; //照明燈
sbit led_R = P5^5; //測試
bit busy=0;
char wptr;
char rptr;
char buffer[16];
unsigned char k_recvie=0;//接受數據
unsigned int up=0,down=0,left=0,right=0;
void Delay1ms() //@12MHz
{
unsigned char i, j;
_nop_();
_nop_();
i = 12;
j = 168;
do
{
while (--j);
} while (--i);
}
void Delay100ms() //@12MHz
{
unsigned char i, j;
i = 195;
j = 138;
do
{
while (--j);
} while (--i);
}
//串口接收處理函數
void Uart2Isr() interrupt 8
{
if (S2CON & 0x02)
{
S2CON &= ~0x02;
busy = 0;
}
if (S2CON & 0x01)
{
S2CON &= ~0x01;
k_recvie= S2BUF;
if(k_recvie==0xA0)
{
up=1;
down=0;
left=0;
right=0;
}
if(k_recvie==0xA1)
{
down=1;
up=0;
left=0;
right=0;
}
if(k_recvie==0xA3)
{
left=1;
up=0;
down=0;
right=0;
}
if(k_recvie==0xA4)
{
right=1;
up=0;
down=0;
left=0;
}
if(k_recvie==0xF2)
{
up=0;
down=0;
left=0;
right=0;
TURN_UP=0;
TURN_DOWN=0;
TURN_LEFT=0;
TURN_RIGHT=0;
}
}
}
void UartInit(void) //9600bps@11.0592MHz
{
S2CON = 0x50; //8位數據,可變波特率
AUXR |= 0x04; //定時器時鐘1T模式
T2L = 0xE0; //設置定時初始值
T2H = 0xFE; //設置定時初始值
AUXR |= 0x10; //定時器2開始計時
busy = 0;
}
void Uart2Send(char dat)
{
while (busy);
busy = 1;
S2BUF = dat;
}
void Uart2SendStr(char *p)
{
while (*p)
{
Uart2Send(*p++);
}
}
void main()
{
P0M0 = 0xff; P0M1 = 0x00;
P3M0 = 0x20; P3M1 = 0x00;
P5M0 = 0x00; P5M1 = 0x00;
TURN_UP=0;
TURN_DOWN=0;
TURN_LEFT=0;
TURN_RIGHT=0;
UartInit();
IE2 = 0x01;
EA = 1;
led=1;
led_R=0;
Delay100ms();
Delay100ms();
Uart2SendStr("Uart Test !\r\n");
while(1)
{
if(Flag_UP==1&&up==1)
{
TURN_UP=1;
}
if(Flag_DOWN==1&&down==1)
{
TURN_DOWN=1;
}
if(Flag_LEFT==1&&left==1)
{
TURN_LEFT=1;
}
if(Flag_RIGHT==1&&right==1)
{
TURN_RIGHT=1;
}
Uart2Send(0x51);
}//while
}
|