串口通信和定时器0同时工作时.串口不能发送数据。 5
voidinit(){TMOD|=0x21;//定时器0工作方式1,TH1是重装值,TL1是初值定时器1工作方式2SCON=0x50;//在11.0592MHz下,设置串...
void init()
{
TMOD |= 0x21;//定时器0工作方式1,TH1是重装值,TL1是初值 定时器1工作方式2
SCON = 0x50;//在11.0592MHz下,设置串行口波特率为9600,方式1,并允许接收
PCON=0x00;
TH1=0xFD; //重载值
TL1=0xFD; //初始化值
TH0 =(65535-50000)/256;
TL0 =(65535-50000)%256;
TR0 = 1;
ET0 = 1;
TR1 = 1;
ET1 = 0;
ES = 1; //打开串口中断
EA = 1; //开总中断
}
void uart() interrupt 4 using 3//串口中断
{
if(RI)
{
RI = 0;
//add your code here!
}
else
TI = 0;
}
void Timer0(void) interrupt 1 using 1 //定时器0中断
{
TH0 =(65535-50000)/256;
TL0 =(65535-50000)%256;
Time++;
}
void main()
{
init();
while(1)
{
if(Time==20)
{
Time=0;
SendString(“table”);
} } 展开
{
TMOD |= 0x21;//定时器0工作方式1,TH1是重装值,TL1是初值 定时器1工作方式2
SCON = 0x50;//在11.0592MHz下,设置串行口波特率为9600,方式1,并允许接收
PCON=0x00;
TH1=0xFD; //重载值
TL1=0xFD; //初始化值
TH0 =(65535-50000)/256;
TL0 =(65535-50000)%256;
TR0 = 1;
ET0 = 1;
TR1 = 1;
ET1 = 0;
ES = 1; //打开串口中断
EA = 1; //开总中断
}
void uart() interrupt 4 using 3//串口中断
{
if(RI)
{
RI = 0;
//add your code here!
}
else
TI = 0;
}
void Timer0(void) interrupt 1 using 1 //定时器0中断
{
TH0 =(65535-50000)/256;
TL0 =(65535-50000)%256;
Time++;
}
void main()
{
init();
while(1)
{
if(Time==20)
{
Time=0;
SendString(“table”);
} } 展开
2个回答
展开全部
//创建一个串口通讯
SerialPort CurrentPort = null;
CurrentPort = new SerialPort();
CurrentPort.ReadBufferSize = 128;
CurrentPort.PortName = comName; //端口号
CurrentPort.BaudRate = bandRate; //比特率
CurrentPort.Parity =parity;//奇偶校验
CurrentPort.StopBits = stop;//停止位
CurrentPort.DataBits = databit;//数据位
CurrentPort.ReadTimeout = 1000; //读超时,即在1000内未读到数据就引起超时异常
//绑定数据接收事件,因为发送是被动的,所以你无法主动去获取别人发送的代码,只能通过这个事件来处理
CurrentPort.DataReceived += Sp_DataReceived;
CurrentPort.Open();
定义一个变量 byte[] receiveStr;
//绑定的事件处理函数
private static void Sp_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
SerialPort sp = sender as SerialPort;
if (sp == null)
return;
byte[] readBuffer = new byte[sp.ReadBufferSize];
sp.Read(readBuffer, 0, readBuffer.Length);
//赋值
receiveStr=readBuffer;//当然你可以通过转换将byte[]转换为字符串。
}
//你要求的按钮事件可以这么写
private void button1_Click(object sender, EventArgs e)
{
if(receiveStr!=null)
{
变量 xxx=receiveStr;
}
}
SerialPort CurrentPort = null;
CurrentPort = new SerialPort();
CurrentPort.ReadBufferSize = 128;
CurrentPort.PortName = comName; //端口号
CurrentPort.BaudRate = bandRate; //比特率
CurrentPort.Parity =parity;//奇偶校验
CurrentPort.StopBits = stop;//停止位
CurrentPort.DataBits = databit;//数据位
CurrentPort.ReadTimeout = 1000; //读超时,即在1000内未读到数据就引起超时异常
//绑定数据接收事件,因为发送是被动的,所以你无法主动去获取别人发送的代码,只能通过这个事件来处理
CurrentPort.DataReceived += Sp_DataReceived;
CurrentPort.Open();
定义一个变量 byte[] receiveStr;
//绑定的事件处理函数
private static void Sp_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
SerialPort sp = sender as SerialPort;
if (sp == null)
return;
byte[] readBuffer = new byte[sp.ReadBufferSize];
sp.Read(readBuffer, 0, readBuffer.Length);
//赋值
receiveStr=readBuffer;//当然你可以通过转换将byte[]转换为字符串。
}
//你要求的按钮事件可以这么写
private void button1_Click(object sender, EventArgs e)
{
if(receiveStr!=null)
{
变量 xxx=receiveStr;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询