微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > AVR和51单片机串口通信简单实验

AVR和51单片机串口通信简单实验

时间:11-30 来源:互联网 点击:
//AVR作为发送机,控制51的喇叭发声和2极管发光

//AVR发送方代码 //采用2400波特率

#include
#include

#define uchar unsigned char
#define uint unsigned int

#define k1 PIND_Bit4 //设置为喇叭
#define k3 PIND_Bit6 //设置为发光管
#define k4 PIND_Bit7 //发送控制命令

#define UDRE 5
#define RXC 7

uchar dat=48;

void delay(uint k)
{
uint i,j;
for(i=0;ifor(j=0;j<1140;j++);
}

#include "1602.h"

void port_init()
{
PORTA=0xff;
DDRA=0xff;
PORTB=0xff;
DDRB=0xff;
PORTC=0xff;
DDRC=0xff;
PORTD=0xff;
DDRD=0x0f;
}

void serial_init()
{
UCSRB=0x00;
UCSRA=0x02; //双倍速
UCSRC=0x06; //8位字符模式
UBRRH=0x01; //2400波特率
UBRRL=0xa0;
UCSRB=0x18;
}

void send(uchar k) //串口发送函数
{
while(!(UCSRA&(1UDR=k;

}

void key1()
{
static uint i,j;
if(k1==0)
{
if(i==0)
{
j++;
if(j>10)
{
if(k1==0)
{
i=1;j=0;
dat=49;
}
}
}
}
else
{
i=0;j=0;
}
}

void key3()
{
static uint i,j;
if(k3==0)
{
if(i==0)
{
j++;
if(j>10)
{
if(k3==0)
{
i=1;j=0;
dat=50;
}
}
}
}
else
{
i=0;j=0;
}
}

void main()
{
port_init();
serial_init();
init1602();
delay(1000);
while(1)
{
displayonechar(1,6,dat);
if(k4==0)
{
send(dat);
}
key1(); //喇叭发声

key3(); //2极管发光
}
}

//----------1602库函数-----------

#define RS PORTB_Bit0
#define RW PORTB_Bit1
#define EN PORTB_Bit2
#define DATA PORTA
#define busy 0x80

void wait()
{
uchar val;
DATA=0xff;
RS=0;
RW=1;
__no_operation();
__no_operation();
EN=1;
__no_operation();
__no_operation();
DDRA=0x00;
val=PINA;
while(val&busy)
{
val=PINA;
}
EN=0;
DDRA=0xff;
}

void writecmd(uchar cmd)
{
wait();
RS=0;
RW=0;
__no_operation();
DATA=cmd;
__no_operation();
EN=1;
__no_operation();
__no_operation();
EN=0;
}

void writedata(uchar data)
{
wait();
RS=1;
RW=0;
__no_operation();
DATA=data;
__no_operation();
EN=1;
__no_operation();
__no_operation();
EN=0;
}

void displayonechar(uchar x,uchar y,uchar dda)
{
y&=0x01;
x&=0x0f;
if(y)x|=0x40;
x|=0x80;
writecmd(x);
writedata(dda);
}

void displaychar(uchar x,uchar y,uchar *p)
{
y&=0x01;
x&=0x0f;
while(*p!=)
{
if(x<=0x0f)
{
displayonechar(x,y,*p);
p++;
x++;
}
}
}

void init1602()
{
delay(15);
writecmd(0x38);
delay(5);
writecmd(0x38);
delay(5);
writecmd(0x38);
writecmd(0x80);
writecmd(0x01);
writecmd(0x06);
writecmd(0x0c);
}

//51单片机接收端程序 12MHZ晶振,采用2400波特率

#include

#define uchar unsigned char
#define uint unsigned int

sbit bz=P3^3;//喇叭发声变量
sbit relay=P3^5; //继电器吸合变量

code uchar seg[]={0x28,0x7e,0xa2,0x62,0x74,0x61,0x21,0x7a,0x20,0x60}; // 数码管码表
code uchar tab[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe}; //数码管位选

uchar dat; //接受的数据值
uchar start; //各方式启动变量

void delay(uint k)
{
while(k--);
}

void show(uint k) //显示函数
{
static uchar i,j;
uint ss[4];
i=0;
do
{
ss[i]=k%10;
i++;
}while(k/=10);
for(j=0;j{
P0=seg[ss[j]];
P2=tab[j];
delay(30);
P0=0xff;
P2=0xff;
}
}

void serial_init() //串口初始化
{
TMOD|=0x20;
SM2=0;
SM1=1;
SM0=0;
REN=1;
TH1=0xf3;
TL1=0xf3;
TR1=1;
}

void serial_iit() //串口中断设置
{
EA=1;
ES=1;
}

void sss() interrupt 4 //串口中断子函数
{
if(RI==1)
{
dat=SBUF;
RI=0;
}
}

void time0_init()
{
TMOD|=0x01;
TH0=0xff;
TL0=0x88;
TR0=1;
}

void time0_event() interrupt 1
{
TH0=0xff;
TL0=0x88;
if(start==1)
{
bz=~bz;
}
else
bz=1;
}

void time0_iit()//定时器0中断开
{
ET0=1;
}

void main()
{
serial_init();
serial_iit();
time0_init();
time0_iit();
relay=1;

while(1)
{
show(dat);
if(dat==49)
{
start=1;
}
if(dat==50)
{
start=2;
}
else
start=0;
if(start==2)
{
relay=0;
}
else
{
relay=1;
}
}

}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top