Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
2.4 kB
2
Indexable
/*
 * GccApplication2.c
 *
 * Created: 2022-05-31 15:47:17
 * Author : Student
 */

#include <avr/io.h>
#include <avr/interrupt.h>
#define F_CPU 1000000
#include <util/delay.h>

#include "hd44780.h"
#include <stdio.h>

//#define TIME_DELTA 1562
#define TIME_DELTA 6250

volatile int g_timer_time = 500;

void print_padded(const char* s1, int s2_length) {
	lcd_puts(s1);
	for (int i = 0; i < 16 - strlen(s1) - s2_length; ++i) {
		lcd_puts(" ");
	}
}

void update_lcd() {
	char buf[256];
	itoa(g_timer_time, buf, 10);
	LCD_CLEAR;
	LCD_LOCATE(0, 0);
	print_padded("NAZWISKO", strlen("I."));
	lcd_puts("I.");
	LCD_LOCATE(0, 1);
	print_padded("TIMER1 ", strlen(buf) + strlen(" ms"));
	lcd_puts(buf);
	lcd_puts(" ms");
}

ISR(INT0_vect) {
	//PORTA++;
	if (OCR1A + TIME_DELTA > OCR1A) {
		TCNT1 = 0;
		OCR1A += TIME_DELTA;
		g_timer_time += 50;
		update_lcd();
	}
}

ISR(INT1_vect) {
	//PORTA--;
	if (OCR1A - TIME_DELTA < OCR1A && OCR1A - TIME_DELTA > 0) {
		TCNT1 = 0;
		OCR1A -= TIME_DELTA;
		g_timer_time -= 50;
		update_lcd();
	}
}

ISR(INT2_vect) {
	//PORTA = 0;
	if (TCCR1B) {
		TCCR1B = 0;
	} else {
		//TCCR1B = (1 << CS11) | (1 << CS10) | (1 << WGM12);
		TCCR1B = (1 << CS11) | (1 << WGM12);
	}
	
}

ISR(TIMER1_COMPA_vect) {
	//PORTA = ~PORTA;
	PORTB = (PORTB & ~(1)) | ((PORTB & 1) ^ 1);
}

void enable_interrupt() {
	// INT2
	DDRB = 0;
	PORTB = 1 << PB2;
	
	// INT0, INT1
	DDRD = 0;
	PORTD = (1 << PD2) | (1 << PD3);
	
	MCUCR = (1 << ISC01) | (1 << ISC11);
	MCUCSR &= ~(1 << ISC2);
	
	GICR = (1 << INTF0) | (1 << INTF1) | (1 << INTF2);
	
	sei();
}

void init_led() {
	DDRA = 0xff;
	PORTA = 0xff;
}

void zad1() {
	init_led();
	enable_interrupt();
	
	while (1);
}

void zad2() {
	init_led();
	enable_interrupt();
	PORTA = 0xff;
	
	TCCR1B = (1 << CS11) | (1 << CS10) | (1 << WGM12);
	
	TIMSK = 1 << OCIE1A;
	
	// 0.5 * (1000000 / 64) = 7812.5
	OCR1A = 7812;
	TCNT1 = 0;
	
	while (1);
}

void zad3() {
	init_led();
	enable_interrupt();
	PORTA = 0xff;
	
	DDRB |= 1;
	lcd_init();
	LCD_DISPLAY(LCDDISPLAY);
	update_lcd();
	
	TCCR1B = (1 << CS11) | (1 << WGM12);
	
	TIMSK = 1 << OCIE1A;
	// 0.5 * (1000000 / 8) = 62500
	OCR1A = 62500;
	TCNT1 = 0;
	
	while (1);
}

int main(void)
{
	//zad1();
	//zad2();
	zad3();
}