一个不错的AVR GCC通讯例程,已经验证无误,此为GCC串口通讯实验绝佳学习资料;
功能说明:
1.开机串口发送 hello word!
2.8个LED逐一点亮
3.通过串口控制LED的亮灭
例如:
LED00 LED0灯亮
LED01 LED0灯灭
LED02 LED全灭
LED03 LED全亮
使用MEGA162实验成功!不敢独享,公布如下:
-----------------------------------------------------------------------------------------------------------
/*****************************************************************************
* - COM.H -
*****************************************************************************/
#ifndef COM_INCLUDED
#define COM_INCLUDED
#i nclude <avr/io.h>
#define UART_PORT PORTD //UART port number
#define UART_RX (0) //RX pin number
#define UART_TX (1) //TX pin number
extern void InitUART(long clockFreq, long Baud); //Initialize UART and buffers
extern int SendByte(char DataByte);
extern int GetByte(void);
#endif
----------------------------------------------------------------------------------------------------------
/*****************************************************************************
* Com.c
*
*****************************************************************************/
#i nclude <Com.h>
/*****************************************************************************
*
*/
void InitUART(long clockFreq, long Baud)
{
char temp;
Baud = clockFreq/Baud/16-1;
UBRR0L = (unsigned char)Baud; //Update baud rate register -> BAUDRATECODE_LO;
UBRR0H = (unsigned char)(Baud >> 8); //Update baud rate register -> BAUDRATECODE_HI;
UCSR0A = 0xC0; //0b1100 0000 = Clear tx complete bit
//Enable receive interrupt, and the UART's receiver and transmitter.
UCSR0B = 0x18; //0b10011000 = 0x98
UCSR0C = 0x86; //0b10000110 = 0x86
}
/*****************************************************************************
* Send a char to the UART. The UDRE bit is monitored and the char is sent when
* the bit is set high.
*/
int SendByte(char DataByte)
{
if(DataByte=='\n')
SendByte('\r');
loop_until_bit_is_set(UCSR0A,UDRE0);
UDR0=DataByte;
return 0;
}
/*****************************************************************************
* GetByte:
*/
int GetByte(void)
{
loop_until_bit_is_set(UCSR0A,RXC0);
return UDR0;
}
---------------------------------------------------------------------------------------------------------
/*
功能说明:
1.开机串口发送 hello word!
2.8个LED逐一点亮
3.通过串口控制LED的亮灭
例如:
LED00 LED0灯亮
LED01 LED0灯灭
LED02 LED全灭
LED03 LED全亮
*/
#i nclude <avr/io.h> // include I/O definitions (port names, pin names, etc)
#i nclude <avr/signal.h> // include "signal" names (interrupt names)
#i nclude <avr/interrupt.h> // include interrupt support
#i nclude <avr/delay.h>
#i nclude <Com.h>
#i nclude <string.h>
#i nclude <stdio.h>
#define LED PORTA
#define LEDDDR DDRA
void delay_nms(unsigned int ms);
void LedControl(char bit, char status);
int main(void)
{
unsigned char str[50], ch, n;
unsigned char *pt;
InitUART(F_CPU,9600);
fdevopen(SendByte,GetByte);
LEDDDR = 0xFF; //Set PORTB as output;
LED = 0xFF; //LED off
printf("Hello World.\r\n");
LedControl(0,1);
delay_nms(300);
LedControl(1,1);
delay_nms(300);
LedControl(2,1);
delay_nms(300);
LedControl(3,1);
delay_nms(300);
LedControl(4,1);
delay_nms(300);
LedControl(5,1);
delay_nms(300);
LedControl(6,1);
delay_nms(300);
LedControl(7,1);
delay_nms(300);
LedControl(0,2);
while(1)
{
n = 0;
while((ch = GetByte())!= '\r')
{
str[n++] = ch;
}
str[n] = 0x00;
pt = strstr(str,"LED");
if(pt)
{
LedControl((pt[3]-0x30),(pt[4]-0x30));
}
else
{
printf("You type in: %s\n", str);
}
}
return 0;
}
void delay_nms(unsigned int ms)
{
int i;
for(i = 0; i<ms; ++i)
{
_delay_ms(1);
}
}
/*******************************************
***********Control Led On/Off***************
*******************************************/
void LedControl(char bit, char status)
{
switch (status) //LED ON
{
case 0: //clear Led bit off
LED |= 1<<bit;
break;
case 1: //set Led bit on
LED &= ~(1<<bit);
break;
case 2: //clear Led bit off
LED = 0xFF;
break;
case 3: //set Led bit on
LED = 0x00;
break;
default:
break;
}
}
文章评论(0条评论)
登录后参与讨论