#include <reg52.h>
#include<string.h>
#define DELAY_
tiME 60000 //Delay time
#define DELAY_TIME1 1
/**********************I/O 定义***********************/
sbit MS=P3^6; //GM8123 工作模式控制
sbit RESET=P3^7; //GM8123 复位引脚控制
sbit STADD0=P1^4; //GM8123 发送地址线0
sbit STADD1=P1^5; //GM8123 发送地址线1
sbit SRADD0=P1^6; //GM8123 接受地址线0
sbit SRADD1=P1^7; //GM8123 接受地址线1
typedef unsigned char uint8;/* defined for unsigned 8-bits integer variable 无符号8位整型变量 */
typedef signed char int8;/* defined for signed 8-bits integer variable 有符号8位整型变量 */
typedef unsigned short uint16;/* defined for unsigned 16-bits integer variable 无符号16位整型变量 */
typedef signed short int16;/* defined for signed 16-bits integer variable 有符号16位整型变量 */
typedef unsigned int uint32;/* defined for unsigned 32-bits integer variable 无符号32位整型变量 */
typedef signed int int32;/* defined for signed 32-bits integer variable 有符号32位整型变量 */
/******************数据变量定义*****************/
unsigned char ReceiveBuff0;
unsigned char ReceiveBuff1;
unsigned char ReceiveBuff2;
unsigned char ReceiveBuff3;
unsigned char SendBuff0;
unsigned char SendBuff1;
unsigned char SendBuff2;
unsigned char SendBuff3;
unsigned char Contr_data;
unsigned char j1=0 ,j2=0 ,j3=0 ,j4=0 ,j5=0 ,RXflag=0;
uint8 TXbuff[]="G";
uint8 UpData[5];
/*************函数功能: 延时程序******************/
/*void delay(unsigned int m)
{
unsigned int n;
n=0;
while(n < m)
{n++;}
return;
}
*/
void DelayUs2x(unsigned char t)
{
while(--t);
}
void DelayMs(unsigned char t)
{
while(t--)
{
//大致延时1mS
DelayUs2x(254);
DelayUs2x(254);
}
}
void GM8123_Init()
{
TMOD=0x20;//定时器1工作方式2
TH1=0xf4;
TL1=0xf4;//主机工作波特率为4800
PCON=0x80;
IE=0x90;
SCON=0xc0;//串口工作方式1
RESET=0;
DelayUs2x(5);//延时10us
RESET=1;
DelayMs(300);
//
通信帧和波特率的设置
TR1=1;//启动定时器1
// STADD1=0;//命令字地址
// STADD0=0;
P1=0x00;
DelayMs(4);
MS=0;//写命令字模式
DelayMs(4);
SBUF=0xf2;//子串口4800,木串口19200bps
while(TI==0);
TI=0;
DelayMs(4);
REN=1;
MS=1;
DelayMs(6);
while(ReceiveBuff0!=0xf2)//验证写入的命令字是否正确
{
ReceiveBuff0=0;
//STADD1=0;//命令字地址
// STADD0=0;
P1=0x00;
DelayMs(4);
MS=0;//写命令字模式
DelayMs(4);
SBUF=0xf2;//子串口4800,木串口19200bps
while(TI==0);
TI=0;
DelayMs(4);
REN=1;
MS=1;//读命令字
DelayMs(6);
}
REN=0;
MS=0;//8123工作在多通道模式下
TR1=0;
TH1=0xfd;
TL1=0xfd;//主控MCU波特率为19200bps
PCON=0x80;
SCON=0x50;//串口工作方式1:8位。允许接收
TR1=1;//启动定时器1
}
//向GM8123写数据
void GM8123_Write(uint8 *buff)
{
uint16 count=0;
MS=0;//多通道模式
STADD1=1;
STADD0=0;//通道2发送
while(buff[count]!=0x00)
{
SBUF=buff[count];
while(TI==0);
TI=0;
count++;
DelayMs(1);
}
}
//读GM8123数据
void GM8123_Read(uint8 RXflag,uint8 *buff,uint16 datasiz)
{
memset(buff,0,sizeof(buff));
if(RXflag==0x01)
{
while(ReceiveBuff1!=0x00)
{
buff[datasiz]=ReceiveBuff1;
datasiz++;
}
}
else if(RXflag==0x02)
{
while(ReceiveBuff2!=0x00)
{
buff[datasiz]=ReceiveBuff2;
datasiz++;
}
}
else if(RXflag==0x03)
{
while(ReceiveBuff3!=0x00)
{
buff[datasiz]=ReceiveBuff3;
datasiz++;
}
}
}
void CommReceive(void) interrupt 4
{
if(RI)
{
RI=0;
switch(P1&0xc0)//通过P1口的高两位来判断接收子通道是哪个
{
case 0x00: //返回命令字
ReceiveBuff0=SBUF;
break;
case 0x40://通道1接收到数据
RXflag=0x01;
ReceiveBuff1=SBUF;
//if(RXbuff1[RXcount1++]==' ')
//RXcount1=0;
break;
case 0x80://通道2接收到数据
RXflag=0x02;
ReceiveBuff2=SBUF;
//if(RXbuff2[RXcount1++]==' ')
//RXcount2=0;
break;
case 0xc0://通道3接收到数据
RXflag=0x03;
ReceiveBuff3=SBUF;
//if(RXbuff3[RXcount1++]==' ')
//RXcount3=0;
break;
}
}
}
void main()
{
GM8123_Init();
while(1)
{
GM8123_Write(TXbuff);
DelayMs(30);
GM8123_Read(0x02,UpData,0);
// if(UpData[0]==0x11)
// P0=0; 这是我写的测试有没有收到数的语句,但是在串口助手里发0x11,P0口没反应
}
}
求高手看看哪里错了,在串口调试助手里发送和接收没有任何反应
一周热门 更多>