Added small changes to UART and ADC

This commit is contained in:
_N0x 2022-01-20 17:22:50 +01:00
parent a2203b7cd5
commit dece559b52
6 changed files with 73 additions and 63 deletions

View File

@ -63,6 +63,7 @@ blinkLedWithTimer (void)
if(Timer_getTick() - g_startMS >= 1000) { /* Wait 1000ms before switching LED1 */
g_startMS=Timer_getTick();
/* Flip-Flop LED to on/off */
if(g_ledStatus % 2 == 0){
Led1_On();
} else {

View File

@ -20,16 +20,23 @@ Taster_init (void){
uint8_t
Taster1_get (void){
/* Check if pin is low
-> low active pin
-> button is pressed
-> return 1 */
return ((PIND & (1<<PIND7)) == 0);
}
uint8_t
Taster2_get (void){
return ((PIND & (1<<PIND6)) == 0);
}
uint8_t
Taster3_get (void){
return ((PIND & (1<<PIND5)) == 0);
}
uint8_t
Taster4_get (void){
return ((PINC & (1<<PINC2)) == 0);

View File

@ -6,16 +6,11 @@
*/
#include "UART.h"
CircularBuffer TxBuffer;
CircularBuffer* pTxBuffer = &TxBuffer;
CircularBuffer RxBuffer;
CircularBuffer* pRxBuffer = &RxBuffer;
#define SIZE_BUFFER 500
struct CircularBuffer{
volatile char data[SIZE_BUFFER];
volatile uint16_t Readpointer;
volatile uint16_t Writepointer;
};
struct CircularBuffer TxBuffer;
struct CircularBuffer RxBuffer;
volatile uint8_t TxActive;
void
@ -34,10 +29,10 @@ uart_init(void)
(1<<TXEN0)|(1<<RXEN0)| /* enable send and receive */
(1<<RXCIE0); /* enable Receive interrupts */
TxBuffer.Readpointer = 0;
TxBuffer.Writepointer = 0;
RxBuffer.Readpointer = 0;
RxBuffer.Writepointer = 0;
pTxBuffer->Readpointer = 0;
pTxBuffer->Writepointer = 0;
pRxBuffer->Readpointer = 0;
pRxBuffer->Writepointer = 0;
}
/* ------------------------ */
@ -57,28 +52,30 @@ uart_send_string(char* string)
void
uart_send_byte(char c)
{
UCSR0B&=~(1<<TXCIE0);
/* Disable the TX Interrupt */
UCSR0B &= ~(1<<TXCIE0);
if(TxActive){
TxBuffer.data[TxBuffer.Writepointer++] = c;
if (TxBuffer.Writepointer>=SIZE_BUFFER){
TxBuffer.Writepointer = 0;
pTxBuffer->data[pTxBuffer->Writepointer++] = c;
if (pTxBuffer->Writepointer>=SIZE_BUFFER){
pTxBuffer->Writepointer = 0;
}
} else {
TxActive = 1;
UDR0 = c;
}
/* Enable the TX Interrupt again*/
UCSR0B |= (1<<TXCIE0);
}
ISR(USART0_TX_vect)
{
if(TxBuffer.Readpointer != TxBuffer.Writepointer)
if(pTxBuffer->Readpointer != pTxBuffer->Writepointer)
{
UDR0 = TxBuffer.data[TxBuffer.Readpointer++];
UDR0 = pTxBuffer->data[pTxBuffer->Readpointer++];
if(TxBuffer.Readpointer >= SIZE_BUFFER){
TxBuffer.Readpointer = 0;
if(pTxBuffer->Readpointer >= SIZE_BUFFER){
pTxBuffer->Readpointer = 0;
}
} else {
TxActive = 0;
@ -94,7 +91,7 @@ uart_data_available(void)
{
uint8_t dataAvailabel = 0;
UCSR0B &= ~(1<<RXCIE0);
if(RxBuffer.Readpointer != RxBuffer.Writepointer){
if(pRxBuffer->Readpointer != pRxBuffer->Writepointer){
dataAvailabel = 1;
}
UCSR0B |= (1<<RXCIE0);
@ -106,11 +103,11 @@ uart_get_data(void)
{
char data = 0;
UCSR0B &= ~(1<<RXCIE0);
if(RxBuffer.Readpointer != RxBuffer.Writepointer)
if(pRxBuffer->Readpointer != pRxBuffer->Writepointer)
{
data = RxBuffer.data[RxBuffer.Readpointer++];
if(RxBuffer.Readpointer >= SIZE_BUFFER){
RxBuffer.Readpointer = 0;
data = pRxBuffer->data[pRxBuffer->Readpointer++];
if(pRxBuffer->Readpointer >= SIZE_BUFFER){
pRxBuffer->Readpointer = 0;
}
}
UCSR0B |= (1<<RXCIE0);
@ -123,9 +120,9 @@ ISR(USART0_RX_vect)
uint8_t data = UDR0;
if((status & ((1<<DOR0) | (1>>FE0))) == 0){
RxBuffer.data[RxBuffer.Writepointer++] = data;
if(RxBuffer.Writepointer >= SIZE_BUFFER) {
RxBuffer.Writepointer = 0;
pRxBuffer->data[pRxBuffer->Writepointer++] = data;
if(pRxBuffer->Writepointer >= SIZE_BUFFER) {
pRxBuffer->Writepointer = 0;
}
}
}

View File

@ -11,6 +11,15 @@
#ifndef UART2_H_
#define UART2_H_
#define SIZE_BUFFER 500
typedef struct CircularBuffer CircularBuffer;
struct CircularBuffer{
volatile char data[SIZE_BUFFER];
volatile uint16_t Readpointer;
volatile uint16_t Writepointer;
};
void uart_init(void);
void uart_send_byte(char c);
void uart_send_string(char* string);

View File

@ -7,36 +7,6 @@
#include "adc.h"
typedef struct ADC_t ADC_t;
struct ADC_t{
/* ADC Data */
uint16_t uiADC :16;
/* ADCSRA */
uint8_t uiADPS0 :1;
uint8_t uiADPS1 :1;
uint8_t uiADPS2 :1;
uint8_t uiADIE :1;
uint8_t uiADIF :1;
uint8_t uiADATE :1;
uint8_t uiADSC :1;
uint8_t uiADEN :1;
/* ADCSRB */
uint8_t uiADTS0 :1;
uint8_t uiADTS1 :1;
uint8_t uiADTS2 :1;
uint8_t nui2 :3;
uint8_t uiACME :1;
uint8_t nui1 :1;
/* ADMUX */
uint8_t uiMUX :5;
uint8_t uiADLAR :1;
uint8_t uiREFS0 :1;
uint8_t uiREFS1 :1;
};
#define myADC ((ADC_t*)(0x78))
void
@ -46,9 +16,7 @@ adc_init(void)
myADC->uiREFS0 = 0; /* Set ADLAR to 0 to not left adjust the presentation of the conversion result */
myADC->uiREFS1 = 0; /* Set Voltage reference to 2.56V */
myADC->uiADPS0 = 1; /* Set ADC Prescaler to 128 */
myADC->uiADPS1 = 1;
myADC->uiADPS2 = 1;
myADC->uiADPS = 7; /* Set ADC Prescaler to 128 */
myADC->uiADIE = 1; /* Enable the ADC interrupt */
myADC->uiADATE = 0; /* Disable the ADC auto trigger */
myADC->uiADEN = 1; /* Enable ADC */

View File

@ -11,6 +11,34 @@
#ifndef ADC_H_
#define ADC_H_
typedef struct ADC_t ADC_t;
struct ADC_t{
/* ADC Data */
uint16_t uiADC :16;
/* ADCSRA */
uint8_t uiADPS :3;
uint8_t uiADIE :1;
uint8_t uiADIF :1;
uint8_t uiADATE :1;
uint8_t uiADSC :1;
uint8_t uiADEN :1;
/* ADCSRB */
uint8_t uiADTS0 :1;
uint8_t uiADTS1 :1;
uint8_t uiADTS2 :1;
uint8_t nui2 :3;
uint8_t uiACME :1;
uint8_t nui1 :1;
/* ADMUX */
uint8_t uiMUX :5;
uint8_t uiADLAR :1;
uint8_t uiREFS0 :1;
uint8_t uiREFS1 :1;
};
void adc_init(void);
uint16_t adc_get_poti(void);