Raw Text Content QR
mole-sloth-rabbit



/*
 * menu.c
 *
 * Created: 30.05.2025 08:26:15
 * Author : Student
 */ 


#include <avr/io.h>
#define F_CPU 11059200UL
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include "lcd.h"
#include "ds18b20.h"
#include "spi.h"
#include "I2C.h"

#define HIH8120_ADDR 0x27

volatile int8_t ekran = 0;      // 0..4
volatile int8_t subscreen = 0;  // 0 lub 1
volatile uint8_t running = 1;   // sekundnik ON/OFF






// --- Enkoder INT0 ---
// PD2 - A (CLK)
// PD3 - B (DT)
// PD4 - SW (przycisk)
void encoder_init(void) {
	DDRD &= ~((1 << PD2) | (1 << PD3) | (1 << PD4));
	PORTD |= (1 << PD2) | (1 << PD3) | (1 << PD4); // Pull-up
	MCUCR |= (1 << ISC01); MCUCR &= ~(1 << ISC00); // INT0 on falling edge
	GICR |= (1 << INT0);
	sei();
}
ISR(INT0_vect) {
	if (subscreen == 1) {
		subscreen = 0; // wyj�cie z podmenu
		return;
	}

	if (PIND & (1 << PD3)) ekran++;
	else ekran--;

	if (ekran > 5) ekran = 0;
	if (ekran < 0) ekran = 5;
	subscreen = 0;
}


int main(void) {
	lcd_init();
	encoder_init();
	ds18b20_init();
	SPI_MasterInit();
	_delay_ms(50);
	DDRB = 0xFF;
	PORTB = 0x00;

	uint16_t sekundy = 0;
	char buf[17];

	while (1) {
		lcd_clear();

		if (!(PIND & (1 << PD4))) {
			_delay_ms(300);
			if (ekran == 2) {
				lcd_show_startstop(running);
				running = !running;
				} else if (ekran != 3) {
				lcd_show_loading();
				subscreen = 1;
			}
			while (!(PIND & (1 << PD4))) _delay_ms(10);
		}

		switch (ekran) {
			case 0:
			if (subscreen == 0) {
				lcd_gotoxy(0, 0); lcd_puts("Imie, nazwisko");
				} else {
				lcd_gotoxy(0, 0); lcd_puts("Daniel");
				lcd_gotoxy(0, 1);
				lcd_send_byte(0, 1); // �
				lcd_puts("wi");
				lcd_send_byte(1, 1); // "�"
				lcd_puts("cki");
			}
			break;

			case 1:
			if (subscreen == 0) {
				lcd_gotoxy(0, 0); lcd_puts("Numer indeksu");
				} else {
				lcd_gotoxy(0, 0); lcd_puts("275777");
			}
			break;

			case 2:
			lcd_gotoxy(0, 0); lcd_puts("Czas:");
			lcd_gotoxy(0, 1);
			sprintf(buf, "%us", sekundy);
			lcd_puts(buf);
			if (running) sekundy++;
			_delay_ms(1000);
			break;

			case 3:
			lcd_gotoxy(0, 0); lcd_puts("Resetuj czas");
			lcd_gotoxy(0, 1); lcd_puts("Wcisnij przycisk");
			break;

			case 4:
			if (subscreen == 0) {
				lcd_gotoxy(0, 0); lcd_puts("Temperatura");
				} else {
				lcd_gotoxy(0, 0); lcd_puts("Temperatura:");
				int16_t temp_raw = ds18b20_read_temp();
				if (temp_raw == -1000) {
					lcd_gotoxy(0, 1); lcd_puts("Brak czujnika");
					} else {
					int16_t temp_c = temp_raw / 16;
					uint16_t temp_frac = ((temp_raw & 0x0F) * 625) / 100;
					lcd_gotoxy(0, 1);
					sprintf(buf, "%d.%02d%cC", temp_c, temp_frac, 223);
					lcd_puts(buf);
					

				}
			}
			break;
			case 5:
			if (subscreen == 0) {
				lcd_gotoxy(0, 0); lcd_puts("Temp z czujnika");
				lcd_gotoxy(0, 1); lcd_puts("SPI TC77");
				} else {
				lcd_gotoxy(0, 0); lcd_puts("Temperatura:");
				int16_t raw = readTemperatureRaw_TC77();

				if (raw == 0xFFFF) {
					lcd_gotoxy(0, 1); lcd_puts("Brak SPI danych");
					} else {
					float temp = raw * 0.0625;
					int8_t cal = (int8_t)temp;
					uint8_t frac = (uint8_t)((temp - cal) * 100);

					lcd_gotoxy(0, 1);
					sprintf(buf, "%d.%02d%cC", cal, frac, 223);
					lcd_puts(buf);
				}
			}
			break;
			


		}

		_delay_ms(300);
	}
}

Read 5 times, last 46 minutes ago