/**********************************************
* File: Serial_Comm_1.C
* Description: Serial Communication Example 1
* Created Date: 2007-10-01
* Last Modified: 2007-10-01
* Author: Jeffrey -Schicksal@126.com
* Notes: None
**********************************************/
#include “Atmel/AT89X51.h”
* Description: Serial Communication Example 1
* Created Date: 2007-10-01
* Last Modified: 2007-10-01
* Author: Jeffrey -Schicksal@126.com
* Notes: None
**********************************************/
#include “Atmel/AT89X51.h”
unsigned char Read_Keyboard();
void RS232_SendData(unsigned char Sdata);
void RS232_SendData(unsigned char Sdata);
/**********************************************
* Function: delay(unsigned int t)
* Input Variables: t
* Return Variables: None
* Usage: Common Delay Routine, t as the delay time ticks
**********************************************/
void delay(unsigned int t)
{
for(;t>0;t–);// 延时循环
}
* Function: delay(unsigned int t)
* Input Variables: t
* Return Variables: None
* Usage: Common Delay Routine, t as the delay time ticks
**********************************************/
void delay(unsigned int t)
{
for(;t>0;t–);// 延时循环
}
/**********************************************
* Function: RS232_Init(void)
* Input Variables: none
* Return Variables: None
* Usage: Init UART
**********************************************/
void RS232_Init()
{
SCON = 0x50;// UART作业于形式1, REN=1
TMOD = 0x20 ;// Timer1 工业于形式2
PCON |= 0x80;// SMOD1 = 1
TH1= 0xF3;// 波特率9600 Bds at 24MHz
TL1= 0xF3;// 波特率9600 Bds at 24MHz
TR1 = 1; // 发动Timer1
}
* Function: RS232_Init(void)
* Input Variables: none
* Return Variables: None
* Usage: Init UART
**********************************************/
void RS232_Init()
{
SCON = 0x50;// UART作业于形式1, REN=1
TMOD = 0x20 ;// Timer1 工业于形式2
PCON |= 0x80;// SMOD1 = 1
TH1= 0xF3;// 波特率9600 Bds at 24MHz
TL1= 0xF3;// 波特率9600 Bds at 24MHz
TR1 = 1; // 发动Timer1
}
/**********************************************
* Function: main()
* Input Variables: None
* Return Variables: None
* Usage: Program Entry
*********************************************/
void main()
{
unsigned char Keyboard_Status = 0xff;// 界说键值
RS232_Init();
while(1)
{
Keyboard_Status = Read_Keyboard();// 读取按键值
//********************主程序部分*********************//
switch(Keyboard_Status)
{
case (~0x01):
{
RS232_SendData(A);
break;
}
case (~0x02):
{
RS232_SendData(B);
break;
}
case (~0x04):
{
RS232_SendData(C);
break;
}
case (~0x08):
{
RS232_SendData(D);
break;
}
case (~0x10):
{
RS232_SendData(E);
break;
}
case (~0x20):
{
RS232_SendData(F);
break;
}
case (~0x40):
{
RS232_SendData(G);
break;
}
case (~0x80):
{
RS232_SendData(H);
break;
}
default:
//RS232_SendData(X);
break;
}
//********************主程序部分*********************//
}
}
* Function: main()
* Input Variables: None
* Return Variables: None
* Usage: Program Entry
*********************************************/
void main()
{
unsigned char Keyboard_Status = 0xff;// 界说键值
RS232_Init();
while(1)
{
Keyboard_Status = Read_Keyboard();// 读取按键值
//********************主程序部分*********************//
switch(Keyboard_Status)
{
case (~0x01):
{
RS232_SendData(A);
break;
}
case (~0x02):
{
RS232_SendData(B);
break;
}
case (~0x04):
{
RS232_SendData(C);
break;
}
case (~0x08):
{
RS232_SendData(D);
break;
}
case (~0x10):
{
RS232_SendData(E);
break;
}
case (~0x20):
{
RS232_SendData(F);
break;
}
case (~0x40):
{
RS232_SendData(G);
break;
}
case (~0x80):
{
RS232_SendData(H);
break;
}
default:
//RS232_SendData(X);
break;
}
//********************主程序部分*********************//
}
}
/**********************************************
* Function: RS232_SendData(unsigned char Sdata)
* Input Variables: unsigned char Sdata
* Return Variables: none
* Usage: Send a Char to UART
*********************************************/
void RS232_SendData(unsigned char Sdata)
{
SBUF = Sdata;// 发送数据
while(TI==0);// 等候发送完毕
TI = 0; // 铲除发送完毕标志
}
* Function: RS232_SendData(unsigned char Sdata)
* Input Variables: unsigned char Sdata
* Return Variables: none
* Usage: Send a Char to UART
*********************************************/
void RS232_SendData(unsigned char Sdata)
{
SBUF = Sdata;// 发送数据
while(TI==0);// 等候发送完毕
TI = 0; // 铲除发送完毕标志
}
/**********************************************
* Function: Read_Keyboard
* Input Variables: None
* Return Variables: Keys Value
* Usage: Read Keyboard Status
*********************************************/
unsigned char Read_Keyboard()
{
unsigned char Keys_Value=0;// 界说键值变量
unsigned char Keys_Temp;
P2=0xff;
Keys_Value=P2;
* Function: Read_Keyboard
* Input Variables: None
* Return Variables: Keys Value
* Usage: Read Keyboard Status
*********************************************/
unsigned char Read_Keyboard()
{
unsigned char Keys_Value=0;// 界说键值变量
unsigned char Keys_Temp;
P2=0xff;
Keys_Value=P2;
do
{
P2 = 0xff;// P2口置高,预备读取按键状况
Keys_Temp = P2;// 读取按键状况
delay(150);// 消抖延时
P2 = 0xff;// P2口置高,预备读取按键状况
Keys_Value = P2;// 读取按键状况
}
while(Keys_Value != Keys_Temp);// 假如读取状况不一致,则重复读取键值
while(P2!=0xff);// 直到按键开释
return Keys_Value;// 回来按键状况值
}