From f729891fa914c400c0e91ad4fbafa83e67a24b98 Mon Sep 17 00:00:00 2001 From: LDprg <71488985+LDprg@users.noreply.github.com> Date: Sat, 18 Dec 2021 16:06:27 +0100 Subject: [PATCH] Add USART functions Add USART functions Add _delay_s makro --- glob_def.h | 7 +++++ shield_def.h | 6 ++-- uno_def.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++ uno_def.h | 50 +++++++++++++++++++++++++++++ 4 files changed, 148 insertions(+), 3 deletions(-) create mode 100644 uno_def.c diff --git a/glob_def.h b/glob_def.h index 7f634a4..3396286 100644 --- a/glob_def.h +++ b/glob_def.h @@ -10,11 +10,18 @@ #define GLOB_DEF_H_ #include +#include #ifndef F_CPU #define F_CPU 16000000UL // 16 MHZ #endif /* F_CPU */ +#ifndef BAUD + #define BAUD 9600UL +#endif /* BAUD */ + +#define _delay_s(__s) _delay_ms((__s)*1000) + #define BIT(x) (1 << (x)) #define _ON(x, y) (x |= BIT(y)) diff --git a/shield_def.h b/shield_def.h index 4de4339..64719b4 100644 --- a/shield_def.h +++ b/shield_def.h @@ -5,12 +5,12 @@ * Author: LD */ -#include "uno_def.h" -#include "glob_def.h" - #ifndef SHIELD_DEF_H_ #define SHIELD_DEF_H_ +#include "glob_def.h" +#include "uno_def.h" + // LEDs #define D1 P13 #define D2 P12 diff --git a/uno_def.c b/uno_def.c new file mode 100644 index 0000000..b56ca7e --- /dev/null +++ b/uno_def.c @@ -0,0 +1,88 @@ +/* + * uno_def.c + * + * Created: 18.12.2021 11:56:46 + * Author: LD + */ +#include "uno_def.h" +#include + +void usart_std_init() +{ + UBRR0 = UBRR_VALUE; + + USART_TRANSMIT_ENABLE(); + USART_RECIVE_ENABLE(); + + _ON(UCSR0C, UCSZ01); + _ON(UCSR0C, UCSZ00); +} + +void usart_init(USART_MODE _mode, USART_CHAR_SIZE _charsize, USART_STOP_BIT _stopbit, USART_PARITY _parity) +{ + UBRR0 = UBRR_VALUE; + + USART_TRANSMIT_ENABLE(); + USART_RECIVE_ENABLE(); + + _SET(_mode,UCSR0C, UMSEL00); + _SET(_stopbit,UCSR0C, USBS0); + + switch(_charsize) + { + default: + case BIT5: + break; + case BIT6: + _ON(UCSR0C, UCSZ00); + break; + case BIT9: + _ON(UCSR0C, UCSZ02); + case BIT8: + _ON(UCSR0C, UCSZ00); + case BIT7: + _ON(UCSR0C, UCSZ01); + break; + } + + switch(_parity) + { + default: + case DISABLED: + break; + case EVEN: + _ON(UCSR0C, UPM00); + case ODD: + _ON(UCSR0C, UPM01); + break; + } + + _ON(UCSR0C, UCSZ01); + _ON(UCSR0C, UCSZ00); +} + +char usart_getc() +{ + while(USART_RECIVE_COMPLETE); + return UDR0; +} + +char usart_getc_ifready() +{ + if(!USART_RECIVE_COMPLETE) + return UDR0; + else + return 0; +} + +void usart_setc(char c) +{ + while(USART_READY); + UDR0 = c; +} + +void usart_setc_ifready(char c) +{ + if(!USART_READY) + UDR0 = c; +} diff --git a/uno_def.h b/uno_def.h index ddb3ef9..3bbc730 100644 --- a/uno_def.h +++ b/uno_def.h @@ -9,6 +9,9 @@ #ifndef UNO_DEF_H_ #define UNO_DEF_H_ +#include "glob_def.h" +#include + // LED #define LED P13 #define _LED _P13 @@ -107,4 +110,51 @@ #define _P1_ DDRD #define _P0_ DDRD +#define USART_READY _GET(UCSR0A, UDRE0) +#define USART_RECIVE_COMPLETE _GET(UCSR0A, RXC0) +#define USART_RECIVE_ENABLE() _ON(UCSR0B, RXEN0) +#define USART_RECIVE_DISABLE() _OFF(UCSR0B, RXEN0) +#define USART_TRANSMIT_COMPLETE _GET(UCSR0A, TXC0) +#define USART_TRANSMIT_ENABLE() _ON(UCSR0B, TXEN0) +#define USART_TRANSMIT_DISABLE() _OFF(UCSR0B, TXEN0) + +#define USART_ENABLE_INT_RX() _ON(UCSR0B, RXCIE0) +#define USART_ENABLE_INT_TX() _ON(UCSR0B, TXCIE0) +#define USART_ENABLE_INT_UDR() _ON(UCSR0B, UDRIE0) + +typedef enum +{ + DISABLED, + EVEN, + ODD +} USART_PARITY; + +typedef enum +{ + ONE = 0, + TWO = 1 +} USART_STOP_BIT; + +typedef enum +{ + ASYNC = 0, + SYNC = 1 +} USART_MODE; + +typedef enum +{ + BIT5, + BIT6, + BIT7, + BIT8, + BIT9 +} USART_CHAR_SIZE; + +void usart_std_init(); +void usart_init(USART_MODE _mode, USART_CHAR_SIZE _charsize, USART_STOP_BIT _stopbit, USART_PARITY _parity); +char usart_getc(); +char usart_getc_ifready(); +void usart_setc(char c); +void usart_setc_ifready(char c); + #endif /* UNO_DEF_H_ */ \ No newline at end of file