
#include <REG52H>
#define uchar unsigned char
#define uint unsigned int
sbit ring=P3^7;
sbit CASE1=P2^0;
sbit CASE2=P2^1;
sbit CASE3=P2^2;
sbit CASE4=P2^3;
uchar se=0,re=0;
uchar temp=0;
void wait(uint cnt)
{
while(--cnt);
}
void send(uchar se)
{
SBUF=se; //发送数据
while(TI == 0);
TI = 0;
}
//串口接收程序
uchar receive(void)
{
re=SBUF; //接收数据
while(RI==0);
RI=0;
return re;
}
//串口初始化
void sinti(void)
{
SCON = 0x50;
TMOD |= 0x20;
TH1 = 0xFD;
TR1 = 1;
EA = 1;
ES = 1;
}
void delay(int cnt)
{
while(--cnt);
}
//主程序
int main (void)
{
int i;
sinti(); //串口初始化程序
ring=1;
while(1)
{
while (1)
{
if(CASE1==0)
{
send('a');
ring=0;
break;
}
if(CASE2==0)
{
send('b');
ring=0;
break;
}
if(CASE3==0)
{
send('c');
ring=0;
break;
}
if(CASE4==0)
{
send('d');
ring=0;
break;
}
}
if(ring==0)
{
wait(60000);
ring=1;
}
for(i=0;i<10000;i++);
}
}
//串口中断程序
void UART_SER (void) interrupt 4 //串行中断服务程序
{
if(RI) //判断是接收中断产生
{
RI=0; //标志位清零
temp=SBUF;
}
if(TI) //如果是发送标志位,清零
TI=0;
}
你用的串口接收是什么机制?
如果是主动接收的话,就是说你自己写了个线程专门不停读取串口,中断程序的调用放在这里面就可以了。如果你是用的消息响应机制的话,那就要正确设置OnCommRec()函数。
不过看你用的是C语言,如果全部程序都是C语言写的话,应该是只能主动读取。
/ 甲机串口程序:甲机向乙机发送控制命令字符,甲机同时接收乙机发送的数字,并显示在数码管上。/
#include<reg51h>
#define uchar unsigned char
#define uint unsigned int
sbit LED1=P1^0;
sbit LED2=P1^3;
sbit K1=P1^7;
uchar Operation_No=0; // *** 作代码
//数码管代码
uchar codeDSY_CODE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
//延时
void DelayMS(uint ms)
{
uchari;
while(ms--)for(i=0;i<120;i++);
}
//向串口发送字符
void Putc_to_SerialPort(uchar c)
{
SBUF=c;
while(TI==0);
TI=0;
}
//主程序
void main()
{
LED1=LED2=1;
P0=0x00;
SCON=0x50; //串口模式1,允许接收
TMOD=0x20; //T1工作模式2
PCON=0x00; //波特率不倍增
TH1=0xfd;
TL1=0xfd;
TI=RI=0;
TR1=1;
IE=0x90; //允许串口中断
while(1)
{
DelayMS(100);
if(K1==0) //按下K1时选择 *** 作代码0,1,2,3
{
while(K1==0);
Operation_No=(Operation_No+1)%4;
switch(Operation_No) //根据 *** 作代码发送A/B/C或停止发送
{
case0: Putc_to_SerialPort('X');
LED1=LED2=1;
break;
case1: Putc_to_SerialPort('A');
LED1=~LED1;LED2=1;
break;
case2: Putc_to_SerialPort('B');
LED2=~LED2;LED1=1;
break;
case3: Putc_to_SerialPort('C');
LED1=~LED1;LED2=LED1;
break;
}
}
}
}
//甲机串口接收中断函数
void Serial_INT() interrupt 4
{
if(RI)
{
RI=0;
if(SBUF>=0&&SBUF<=9)P0=DSY_CODE[SBUF];
elseP0=0x00;
}
}
/ 乙机程序接收甲机发送字符并完成相应动作:乙机接收到甲机发送的信号后,根据相应信号控制LED完成不同闪烁动作。/
#include<reg51h>
#define uchar unsigned char
#define uint unsigned int
sbit LED1=P1^0;
sbit LED2=P1^3;
sbit K2=P1^7;
uchar NumX=-1;
//延时
void DelayMS(uint ms)
{
uchari;
while(ms--)for(i=0;i<120;i++);
}
//主程序
void main()
{
LED1=LED2=1;
SCON=0x50; //串口模式1,允许接收
TMOD=0x20; //T1工作模式2
TH1=0xfd; //波特率9600
TL1=0xfd;
PCON=0x00; //波特率不倍增
RI=TI=0;
TR1=1;
IE=0x90;
while(1)
{
DelayMS(100);
if(K2==0)
{
while(K2==0);
NumX=++NumX%11; //产生0~10范围内的数字,其中10表示关闭
SBUF=NumX;
while(TI==0);
TI=0;
}
}
}
void Serial_INT() interrupt 4
{
if(RI) //如收到则LED则动作
{
RI=0;
switch(SBUF)//根据所收到的不同命令字符完成不同动作
{
case'X': LED1=LED2=1;break; //全灭
case'A': LED1=0;LED2=1;break; //LED1亮
case'B': LED2=0;LED1=1;break; //LED2亮
case'C': LED1=LED2=0; //全亮
}
}
}
以上就是关于51单片机串口通信c语言编程全部的内容,包括:51单片机串口通信c语言编程、有关串口通信的c程序问题 跳不进中断 用的8259 16550、两片单片机通过串口一发一收的C语言例程等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)