//F_CPU 7.327Mhz
//IDE: avrstudio418setup.exe
//complier: WinAVR-20100110-install.exe
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include "global.h"
#include <stdio.h>
#define UART0_DEFAULT_BAUD_RATE 9600 ///< default baud rate for UART0
#define UART1_DEFAULT_BAUD_RATE 9600 ///< default baud rate for UART1
//Timerout Count use TCC1
#define TimerReg TCCR1A
#define TimerCountReg TCNT1
#define DisableTimerOut TCCR1B &= ~BV(CS32) | ~BV(CS31) | ~BV(CS30)
// Clk/256 7.372MHz/1024 =>
#define EnableTimerOut \
do{ \
TCCR1B |= (1 << CS12) | (1 << CS10); \
TCNT1 = 0;\
}while(0);
#define TimeOut (TCNT1 > 2160)
#ifndef RXCIE
#define RXCIE RXCIE0
#define TXCIE TXCIE0
#define UDRIE UDRIE0
#define RXEN RXEN0
#define TXEN TXEN0
#define CHR9 CHR90
#define RXB8 RXB80
#define TXB8 TXB80
#endif
#ifndef UBRR0L
#define UBRR0L UBRR0
#define UBRR1L UBRR1
#endif
//RX interrupt-> RX Get Data-> Data to Tx Send
int main(void)
{
MCUCSR = 0;
uart0Init(UART0_DEFAULT_BAUD_RATE);
uart1Init(UART1_DEFAULT_BAUD_RATE);
sei(); //enable global interrupt
while(1)
{
wdt_reset();
//your program
{
}
}
}
void uart0Init(unsigned long baudrate)
{
PORTE |= (1 << PORTE1) | (1 << PORTE0) ;
uartSetBaudRate(0, baudrate);
//outb(UCSR0B, BV(RXCIE0)|BV(TXCIE0)|BV(RXEN0)|BV(TXEN0));
outb(UCSR0B, BV(RXCIE0)|BV(RXEN0)|BV(TXEN0));
//sei();
}
void uart1Init(unsigned long baudrate)
{
PORTD |= (1 << PORTD3) | (1 << PORTD2) ;
uartSetBaudRate(1, baudrate);
//outb(UCSR0B, BV(RXCIE0)|BV(TXCIE0)|BV(RXEN0)|BV(TXEN0));
outb(UCSR1B, BV(RXCIE1)|BV(RXEN1)|BV(TXEN1));
//sei();
}
void uartSetBaudRate(unsigned char uart_sel, unsigned long baudrate)
{
// calculate division factor for requested baud rate, and set it
unsigned int bauddiv = ((F_CPU+(baudrate*8L))/(baudrate*16L)-1);
if(uart_sel)
{
outb(UBRR1L, bauddiv);
#ifdef UBRR1H
outb(UBRR1H, bauddiv>>8);
#endif
}
else
{
outb(UBRR0L, bauddiv);
#ifdef UBRR0H
outb(UBRR0H, bauddiv>>8);
#endif
}
}
//uart_sel = 0 use uart0, uart_sel = 1 use uart1
void uartSendByte(unsigned char uart_sel, unsigned char txData)
{
if(uart_sel)
{
while(!(UCSR1A & (1<<UDRE)));
outb(UDR1, txData);
}
else
{
while(!(UCSR0A & (1<<UDRE)));
outb(UDR0, txData);
}
}
unsigned char uartReceiveByte(unsigned char uart_sel)
{
TimerCountReg = 0;
EnableTimerOut;
if(uart_sel == 0)
{
while(!(UCSR0A & (1<<RXC0)))
{
if(TimeOut){
break;
}
}
DisableTimerOut;
return UDR0;
}
else
{
while(!(UCSR1A & (1<<RXC1)))
{
if(TimeOut){
break;
}
}
DisableTimerOut;
return UDR1;
}
}
//UART0 RX reevied ISR vector
ISR(USART0_RX_vect)
{
cli();
wdt_reset();
UCSR0B &= ~(1 << RXCIE0); //Disable UART0 RX intperrupt.
uartSendByte(0, uartReceiveByte(0));
UCSR0B |= (1 << RXCIE0); //Enable UART0 RX intperrupt.
wdt_reset();
sei();
}
//UART1(Ethernet) RX reevied ISR vector
ISR(USART1_RX_vect)
{
cli();
wdt_reset();
UCSR1B &= ~(1 << RXCIE1); //Disable UART1 RX intperrupt.
uartSendByte(1, uartReceiveByte(1));
UCSR1B |= (1 << RXCIE1); //Enable UART1 RX intperrupt.
wdt_reset();
sei();
}
程式不保證會動..因為就是從以前寫的程式剪下貼上組合出來的。
簡單使用ISR,未使用FIFO與OS概念。
其他相關請自行google網路,AVR的論壇很講解
http://www.avrfreaks.net/
因為台灣使用AVR的不算多,所以相關資料大多數為英文與簡體(大陸一堆人再用)