Added separate Task file for introduction tasks, added basic UART functionality

This commit is contained in:
_N0x 2021-11-18 19:34:41 +01:00
parent db16d89b80
commit 4f5b174bb1
9 changed files with 257 additions and 77 deletions

View File

@ -166,6 +166,12 @@
<Compile Include="StopLight.h"> <Compile Include="StopLight.h">
<SubType>compile</SubType> <SubType>compile</SubType>
</Compile> </Compile>
<Compile Include="Tasks.c">
<SubType>compile</SubType>
</Compile>
<Compile Include="Tasks.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="Taster.c"> <Compile Include="Taster.c">
<SubType>compile</SubType> <SubType>compile</SubType>
</Compile> </Compile>
@ -178,6 +184,12 @@
<Compile Include="Timer.h"> <Compile Include="Timer.h">
<SubType>compile</SubType> <SubType>compile</SubType>
</Compile> </Compile>
<Compile Include="UART.c">
<SubType>compile</SubType>
</Compile>
<Compile Include="UART.h">
<SubType>compile</SubType>
</Compile>
</ItemGroup> </ItemGroup>
<Import Project="$(AVRSTUDIO_EXE_PATH)\\Vs\\Compiler.targets" /> <Import Project="$(AVRSTUDIO_EXE_PATH)\\Vs\\Compiler.targets" />
</Project> </Project>

View File

@ -8,7 +8,7 @@
#include "Led.h"; #include "Led.h";
#include "Taster.h"; #include "Taster.h";
#include "Timer.h" #include "Timer.h"
#include <util/delay.h>; #include "StopLight.h"
/* enums */ /* enums */
enum{ enum{
@ -102,7 +102,8 @@ runStopLightTask1()
break; break;
case S1_RED_YELLOW: case S1_RED_YELLOW:
setS2ToRedYellow(); setS1ToRedYellow();
if(Timer_getTick() - g_startMS >= delayShort) { if(Timer_getTick() - g_startMS >= delayShort) {
g_startMS=Timer_getTick(); g_startMS=Timer_getTick();
StoplightState = S1_GREEN; StoplightState = S1_GREEN;
@ -111,6 +112,7 @@ runStopLightTask1()
case S1_GREEN: case S1_GREEN:
setS1ToGreen(); setS1ToGreen();
if(Timer_getTick() - g_startMS >= delayLong) { if(Timer_getTick() - g_startMS >= delayLong) {
g_startMS=Timer_getTick(); g_startMS=Timer_getTick();
StoplightState = S1_YELLOW; StoplightState = S1_YELLOW;
@ -182,8 +184,6 @@ runStopLightTask2(void)
case S1_GREEN: case S1_GREEN:
setS1ToGreen(); setS1ToGreen();
// Wait 30 seconds before allowing the button to be pressed again
if(Timer_getTick() - g_startMS >= delayLong) { if(Timer_getTick() - g_startMS >= delayLong) {
g_startMS=Timer_getTick(); g_startMS=Timer_getTick();
StoplightState = WAIT; StoplightState = WAIT;

View File

@ -5,6 +5,7 @@
* Author: n0x * Author: n0x
*/ */
#ifndef STOPLIGHT_H_ #ifndef STOPLIGHT_H_
#define STOPLIGHT_H_ #define STOPLIGHT_H_

View File

@ -0,0 +1,73 @@
/*
* 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();
if(g_ledStatus % 2 == 0){
Led1_On();
} else {
Led1_Off();
}
g_ledStatus++;
}
}

View 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_ */

View File

@ -13,13 +13,13 @@ volatile uint16_t cntr = 0;
void void
Timer_init (void){ Timer_init (void){
TCCR0A |= (1<<1); /* Set WGM01 to high to enable CTC mode */ TCCR0A |= (1<<1); /* Set WGM01 to high to enable CTC mode */
TCCR0B |= (1<<1); /* Set the clock select bit to pre-scaler 2 TCCR0B |= (11<<0); /* Set the clock select bit to pre-scaler 3
1MHz / 8 (pre-scaler 2) ==> 125KHz */ 16MHz / 64 (pre-scaler 3) ==> 250KHz */
TIMSK0 |= (1<<1); /* Set OCIE0A to high to rise an interrupt when the counter matches OCR0A */ TIMSK0 |= (1<<1); /* Set OCIE0A to high to rise an interrupt when the counter matches OCR0A */
OCR0A = 125; /* Set the Output Compare Register 0 A to 125 to trigger interrupt every 1ms OCR0A = 250 ; /* Set the Output Compare Register 0 A to 125 to trigger interrupt every 1ms
1MHz / 8 (pre-scaler 2) / 125 ==> 1KHz (1ms) */ 16MHz / 64 (pre-scaler 3) / 250 ==> 1KHz (1ms) */
} }

106
EmbeddedSystemsTHM/UART.c Normal file
View File

@ -0,0 +1,106 @@
/*
* UART.c
*
* Created: 18/11/2021 17:13:25
* Author: n0x
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <string.h>
#include "UART.h"
volatile int enabled = 1;
/* Schreiben Sie eine Funktion, welche die UART Schnittstelle für das Senden von
Daten mit
9600Baud,
8 Bit,
keine Parität und
1 Stopbit
initialisiert. */
void
uart_init(void)
{
/* set BAUD rate to 9600 */
UBRR0 = 103;
/* Async UART */
UCSR0C |= (0<<UMSEL00)|(0<<UMSEL01);
/* Disable Parity */
UCSR0C |= (0<<UPM00)|(0<<UPM01);
/* 8 Bit */
UCSR0C |= (1<<UCSZ00)|(1<<UCSZ01);
/* 1 Stopbit */
UCSR0C |= (0<<USBS0);
/* enable send */
UCSR0B = (1<<TXEN0);
}
/* Schreiben Sie eine Funktion, welche einen String übergeben bekommt, und diesen
auf der UART Schnittstelle ausgibt. */
void
uart_send (char* string){
int len = strlen(string);
for(int i = 0; i < len; i++){
while (!( UCSR0A & (1<<UDRE0)) );
UDR0 = string[i];
}
}
/* Schreiben Sie eine Funktion, welche die UART Schnittstelle für das Senden von Daten mit
9600Baud,
8 Bit,
keine Parität und
1 Stoppbit
initialisiert. Nutzen Sie dabei ein Uart-Interrupt. */
void
uart_init_isr(void)
{
/* set BAUD rate to 9600 */
UBRR0 = 103;
/* Async UART */
UCSR0C |= (0<<UMSEL00)|(0<<UMSEL01);
/* Disable Parity */
UCSR0C |= (0<<UPM00)|(0<<UPM01);
/* 8 Bit */
UCSR0C |= (1<<UCSZ00)|(1<<UCSZ01);
/* 1 Stopbit */
UCSR0C |= (0<<USBS0);
/* enable send */
UCSR0B |= (1<<TXEN0);
/* enable interrupt */
UCSR0B |= (1<<UDRIE0);
}
/* Schreiben Sie eine Funktion, welche einen String übergeben bekommt, und diesen
auf der UART Schnittstelle ausgibt. (Interrupts) */
void
uart_send_isr(char* string)
{
int i = 0;
while( i < strlen(string)){
if(enabled){
UDR0 = string[i];
i++;
enabled = 0;
}
}
}
ISR(USART0_UDRE_vect){
enabled = 1;
}

21
EmbeddedSystemsTHM/UART.h Normal file
View File

@ -0,0 +1,21 @@
/*
* UART.h
*
* Created: 18/11/2021 17:13:16
* Author: n0x
*/
#ifndef UART_H_
#define UART_H_
void uart_init(void);
void uart_send(char* string);
void uart_init_isr(void);
void uart_send_isr(char* string);
int checkIfBlocked(void);
#endif /* UART_H_ */

View File

@ -8,21 +8,15 @@
#include "Led.h" #include "Led.h"
#include "Taster.h" #include "Taster.h"
#include "Timer.h" #include "Timer.h"
#include "Tasks.h"
#include "StopLight.h" #include "StopLight.h"
#include "UART.h"
#include <avr/interrupt.h> #include <avr/interrupt.h>
#define F_CPU 1000000 #define F_CPU 1000000
#include <util/delay.h> #include <util/delay.h>
/* function declarations */ /* function declarations */
void buttonTest (void); void buttonTest (void);
void task1 (void);
void task2 (void);
/* global variables */
volatile int g_counter = 0;
uint8_t g_ledStatus = 0;
uint16_t g_startMS;
/* functions */ /* functions */
@ -31,20 +25,28 @@ main (void)
{ {
sei(); sei();
/* Initialize the LEDs and Buttons */ /* Initialize the LEDs, Buttons, etc. */
Led_init(); //Led_init();
Taster_init(); //Taster_init();
Timer_init(); //Timer_init();
//initTasks();
//uart_init();
uart_init_isr();
g_startMS=Timer_getTick();
while (1) while (1)
{ {
//buttonTest(); //buttonTest();
//task1(); //runningLight();
//task2(); //blinkLedWithTimer();
//runStopLightTask1(); //runStopLightTask1();
runStopLightTask2(); //runStopLightTask2();
//uart_send ("Hello World\n");
uart_send_isr ("Hello World\n");
} }
} }
@ -57,56 +59,5 @@ buttonTest (void)
Taster4_get() ? Led4_On() : Led4_Off(); Taster4_get() ? Led4_On() : Led4_Off();
} }
/* Task 1 (2021-10-21) */
void
task1 (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
task2 (void)
{
if(Timer_getTick() - g_startMS >= 1000) { /* Wait 1000ms before switching LED1 */
g_startMS=Timer_getTick();
if(g_ledStatus % 2 == 0){
Led1_On();
} else {
Led1_Off();
}
g_ledStatus++;
}
}