Skip to content

Commit

Permalink
Add USART functions
Browse files Browse the repository at this point in the history
Add USART functions
Add _delay_s makro
  • Loading branch information
LDprg committed Dec 18, 2021
1 parent e456547 commit f729891
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 3 deletions.
7 changes: 7 additions & 0 deletions glob_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@
#define GLOB_DEF_H_

#include <stdbool.h>
#include <stddef.h>

#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))
Expand Down
6 changes: 3 additions & 3 deletions shield_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
88 changes: 88 additions & 0 deletions uno_def.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* uno_def.c
*
* Created: 18.12.2021 11:56:46
* Author: LD
*/
#include "uno_def.h"
#include <avr/io.h>

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;
}
50 changes: 50 additions & 0 deletions uno_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#ifndef UNO_DEF_H_
#define UNO_DEF_H_

#include "glob_def.h"
#include <util/setbaud.h>

// LED
#define LED P13
#define _LED _P13
Expand Down Expand Up @@ -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_ */

0 comments on commit f729891

Please sign in to comment.