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

master
_N0x 2 years ago
parent db16d89b80
commit 4f5b174bb1

@ -166,6 +166,12 @@
<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>
@ -178,6 +184,12 @@
<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>

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

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

@ -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++;
}
}

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

@ -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 |= (1<<1); /* Set the clock select bit to pre-scaler 2
1MHz / 8 (pre-scaler 2) ==> 125KHz */
TCCR0A |= (1<<1); /* Set WGM01 to high to enable CTC mode */
TCCR0B |= (11<<0); /* Set the clock select bit to pre-scaler 3
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
1MHz / 8 (pre-scaler 2) / 125 ==> 1KHz (1ms) */
OCR0A = 250 ; /* Set the Output Compare Register 0 A to 125 to trigger interrupt every 1ms
16MHz / 64 (pre-scaler 3) / 250 ==> 1KHz (1ms) */
}

@ -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;
}

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

@ -8,21 +8,15 @@
#include "Led.h"
#include "Taster.h"
#include "Timer.h"
#include "Tasks.h"
#include "StopLight.h"
#include "UART.h"
#include <avr/interrupt.h>
#define F_CPU 1000000
#include <util/delay.h>
/* function declarations */
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 */
@ -31,20 +25,28 @@ main (void)
{
sei();
/* Initialize the LEDs and Buttons */
Led_init();
Taster_init();
Timer_init();
/* Initialize the LEDs, Buttons, etc. */
//Led_init();
//Taster_init();
//Timer_init();
//initTasks();
//uart_init();
uart_init_isr();
g_startMS=Timer_getTick();
while (1)
{
//buttonTest();
//task1();
//task2();
//runningLight();
//blinkLedWithTimer();
//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();
}
/* 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++;
}
}

Loading…
Cancel
Save