Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1563834
  • 博文数量: 354
  • 博客积分: 8137
  • 博客等级: 中将
  • 技术积分: 5137
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-26 15:40
文章分类

全部博文(354)

文章存档

2010年(300)

2009年(54)

分类: 嵌入式

2010-04-22 10:13:36

/**********************************************************************
* 文件名称: main.c
* 程序作者: kidcao1987
* 程序版本: V1.0
* 功能描述: 接上串口,在串口调试助手里显示
“Serial Communication Test!
曹佃生的串口测试程序!!QQ:289718970

* 编译器:WinAVR-20090313
* 芯片:ATmega16,外部11.0592MHZ晶振
* 技术支持:
**********************************************************************/

#include
#include
#include

#define BAUD 9600

volatile unsigned char ucReceivedData; //接收到的数据
unsigned char const ucStr1[]={"Serial Communication Test!"};
unsigned char const ucStr2[]={"曹佃生的串口测试程序!!QQ:289718970"};
unsigned char const ucStr3[]={""};


void USARTInit(void); //初始化
void USARTTransmitData(unsigned char); //发送数据
void USARTTransmitString(unsigned char const *pStr); //发送字符串


int main(void)
{
PORTD = 0x03;
DDRD = 0x02; //PD0输入 PD1输出

PORTB = 0xff;
DDRB = 0xff; //LED指示灯

USARTInit();

USARTTransmitString(ucStr1);
USARTTransmitString(ucStr2);
USARTTransmitString(ucStr3);
sei();
while(1)
{
USARTTransmitString(ucStr1);
USARTTransmitString(ucStr2);
USARTTransmitString(ucStr3);
USARTTransmitData(0x0d);
USARTTransmitData(0x0a);
_delay_ms(2000);
}
}


void USARTInit(void) //初始化
{
UCSRA = 0X00;
UCSRC |= (1<UBRRL = (11059200 / BAUD / 16 - 1) % 256; //波特率设置为9600
UBRRH = (11059200 / BAUD / 16 - 1) / 256;
UCSRB |= (1 << RXCIE) | (1 << RXEN) | (1 << TXEN); //接收中断使能,接收使能,发送使能
}



void USARTTransmitData(unsigned char ucTransmitData)
{
while(!(UCSRA & (1<
UDR = ucTransmitData; //发送
}



void USARTTransmitString(const unsigned char* pStr)
{
while(*pStr != '\0')
{
USARTTransmitData(*pStr++);
}
USARTTransmitData(0x0d);
USARTTransmitData(0x0a);
}


ISR(USART_RXC_vect)
{
ucReceivedData = UDR; //读数据

PORTB = ucReceivedData; //点亮LED
USARTTransmitData(ucReceivedData); //发送数据
}


视频地址:

阅读(801) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~