AVR和51单片机串口通信简单实验
//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;i
}
#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&(1
}
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 sbit bz=P3^3;//喇叭发声变量 code uchar seg[]={0x28,0x7e,0xa2,0x62,0x74,0x61,0x21,0x7a,0x20,0x60}; // 数码管码表 uchar dat; //接受的数据值 void delay(uint k) void show(uint k) //显示函数 void serial_init() //串口初始化 void serial_iit() //串口中断设置 void sss() interrupt 4 //串口中断子函数 void time0_init() void time0_event() interrupt 1 void time0_iit()//定时器0中断开 void main() while(1) }
#define uint unsigned int
sbit relay=P3^5; //继电器吸合变量
code uchar tab[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe}; //数码管位选
uchar start; //各方式启动变量
{
while(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;
}
}
{
TMOD|=0x20;
SM2=0;
SM1=1;
SM0=0;
REN=1;
TH1=0xf3;
TL1=0xf3;
TR1=1;
}
{
EA=1;
ES=1;
}
{
if(RI==1)
{
dat=SBUF;
RI=0;
}
}
{
TMOD|=0x01;
TH0=0xff;
TL0=0x88;
TR0=1;
}
{
TH0=0xff;
TL0=0x88;
if(start==1)
{
bz=~bz;
}
else
bz=1;
}
{
ET0=1;
}
{
serial_init();
serial_iit();
time0_init();
time0_iit();
relay=1;
{
show(dat);
if(dat==49)
{
start=1;
}
if(dat==50)
{
start=2;
}
else
start=0;
if(start==2)
{
relay=0;
}
else
{
relay=1;
}
}
AVR51单片机串口通 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)