AVR单片机—(五)、ATMEGA16的USART与PC机串行通信—02
扫描二维码
随时随地手机看文章
五、ATMEGA16的USART与PC机串行通信
五—(02)、PC机发送字符给单片机,然后用液晶显示发送的字符~~~
这章节也是相对挺简单的~~直接上图片吧~~
基本上就这么点吧~~然后就是程序了,这里我只是粘贴一下重要的程序~~,有的程序在以前的文章中用过了,我就不粘贴了~~比如说是lcdinit这个.c和.h文件吧~~都是以前就贴出来的,这里我就不说了~
IAR5_2.c文件:
//------------------------------------------------------------------------------
//这个实验进行多个字节的发送和接收
//单片机收到多个字节后既将其回传给PC,同时送入数组暂存,最后在液晶模块上显示出来。
#include"ioavr.h"
#include"delayics.h"
#include"intrinsics.h"
#include"usart.h"
#include"lcdinit.h"
//------------------------------------------------------------------------------
uchar__flash title[]="Atmega16 LCD DIS";
uchar data_send[5];
//------------------------------------------------------------------------------
//端口初始化函数
void port_init()
{
DDRA=0XFF;
PORTA=0XFF;
DDRB=0XFF;
PORTB=0XFF;
DDRD=0X02;
PORTD=0XFF;
}
//------------------------------------------------------------------------------
//USART初始化函数
void usart_init()
{
UCSRA=0X02;//置位U2X,倍速发送
UCSRC=0X06;//选择字符长度为8位
UBRRL=0X67;//设置波特率9600,查看datesheet
UBRRH=0X00;
UCSRB=0X18;//发送使能、接收使能
}
//------------------------------------------------------------------------------
//芯片初始化函数
void device_init()
{
port_init();
usart_init();
}
//------------------------------------------------------------------------------
//main
void main()
{
uchar temp;
uchar s=0;
device_init();
lcd_init();
lcd_display_string(0x80,title,16);
delay_s(1);
while(1)
{
while(s<5)//等待接收完5个帧
{
temp=usart_receive_char();
data_send[s]=temp;
s++;
}
s=0;
usart_send_string("当前的数据是:");//发送“当前的数据是:”
while(s<5)//向PC发回5个帧
{
usart_send_char(data_send[s]);
s++;
}
s=0;
usart_send_char(0x0d);//发送回车换行
usart_send_char(0x0a);
lcd_display_char(0x80+0x40,'D');
lcd_display_char(0x80+0x40+1,'a');
lcd_display_char(0x80+0x40+2,'t');
lcd_display_char(0x80+0x40+3,'a');
lcd_display_char(0x80+0x40+5,data_send[0]);
lcd_display_char(0x80+0x40+7,data_send[1]);
lcd_display_char(0x80+0x40+9,data_send[2]);
lcd_display_char(0x80+0x40+11,data_send[3]);
lcd_display_char(0x80+0x40+13,data_send[4]);
}
}
lcdinit.c文件(感觉还是贴出来吧~~,不过.h文件我就不贴了~~):
//------------------------------------------------------------------------------
//LCD1602的初始化程序—可调用函数
#include"ioavr.h"
#include"intrinsics.h"
#include"delayics.h"
#include"bitics.h"
#include"lcdinit.h"
//------------------------------------------------------------------------------
//RS、RW、EN引脚输出高低电平的宏定义
#define lcd_rs_1SET_BIT(PORTB,0)
#define lcd_rs_0CLR_BIT(PORTB,0)
#define lcd_rw_1SET_BIT(PORTB,1)
#define lcd_rw_0CLR_BIT(PORTB,1)
#define lcd_en_1SET_BIT(PORTB,2)
#define lcd_en_0CLR_BIT(PORTB,2)
//------------------------------------------------------------------------------
#define data_port PORTA
#define busy0x80
//------------------------------------------------------------------------------
//LCD写数据函数
void lcd_write_data(uchar dat)
{
lcd_rs_1;
lcd_rw_0;
data_port=dat;
delay_ms(1);
lcd_en_1;
delay_ms(1);
lcd_en_0;
}
//------------------------------------------------------------------------------
//LCD写指令函数
voidlcd_write_command(uchar com)
{
lcd_rs_0;
lcd_rw_0;
data_port=com;
delay_ms(1);
lcd_en_1;
delay_ms(1);
lcd_en_0;
}
//------------------------------------------------------------------------------
//LCD初始化函数
void lcd_init()
{
lcd_write_command(0x01);
delay_ms(1);
lcd_write_command(0x38);
delay_ms(1);
lcd_write_command(0x0c);
delay_ms(1);
lcd_write_command(0x06);
delay_ms(1);
}
//------------------------------------------------------------------------------
//LCD显示一个字节函数
voidlcd_display_char(uchar add,uchar dat)
{
lcd_write_command(add);
delay_ms(1);
lcd_write_data(dat);
delay_ms(1);
}
//------------------------------------------------------------------------------
//LCD显示多个字节函数
voidlcd_display_string(uchar add,uchar __flash *s,uchar num)
{
uchar i;
lcd_write_command(add);
for(i=0;i
lcd_write_data(s[i]);
delay_ms(1);
}
}
USART.c文件:
//------------------------------------------------------------------------------
//可供其它文件调用的USART函数,方便移植
#include"ioavr.h"
#include"intrinsics.h"
//------------------------------------------------------------------------------
typedef unsigned char uchar;
typedef unsigned intuint;
#define UDRE 5
#define RXC7
//------------------------------------------------------------------------------
//发送5~8位数据位的帧
void usart_send_char(uchar i)
{
while(!(UCSRA&(1<
}
//------------------------------------------------------------------------------
//接收5~8位数据位的帧
uchar usart_receive_char()
{
while(!(UCSRA&(1<
}
//------------------------------------------------------------------------------
//发送多个帧,其中每个帧为5~8位
void usart_send_string(char *s)
{
while(*s)//发送一个字符串,字符串以"0"结束,它也就结束喽
{
usart_send_char(*s);
s++;
}
}
hey