P5 initial

This commit is contained in:
S170H
2024-01-20 22:27:43 +01:00
parent 237e085b51
commit 6655d01102
26 changed files with 1132 additions and 453 deletions

Binary file not shown.

34
Abgaben/P4/keys.c Normal file
View File

@@ -0,0 +1,34 @@
#include "keys.h"
void setupTimer0() {
// Configure Timer0 for CTC mode
TCCR0A |= (1 << WGM01); // Set Waveform Generation Mode bits for CTC
TCCR0B |= (1 << CS01) | (1 << CS00); // Set prescaler to 64
OCR0A = 249; // Set Output Compare Register to 249 for ~1ms interrupt
TIMSK0 |= (1 << OCIE0A); // Enable Timer0 Output Compare A Match interrupt
}
void setupRegisters(){
DDRD = 0xff; // Data direction register D (D0 -> D6) as output
// PORTD = 0xff; // setting bit for 7-segment show 0
// => spielt keine Rolle, wird von der while Schleife <20>bernommen
DDRB = 0x01; // Data direction register B (B0) as output and DDRB (B1, B2) as input
PORTB |= (1 << PINB1) | (1 << PINB2) ; // setting bit for teens B0=0 and SW1-2
// PINB=0 => start with LED3 (Einer)
// mit unserem Atmega nicht m<>glich, da keine PCINT8_vect und PCINT9_vect vorhanden
// DDRC = 0x00; // Data direction register C as input for SW1 and SW2
// PORTC |= (1<<PINC0) | (1<<PINC1); // settings pins for SW1 -> PINC0 and SW2 -> PINC1
cli(); // clear global interrupt flag: interrupts will be immediately disabled
PCICR |= 0x01; // Pin Change Interrupt Control Register: turn on PortB
PCMSK0 |= (1 << PINB1) | (1 << PINB2); // trigger interrupt when SW1 or SW2 is pressed
sei(); // set global interrupt enable
}
void init(){
setupTimer0();
setupRegisters();
}

12
Abgaben/P4/keys.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef KEYS_H_
#define KEYS_H_
#include <avr/io.h>
#include <avr/interrupt.h>
void setupTimer0();
void setupRegisters();
void init();
#endif /* KEYS_H_ */

12
Abgaben/P4/main.c Normal file
View File

@@ -0,0 +1,12 @@
#include "keys.h"
#include "sevenseg.h"
int main(void) {
init();
while(1) {
display();
}
}

71
Abgaben/P4/sevenseg.c Normal file
View File

@@ -0,0 +1,71 @@
#include "sevenseg.h"
//-Timer---------------------------------------
volatile uint32_t systemClock = 0;
ISR(TIMER0_COMPA_vect) {
systemClock++;
}
uint32_t getSystemClock(){
return systemClock;
}
void waitFor(uint32_t ms) {
uint32_t endTime = getSystemClock() + ms;
while (getSystemClock() != endTime);
}
//-Counter-------------------------------------
volatile int count = 0;
ISR(PCINT0_vect) { // every 10ms interrupt is triggered
if (!(PINB & (1<<PINB1))) { // count down => sw1 is pressed
if (count == 0) count = 100; // 100 - 1 = 99
count--;
}
if (!(PINB & (1<<PINB2))) { // count up => sw2 is pressed
if (count == 99) count = -1; // -1 + 1 = 0
count++;
}
}
//-Display--------------------------------------
void display() {
/*
logical 0: on
logical 1: off
.gfe dcba HEX int
0 : 1100 0000 = C0 192
1 : 1111 1001 = F9 249
2 : 1010 0100 = A4 164
3 : 1011 0000 = 30 48
4 : 1001 1001 = 99 153
5 : 1001 0010 = 92 146
6 : 1000 0010 = 82 130
7 : 1111 1000 = F8 248
8 : 1000 0000 = 80 128
9 : 1001 0000 = 90 144
*/
int numbersAsSegments[10] = { 192, 249, 164, 48, 153, 146, 130, 248, 128, 144 }; // display number from 0 to 9;
volatile int led_Einer = numbersAsSegments[(int) count % 10]; // Segmentanzeige f<>r Zehner
volatile int led_Zehner = numbersAsSegments[(int) (count - (count % 10)) / 10]; // Segmentanzeige f<>r Einer
PORTD = led_Einer;
//_delay_ms(10);
waitFor(10);
PORTB ^= (1 << PINB0);
PORTD = led_Zehner;
//_delay_ms(10);
waitFor(10);
PORTB ^= (1 << PINB0);
}

12
Abgaben/P4/sevenseg.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef SEVENSEG_H_
#define SEVENSEG_H_
#define F_CPU 16000000UL // 16MHz clock frequency
#include <util/delay.h>
#include <avr/interrupt.h>
#include "avr/io.h"
uint32_t getSystemClock();
void waitFor(uint32_t ms);
void display();
#endif /* SEVENSEG_H_ */