Instantly share code, notes, and snippets.

Embed
What would you like to do?
RCB128RFA1 & RCB_BB UART serial echo client
#include <stdint.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#define BAUD 9600
// doc8266: ATmega128RFA1 data sheet. Available at:
//
// http://www.atmel.com/dyn/resources/prod_documents/doc8266.pdf
void clock_init()
{
cli();
// See doc8266 p154: 11.10 System Clock Prescaler
CLKPR = (1 << CLKPCE);
// CLKPS = 1: RC oscillator divided by 4
CLKPR = (1 << CLKPS0);
sei();
}
void usart_init()
{
// See doc8266 p342: 23.3.1 Internal Clock Generation
// Asynchronous normal mode (U2X1 = 0): (Fosc/(16 * Baud)) - 1
UBRR1 = ((F_CPU/16)/BAUD) - 1;
// Enable receive and transmit
UCSR1B = (1 << RXEN1) | (1 << TXEN1);
// Set 8 data bits (UCSZ12:10 = 011) and 2 stop bits (USBS1 = 1)
UCSR1C = (1 << UCSZ11) | (1 << UCSZ10) | (1 << USBS1);
// Configure the MAX3221E: PD7 = !FORCEOFF, PD6 = FORCEON, PD4 = !EN
DDRD = (1 << PD7) | (1 << PD6) | (1 << PD4);
// Force the MAX3221E on: !FORCEOFF = 1, FORCEON = 1, !EN = 0
PORTD = (1 << PD7) | (1 << PD6);
}
uint8_t usart_recv()
{
// See doc8266 p349: 23.7.1 Receiving Frames with 5 to 8 Data Bits
// RXC1: USART Receive Complete
while (!(UCSR1A & (1 << RXC1)))
;
return UDR1;
}
void usart_send(uint8_t b)
{
// See doc8266 p347: 23.6.1 Sending Frames with 5 to 8 Data Bits
// UDRE1: USART Data Register Empty
while (!(UCSR1A & (1 << UDRE1)))
;
UDR1 = b;
}
int main(void)
{
clock_init();
usart_init();
while(1)
usart_send(usart_recv());
}
@alisdair

This comment has been minimized.

Owner

alisdair commented Nov 10, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment