您的位置 首页 元件

AVR和51单片机串口通讯简略试验

AVR作为发送机,控制51的喇叭发声和2极管发光AVR发送方代码采用2400波特率includeiom16h>includeintrinsicsh>defineu

//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&(1<UDR=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;
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/xinpin/yuanjian/318400.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部