diff --git a/EmbeddedSystemsTHM/CLI.c b/EmbeddedSystemsTHM/CLI.c index 57e22e6..14ce601 100644 --- a/EmbeddedSystemsTHM/CLI.c +++ b/EmbeddedSystemsTHM/CLI.c @@ -10,6 +10,8 @@ #include "Taster.h" #include "Timer.h" #include "Led.h" +#include "adc.h" +#include "rotaryEncoder.h" //================================================================================================================================ @@ -17,8 +19,6 @@ //================================================================================================================================ #define COUNT_COMMANDS ( sizeof(command) / sizeof(command[0]) ) - - #define CHAR_DEL 0x7F #define CHAR_BACKSPACE 0x08 #define CHAR_CR 0x0D @@ -52,6 +52,8 @@ static void cmd_taster(const char* _data); static void cmd_led_on(const char* _data); static void cmd_led_off(const char* _data); static void cmd_alert(const char* _data); +static void cmd_adc(const char* _data); +static void cmd_rotary(const char* _data); //================================================================================================================================ // command-Tabelle @@ -65,6 +67,8 @@ const struct { {"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"}, + {"ADC", &cmd_adc, "Read the ADC output"}, + {"ROTARY", &cmd_rotary, "Read the rotary encoder"}, {"ALERT", &cmd_alert, "RING THE ALTER!!!"}, }; @@ -239,12 +243,16 @@ static void cmd_help(const char* _data){ // Rückgabewert: keine //================================================================================================================================ static void cmd_taster(const char* _data){ + print_string("Taster 1: "); print_string(Taster1_get() ? "1" : "0"); print_string(NEXT_LINE); + print_string("Taster 2: "); print_string(Taster2_get() ? "1" : "0"); print_string(NEXT_LINE); + print_string("Taster 3: "); print_string(Taster3_get() ? "1" : "0"); print_string(NEXT_LINE); + print_string("Taster 4: "); print_string(Taster4_get() ? "1" : "0"); print_string(NEXT_LINE); } @@ -329,3 +337,68 @@ static void cmd_alert(const char* _data){ } +//================================================================================================================================ +// Funktion: cmd_adc +// Wird verwendet um die Daten von Poti und LM35 zu lesen +// Parameter: _data: Zeichenkette mit Parametern des Befehls. +// +// Rückgabewert: keine +//================================================================================================================================ +static void cmd_adc(const char* _data){ + char potiData[50]; + char lm35Data[50]; + int loopInterval = 500; + + int repeat = strcmp(_data, "R") == 0 ? 1 : 0; /* Check if repeat flag is set*/ + + do + { + if(Timer_getTick() - g_startMS >= loopInterval){ + g_startMS=Timer_getTick(); + + // Print Potentiometer info + sprintf(potiData, "Poti: %d", adc_get_poti()); + //sprintf(potiData, "Poti: %f", (adc_get_poti() / 1023.0)); // uses floats - disabled for performance reasons. + print_string(potiData); + print_string(NEXT_LINE); + + // Print LM35 temperature + sprintf(lm35Data, "LM35: %d\xC2\xB0 C", (int)(adc_get_LM35() * (5000 / 1024) / 10)); + //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); + + + print_string("----------"); + print_string(NEXT_LINE); + } + } while (repeat); // repeat if repeat-flag is given + + +} + +//================================================================================================================================ +// Funktion: cmd_rotary +// Wird verwendet um die Daten von Poti und LM35 zu lesen +// Parameter: _data: Zeichenkette mit Parametern des Befehls. +// +// Rückgabewert: keine +//================================================================================================================================ +static void cmd_rotary(const char* _data){ + char encoderText[50]; + int16_t tmp=-32768, cnt=0; + + do + { + cnt = drehgeber_get(); + if(cnt != tmp) + { + sprintf(encoderText, "Rotary Encoder: %d", drehgeber_get()); + print_string(encoderText); + print_string(NEXT_LINE); + + tmp = cnt; + } + + } while (strcmp(_data, "R") == 0 ? 1 : 0); /* Check if repeat flag is set*/ +} \ No newline at end of file diff --git a/EmbeddedSystemsTHM/EmbeddedSystemsTHM.cproj b/EmbeddedSystemsTHM/EmbeddedSystemsTHM.cproj index aece552..ae7834f 100644 --- a/EmbeddedSystemsTHM/EmbeddedSystemsTHM.cproj +++ b/EmbeddedSystemsTHM/EmbeddedSystemsTHM.cproj @@ -112,45 +112,51 @@ - -mmcu=atmega1284p -B "%24(PackRepoDir)\atmel\ATmega_DFP\1.6.364\gcc\dev\atmega1284p" - True - True - True - True - True - False - True - True - - - DEBUG - - - - - %24(PackRepoDir)\atmel\ATmega_DFP\1.6.364\include\ - - - Optimize debugging experience (-Og) - True - True - Default (-g2) - True - - - libm - - - - - %24(PackRepoDir)\atmel\ATmega_DFP\1.6.364\include\ - - - Default (-Wa,-g) - + -mmcu=atmega1284p -B "%24(PackRepoDir)\atmel\ATmega_DFP\1.6.364\gcc\dev\atmega1284p" + True + True + True + True + True + False + True + True + + + DEBUG + + + + + %24(PackRepoDir)\atmel\ATmega_DFP\1.6.364\include\ + + + Optimize debugging experience (-Og) + True + True + Default (-g2) + True + + + libm + + + + + %24(PackRepoDir)\atmel\ATmega_DFP\1.6.364\include\ + + + Default (-Wa,-g) + + + compile + + + compile + compile @@ -166,6 +172,12 @@ compile + + compile + + + compile + compile diff --git a/EmbeddedSystemsTHM/Timer.c b/EmbeddedSystemsTHM/Timer.c index 42689e6..941f81d 100644 --- a/EmbeddedSystemsTHM/Timer.c +++ b/EmbeddedSystemsTHM/Timer.c @@ -12,14 +12,14 @@ volatile uint16_t cntr = 0; void Timer_init (void){ - TCCR0A |= (1<<1); /* Set WGM01 to high to enable CTC mode */ - TCCR0B |= (11<<0); /* Set the clock select bit to pre-scaler 3 + TCCR0A |= (1< 250KHz */ - TIMSK0 |= (1<<1); /* Set OCIE0A to high to rise an interrupt when the counter matches OCR0A */ + TIMSK0 |= (1< 1KHz (1ms) */ + OCR0A = 250 - 1; /* Set the Output Compare Register 0 A to 125 to trigger interrupt every 1ms + 16MHz / 64 (pre-scaler 3) / (250 -1 ) ==> 1KHz (1ms) */ } diff --git a/EmbeddedSystemsTHM/UART.c b/EmbeddedSystemsTHM/UART.c index bf22af6..f4bc6b3 100644 --- a/EmbeddedSystemsTHM/UART.c +++ b/EmbeddedSystemsTHM/UART.c @@ -21,7 +21,6 @@ volatile uint8_t TxActive; void uart_init(void) { - UBRR0 = 103; /* set BAUD rate to 9600 */ UCSR0C |= diff --git a/EmbeddedSystemsTHM/adc.c b/EmbeddedSystemsTHM/adc.c new file mode 100644 index 0000000..e3499d8 --- /dev/null +++ b/EmbeddedSystemsTHM/adc.c @@ -0,0 +1,74 @@ +/* + * adc.c + * + * Created: 09/12/2021 16:31:16 + * Author: n0x + */ + +#include "adc.h" + +void +adc_init(void) +{ + ADMUX = (1< +#include + +#ifndef ADC_H_ +#define ADC_H_ + +void adc_init(void); + +uint16_t adc_get_poti(void); +uint16_t adc_get_LM35(void); + +#endif /* ADC_H_ */ \ No newline at end of file diff --git a/EmbeddedSystemsTHM/main.c b/EmbeddedSystemsTHM/main.c index 1da0759..4fb1f88 100644 --- a/EmbeddedSystemsTHM/main.c +++ b/EmbeddedSystemsTHM/main.c @@ -10,12 +10,16 @@ #include "Timer.h" #include "Tasks.h" #include "StopLight.h" -#include "UART.h" -#include "CLI.h" +#include "uart.h" +#include "cli.h" +#include "adc.h" +#include "rotaryEncoder.h" #include #define F_CPU 1000000 #include +#include + /* function declarations */ void buttonTest (void); @@ -31,32 +35,18 @@ main (void) Taster_init(); Led_init(); uart_init(); + adc_init(); + + drehgeber_init(); line_interpreter_init(uart_send_byte); - uart_send_string("Welcome to the ATmega1284P CLI. Type HELP to display all options.\r\n"); - + uart_send_string("Welcome to the ATmega1284P CLI. Type HELP to display all options.\r\n"); + while (1) { - /* UART Standalone Test */ - /* - if(uart_data_available()){ - char data[500]; - data[0] = uart_get_data(); - uart_send_isr(data); - } - */ - if(uart_data_available()){ line_interpreter_get_data(uart_get_data()); } - - //buttonTest(); - //runningLight(); - //blinkLedWithTimer(); - //runStopLightTask1(); - //runStopLightTask2(); - //uart_send ("uart_send: Hello World\n"); - } } diff --git a/EmbeddedSystemsTHM/rotaryEncoder.c b/EmbeddedSystemsTHM/rotaryEncoder.c new file mode 100644 index 0000000..bdef5fa --- /dev/null +++ b/EmbeddedSystemsTHM/rotaryEncoder.c @@ -0,0 +1,148 @@ +/* + * rotaryEncoder.c + * + * Created: 16/12/2021 16:34:05 + * Author: n0x + */ + +#include "rotaryEncoder.h" + +#define ROTA PORTC7 +#define ROTB PORTC6 +#define ROTBUTTON PORTC5 + +/* enums */ +enum{ + S2, S3, S4, S5, S6, S7, + INIT, WAIT + } RotaryState = INIT; /* Emus for the stoplight task (task 3) */ + +int16_t count; + +void +drehgeber_init(void) +{ + DDRC &= ~(7< +#include + +#ifndef ROTARYENCODER_H_ +#define ROTARYENCODER_H_ + +void drehgeber_init(void); +void drehgeber_process(void); +int16_t drehgeber_get(void); + + +#endif /* ROTARYENCODER_H_ */ \ No newline at end of file