when the segments are seven

This commit is contained in:
Lea 2023-08-26 19:08:09 +02:00
parent 600de71fd8
commit 3021ac8c8e
Signed by: Lea
GPG key ID: 1BAFFE8347019C42
3 changed files with 60 additions and 4 deletions

34
lib/7segment.c Normal file
View file

@ -0,0 +1,34 @@
#include <avr/io.h>
#include <stdbool.h>
/**
* Display pins should be mapped like this:
* a b c d e f g DP
* 0 1 2 3 4 5 6 7
*
* TODO pass register as parameter
*/
void set_display_raw(volatile uint8_t * header, uint8_t data) {
*header = data;
}
void set_display(volatile uint8_t * header, uint8_t value, bool period) {
uint8_t byte = period << 7;
switch(value) {
case 0: byte |= 0x3f; break;
case 1: byte |= 0x06; break;
case 2: byte |= 0x5b; break;
case 3: byte |= 0x4f; break;
case 4: byte |= 0x66; break;
case 5: byte |= 0x6d; break;
case 6: byte |= 0x7d; break;
case 7: byte |= 0x07; break;
case 8: byte |= 0x7f; break;
case 9: byte |= 0x6f; break;
default: byte &= 0x10000000; break;
}
set_display_raw(header, byte);
}

5
lib/7segment.h Normal file
View file

@ -0,0 +1,5 @@
#include <avr/io.h>
void set_display_raw(volatile uint8_t * header, uint8_t data);
void set_display(volatile uint8_t * header, uint8_t value, bool period);

25
main.c
View file

@ -1,8 +1,11 @@
#define __AVR_ATmega2560__ #define __AVR_ATmega2560__
#define F_CPU 16000000UL #define F_CPU 16000000UL
#define DISPLAY PORTF
#include <avr/io.h> #include <avr/io.h>
#include <util/delay.h> #include <util/delay.h>
#include <stdbool.h>
#include "lib/7segment.h"
void main() { void main() {
// DDRB = 0b10000000; // DDRB = 0b10000000;
@ -15,12 +18,26 @@ void main() {
// } // }
DDRF = 0b11111111; DDRF = 0b11111111;
PORTF = 0b11111111; DDRK = 0b00000000;
PORTK = 0b00000001; // enable pull-up resistor
uint8_t a = 0; uint8_t val = 0;
set_display(&PORTF, val, false);
while(1) { while(1) {
PORTF = a++; loop_until_bit_is_clear(PINK, PINK0);
_delay_ms(10);
if (val >= 9) {
val = 0xff; // will overflow to 0 when incremented
}
set_display(&PORTF, ++val, true);
// debounce
_delay_ms(200);
loop_until_bit_is_set(PINK, PINK0);
set_display(&PORTF, val, false);
} }
} }