您的位置 首页 ADAS

AVR与RS232——一个简略易懂的串口通讯比如程序

只有基本的功能,用于串口操作的演示,如果需要结构化的程序,到http:wwwavrvicomavr_examplesusarthtmlRS232电路图本程序在本站的

只要根本的功用,用于串口操作的演示,假如需求结构化的程序,到http://www.avrvi.com/avr_examples/usart.html

RS232电路图

本程序在本站的开发板上经过,肯定没有问题,欢迎评论。
测验作用,启动时发送http://www.avrvi.com加回车换行,然后,收到什么,回来什么,假如给它发送一个小a,会多发回一个OK,假如发的是大A,则发回Hello字符串。

串口程序测验

// ICC-AVR application builder : 2007-5-20 17:21:25
// Target : M16
// Crystal: 7.3728Mhz
// AVR mega16 串口测验
// AVR与虚拟仪器http://www.avrvi.com古欣

#include
#include

#define F_CPU 7372800
const unsigned char buffer[]=”http://www.avrvi.com”;

void USART_Init( unsigned int baud )
{
unsigned int tmp;

tmp= F_CPU/baud/16-1;
UBRRH = (unsigned char)(tmp>>8);
UBRRL = (unsigned char)tmp;

UCSRB = (1<}

// %&&&&&%C 生成的初始化
//UART0 initialize
// desired baud rate: 9600
// actual: baud rate:9600 (0.0%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x00;
UCSRC = BIT(URSEL) | 0x06;
UBRRL = 0x2F; //set baud rate lo
UBRRH = 0x00; //set baud rate hi
UCSRB = 0x18;
}

//下面两个函数直接从数据手册上复制过来的

// 数据发送【发送5 到8 位数据位的帧】
void USART_Transmit( unsigned char data )
{

while ( !( UCSRA & (1<;

UDR = data;
}

// 数据接纳【以5 到8 个数据位的方法接纳数 据帧】
unsigned char USART_Receive( void )
{

while ( !(UCSRA & (1<;

return UDR;
}

//接连发送字符
void USART_Transmit_2( void )
{
while ( !( UCSRA & (1<;
UDR = H;
while ( !( UCSRA & (1<;
UDR = e;
while ( !( UCSRA & (1<;
UDR = l;
while ( !( UCSRA & (1<;
UDR = l;
while ( !( UCSRA & (1<;
UDR = o;
}

void main(void)
{
unsigned char n=0,tmp=0;

USART_Init(9600); //波特率9600 初始化串口
// uart0_init();

for(i=0;n<20;n++) //发送数组里边的字符串,http://www.avrvi.com
{
USART_Transmit(buffer[n]);
}

USART_Transmit(0x0d); //发送一个回车
USART_Transmit(0x0a); //发送一个换行

while(1)
{
if(UCSRA&(1<{
tmp=USART_Receive(); //接纳数据
USART_Transmit(tmp); //发送数据

if(tmp==a) //对接纳到的数据进行,假如是a,再发一个OK回来
{
USART_Transmit(O);
USART_Transmit(K);
}
if(tmp==A) //对接纳到的数据进行,假如是A,再发一个Hello回来
{
USART_Transmit_2();
}
}
}
}

测验作用,启动时发送http://www.avrvi.com加回车换行,然后,收到什么,回来什么,假如给它发送一个小a,会多发回一个OK,假如发的是大A,则发回Hello字符串。

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

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

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

微信扫一扫关注我们

返回顶部