Last active
February 19, 2019 13:18
-
-
Save embedded4ever/1a09cd40945248b35fbdff50cb239feb to your computer and use it in GitHub Desktop.
Atmega328p_300BaudSERIAL7E1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Description video : https://www.youtube.com/watch?v=2s4W6sOT0mU | |
typedef struct uconfig | |
{ | |
uint32_t baud; | |
uint8_t _UBRR0H; | |
uint8_t _UBRR0L; | |
uint8_t _UCSR0B; | |
uint8_t _UCSR0C; | |
} baud_registers; | |
/* | |
* How can i calculate _UBRROH and _UBRR0L. Here is the cpp script. | |
* You need to check the atmega328p datasheet to understand | |
* | |
#include <iostream> | |
#define BAUDRATE 300 | |
#define _UBRR (((16000000 / (BAUDRATE * 16UL))) - 1) | |
int main() | |
{ | |
int msb= ((_UBRR) & 0xF00); | |
int lsb = ((_UBRR) & 0xFF); | |
if(msb>256) | |
{ | |
msb= msb>>8; | |
} | |
std::cout<<"msb: " << msb; | |
std::cout<<"\n"; | |
std::cout<<"lsb : "<<lsb; | |
} | |
* | |
* | |
*/ | |
static const uconfig baud_table[]= { | |
{300,0x0D,0x04,0x18,0x24}, | |
}; | |
int uart_init(uint32_t baudRate_u32) | |
{ | |
int status=0; | |
uint8_t i=0; | |
for( i=0; i< sizeof(baud_table)/sizeof(baud_table[0]); i++) | |
{ | |
if(baud_table[i].baud == baudRate_u32) | |
{ | |
break; | |
} | |
} | |
if( i < sizeof(baud_table)/sizeof(baud_table[0])) { | |
UBRR0H=baud_table[i]._UBRR0H; | |
UBRR0L=baud_table[i]._UBRR0L; | |
UCSR0B=baud_table[i]._UCSR0B; | |
UCSR0C=baud_table[i]._UCSR0C; | |
status=1; | |
} | |
return status; | |
} | |
void USART_transmit( unsigned char data ) { | |
while(!(UCSR0A & (1 << UDRE0))); | |
UDR0 = data; | |
} | |
void setup() { | |
while(!uart_init(300)); | |
} | |
void loop() { | |
USART_transmit('a'); | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment