#if 1
/*---------------------------------------------------------------
文件名称:uart.c
文件标识:
摘 要:
当前版本: 2.0
作 者: shangeyao
修改日期: 2008年10月07日
芯片类型:SP3232
获取数据的格式为:CC 33 OID LEN DATA SUM C3 3C
OID ,LEN,SUM 为一字节,DATA为多个字节
音箱地址码:
编码
0 --------------- 0X9696
1 ----------------0X5656
2 ----------------0X6A6A
3 ----------------0X5959
4 ----------------0X9A9A
5 ----------------0X9595
6 ----------------0XA9A9
7 ----------------0XA6A6
8 ----------------0X6565
9 ----------------0X5A5A
//---------------------------------------------------------------*/
#include "uart.h"
#include "key.h"
#include "IIC.H"
/*--------0-10的编码-------------------------*/
code uchar NUMCODE[10][2]={
{0X96,0X96}, //0
{0X56,0X56}, // 1
{0X6A,0X6A}, // 2
{0X59,0X59}, // 3
{0x9A,0X9A}, // 4
{0X95,0X95}, // 5
{0XA9,0XA9}, // 6
{0XA6,0XA6}, // 7
{0X65,0X65}, // 8
{0X5A,0X5A}, // 9
};
#define Fosc 11059200
//地址码为BCD 码
extern uint addrvalue;
extern idata uchar sendcodeflag,GetACK;
extern idata uchar priority;
extern idata uchar Current_Menu;
extern idata uint tempaddrvalue;
extern idata uint tempprotectcode;
extern idata uchar currentch;
/************************************************************
* Name: SetBaud
* Func:
* 设置波特率
* Para:
* void
* Return: None
***********************************************************/
#if 0
void SetBaud(uint BaudRate )
{
uint OveRate;//溢出率
uint TimerValue;
if((PCON&0x80)!=0) //PCON |= 0x80;
{
OveRate=BaudRate/2;//没有在这里乘以32主要是防止数据过大
}
else
{
OveRate=BaudRate;
}
TimerValue=256-(Fosc/OveRate/32/12); //最好不要大于9600
TH1=TimerValue;
TL1=TimerValue;
}
#endif
/************************************************************
* Name: UartInit
* Func:
* 串行通信初始化
* Para:
* uchar ch: 发送的字符
* Return: None
***********************************************************/
void UartInit(void)
{
#if 0
SCON = 0x50; // SCON: serail mode 1, 8-bit UART recieve enable
//SCON = 0x90; // SCON: serail mode 2, 9-bit UART
TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit reload
PCON |= 0x80; // SMOD="1"; 波特率加倍
TMOD|=0x20; //定时器1工作在方式2
TH1 = 0xFA; // Baud:9600 fosc="11".0592MHz
TL1 = 0xFA;
IE |= 0x90; // 使能串行中断
/*----------------------*/
SetBaud(4800);
//SetBaud(19200);
ES=1; // 使能串行中断
TR1 = 1; // 启动TIMER1
EA = 1;
TI=0;
RI=0;
#else
SCON = 0x90; // SCON: serail mode 2, 9-bit UART
ES=1; // 使能串行中断
IE |= 0x90; // 使能串行中断
TR1 = 1; // 启动TIMER1
TI=0;
RI=0;
#endif
//---------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------
//UartSendChar(0x47);
}
/*----PC控制初始化--------*/
void PCControlInit(void)
{
EX1=1;/*-----使能PC中断,即外部中断1------*/
IP=0X10;
}
//
/************************************************************
* Name: UartSendChar
* Func:
* 向串口发送一个字符
* Para:
* uchar ch: 发送的字符
* Return: None
***********************************************************/
void PutChar(char ch)//unsigned
{
SBUF = ch;
while(TI==0);
TI=0;
}
/*----IO 模拟串口----*/
#define UART4800_Deyv 16
#define deyv UART4800_Deyv
sbit TX=P0^7;
extern void delay(unsigned int delay_Time) ;
void PutCharByIO(unsigned char BYTE)
{
int dey=deyv,i;
TX=0;
while(dey--);
for(i=0;i<8;i++){
if((BYTE>>i)&0x01)
TX=1;
else
TX=0;
dey=deyv;
while(dey--);
}
TX=1;
dey=deyv;
while(dey--);
delay(5);
}
#define UART4800_TOPC 18
//CC 33 B0 08 5A 5A 96 96 96 96 56 56 03 B8 C3 3C PC格式
void PutCharToPc(unsigned char BYTE)
{
int dey=UART4800_TOPC,i;
TX=0;
while(dey--);
for(i=0;i<8;i++){
if((BYTE>>i)&0x01)
TX=1;
else
TX=0;
dey=UART4800_TOPC;
while(dey--);
}
TX=1;
dey=UART4800_TOPC;
while(dey--);
delay(5);
}
/************************************************************
* Name: UartRev
* Func:
* 串口中断服务程序
* Para:
* None
* Return: None
***********************************************************/
uchar uartgetflag=0;
uchar OID=0,templen=0;
uchar *p;
/*-----------UART 数据接收函数-----------------------------------------------*/
uchar index=0;
extern uchar uartgetdata;
idata uchar UARTData[16];
void UartRev() interrupt 4 using 2 //接收中断不能定义太长的数组数据比如string[]"recive 0k !",栈溢出???
{
uchar ch,i=0;
uint sum=0,GetSum=0;
EA=0;
ch = SBUF;
if(RI)
{
RI = 0;
/*
if(ch==0XCC)
{
OPENMACHINE;
LedDataShowAddr(ch);
}
EA=1;
return ;
*/
if(ch==0xB5)
{
idata uchar temp;
PutChar(0xb5);
temp=(tempprotectcode>>12);
PutChar(temp&0x0f);
temp=(tempprotectcode>>8);
PutChar(temp&0x0f);
temp=(tempprotectcode>>4);
PutChar(temp&0x0f);
temp=(tempprotectcode>>0);
PutChar(temp&0x0f);
PutChar(0xc0);
//testflag=1;
}
if(ch==0xb9)
{
GetACK=CHAGECHACK;
}
if(ch==0xBA)
{
GetACK=SPEAKINGACK;
}
if(uartgetflag==0)
{
if(ch==0xb1||ch==0xb2)
{
OID=ch;
PutChar(0XD0);
uartgetflag=1;
}
else if(ch==0xb3||ch==0xb4)
{
OID=ch;
PutChar(0XD0);
uartgetflag=1;
//地址码100 接收到数据为B1AA
}
else if(ch==0xBF)
{
/*----电话控制结束挂机首字节-------*/
uartgetflag=1;
}
}
else if(uartgetflag==1)
{
if(ch==0xc0)//&&conflag==0x5)
{
idata int temp;
index=0X00;
PutChar(0XDD);
if(OID==0XB1)
{
sendcodeflag=ALLOPENCMD;
}
else if(OID==0XB2)
{
sendcodeflag=ALLCLOSECMD;
}
else if(OID==0XB3)
{
sendcodeflag=ONEOPENCMD;
temp=UARTData[0]>>4&0x0f;
if(temp==0x0B)
tempaddrvalue=0;
temp=UARTData[0]&0x0f;
if(temp==0x0a)
temp=0;
tempaddrvalue=temp<<8;
temp=UARTData[1]>>4&0x0f;
if(temp==0x0a)
temp=0;
tempaddrvalue|=temp<<4;
temp=UARTData[1]&0x0f;
if(temp==0x0a)
temp=0;
tempaddrvalue|=temp<<0;
}
else if(OID==0XB4)
{
sendcodeflag=ONECLOSECMD;
temp=UARTData[0]>>4&0x0f;
if(temp==0x0B)
tempaddrvalue=0;
temp=UARTData[0]>>0&0x0f;
if(temp==0x0a)
temp=0;
tempaddrvalue=temp<<8;
temp=UARTData[1]>>4&0x0f;
if(temp==0x0a)
temp=0;
tempaddrvalue|=temp<<4;
temp=UARTData[1]>>0&0x0f;
if(temp==0x0a)
temp=0;
tempaddrvalue|=temp<<0;
}
OID=0x00;
uartgetflag=0;
}
else if(ch==0x0c)
{/*----电话控制结束挂机j结束位-------*/
priority=KEYPRO;
uartgetflag=0;
/*-------*/
}
else if(ch==0xcc)
{
priority=CALLING; /*当前电话控制*/
uartgetflag=0;
}
else if(OID==0xb3||OID==0xb4)
{
/*获取地址码*/
if(index>6)
index=6;
UARTData[index++]=ch;
PutChar(0XD0);
}
}
}
EA=1;
}
#define UartRevBaud4800 41
sbit RX=P3^3;
//idata uchar baud=UartRevBaud4800;
//idata uchar baud=UartRevBaud4800;
idata uchar intcount,RXData=0;
idata uint sum=0,GetSum=0;
void test1(uchar byte);
void Int