Skip to content

Instantly share code, notes, and snippets.

@manashmandal
Last active October 5, 2016 08:31
Show Gist options
  • Save manashmandal/fa54618c3f5104e9f645c37793f9da99 to your computer and use it in GitHub Desktop.
Save manashmandal/fa54618c3f5104e9f645c37793f9da99 to your computer and use it in GitHub Desktop.
AVR UART
void setup(){
Serial.begin(9600);
Serial1.begin(9600);
Serial.println("Init");
pinMode(13, OUTPUT);
}
char b;
void loop(){
if ((b = Serial1.read()) > 0){
Serial.println(b);
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
}
#define F_CPU 8000000UL
#define BAUD 9600
#define MYUBRR F_CPU/16/BAUD - 1
#include <avr/io.h>
#include <util/delay.h>
void USART_Init(unsigned int ubrr)
{
UBRRH = (unsigned char) (ubrr >> 8);
UBRRL = (unsigned char) ubrr;
UCSRB = (1 << RXEN) | (1 << TXEN);
UCSRC = (1 << URSEL) | (1 << USBS) | (3 <<UCSZ0);
}
void USART_Transmit(unsigned char data){
while (!(UCSRA & (1 << UDRE))) {};
UDR = data;
}
int main(void){
USART_Init(MYUBRR);
while(1){
USART_Transmit('b');
_delay_ms(1000);
}
}
/*
* GccApplication5.c
*
* Created: 10/5/2016 1:26:23 PM
* Author : Manash
*/
#define F_CPU 16000000UL
#define BAUD 9600
#define MYUBRR F_CPU/16/BAUD - 1
#include <avr/io.h>
#include <util/delay.h>
void USART_Init(unsigned int ubrr)
{
UBRRH = (unsigned char) (ubrr >> 8);
UBRRL = (unsigned char) ubrr;
UCSRB = (1 << RXEN) | (1 << TXEN);
UCSRC = (1 << URSEL) | (1 << USBS) | (3 <<UCSZ0);
}
void USART_Transmit(unsigned char data){
while (!(UCSRA & (1 << UDRE)));
UDR = data;
}
void USART_putstring(char* StringPtr){
while(*StringPtr != 0x00){ //Here we check if there is still more chars to send, this is done checking the actual char and see if it is different from the null char
USART_Transmit(*StringPtr); //Using the simple send function we send one char at a time
StringPtr++;
} //We increment the pointer so we can read the next char
}
int main(void)
{
USART_Init(MYUBRR);
char strng[] = {'a', 'b', 'c', '\n', '\0'};
/* Replace with your application code */
while (1)
{
while (( UCSRA & (1<<UDRE)) == 0){};
int i;
for (i = 0; i < strlen(strng); i++){
while (( UCSRA & (1<<UDRE)) == 0){};
UDR = strng[i];
if (i == (strlen(strng) - 1)){
_delay_ms(1000);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment