Compare commits
20 Commits
2021-10-21
...
master
Author | SHA1 | Date | |
---|---|---|---|
da447a2efa | |||
6310b91164 | |||
dece559b52 | |||
a2203b7cd5 | |||
2e6026421e | |||
4173c08c95 | |||
4021359230 | |||
4f5b174bb1 | |||
db16d89b80 | |||
61c444beda | |||
e9058086ca | |||
1720365a53 | |||
bd8f4ea63a | |||
082bc7e986 | |||
663d817b11 | |||
0eace5b6fe | |||
2bd2215ee9 | |||
578a2fd693 | |||
8cd2a63ce5 | |||
48ea8062c0 |
402
EmbeddedSystemsTHM/CLI.c
Normal file
402
EmbeddedSystemsTHM/CLI.c
Normal file
@ -0,0 +1,402 @@
|
||||
//================================================================================================================================
|
||||
// Includes
|
||||
//================================================================================================================================
|
||||
#include <avr/pgmspace.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "string.h"
|
||||
|
||||
#include "CLI.h"
|
||||
#include "Taster.h"
|
||||
#include "Timer.h"
|
||||
#include "Led.h"
|
||||
#include "adc.h"
|
||||
#include "rotaryEncoder.h"
|
||||
|
||||
|
||||
//================================================================================================================================
|
||||
// defines
|
||||
//================================================================================================================================
|
||||
#define COUNT_COMMANDS ( sizeof(command) / sizeof(command[0]) )
|
||||
|
||||
#define CHAR_DEL 0x7F
|
||||
#define CHAR_BACKSPACE 0x08
|
||||
#define CHAR_CR 0x0D
|
||||
#define CHAR_LF 0x0A
|
||||
#define CHAR_ESC 0x1B
|
||||
|
||||
#define NEXT_LINE (char*)"\r\n"
|
||||
#define CURSOR (char*)">> "
|
||||
|
||||
//================================================================================================================================
|
||||
// Lokale Variablen
|
||||
//================================================================================================================================
|
||||
uint16_t g_startMS;
|
||||
static char data[200]; // Zwischenspeicher für die Befehlsdecodierung
|
||||
static char rs232_buffer[50];
|
||||
static uint8_t pointer=0; // Zeiger zu obiger Variable
|
||||
static void (*callback_function)(char _data) = NULL; // Funktionszeiger auf eine Funktion, welche die Daten entgegen nimmt,
|
||||
// und auf der Ausgabeschnittstelle ausgibt.
|
||||
//================================================================================================================================
|
||||
// Lokale Functionen
|
||||
//================================================================================================================================
|
||||
static void run_cmd(char *_data);
|
||||
static char* get_next(char *_data);
|
||||
static char to_uppercase (char _data);
|
||||
static void print_char (char _data);
|
||||
static void print_string (char* _data);
|
||||
|
||||
// Funktionen für Befehlsdecoder
|
||||
static void cmd_help(const char* _data);
|
||||
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
|
||||
//================================================================================================================================
|
||||
const struct {
|
||||
const char cmd[20];
|
||||
void (*funktion)(const char* _data);
|
||||
const char info[40];
|
||||
} command[] PROGMEM = {
|
||||
{"HELP", &cmd_help, "Display this Help Menu"},
|
||||
{"BUTTON", &cmd_taster, "Current State of the Buttons"},
|
||||
{"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!!!"},
|
||||
};
|
||||
|
||||
//================================================================================================================================
|
||||
// Funktion: line_interpreter_init
|
||||
// Initialisierung des Line-Interpreters.
|
||||
// Parameter: _ptr: Zeiger auf die Funktion, welche die einzelne Zeichen auf der Konsole ausgeben kann.
|
||||
// Rückgabewert: keine
|
||||
//================================================================================================================================
|
||||
void line_interpreter_init(void (*_ptr)(char _data)){
|
||||
callback_function = _ptr;
|
||||
}
|
||||
|
||||
|
||||
//================================================================================================================================
|
||||
// Funktion: line_interpreter_get_data
|
||||
// Dieser Funktion werden die Daten von der RS232 Schnittstelle übergeben. In dieser Funktion werden die Empfangen
|
||||
// Zeichen zu einer Zeichenkette zusammengefügt, und wenn die ein Carryage-Return empfangen wurde, wird die
|
||||
// Zeichenkette an den Decoder übergeben.
|
||||
// Parameter: _char: Empfangenes Zeichen
|
||||
// Rückgabewert: keine
|
||||
//================================================================================================================================
|
||||
void line_interpreter_get_data(char _char){
|
||||
|
||||
switch (_char){
|
||||
case CHAR_BACKSPACE:
|
||||
case CHAR_DEL:
|
||||
if (pointer > 0){
|
||||
pointer--; // Zeichen aus Zeichenkette entfernen
|
||||
print_char (CHAR_BACKSPACE);
|
||||
print_char (' ');
|
||||
print_char (CHAR_BACKSPACE);
|
||||
}
|
||||
break;
|
||||
case CHAR_CR: // Enter wurde gedrückt,
|
||||
data[pointer] = 0; // Terminate String;
|
||||
print_string(NEXT_LINE);
|
||||
if (pointer>0){ // Wenn mindestens ein Zeichen empfangen wurde dann
|
||||
run_cmd (data); // Empfangene Zeichenkette Ausweten
|
||||
pointer = 0;
|
||||
}
|
||||
print_string (CURSOR);
|
||||
break;
|
||||
default: // Normales Zeichen empfangen.
|
||||
_char = to_uppercase(_char); // Kleinbuchstaben zu Großbuchstaben wandeln
|
||||
print_char(_char); // Empfangenes Zeichen Ausgeben
|
||||
data[pointer++] = _char; // Empfangenes Zeichen in Zwischenspeicher speichern.
|
||||
break;
|
||||
}//end switch
|
||||
}
|
||||
|
||||
//================================================================================================================================
|
||||
// Funktion: Ausfuehren
|
||||
// Diese Funktion überprüft das erste Wort in der Zeichenkette mit der Command-Tabelle und wenn es eine Über-
|
||||
// einstimmung gibt, wird die entsprechende Funktion aufgerufen.
|
||||
// Parameter: _string: Auszuwertende Zeichenkette
|
||||
// Rückgabewert: keine
|
||||
//================================================================================================================================
|
||||
static void run_cmd (char *_string){
|
||||
char *temp = 0;
|
||||
uint8_t i;
|
||||
void (*temp_func)(char* const _string);
|
||||
|
||||
temp = get_next(data);
|
||||
|
||||
for (i=0; i<COUNT_COMMANDS; i++){
|
||||
if (strcmp_P(_string, command[i].cmd)==0){
|
||||
memcpy_P(&temp_func, &command[i].funktion, sizeof (temp_func));
|
||||
temp_func(temp);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (i==COUNT_COMMANDS){
|
||||
sprintf_P (rs232_buffer, PSTR("COMMAND ERROR\r\n\r\n"));
|
||||
print_string(rs232_buffer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//================================================================================================================================
|
||||
// Funktion: get_next
|
||||
// Diese Funktion separiert den übergebenen String.
|
||||
// Parameter: _data: Auszuwertende Zeichenkette
|
||||
// Rückgabewert: keine
|
||||
//================================================================================================================================
|
||||
static char* get_next(char *_data){
|
||||
char *temp = _data;
|
||||
|
||||
while (*temp != 0){
|
||||
if (*temp==' '){
|
||||
*temp = 0;
|
||||
temp++;
|
||||
break;
|
||||
} else{
|
||||
temp++;
|
||||
}
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
||||
|
||||
//================================================================================================================================
|
||||
// Funktion: Grossbuchstaben
|
||||
// Diese Funktion wandelt Kleinbuchstaben in Großbuchstaben.
|
||||
// Parameter: _char: Auszuwertendes Zeichen
|
||||
// Rückgabewert: Das eingegebene Zeichen als Großbuchstabe.
|
||||
//================================================================================================================================
|
||||
static char to_uppercase (char _char){
|
||||
if ( (_char>='a') && (_char<='z') ){
|
||||
_char -= 'a' - 'A';
|
||||
}
|
||||
return _char;
|
||||
}
|
||||
|
||||
|
||||
//================================================================================================================================
|
||||
// Funktion: print_char
|
||||
// Ausgabe eines Zeichens auf der Konsole.
|
||||
// Parameter: _data: Zeichen, welches an die Callbackfunktion übergeben werden soll.
|
||||
// Rückgabewert: keine
|
||||
//================================================================================================================================
|
||||
static void print_char (char _data){
|
||||
if (callback_function != NULL){
|
||||
callback_function(_data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//================================================================================================================================
|
||||
// Funktion: print_string
|
||||
// Ausgabe eines Strings auf der Konsole.
|
||||
// Parameter: _data: Zeichenkette
|
||||
// Rückgabewert: keine
|
||||
//================================================================================================================================
|
||||
static void print_string (char* _data){
|
||||
for(uint8_t i=0; i<200; i++) {
|
||||
if(_data[i] == 0){
|
||||
break;
|
||||
}
|
||||
print_char(_data[i]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
//================================================================================================================================
|
||||
// Funktion: cmd_temp
|
||||
// Hilfe Funktion. Gibt die Hilfe Tabelle aus.
|
||||
// Parameter: _data: Zeichenkette mit Parametern des Befehls
|
||||
// Rückgabewert: keine
|
||||
//================================================================================================================================
|
||||
static void cmd_help(const char* _data){
|
||||
for (uint8_t i=0; i<COUNT_COMMANDS; i++){
|
||||
strcpy_P(rs232_buffer, command[i].cmd);
|
||||
print_string(rs232_buffer);
|
||||
print_string((char*)" ");
|
||||
strcpy_P(rs232_buffer, command[i].info);
|
||||
print_string(rs232_buffer);
|
||||
print_string(NEXT_LINE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//================================================================================================================================
|
||||
// Funktion: cmd_taster
|
||||
// Kann verwendet werden, um den aktuellen Status der Taster abzufragen
|
||||
// Parameter: _data: Zeichenkette mit Parametern des Befehls
|
||||
// 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);
|
||||
}
|
||||
|
||||
|
||||
//================================================================================================================================
|
||||
// Funktion: cmd_led_on
|
||||
// Kann verwendet werden, um eine LED einzuschalten
|
||||
// Parameter: _data: Zeichenkette mit Parametern des Befehls.
|
||||
// Hier: Die ID der LED, welche eingeschaltet werden soll
|
||||
// Rückgabewert: keine
|
||||
//================================================================================================================================
|
||||
static void cmd_led_on(const char* _data){
|
||||
switch(_data[0]){
|
||||
case '1': Led1_On(); break;
|
||||
case '2': Led2_On(); break;
|
||||
case '3': Led3_On(); break;
|
||||
case '4': Led4_On(); break;
|
||||
case '5': Led5_On(); break;
|
||||
case '6': Led6_On(); break;
|
||||
case '7': Led7_On(); break;
|
||||
case '8': Led8_On(); break;
|
||||
default:
|
||||
print_string("Led could not be found.");
|
||||
print_string(NEXT_LINE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//================================================================================================================================
|
||||
// Funktion: cmd_led_off
|
||||
// Kann verwendet werden, um eine LED auszuschalten
|
||||
// Parameter: _data: Zeichenkette mit Parametern des Befehls.
|
||||
// Hier: Die ID der LED, welche ausgeschaltet werden soll
|
||||
// Rückgabewert: keine
|
||||
//================================================================================================================================
|
||||
static void cmd_led_off(const char* _data){
|
||||
switch(_data[0]){
|
||||
case '1': Led1_Off(); break;
|
||||
case '2': Led2_Off(); break;
|
||||
case '3': Led3_Off(); break;
|
||||
case '4': Led4_Off(); break;
|
||||
case '5': Led5_Off(); break;
|
||||
case '6': Led6_Off(); break;
|
||||
case '7': Led7_Off(); break;
|
||||
case '8': Led8_Off(); break;
|
||||
default:
|
||||
print_string("LED could not be found.");
|
||||
print_string(NEXT_LINE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//================================================================================================================================
|
||||
// Funktion: cmd_alert
|
||||
// Lässt die Roten LEDs blinken und gibt einen Alarm text aus.
|
||||
// Parameter: _data: Zeichenkette mit Parametern des Befehls.
|
||||
// Rückgabewert: keine
|
||||
//================================================================================================================================
|
||||
static void cmd_alert(const char* _data){
|
||||
uint8_t isOn = 0;
|
||||
|
||||
while (1)
|
||||
{
|
||||
if(Timer_getTick() - g_startMS >= 250){
|
||||
g_startMS = Timer_getTick();
|
||||
|
||||
print_string("ALERT!!!");
|
||||
|
||||
if(!isOn){
|
||||
isOn = 1;
|
||||
Led6_On();
|
||||
Led7_On();
|
||||
Led8_On();
|
||||
} else {
|
||||
isOn = 0;
|
||||
Led6_Off();
|
||||
Led7_Off();
|
||||
Led8_Off();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//================================================================================================================================
|
||||
// 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 lm35;
|
||||
|
||||
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 */
|
||||
/* calculate the temperature from the ADC reading */
|
||||
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);
|
||||
|
||||
|
||||
print_string("----------");
|
||||
print_string(NEXT_LINE);
|
||||
}
|
||||
} while (strcmp(_data, "R") == 0 ? 1 : 0); /* Check if repeat flag is set*/
|
||||
}
|
||||
|
||||
|
||||
//================================================================================================================================
|
||||
// Funktion: cmd_rotary
|
||||
// Wird verwendet um die Bewegung von Dregeber 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*/
|
||||
}
|
@ -28,19 +28,47 @@
|
||||
<ResetRule>0</ResetRule>
|
||||
<eraseonlaunchrule>0</eraseonlaunchrule>
|
||||
<EraseKey />
|
||||
<avrtool>com.atmel.avrdbg.tool.simulator</avrtool>
|
||||
<avrtool>com.atmel.avrdbg.tool.stk500</avrtool>
|
||||
<avrtoolserialnumber />
|
||||
<avrdeviceexpectedsignature>0x1E9705</avrdeviceexpectedsignature>
|
||||
<com_atmel_avrdbg_tool_simulator>
|
||||
<ToolOptions xmlns="">
|
||||
<ToolOptions>
|
||||
<InterfaceProperties>
|
||||
</InterfaceProperties>
|
||||
<InterfaceName>
|
||||
</InterfaceName>
|
||||
</ToolOptions>
|
||||
<ToolType xmlns="">com.atmel.avrdbg.tool.simulator</ToolType>
|
||||
<ToolNumber xmlns="">
|
||||
<ToolType>com.atmel.avrdbg.tool.simulator</ToolType>
|
||||
<ToolNumber>
|
||||
</ToolNumber>
|
||||
<ToolName xmlns="">Simulator</ToolName>
|
||||
<ToolName>Simulator</ToolName>
|
||||
</com_atmel_avrdbg_tool_simulator>
|
||||
<AsfFrameworkConfig>
|
||||
<framework-data xmlns="">
|
||||
<options />
|
||||
<configurations />
|
||||
<files />
|
||||
<documentation help="" />
|
||||
<offline-documentation help="" />
|
||||
<dependencies>
|
||||
<content-extension eid="atmel.asf" uuidref="Atmel.ASF" version="3.49.1" />
|
||||
</dependencies>
|
||||
</framework-data>
|
||||
</AsfFrameworkConfig>
|
||||
<avrtoolinterface>ISP</avrtoolinterface>
|
||||
<com_atmel_avrdbg_tool_stk500>
|
||||
<ToolOptions>
|
||||
<InterfaceProperties>
|
||||
<IspClock>115200</IspClock>
|
||||
</InterfaceProperties>
|
||||
<InterfaceName>ISP</InterfaceName>
|
||||
</ToolOptions>
|
||||
<ToolType>com.atmel.avrdbg.tool.stk500</ToolType>
|
||||
<ToolNumber>
|
||||
</ToolNumber>
|
||||
<ToolName>STK500</ToolName>
|
||||
</com_atmel_avrdbg_tool_stk500>
|
||||
<avrtoolinterfaceclock>115200</avrtoolinterfaceclock>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
|
||||
<ToolchainSettings>
|
||||
@ -123,6 +151,18 @@
|
||||
</ToolchainSettings>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="adc.c">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="adc.h">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="cli.c">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="cli.h">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Led.c">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
@ -132,12 +172,42 @@
|
||||
<Compile Include="main.c">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="rotaryEncoder.c">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="rotaryEncoder.h">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="StopLight.c">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="StopLight.h">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Tasks.c">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Tasks.h">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Taster.c">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Taster.h">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Timer.c">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Timer.h">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="uart.c">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="uart.h">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
<Import Project="$(AVRSTUDIO_EXE_PATH)\\Vs\\Compiler.targets" />
|
||||
</Project>
|
@ -1,62 +1,80 @@
|
||||
#include <avr/io.h>
|
||||
|
||||
void Led_init(void){
|
||||
DDRB = 0xFF; // Set all bits in the B Data-Direction Register to output
|
||||
PORTB = 0x00; // Set all bits in the PORT B Register to low to turn all LEDs off
|
||||
void
|
||||
Led_init (void){
|
||||
DDRB = 0xFF; /* Set all bits in the B Data-Direction Register to output */
|
||||
PORTB = 0x00; /* Set all bits in the PORT B Register to low to turn all LEDs off */
|
||||
}
|
||||
|
||||
void Led1_On(void){
|
||||
PORTB |= (1<<7); // Set the bit for LED_1 to hight to turn it on
|
||||
void
|
||||
Led1_On (void){
|
||||
PORTB |= (1<<7); /* Set the bit for LED_1 to hight to turn it on */
|
||||
}
|
||||
void Led1_Off(void){
|
||||
PORTB &= ~(1<<7); // Set the bit for LED_1 to low to turn it off
|
||||
void
|
||||
Led1_Off (void){
|
||||
PORTB &= ~(1<<7); /* Set the bit for LED_1 to low to turn it off */
|
||||
}
|
||||
|
||||
void Led2_On(void){
|
||||
void
|
||||
Led2_On (void){
|
||||
PORTB |= (1<<6);
|
||||
}
|
||||
void Led2_Off(void){
|
||||
void
|
||||
Led2_Off (void){
|
||||
PORTB &= ~(1<<6);
|
||||
}
|
||||
|
||||
void Led3_On(void){
|
||||
void
|
||||
Led3_On (void){
|
||||
PORTB |= (1<<5);
|
||||
}
|
||||
void Led3_Off(void){
|
||||
void
|
||||
Led3_Off (void){
|
||||
PORTB &= ~(1<<5);
|
||||
}
|
||||
|
||||
void Led4_On(void){
|
||||
void
|
||||
Led4_On (void){
|
||||
PORTB |= (1<<4);
|
||||
}
|
||||
void Led4_Off(void){
|
||||
void
|
||||
|
||||
Led4_Off (void){
|
||||
PORTB &= ~(1<<4);
|
||||
}
|
||||
|
||||
void Led5_On(void){
|
||||
void
|
||||
Led5_On (void){
|
||||
PORTB |= (1<<3);
|
||||
}
|
||||
void Led5_Off(void){
|
||||
void
|
||||
Led5_Off (void){
|
||||
PORTB &= ~(1<<3);
|
||||
}
|
||||
|
||||
void Led6_On(void){
|
||||
void
|
||||
Led6_On (void){
|
||||
PORTB |= (1<<2);
|
||||
}
|
||||
void Led6_Off(void){
|
||||
void
|
||||
Led6_Off (void){
|
||||
PORTB &= ~(1<<2);
|
||||
}
|
||||
|
||||
void Led7_On(void){
|
||||
void
|
||||
Led7_On (void){
|
||||
PORTB |= (1<<1);
|
||||
}
|
||||
void Led7_Off(void){
|
||||
void
|
||||
Led7_Off (void){
|
||||
PORTB &= ~(1<<1);
|
||||
}
|
||||
|
||||
void Led8_On(void){
|
||||
void
|
||||
Led8_On (void){
|
||||
PORTB |= (1<<0);
|
||||
}
|
||||
void Led8_Off(void){
|
||||
void
|
||||
Led8_Off (void){
|
||||
PORTB &= ~(1<<0);
|
||||
}
|
252
EmbeddedSystemsTHM/StopLight.c
Normal file
252
EmbeddedSystemsTHM/StopLight.c
Normal file
@ -0,0 +1,252 @@
|
||||
/*
|
||||
* StopLight.c
|
||||
*
|
||||
* Created: 11/11/2021 16:26:22
|
||||
* Author: n0x
|
||||
*/
|
||||
|
||||
#include "Led.h";
|
||||
#include "Taster.h";
|
||||
#include "Timer.h"
|
||||
#include "StopLight.h"
|
||||
|
||||
/* enums */
|
||||
enum{
|
||||
S1_GREEN, S1_YELLOW, S1_RED, S1_RED_YELLOW,
|
||||
S2_GREEN, S2_YELLOW, S2_RED, S2_RED_YELLOW,
|
||||
INIT, WAIT
|
||||
} StoplightState = INIT; /* Emus for the stoplight task (task 3) */
|
||||
|
||||
|
||||
/* Programmieren eine Ampelsteuerung auf einer Straßenkreuzung */
|
||||
|
||||
/* Stoplight and the corresponding LEDs
|
||||
| GREEN | YELLOW | RED
|
||||
----------------------------------
|
||||
SL 1 | Led1 | Led4 | Led6
|
||||
SL 2 | Led2 | Led5 | Led7
|
||||
|
||||
*/
|
||||
|
||||
uint16_t g_startMS;
|
||||
int delayShort = 5000;
|
||||
int delayLong = 30000;
|
||||
|
||||
/* Task 3 (2021-11-11) */
|
||||
void
|
||||
runStopLightTask1()
|
||||
{
|
||||
switch(StoplightState){
|
||||
case INIT:
|
||||
setS1ToGreen();
|
||||
setS2ToRed();
|
||||
|
||||
if(Timer_getTick() - g_startMS >= delayLong) {
|
||||
g_startMS=Timer_getTick();
|
||||
StoplightState = S1_YELLOW;
|
||||
}
|
||||
break;
|
||||
|
||||
case S1_YELLOW:
|
||||
setS1ToYellow();
|
||||
|
||||
if(Timer_getTick() - g_startMS >= delayShort) {
|
||||
g_startMS=Timer_getTick();
|
||||
StoplightState = S1_RED;
|
||||
}
|
||||
break;
|
||||
|
||||
case S1_RED:
|
||||
setS1ToRed();
|
||||
|
||||
if(Timer_getTick() - g_startMS >= delayShort) {
|
||||
g_startMS=Timer_getTick();
|
||||
StoplightState = S2_RED_YELLOW;
|
||||
}
|
||||
break;
|
||||
|
||||
case S2_RED_YELLOW:
|
||||
setS2ToRedYellow();
|
||||
|
||||
if(Timer_getTick() - g_startMS >= delayShort) {
|
||||
g_startMS=Timer_getTick();
|
||||
StoplightState = S2_GREEN;
|
||||
}
|
||||
break;
|
||||
|
||||
case S2_GREEN:
|
||||
setS2ToGreen();
|
||||
|
||||
if(Timer_getTick() - g_startMS >= delayLong) {
|
||||
g_startMS=Timer_getTick();
|
||||
StoplightState = S2_YELLOW;
|
||||
}
|
||||
break;
|
||||
|
||||
case S2_YELLOW:
|
||||
setS2ToYellow();
|
||||
|
||||
if(Timer_getTick() - g_startMS >= delayShort) {
|
||||
g_startMS=Timer_getTick();
|
||||
StoplightState = S2_RED;
|
||||
}
|
||||
break;
|
||||
|
||||
case S2_RED:
|
||||
setS2ToRed();
|
||||
|
||||
if(Timer_getTick() - g_startMS >= delayShort) {
|
||||
g_startMS=Timer_getTick();
|
||||
StoplightState = S1_RED_YELLOW;
|
||||
}
|
||||
break;
|
||||
|
||||
case S1_RED_YELLOW:
|
||||
setS1ToRedYellow();
|
||||
|
||||
if(Timer_getTick() - g_startMS >= delayShort) {
|
||||
g_startMS=Timer_getTick();
|
||||
StoplightState = S1_GREEN;
|
||||
}
|
||||
break;
|
||||
|
||||
case S1_GREEN:
|
||||
setS1ToGreen();
|
||||
|
||||
if(Timer_getTick() - g_startMS >= delayLong) {
|
||||
g_startMS=Timer_getTick();
|
||||
StoplightState = S1_YELLOW;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
runStopLightTask2(void)
|
||||
{
|
||||
switch(StoplightState){
|
||||
case INIT:
|
||||
setS1ToGreen();
|
||||
setS2ToRed();
|
||||
|
||||
StoplightState = WAIT;
|
||||
break;
|
||||
|
||||
case WAIT:
|
||||
if(Taster1_get()) {
|
||||
g_startMS=Timer_getTick();
|
||||
StoplightState = S1_YELLOW;
|
||||
}
|
||||
break;
|
||||
|
||||
case S1_YELLOW:
|
||||
setS1ToYellow();
|
||||
|
||||
if(Timer_getTick() - g_startMS >= delayShort) {
|
||||
g_startMS=Timer_getTick();
|
||||
StoplightState = S1_RED;
|
||||
}
|
||||
break;
|
||||
|
||||
case S1_RED:
|
||||
setS1ToRed();
|
||||
|
||||
if(Timer_getTick() - g_startMS >= delayShort) {
|
||||
g_startMS=Timer_getTick();
|
||||
StoplightState = S2_GREEN;
|
||||
}
|
||||
break;
|
||||
|
||||
case S2_GREEN:
|
||||
setS2ToGreen();
|
||||
|
||||
if(Timer_getTick() - g_startMS >= delayLong) {
|
||||
g_startMS=Timer_getTick();
|
||||
StoplightState = S2_RED;
|
||||
}
|
||||
break;
|
||||
|
||||
case S2_RED:
|
||||
setS2ToRed();
|
||||
if(Timer_getTick() - g_startMS >= delayShort) {
|
||||
g_startMS=Timer_getTick();
|
||||
StoplightState = S1_RED_YELLOW;
|
||||
}
|
||||
break;
|
||||
|
||||
case S1_RED_YELLOW:
|
||||
setS1ToRedYellow();
|
||||
if(Timer_getTick() - g_startMS >= delayShort) {
|
||||
g_startMS=Timer_getTick();
|
||||
StoplightState = S1_GREEN;
|
||||
}
|
||||
break;
|
||||
|
||||
case S1_GREEN:
|
||||
setS1ToGreen();
|
||||
if(Timer_getTick() - g_startMS >= delayLong) {
|
||||
g_startMS=Timer_getTick();
|
||||
StoplightState = WAIT;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* LED Helper methods to keep the code a little cleaner */
|
||||
|
||||
void setS1ToGreen(void)
|
||||
{
|
||||
Led1_On();
|
||||
Led4_Off();
|
||||
Led6_Off();
|
||||
}
|
||||
|
||||
void setS1ToYellow(void)
|
||||
{
|
||||
Led1_Off();
|
||||
Led4_On();
|
||||
Led6_Off();
|
||||
}
|
||||
|
||||
void setS1ToRedYellow(void)
|
||||
{
|
||||
Led1_Off();
|
||||
Led4_On();
|
||||
Led6_On();
|
||||
}
|
||||
|
||||
void setS1ToRed(void)
|
||||
{
|
||||
Led1_Off();
|
||||
Led4_Off();
|
||||
Led6_On();
|
||||
}
|
||||
|
||||
void setS2ToGreen(void)
|
||||
{
|
||||
Led2_On();
|
||||
Led5_Off();
|
||||
Led7_Off();
|
||||
}
|
||||
|
||||
void setS2ToYellow(void)
|
||||
{
|
||||
Led2_Off();
|
||||
Led5_On();
|
||||
Led7_Off();
|
||||
}
|
||||
|
||||
void setS2ToRedYellow(void)
|
||||
{
|
||||
Led2_Off();
|
||||
Led5_On();
|
||||
Led7_On();
|
||||
}
|
||||
|
||||
void setS2ToRed(void)
|
||||
{
|
||||
Led2_Off();
|
||||
Led5_Off();
|
||||
Led7_On();
|
||||
}
|
||||
//*/
|
24
EmbeddedSystemsTHM/StopLight.h
Normal file
24
EmbeddedSystemsTHM/StopLight.h
Normal file
@ -0,0 +1,24 @@
|
||||
/*
|
||||
* StopLight.h
|
||||
*
|
||||
* Created: 11/11/2021 16:24:16
|
||||
* Author: n0x
|
||||
*/
|
||||
|
||||
|
||||
#ifndef STOPLIGHT_H_
|
||||
#define STOPLIGHT_H_
|
||||
|
||||
void runStopLightTask1(void);
|
||||
void runStopLightTask2(void);
|
||||
|
||||
void setS1ToGreen(void);
|
||||
void setS1ToYellow(void);
|
||||
void setS1ToRedYellow(void);
|
||||
void setS1ToRed(void);
|
||||
void setS2ToGreen(void);
|
||||
void setS2ToYellow(void);
|
||||
void setS2ToRedYellow(void);
|
||||
void setS2ToRed(void);
|
||||
|
||||
#endif /* STOPLIGHT_H_ */
|
74
EmbeddedSystemsTHM/Tasks.c
Normal file
74
EmbeddedSystemsTHM/Tasks.c
Normal file
@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Tasks.c
|
||||
*
|
||||
* Created: 17/11/2021 16:58:20
|
||||
* Author: n0x
|
||||
*/
|
||||
|
||||
#include "Led.h"
|
||||
#include "Timer.h"
|
||||
#include "Taster.h"
|
||||
#include <util/delay.h>
|
||||
|
||||
int g_counter = 0;
|
||||
uint8_t g_ledStatus = 0;
|
||||
uint16_t g_startMS;
|
||||
|
||||
void
|
||||
initTasks (void){
|
||||
g_startMS=Timer_getTick();
|
||||
}
|
||||
|
||||
/* Task 1 (2021-10-21) */
|
||||
void
|
||||
runningLight (void)
|
||||
{
|
||||
_delay_ms(100);
|
||||
|
||||
/* Programmieren Sie ein Lauflicht. Nutzen Sie dazu die in „Led.h“ deklarierten
|
||||
* Funktionen.
|
||||
* 2.Schreiben Sie ein Programm mit folgenden Funktionen:
|
||||
* Wenn Taste 1 gedrückt wird, wird die Variable „Counter“ inkrementiert.
|
||||
* Wird Taste 2 gedrückt wird, wird die Variable „Counter“ dekrementiert.
|
||||
* Variable „Counter“ soll sich dabei zwischen 0 und 8 bewegen.
|
||||
* Der Inhalt des Wertes soll mit Hilfe der LEDs angezeigt werden.
|
||||
* 0 = keine LED an, 1 = LED1 an, 2 = LED1+LED2 an, usw.
|
||||
* 8 = alle LEDs an.
|
||||
*/
|
||||
|
||||
/* Check if counter needs to be incremented */
|
||||
if (Taster1_get()) g_counter++;
|
||||
|
||||
/* Check if counter needs to be decremented */
|
||||
if (Taster2_get()) g_counter--;
|
||||
|
||||
/* Keep counter within boundaries (0-8) */
|
||||
g_counter = (g_counter + 9) % 9;
|
||||
|
||||
/* Set the LEDs according to the counter */
|
||||
g_counter >= 1 ? Led1_On() : Led1_Off();
|
||||
g_counter >= 2 ? Led2_On() : Led2_Off();
|
||||
g_counter >= 3 ? Led3_On() : Led3_Off();
|
||||
g_counter >= 4 ? Led4_On() : Led4_Off();
|
||||
g_counter >= 5 ? Led5_On() : Led5_Off();
|
||||
g_counter >= 6 ? Led6_On() : Led6_Off();
|
||||
g_counter >= 7 ? Led7_On() : Led7_Off();
|
||||
g_counter >= 8 ? Led8_On() : Led8_Off();
|
||||
}
|
||||
|
||||
/* Task 2 (2021-10-28) */
|
||||
void
|
||||
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 {
|
||||
Led1_Off();
|
||||
}
|
||||
g_ledStatus++;
|
||||
}
|
||||
}
|
16
EmbeddedSystemsTHM/Tasks.h
Normal file
16
EmbeddedSystemsTHM/Tasks.h
Normal file
@ -0,0 +1,16 @@
|
||||
/*
|
||||
* Tasks.h
|
||||
*
|
||||
* Created: 17/11/2021 16:58:04
|
||||
* Author: n0x
|
||||
*/
|
||||
|
||||
|
||||
#ifndef TASKS_H_
|
||||
#define TASKS_H_
|
||||
|
||||
void initTasks(void);
|
||||
void blinkLedWithTimer(void);
|
||||
void runningLight(void);
|
||||
|
||||
#endif /* TASKS_H_ */
|
@ -1,22 +1,43 @@
|
||||
#include <avr/io.h>
|
||||
|
||||
void Taster_init(void){
|
||||
DDRD &= ~(7<<5); // Set bit 5-7 of Data Direction Register D as input
|
||||
DDRC &= ~(1<<2); // Set bit 2 of Data Direction Register B as input
|
||||
void
|
||||
Taster_init (void){
|
||||
DDRD &= ~(0b111<<5); /* Set bit 5-7 of Data Direction Register D as input */
|
||||
DDRC &= ~(1<<2); /* Set bit 2 of Data Direction Register C as input */
|
||||
|
||||
PORTD &= ~(7<<5); // Initialize bit 5-7 of PORTD as low
|
||||
PORTC &= ~(1<<2); // Initialize bit 2 of PORTC as low
|
||||
PORTD |= (0b111<<5); /* Initialize bit 5-7 of PORTD as for pull up resistor */
|
||||
PORTC |= (1<<PINC2); /* Initialize bit 2 of PORTC as for pull up resistor */
|
||||
|
||||
/** Disable JTAG so Pin 2 of PORTC can be used as a GPIO
|
||||
* Command needs to be run twice - see MCU Control Register Description (23.8.1)
|
||||
* Bits 7 – JTD: JTAG Interface Disable
|
||||
* [..] The application software must write this bit to the desired value
|
||||
* twice within four cycles to change its value. [...]
|
||||
*/
|
||||
MCUCR |= (1<<JTD);
|
||||
MCUCR |= (1<<JTD);
|
||||
}
|
||||
|
||||
uint8_t Taster1_get(void){
|
||||
return PORTD&(1<<7);
|
||||
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 PORTD&(1<<6);
|
||||
|
||||
uint8_t
|
||||
Taster2_get (void){
|
||||
return ((PIND & (1<<PIND6)) == 0);
|
||||
}
|
||||
uint8_t Taster3_get(void){
|
||||
return PORTD&(1<<5);
|
||||
|
||||
uint8_t
|
||||
Taster3_get (void){
|
||||
return ((PIND & (1<<PIND5)) == 0);
|
||||
}
|
||||
uint8_t Taster4_get(void){
|
||||
return PORTC&(1<<2);
|
||||
|
||||
uint8_t
|
||||
Taster4_get (void){
|
||||
return ((PINC & (1<<PINC2)) == 0);
|
||||
}
|
44
EmbeddedSystemsTHM/Timer.c
Normal file
44
EmbeddedSystemsTHM/Timer.c
Normal file
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Timer.c
|
||||
*
|
||||
* Created: 28/10/2021 17:51:29
|
||||
* Author: n0x
|
||||
*/
|
||||
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
|
||||
volatile uint16_t cntr = 0;
|
||||
|
||||
void
|
||||
Timer_init (void){
|
||||
TCCR0A |= (1<<WGM01); /* Set WGM01 to high to enable CTC mode */
|
||||
TCCR0B |= (3<<CS00); /* Set the clock select bit to pre-scaler 3
|
||||
16MHz / 64 (pre-scaler 3) ==> 250KHz */
|
||||
|
||||
TIMSK0 |= (1<<OCIE0A); /* Set OCIE0A to high to rise an interrupt when the counter matches OCR0A */
|
||||
|
||||
OCR0A = 250 - 1; /* Set the Output Compare Register 0 A to 125 to trigger interrupt every 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)
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
uint16_t
|
||||
Timer_getTick(void){
|
||||
uint16_t tmp;
|
||||
cli();
|
||||
tmp = cntr;
|
||||
sei();
|
||||
return tmp;
|
||||
}
|
||||
|
||||
|
||||
ISR(TIMER0_COMPA_vect){
|
||||
cntr++;
|
||||
}
|
16
EmbeddedSystemsTHM/Timer.h
Normal file
16
EmbeddedSystemsTHM/Timer.h
Normal file
@ -0,0 +1,16 @@
|
||||
/*
|
||||
* Timer.h
|
||||
*
|
||||
* Created: 28/10/2021 17:49:26
|
||||
* Author: n0x
|
||||
*/
|
||||
|
||||
|
||||
#ifndef TIMER_H_
|
||||
#define TIMER_H_
|
||||
#include <avr/io.h>
|
||||
|
||||
void Timer_init(void);
|
||||
uint16_t Timer_getTick(void);
|
||||
|
||||
#endif /* TIMER_H_ */
|
145
EmbeddedSystemsTHM/UART.c
Normal file
145
EmbeddedSystemsTHM/UART.c
Normal file
@ -0,0 +1,145 @@
|
||||
/*
|
||||
* UART.c
|
||||
*
|
||||
* Created: 25/11/2021 16:26:10
|
||||
* Author: n0x
|
||||
*/
|
||||
|
||||
#include "UART.h"
|
||||
|
||||
/* create structs for TX and TX buffer */
|
||||
CircularBuffer TxBuffer;
|
||||
CircularBuffer* pTxBuffer = &TxBuffer;
|
||||
CircularBuffer RxBuffer;
|
||||
CircularBuffer* pRxBuffer = &RxBuffer;
|
||||
|
||||
volatile uint8_t TxActive;
|
||||
|
||||
void
|
||||
uart_init(void) {
|
||||
UBRR0 = 103; /* set BAUD rate to 9600 */
|
||||
|
||||
UCSR0C |=
|
||||
(0<<UMSEL00)|(0<<UMSEL01)| /* Async UART */
|
||||
(0<<UPM00)|(0<<UPM01)| /* Disable Parity */
|
||||
(1<<UCSZ00)|(1<<UCSZ01)| /* 8 Bit */
|
||||
(0<<USBS0); /* 1 Stopbit */
|
||||
|
||||
UCSR0B |=
|
||||
(1<<TXEN0)|(1<<RXEN0)| /* enable send and receive */
|
||||
(1<<RXCIE0); /* enable Receive interrupts */
|
||||
|
||||
|
||||
/* Initiate read- and write-pointers with 0 */
|
||||
pTxBuffer->Readpointer = 0;
|
||||
pTxBuffer->Writepointer = 0;
|
||||
pRxBuffer->Readpointer = 0;
|
||||
pRxBuffer->Writepointer = 0;
|
||||
}
|
||||
|
||||
/********Sending Data********/
|
||||
|
||||
void
|
||||
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]);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
uart_send_byte(char c) {
|
||||
/* Disable the TX Interrupt to prevent issues while writing to the send buffer*/
|
||||
UCSR0B &= ~(1<<TXCIE0);
|
||||
|
||||
/* Check if a transmit is in progress
|
||||
if yes -> 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){
|
||||
pTxBuffer->Writepointer = 0;
|
||||
}
|
||||
} else {
|
||||
TxActive = 1;
|
||||
UDR0 = c;
|
||||
}
|
||||
/* Enable the TX Interrupt again*/
|
||||
UCSR0B |= (1<<TXCIE0);
|
||||
}
|
||||
|
||||
/* when data (a data frame) is to be send (written to UDR0) */
|
||||
ISR(USART0_TX_vect) {
|
||||
/* check if the read and write pointer of the send buffer are not aligned
|
||||
if not aligned -> 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){
|
||||
pTxBuffer->Readpointer = 0;
|
||||
}
|
||||
} else {
|
||||
TxActive = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/*******Receiving Data*******/
|
||||
uint8_t
|
||||
uart_data_available(void) {
|
||||
uint8_t dataAvailabel = 0;
|
||||
/* Disable the RX Interrupt to prevent issues when checking if data is available */
|
||||
UCSR0B &= ~(1<<RXCIE0);
|
||||
|
||||
/* check if read and write pointer are at different locations
|
||||
if yes -> data is available and can be read */
|
||||
if(pRxBuffer->Readpointer != pRxBuffer->Writepointer){
|
||||
dataAvailabel = 1;
|
||||
}
|
||||
|
||||
/* Enable the RX Interrupt again*/
|
||||
UCSR0B |= (1<<RXCIE0);
|
||||
return dataAvailabel;
|
||||
}
|
||||
|
||||
char
|
||||
uart_get_data(void) {
|
||||
char data = 0;
|
||||
/* Disable the RX Interrupt to prevent issues when reading out a char from the buffer */
|
||||
UCSR0B &= ~(1<<RXCIE0);
|
||||
|
||||
/* check if read and write pointer are at different locations
|
||||
if yes -> data is available and can be read */
|
||||
if(pRxBuffer->Readpointer != pRxBuffer->Writepointer)
|
||||
{
|
||||
/* read a char from the buffer and increment the read pointer */
|
||||
data = pRxBuffer->data[pRxBuffer->Readpointer++];
|
||||
if(pRxBuffer->Readpointer >= SIZE_BUFFER){
|
||||
pRxBuffer->Readpointer = 0;
|
||||
}
|
||||
}
|
||||
/* Enable the RX Interrupt again*/
|
||||
UCSR0B |= (1<<RXCIE0);
|
||||
/* give back the read in character */
|
||||
return data;
|
||||
}
|
||||
|
||||
/* when data (a data frame) is received */
|
||||
ISR(USART0_RX_vect) {
|
||||
uint8_t status = UCSR0A;
|
||||
uint8_t data = UDR0;
|
||||
|
||||
/* check status flags if data is correctly accessible */
|
||||
if((status & ((1<<DOR0) | (1<<FE0))) == 0){
|
||||
/* Save data to buffer and increment the write pointer */
|
||||
pRxBuffer->data[pRxBuffer->Writepointer++] = data;
|
||||
if(pRxBuffer->Writepointer >= SIZE_BUFFER) {
|
||||
pRxBuffer->Writepointer = 0;
|
||||
}
|
||||
}
|
||||
}
|
30
EmbeddedSystemsTHM/UART.h
Normal file
30
EmbeddedSystemsTHM/UART.h
Normal file
@ -0,0 +1,30 @@
|
||||
/*
|
||||
* UART2.h
|
||||
*
|
||||
* Created: 25/11/2021 16:25:59
|
||||
* Author: n0x
|
||||
*/
|
||||
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
|
||||
#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);
|
||||
|
||||
uint8_t uart_data_available(void);
|
||||
char uart_get_data(void);
|
||||
|
||||
#endif /* UART2_H_ */
|
69
EmbeddedSystemsTHM/adc.c
Normal file
69
EmbeddedSystemsTHM/adc.c
Normal file
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* adc.c
|
||||
*
|
||||
* Created: 09/12/2021 16:31:16
|
||||
* Author: n0x
|
||||
*/
|
||||
|
||||
#include "adc.h"
|
||||
|
||||
#define myADC ((ADC_t*)(0x78))
|
||||
|
||||
void
|
||||
adc_init(void)
|
||||
{
|
||||
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 */
|
||||
myADC->uiADATE = 0; /* Disable the ADC auto trigger */
|
||||
myADC->uiADEN = 1; /* Enable ADC */
|
||||
}
|
||||
|
||||
volatile int done = 1;
|
||||
|
||||
/* ADC 1 */
|
||||
uint16_t
|
||||
adc_get_poti(void)
|
||||
{
|
||||
uint16_t adc;
|
||||
|
||||
myADC->uiMUX = 1; /* Set ADMUX to access ADC channel 1 */
|
||||
|
||||
myADC->uiADSC = 1; /* Start the conversion */
|
||||
|
||||
done = 0; /* reset the done flag to false */
|
||||
while(done == 0); /* Wait till conversion completes */
|
||||
|
||||
adc = myADC->uiADC;
|
||||
|
||||
return adc;
|
||||
}
|
||||
|
||||
|
||||
/* ADC 0 */
|
||||
uint16_t
|
||||
adc_get_LM35(void)
|
||||
{
|
||||
uint16_t adc;
|
||||
|
||||
//myADC->uiADIE = 0; /* Disable interrupt */
|
||||
|
||||
myADC->uiMUX = 0; /* Set ADMUX to access ADC channel 1 */
|
||||
|
||||
myADC->uiADSC = 1; /* Start the conversion */
|
||||
|
||||
done = 0; /* reset the done flag to false */
|
||||
while(done == 0); /* Wait till conversion completes */
|
||||
|
||||
adc = myADC->uiADC;
|
||||
|
||||
//myADC->uiADIE = 1; /* Enable the interrupt again */
|
||||
|
||||
return adc;
|
||||
}
|
||||
|
||||
ISR(ADC_vect){
|
||||
done = 1; /* set the done flag to true */
|
||||
}
|
46
EmbeddedSystemsTHM/adc.h
Normal file
46
EmbeddedSystemsTHM/adc.h
Normal file
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* adc.h
|
||||
*
|
||||
* Created: 09/12/2021 16:31:06
|
||||
* Author: n0x
|
||||
*/
|
||||
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
|
||||
#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 uiREFS :2;
|
||||
};
|
||||
|
||||
void adc_init(void);
|
||||
|
||||
uint16_t adc_get_poti(void);
|
||||
uint16_t adc_get_LM35(void);
|
||||
|
||||
#endif /* ADC_H_ */
|
8
EmbeddedSystemsTHM/cli.h
Normal file
8
EmbeddedSystemsTHM/cli.h
Normal file
@ -0,0 +1,8 @@
|
||||
|
||||
#ifndef CLI_H_
|
||||
#define CLI_H_
|
||||
|
||||
extern void line_interpreter_init(void (*_ptr)(char _data));
|
||||
extern void line_interpreter_get_data(char _d);
|
||||
|
||||
#endif /* CLI_H_ */
|
@ -7,52 +7,62 @@
|
||||
|
||||
#include "Led.h"
|
||||
#include "Taster.h"
|
||||
#include <util/delay.h>
|
||||
#ifdef F_CPU
|
||||
#include "Timer.h"
|
||||
#include "Tasks.h"
|
||||
#include "StopLight.h"
|
||||
#include "uart.h"
|
||||
#include "cli.h"
|
||||
#include "adc.h"
|
||||
#include "rotaryEncoder.h"
|
||||
#include <avr/interrupt.h>
|
||||
#define F_CPU 1000000
|
||||
#endif
|
||||
#include <util/delay.h>
|
||||
|
||||
int main(void)
|
||||
#include <stdio.h>
|
||||
|
||||
/* function declarations */
|
||||
void buttonTest (void);
|
||||
|
||||
uint16_t g_startMS;
|
||||
|
||||
int
|
||||
main (void)
|
||||
{
|
||||
// Initialize the LEDs and Buttons
|
||||
Led_init();
|
||||
sei();
|
||||
|
||||
/* Initialize the LEDs, Buttons, etc. */
|
||||
Timer_init();
|
||||
Taster_init();
|
||||
Led_init();
|
||||
initTasks();
|
||||
|
||||
|
||||
int counter = 0;
|
||||
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");
|
||||
|
||||
while (1)
|
||||
{
|
||||
_delay_ms(50);
|
||||
//blinkLedWithTimer();
|
||||
|
||||
/* Programmieren Sie ein Lauflicht. Nutzen Sie dazu die in „Led.h“ deklarierten
|
||||
* Funktionen.
|
||||
* 2.Schreiben Sie ein Programm mit folgenden Funktionen:
|
||||
* Wenn Taste 1 gedrückt wird, wird die Variable „Counter“ inkrementiert.
|
||||
* Wird Taste 2 gedrückt wird, wird die Variable „Counter“ dekrementiert.
|
||||
* Variable „Counter“ soll sich dabei zwischen 0 und 8 bewegen.
|
||||
* Der Inhalt des Wertes soll mit Hilfe der LEDs angezeigt werden.
|
||||
* 0 = keine LED an, 1 = LED1 an, 2 = LED1+LED2 an, usw.
|
||||
* 8 = alle LEDs an.
|
||||
*/
|
||||
|
||||
// Check if counter needs to be incremented
|
||||
if (Taster1_get()) counter++;
|
||||
|
||||
// Check if counter needs to be decremented
|
||||
if (Taster2_get()) counter--;
|
||||
|
||||
// Keep counter within boundaries (0-8)
|
||||
counter = (counter + 9) % 9;
|
||||
|
||||
// Set the LEDs according to the counter
|
||||
counter >= 1 ? Led1_On() : Led1_Off();
|
||||
counter >= 2 ? Led2_On() : Led2_Off();
|
||||
counter >= 3 ? Led3_On() : Led3_Off();
|
||||
counter >= 4 ? Led4_On() : Led4_Off();
|
||||
counter >= 5 ? Led5_On() : Led5_Off();
|
||||
counter >= 6 ? Led6_On() : Led6_Off();
|
||||
counter >= 7 ? Led7_On() : Led7_Off();
|
||||
counter >= 8 ? Led8_On() : Led8_Off();
|
||||
if(uart_data_available()){
|
||||
line_interpreter_get_data(uart_get_data());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
buttonTest (void)
|
||||
{
|
||||
Taster1_get() ? Led1_On() : Led1_Off();
|
||||
Taster2_get() ? Led2_On() : Led2_Off();
|
||||
Taster3_get() ? Led3_On() : Led3_Off();
|
||||
Taster4_get() ? Led4_On() : Led4_Off();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
140
EmbeddedSystemsTHM/rotaryEncoder.c
Normal file
140
EmbeddedSystemsTHM/rotaryEncoder.c
Normal file
@ -0,0 +1,140 @@
|
||||
/*
|
||||
* rotaryEncoder.c
|
||||
*
|
||||
* Created: 16/12/2021 16:34:05
|
||||
* Author: n0x
|
||||
*/
|
||||
|
||||
#include "rotaryEncoder.h"
|
||||
|
||||
#define ROTA PORTC7 /* define Rotary Encoder Button A */
|
||||
#define ROTB PORTC6 /* define Rotary Encoder Button B */
|
||||
#define ROTBUTTON PORTC5
|
||||
|
||||
/* enums */
|
||||
enum{
|
||||
LEFT1, LEFT2, LEFT3, RIGHT1, RIGHT2, RIGHT3,
|
||||
INIT, WAIT
|
||||
} RotaryState = INIT; /* Enum for the rotary enc */
|
||||
|
||||
int16_t count;
|
||||
|
||||
void
|
||||
drehgeber_init(void)
|
||||
{
|
||||
DDRC &= ~(7<<DDC5); /* Set bit 5-7 of Data Direction Register C as input */
|
||||
PORTC |= (7<<PORTC5); /* Initialize bit 5-7 of PORTC as for pull up resistor */
|
||||
|
||||
/* With external interrupt */
|
||||
PCICR |= (1<<PCIE2);
|
||||
PCMSK2 |= (1<<PCINT23)|(1<<PCINT22);
|
||||
}
|
||||
|
||||
ISR(PCINT2_vect)
|
||||
{
|
||||
drehgeber_process();
|
||||
}
|
||||
|
||||
void
|
||||
drehgeber_process(void)
|
||||
{
|
||||
/* calculate the value of Button A and B of rotary encoder */
|
||||
uint8_t a, b, enc;
|
||||
a = ((PINC & (1<<ROTA)) == 0);
|
||||
b = ((PINC & (1<<ROTB)) == 0)<<1;
|
||||
enc = a+b;
|
||||
|
||||
switch(RotaryState)
|
||||
{
|
||||
case INIT:
|
||||
RotaryState = WAIT;
|
||||
break;
|
||||
|
||||
case WAIT:
|
||||
if(enc == 1)
|
||||
{
|
||||
RotaryState = LEFT1;
|
||||
}
|
||||
if(enc == 2)
|
||||
{
|
||||
RotaryState = RIGHT1;
|
||||
}
|
||||
break;
|
||||
|
||||
case LEFT1:
|
||||
if(enc == 3)
|
||||
{
|
||||
RotaryState = LEFT2;
|
||||
}
|
||||
if(enc == 0)
|
||||
{
|
||||
RotaryState = WAIT;
|
||||
}
|
||||
break;
|
||||
|
||||
case LEFT2:
|
||||
if(enc == 2)
|
||||
{
|
||||
RotaryState = LEFT3;
|
||||
}
|
||||
if(enc == 1)
|
||||
{
|
||||
RotaryState = LEFT1;
|
||||
}
|
||||
break;
|
||||
|
||||
case LEFT3:
|
||||
if(enc == 0)
|
||||
{
|
||||
/* Increment counter and wait */
|
||||
count++;
|
||||
RotaryState = WAIT;
|
||||
}
|
||||
if(enc == 3)
|
||||
{
|
||||
RotaryState = LEFT2;
|
||||
}
|
||||
break;
|
||||
|
||||
case RIGHT1:
|
||||
if(enc == 3)
|
||||
{
|
||||
RotaryState = RIGHT2;
|
||||
}
|
||||
if(enc == 0)
|
||||
{
|
||||
RotaryState = WAIT;
|
||||
}
|
||||
break;
|
||||
|
||||
case RIGHT2:
|
||||
if(enc == 1)
|
||||
{
|
||||
RotaryState = RIGHT3;
|
||||
}
|
||||
if(enc == 2)
|
||||
{
|
||||
RotaryState = RIGHT1;
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case RIGHT3:
|
||||
if(enc == 0)
|
||||
{
|
||||
/* Decrement counter and wait */
|
||||
count--;
|
||||
RotaryState = WAIT;
|
||||
}
|
||||
if(enc == 3)
|
||||
{
|
||||
RotaryState = RIGHT2;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int16_t drehgeber_get(void)
|
||||
{
|
||||
return count;
|
||||
}
|
19
EmbeddedSystemsTHM/rotaryEncoder.h
Normal file
19
EmbeddedSystemsTHM/rotaryEncoder.h
Normal file
@ -0,0 +1,19 @@
|
||||
/*
|
||||
* rotaryEncoder.h
|
||||
*
|
||||
* Created: 16/12/2021 16:33:57
|
||||
* Author: n0x
|
||||
*/
|
||||
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
|
||||
#ifndef ROTARYENCODER_H_
|
||||
#define ROTARYENCODER_H_
|
||||
|
||||
void drehgeber_init(void);
|
||||
void drehgeber_process(void);
|
||||
int16_t drehgeber_get(void);
|
||||
|
||||
|
||||
#endif /* ROTARYENCODER_H_ */
|
Loading…
Reference in New Issue
Block a user