AVR單片機(jī)—(五)、ATMEGA16的USART與PC機(jī)串行通信—02
掃描二維碼
隨時(shí)隨地手機(jī)看文章
五、ATMEGA16的USART與PC機(jī)串行通信
五—(02)、PC機(jī)發(fā)送字符給單片機(jī),然后用液晶顯示發(fā)送的字符~~~
這章節(jié)也是相對(duì)挺簡單的~~直接上圖片吧~~
基本上就這么點(diǎn)吧~~然后就是程序了,這里我只是粘貼一下重要的程序~~,有的程序在以前的文章中用過了,我就不粘貼了~~比如說是lcdinit這個(gè).c和.h文件吧~~都是以前就貼出來的,這里我就不說了~
IAR5_2.c文件:
//------------------------------------------------------------------------------
//這個(gè)實(shí)驗(yàn)進(jìn)行多個(gè)字節(jié)的發(fā)送和接收
//單片機(jī)收到多個(gè)字節(jié)后既將其回傳給PC,同時(shí)送入數(shù)組暫存,最后在液晶模塊上顯示出來。
#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];
//------------------------------------------------------------------------------
//端口初始化函數(shù)
void port_init()
{
DDRA=0XFF;
PORTA=0XFF;
DDRB=0XFF;
PORTB=0XFF;
DDRD=0X02;
PORTD=0XFF;
}
//------------------------------------------------------------------------------
//USART初始化函數(shù)
void usart_init()
{
UCSRA=0X02;//置位U2X,倍速發(fā)送
UCSRC=0X06;//選擇字符長度為8位
UBRRL=0X67;//設(shè)置波特率9600,查看datesheet
UBRRH=0X00;
UCSRB=0X18;//發(fā)送使能、接收使能
}
//------------------------------------------------------------------------------
//芯片初始化函數(shù)
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個(gè)幀
{
temp=usart_receive_char();
data_send[s]=temp;
s++;
}
s=0;
usart_send_string("當(dāng)前的數(shù)據(jù)是:");//發(fā)送“當(dāng)前的數(shù)據(jù)是:”
while(s<5)//向PC發(fā)回5個(gè)幀
{
usart_send_char(data_send[s]);
s++;
}
s=0;
usart_send_char(0x0d);//發(fā)送回車換行
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的初始化程序—可調(diào)用函數(shù)
#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寫數(shù)據(jù)函數(shù)
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寫指令函數(shù)
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初始化函數(shù)
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顯示一個(gè)字節(jié)函數(shù)
voidlcd_display_char(uchar add,uchar dat)
{
lcd_write_command(add);
delay_ms(1);
lcd_write_data(dat);
delay_ms(1);
}
//------------------------------------------------------------------------------
//LCD顯示多個(gè)字節(jié)函數(shù)
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文件:
//------------------------------------------------------------------------------
//可供其它文件調(diào)用的USART函數(shù),方便移植
#include"ioavr.h"
#include"intrinsics.h"
//------------------------------------------------------------------------------
typedef unsigned char uchar;
typedef unsigned intuint;
#define UDRE 5
#define RXC7
//------------------------------------------------------------------------------
//發(fā)送5~8位數(shù)據(jù)位的幀
void usart_send_char(uchar i)
{
while(!(UCSRA&(1<
}
//------------------------------------------------------------------------------
//接收5~8位數(shù)據(jù)位的幀
uchar usart_receive_char()
{
while(!(UCSRA&(1<
}
//------------------------------------------------------------------------------
//發(fā)送多個(gè)幀,其中每個(gè)幀為5~8位
void usart_send_string(char *s)
{
while(*s)//發(fā)送一個(gè)字符串,字符串以"0"結(jié)束,它也就結(jié)束嘍
{
usart_send_char(*s);
s++;
}
}
hey