diff --git a/EmbeddedSystemsTHM/CLI.c b/EmbeddedSystemsTHM/CLI.c index ed8774f..48c5549 100644 --- a/EmbeddedSystemsTHM/CLI.c +++ b/EmbeddedSystemsTHM/CLI.c @@ -26,7 +26,7 @@ #define CHAR_ESC 0x1B #define NEXT_LINE (char*)"\r\n" -#define CURSOR (char*)">>" +#define CURSOR (char*)">> " //================================================================================================================================ // Lokale Variablen @@ -65,8 +65,8 @@ const struct { } command[] PROGMEM = { {"HELP", &cmd_help, "Display this Help Menu"}, {"BUTTON", &cmd_taster, "Current State of the Buttons"}, - {"LED_ON $LED", &cmd_led_on, "Turn on the LED on position $LED"}, - {"LED_OFF $LED", &cmd_led_off, "Turn off the LED on position $LED"}, + {"LED_ON", &cmd_led_on, "Turn on the LED on position $LED"}, + {"LED_OFF", &cmd_led_off, "Turn off the LED on position $LED"}, {"ADC", &cmd_adc, "Read the ADC output"}, {"ROTARY", &cmd_rotary, "Read the rotary encoder"}, {"ALERT", &cmd_alert, "RING THE ALTER!!!"}, @@ -178,7 +178,7 @@ static char* get_next(char *_data){ // Rückgabewert: Das eingegebene Zeichen als Großbuchstabe. //================================================================================================================================ static char to_uppercase (char _char){ - if ( (_char>='a') && (_char<='x') ){ + if ( (_char>='a') && (_char<='z') ){ _char -= 'a' - 'A'; } return _char; @@ -192,9 +192,6 @@ static char to_uppercase (char _char){ // Rückgabewert: keine //================================================================================================================================ static void print_char (char _data){ - - - if (callback_function != NULL){ callback_function(_data); } @@ -349,6 +346,7 @@ static void cmd_adc(const char* _data){ char potiData[50]; char lm35Data[50]; int loopInterval = 500; + int lm35; do { @@ -361,8 +359,9 @@ static void cmd_adc(const char* _data){ print_string(potiData); print_string(NEXT_LINE); - // Print LM35 temperature - sprintf(lm35Data, "LM35: %d\xC2\xB0 C", (int)(adc_get_LM35() * (5000 / 1024) / 10)); + // Print LM35 temperature + lm35 = (int)(adc_get_LM35() * 5000.0 / 1024 / 10); + sprintf(lm35Data, "LM35: %d\xC2\xB0 C", lm35); //sprintf(lm35Data, "LM35: %f\xC2\xB0 C", (adc_get_LM35() * (5000 / 1024.0) / 10)); // uses floats - disabled for performance reasons. print_string(lm35Data); print_string(NEXT_LINE); diff --git a/EmbeddedSystemsTHM/Timer.c b/EmbeddedSystemsTHM/Timer.c index 941f81d..ffbd2ab 100644 --- a/EmbeddedSystemsTHM/Timer.c +++ b/EmbeddedSystemsTHM/Timer.c @@ -19,7 +19,13 @@ Timer_init (void){ TIMSK0 |= (1< 1KHz (1ms) */ + 16MHz / 64 (pre-scaler 3) / (250 ) ==> 1KHz (1ms) */ + + /* + For internal 1MHz clock: + 1MHz / 8 (pre-scaler 2) ==> 125 KHz + 1MHz / 8 (pre-scaler 2) / 125 (OCR) ==> 1 KHz (1ms) + */ } diff --git a/EmbeddedSystemsTHM/UART.c b/EmbeddedSystemsTHM/UART.c index 7b86323..1065314 100644 --- a/EmbeddedSystemsTHM/UART.c +++ b/EmbeddedSystemsTHM/UART.c @@ -6,6 +6,8 @@ */ #include "UART.h" + +/* create structs for TX and TX buffer */ CircularBuffer TxBuffer; CircularBuffer* pTxBuffer = &TxBuffer; CircularBuffer RxBuffer; @@ -14,8 +16,7 @@ CircularBuffer* pRxBuffer = &RxBuffer; volatile uint8_t TxActive; void -uart_init(void) -{ +uart_init(void) { UBRR0 = 103; /* set BAUD rate to 9600 */ UCSR0C |= @@ -24,24 +25,23 @@ uart_init(void) (1<Readpointer = 0; pTxBuffer->Writepointer = 0; pRxBuffer->Readpointer = 0; pRxBuffer->Writepointer = 0; } -/* ------------------------ */ -/* Sending Data */ -/* ------------------------ */ +/********Sending Data********/ void -uart_send_string(char* string) -{ +uart_send_string(char* string) { + /* Take a whole string and transmit every char from it */ int i = 0; while(string[i] != '\0'){ uart_send_byte(string[i]); @@ -50,11 +50,14 @@ uart_send_string(char* string) } void -uart_send_byte(char c) -{ - /* Disable the TX Interrupt */ +uart_send_byte(char c) { + /* Disable the TX Interrupt to prevent issues while writing to the send buffer*/ UCSR0B &= ~(1< write the byte to the buffer + if no -> write the byte to the UDR0 register and enable the transmit in progress flag + */ if(TxActive){ pTxBuffer->data[pTxBuffer->Writepointer++] = c; if (pTxBuffer->Writepointer>=SIZE_BUFFER){ @@ -68,10 +71,14 @@ uart_send_byte(char c) UCSR0B |= (1< read the next byte and write it to UDR0 register + if aligned -> no more data to send, disable the send in progress flag */ if(pTxBuffer->Readpointer != pTxBuffer->Writepointer) { + /* writing data to the register withing the interrupt will cause a new interrupt which then will repeat till no more data is to be read from the send buffer */ UDR0 = pTxBuffer->data[pTxBuffer->Readpointer++]; if(pTxBuffer->Readpointer >= SIZE_BUFFER){ @@ -82,44 +89,54 @@ ISR(USART0_TX_vect) } } -/* ------------------------ */ -/* Receiving Data */ -/* ------------------------ */ - +/*******Receiving Data*******/ uint8_t -uart_data_available(void) -{ +uart_data_available(void) { uint8_t dataAvailabel = 0; + /* Disable the RX Interrupt to prevent issues when checking if data is available */ UCSR0B &= ~(1< data is available and can be read */ if(pRxBuffer->Readpointer != pRxBuffer->Writepointer){ dataAvailabel = 1; } + + /* Enable the RX Interrupt again*/ UCSR0B |= (1< data is available and can be read */ if(pRxBuffer->Readpointer != pRxBuffer->Writepointer) { + /* read a char from the buffer and increment the readpointer */ data = pRxBuffer->data[pRxBuffer->Readpointer++]; if(pRxBuffer->Readpointer >= SIZE_BUFFER){ pRxBuffer->Readpointer = 0; } } + /* Enable the RX Interrupt again*/ UCSR0B |= (1<>FE0))) == 0){ + /* check status flags if data is correctly accessible */ + if((status & ((1<data[pRxBuffer->Writepointer++] = data; if(pRxBuffer->Writepointer >= SIZE_BUFFER) { pRxBuffer->Writepointer = 0; diff --git a/EmbeddedSystemsTHM/adc.c b/EmbeddedSystemsTHM/adc.c index 01620fe..4cffa2e 100644 --- a/EmbeddedSystemsTHM/adc.c +++ b/EmbeddedSystemsTHM/adc.c @@ -12,9 +12,8 @@ void adc_init(void) { - myADC->uiADLAR = 0; - 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->uiADLAR = 0; /* Set ADLAR to 0 to not left adjust the presentation of the conversion result */ + myADC->uiREFS = 0; /* Turn off reference Voltage */ myADC->uiADPS = 7; /* Set ADC Prescaler to 128 */ myADC->uiADIE = 1; /* Enable the ADC interrupt */ @@ -30,8 +29,6 @@ adc_get_poti(void) { uint16_t adc; - //myADC->uiADIE = 0; /* Disable interrupt */ - myADC->uiMUX = 1; /* Set ADMUX to access ADC channel 1 */ myADC->uiADSC = 1; /* Start the conversion */ @@ -41,8 +38,6 @@ adc_get_poti(void) adc = myADC->uiADC; - //myADC->uiADIE = 1; /* Enable the interrupt again */ - return adc; } diff --git a/EmbeddedSystemsTHM/adc.h b/EmbeddedSystemsTHM/adc.h index 180109d..acda777 100644 --- a/EmbeddedSystemsTHM/adc.h +++ b/EmbeddedSystemsTHM/adc.h @@ -35,8 +35,7 @@ struct ADC_t{ /* ADMUX */ uint8_t uiMUX :5; uint8_t uiADLAR :1; - uint8_t uiREFS0 :1; - uint8_t uiREFS1 :1; + uint8_t uiREFS :2; }; void adc_init(void); diff --git a/EmbeddedSystemsTHM/main.c b/EmbeddedSystemsTHM/main.c index 4fb1f88..793f820 100644 --- a/EmbeddedSystemsTHM/main.c +++ b/EmbeddedSystemsTHM/main.c @@ -34,6 +34,9 @@ main (void) Timer_init(); Taster_init(); Led_init(); + initTasks(); + + uart_init(); adc_init(); @@ -44,6 +47,8 @@ main (void) while (1) { + //blinkLedWithTimer(); + if(uart_data_available()){ line_interpreter_get_data(uart_get_data()); } diff --git a/EmbeddedSystemsTHM/rotaryEncoder.c b/EmbeddedSystemsTHM/rotaryEncoder.c index 6e9645f..f6dcbed 100644 --- a/EmbeddedSystemsTHM/rotaryEncoder.c +++ b/EmbeddedSystemsTHM/rotaryEncoder.c @@ -7,8 +7,8 @@ #include "rotaryEncoder.h" -#define ROTA PORTC7 -#define ROTB PORTC6 +#define ROTA PORTC7 /* define Rotary Encoder Button A */ +#define ROTB PORTC6 /* define Rotary Encoder Button B */ #define ROTBUTTON PORTC5 /* enums */ @@ -38,96 +38,99 @@ ISR(PCINT2_vect) void drehgeber_process(void) { - uint8_t a,b, enc; + /* calculate the value of Button A and B of rotary encoder */ + uint8_t a, b, enc; a = ((PINC & (1<