Move UART implementation to seperate module
This commit is contained in:
1
src/common.c
Normal file
1
src/common.c
Normal file
@@ -0,0 +1 @@
|
||||
void *TODO;
|
||||
9
src/common.h
Normal file
9
src/common.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#ifndef MAD_COMMON_H
|
||||
#define MAD_COMMON_H
|
||||
|
||||
#define UNUSED(s) (void)(s)
|
||||
|
||||
#include <util/delay.h>
|
||||
#define Sleep(ms) _delay_ms(ms)
|
||||
|
||||
#endif // MAD_COMMON_H
|
||||
50
src/main.c
50
src/main.c
@@ -1,50 +1,22 @@
|
||||
#include <avr/io.h>
|
||||
#include <util/delay.h>
|
||||
#include <stdlib.h>
|
||||
#include "common.h"
|
||||
#include "uart.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
|
||||
|
||||
void UART_Init(long USART_BAUDRATE)
|
||||
{
|
||||
UCSRB |= (1 << RXEN) | (1 << TXEN);
|
||||
UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1);
|
||||
UBRRL = BAUD_PRESCALE;
|
||||
UBRRH = (BAUD_PRESCALE >> 8);
|
||||
}
|
||||
|
||||
unsigned char UART_RxChar()
|
||||
{
|
||||
while ((UCSRA & (1 << RXC)) == 0);
|
||||
return UDR;
|
||||
}
|
||||
|
||||
void UART_TxChar(char ch)
|
||||
{
|
||||
while ((UCSRA & (1 << UDRE)) == 0);
|
||||
UDR = ch;
|
||||
}
|
||||
|
||||
void UART_SendString(char *str)
|
||||
{
|
||||
unsigned char j = 0;
|
||||
|
||||
while (str[j] != 0) {
|
||||
UART_TxChar(str[j++]);
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char buf[64];
|
||||
unsigned long i;
|
||||
char buf[128];
|
||||
|
||||
UART_Init(9600);
|
||||
UART_Init();
|
||||
|
||||
for (unsigned long i = 1;; i++) {
|
||||
for (i = 1;; i++) {
|
||||
if (i >= 99999) i = 1;
|
||||
|
||||
sprintf(buf, "[CORE] Fetching sensors #%05lu...\r\n", i);
|
||||
UART_SendString(buf);
|
||||
_delay_ms(3000);
|
||||
UART_Write(buf);
|
||||
|
||||
Sleep(3000);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
47
src/uart.c
Normal file
47
src/uart.c
Normal file
@@ -0,0 +1,47 @@
|
||||
#include "common.h"
|
||||
#include <avr/io.h>
|
||||
|
||||
#define BAUD_RATE 9600
|
||||
#define BAUD_PRESCALE (((F_CPU / (BAUD_RATE * 16UL))) - 1)
|
||||
|
||||
static char UART_Rx(void);
|
||||
static void UART_Tx(char ch);
|
||||
|
||||
void UART_Init(void)
|
||||
{
|
||||
UCSRB |= (1 << RXEN) | (1 << TXEN);
|
||||
UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1);
|
||||
UBRRH = (BAUD_PRESCALE >> 8);
|
||||
UBRRL = BAUD_PRESCALE;
|
||||
}
|
||||
|
||||
void UART_Read(char **buf)
|
||||
{
|
||||
// do {
|
||||
// p[n++] = UART_Rx();
|
||||
// } while (p[n] != '\n');
|
||||
|
||||
UNUSED(UART_Rx);
|
||||
UNUSED(buf);
|
||||
}
|
||||
|
||||
void UART_Write(const char *buf)
|
||||
{
|
||||
int n = 0;
|
||||
|
||||
while (buf[n] != '\0') {
|
||||
UART_Tx(buf[n++]);
|
||||
}
|
||||
}
|
||||
|
||||
static char UART_Rx(void)
|
||||
{
|
||||
while ((UCSRA & (1 << RXC)) == 0);
|
||||
return UDR;
|
||||
}
|
||||
|
||||
static void UART_Tx(char ch)
|
||||
{
|
||||
while ((UCSRA & (1 << UDRE)) == 0);
|
||||
UDR = ch;
|
||||
}
|
||||
8
src/uart.h
Normal file
8
src/uart.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#ifndef MAD_UART_H
|
||||
#define MAD_UART_H
|
||||
|
||||
void UART_Init(void);
|
||||
void UART_Read(char **buf);
|
||||
void UART_Write(const char *buf);
|
||||
|
||||
#endif // MAD_UART_H
|
||||
Reference in New Issue
Block a user