Skip to content

Instantly share code, notes, and snippets.

@four0four
Last active December 9, 2015 23:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save four0four/4342512 to your computer and use it in GitHub Desktop.
Save four0four/4342512 to your computer and use it in GitHub Desktop.
couple AVR UART examples
/*
* mega324_UART.c
*
* Created: 12/28/2011 10:39:01 PM
* Author: Galen
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/power.h>
#define F_CPU 20000000
#define USART_BAUDRATE 9600
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
void sendString(uint8_t *data);
void sendByte(uint8_t data);
uint8_t recieveByte();
void initUART1();
int main(void)
{
//uint8_t *msg = "hello!\r\n";
cli();
power_all_disable();
power_usart1_enable();
initUART1();
unsigned char byte = 0;
//for(byte = 255;byte == 0;byte--);
sendByte('T');
sendByte('e');
sendByte('s');
sendByte('t');
sendByte('\n');
//return 1;
//sendString(msg);
while(1)
{
byte = recieveByte();
sendByte(byte);
}
}
void initUART1()
{
UCSR1B |= (1<<RXEN1) | (1<<TXEN1);
UCSR1C |= (1<<UCSZ10) | (1<<UCSZ11);
UBRR1H = (BAUD_PRESCALE>>8);
UBRR1L = BAUD_PRESCALE;
}
void sendByte(uint8_t data)
{
while ((UCSR1A & (1<<UDRE1)) == 0);
UDR1 = data;
return;
}
void sendString(uint8_t *data)
{
while(*data)
{
sendByte(*data);
data++;
}
}
uint8_t recieveByte()
{
while ((UCSR1A & (1<<RXC1)) == 0);
return UDR1;
}
/* CC-BY-NC-SA */
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/power.h>
#define F_CPU 1000000
#define BAUD 9600
// calculate the prescale value (should be optimized at compile time)
#define PRESCALE (((F_CPU / (BAUD * 16UL))) - 1)
void sendString(uint8_t *data);
void sendByte(uint8_t data);
uint8_t recieveByte();
void initUART();
int main(void)
{
initUART();
unsigned char byte = 42; // B
sendByte('T');
sendByte('e');
sendByte('s');
sendByte('t');
sendByte('\n');
while(1) { // echo whatever we get
byte = recieveByte();
sendByte(byte);
}
}
void initUART()
{
UBRR0H = (PRESCALE>>8);
UBRR0L = PRESCALE;
// enable TX and RX hardware
UCSR0B |= (1<<RXEN0) | (1<<TXEN0);
UCSR0C |= (1<<USBS0); // rest are defaults
}
void sendByte(uint8_t data)
{
// wait for clear...
while (!(UCSR0A & (1<<UDRE0)));
UDR0 = data;
return;
}
void sendString(uint8_t *data)
{
while(*data)
{
sendByte(*data);
data++;
}
}
uint8_t recieveByte()
{
while ((UCSR0A & (1<<RXC0)) == 0);
return UDR0;
}
#include <avr/io.h>
#define F_CPU 8000000
#define BAUD 9600
#define PRESCALE (((F_CPU/(BAUD * 16UL))) -1)
void initUART();
void transmitByte(unsigned char data);
void transmitString(unsigned char *data);
unsigned char receiveByte();
int main()
{
unsigned char byte;
initUART();
transmitByte(12); //New page
transmitString("Welcome to the AVR console\n\r");
transmitString(">");
while(1)
{
byte = receiveByte();
transmitByte(byte);
}
}
void initUART()
{
UBRRH = (PRESCALE>>8);
UBRRL = PRESCALE;
UCSRB |= (1<<RXEN) | (1<<TXEN) ;
UCSRC |= (1<<UCSZ1) | (1<<UCSZ0);
}
void transmitByte(unsigned char data)
{
while(!(UCSRA & (1<<UDRE)));
UDR = data;
}
void transmitString(unsigned char *data)
{
while(*data)
{
transmitByte(*data);
data++;
}
}
unsigned char receiveByte()
{
while(!(UCSRA & (1<<RXC)));
return UDR;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment