Create library for managing LED binary display

This commit is contained in:
2020-11-07 22:45:11 +01:00
parent fde78d96d2
commit f9deb66109
3 changed files with 131 additions and 0 deletions

77
led.c Normal file
View File

@@ -0,0 +1,77 @@
#include <avr/io.h>
#include <avr/interrupt.h>
#include "led.h"
#define SET_LED(PORT, IDX, NUM) PORT = ((PORT & ~(1<<(IDX))) | (~(NUM) & 1<<(IDX)))
volatile uint8_t led_hour;
volatile uint8_t led_minute;
volatile uint8_t led_second;
void led_init(void)
{
// Set outputs
ANODES_DIR |= HOUR_ANODE | MINUTE_ANODE | SECOND_ANODE;
LED0_DIR |= LED0;
LED1_DIR |= LED1;
LED2_DIR |= LED2;
LED3_DIR |= LED3;
LED4_DIR |= LED4;
LED5_DIR |= LED5;
// Clear LEDs
ANODES_PORT = HOUR_ANODE | MINUTE_ANODE | SECOND_ANODE;
LED0_PORT |= LED0;
LED1_PORT |= LED1;
LED2_PORT |= LED2;
LED3_PORT |= LED3;
LED4_PORT |= LED4;
LED5_PORT |= LED5;
TCCR0 |= (1<<WGM01); // CTC Mode
TCCR0 |= (1<<CS02) | (1<<CS00); // Prescaler 1024
OCR0 = 38; // 200Hz
TIMSK |= (1<<OCIE0); // Enable CTC interruptions
}
ISR(TIMER0_COMP_vect)
{
static uint8_t counter = 1;
switch(counter) {
case 1:
ANODES_PORT = ~HOUR_ANODE;
SET_LED(LED0_PORT, 0, led_hour);
SET_LED(LED1_PORT, 1, led_hour);
SET_LED(LED2_PORT, 2, led_hour);
SET_LED(LED3_PORT, 3, led_hour);
SET_LED(LED4_PORT, 4, led_hour);
SET_LED(LED5_PORT, 5, led_hour);
break;
case 2:
ANODES_PORT = ~MINUTE_ANODE;
SET_LED(LED0_PORT, 0, led_minute);
SET_LED(LED1_PORT, 1, led_minute);
SET_LED(LED2_PORT, 2, led_minute);
SET_LED(LED3_PORT, 3, led_minute);
SET_LED(LED4_PORT, 4, led_minute);
SET_LED(LED5_PORT, 5, led_minute);
break;
case 4:
ANODES_PORT = ~SECOND_ANODE;
SET_LED(LED0_PORT, 0, led_second);
SET_LED(LED1_PORT, 1, led_second);
SET_LED(LED2_PORT, 2, led_second);
SET_LED(LED3_PORT, 3, led_second);
SET_LED(LED4_PORT, 4, led_second);
SET_LED(LED5_PORT, 5, led_second);
break;
}
counter <<= 1;
if(counter > 4)
{
counter = 1;
}
}

35
led.h Normal file
View File

@@ -0,0 +1,35 @@
#ifndef __LED_H__
#define __LED_H__
#define LED0_PORT PORTA
#define LED0_DIR DDRA
#define LED0 (1<<0)
#define LED1_PORT PORTA
#define LED1_DIR DDRA
#define LED1 (1<<1)
#define LED2_PORT PORTA
#define LED2_DIR DDRA
#define LED2 (1<<2)
#define LED3_PORT PORTA
#define LED3_DIR DDRA
#define LED3 (1<<3)
#define LED4_PORT PORTA
#define LED4_DIR DDRA
#define LED4 (1<<4)
#define LED5_PORT PORTA
#define LED5_DIR DDRA
#define LED5 (1<<5)
#define ANODES_PORT PORTD
#define ANODES_DIR DDRD
#define HOUR_ANODE (1<<PD5)
#define MINUTE_ANODE (1<<PD6)
#define SECOND_ANODE (1<<PD7)
extern volatile uint8_t led_hour;
extern volatile uint8_t led_minute;
extern volatile uint8_t led_second;
void led_init(void);
#endif

19
main.c Normal file
View File

@@ -0,0 +1,19 @@
#include <avr/io.h>
#include <avr/interrupt.h>
#include "led.h"
int main()
{
led_init();
sei();
led_hour = 0;
led_minute = 0;
led_second = 0;
while(1)
{
}
}