From dece559b529e7cfa1aef953a0dfda4da3498546d Mon Sep 17 00:00:00 2001 From: _N0x Date: Thu, 20 Jan 2022 17:22:50 +0100 Subject: [PATCH] Added small changes to UART and ADC --- EmbeddedSystemsTHM/Tasks.c | 3 +- EmbeddedSystemsTHM/Taster.c | 7 +++++ EmbeddedSystemsTHM/UART.c | 55 ++++++++++++++++++------------------- EmbeddedSystemsTHM/UART.h | 9 ++++++ EmbeddedSystemsTHM/adc.c | 34 +---------------------- EmbeddedSystemsTHM/adc.h | 28 +++++++++++++++++++ 6 files changed, 73 insertions(+), 63 deletions(-) diff --git a/EmbeddedSystemsTHM/Tasks.c b/EmbeddedSystemsTHM/Tasks.c index d917864..67244f3 100644 --- a/EmbeddedSystemsTHM/Tasks.c +++ b/EmbeddedSystemsTHM/Tasks.c @@ -63,9 +63,10 @@ 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 { + } else { Led1_Off(); } g_ledStatus++; diff --git a/EmbeddedSystemsTHM/Taster.c b/EmbeddedSystemsTHM/Taster.c index 80aad83..a48740e 100644 --- a/EmbeddedSystemsTHM/Taster.c +++ b/EmbeddedSystemsTHM/Taster.c @@ -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<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<=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<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<Readpointer != pRxBuffer->Writepointer){ dataAvailabel = 1; } UCSR0B |= (1<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<>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; } } } \ No newline at end of file diff --git a/EmbeddedSystemsTHM/UART.h b/EmbeddedSystemsTHM/UART.h index 195815d..293da20 100644 --- a/EmbeddedSystemsTHM/UART.h +++ b/EmbeddedSystemsTHM/UART.h @@ -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); diff --git a/EmbeddedSystemsTHM/adc.c b/EmbeddedSystemsTHM/adc.c index a0c6e34..01620fe 100644 --- a/EmbeddedSystemsTHM/adc.c +++ b/EmbeddedSystemsTHM/adc.c @@ -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 */ diff --git a/EmbeddedSystemsTHM/adc.h b/EmbeddedSystemsTHM/adc.h index f94b60f..180109d 100644 --- a/EmbeddedSystemsTHM/adc.h +++ b/EmbeddedSystemsTHM/adc.h @@ -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);