Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define F_CPU 7372800UL
- #include <stdlib.h>
- #include <string.h>
- #include <avr/interrupt.h>
- #include <util/delay.h>
- #include "adc.h"
- #include "I2C_Master_H_file.h"
- #include "bmp280.h"
- #include "lcd.h"
- uint16_t buzzer_on_off = 1; // Sets buzzer on or off
- uint16_t display = 0; // Displays a certain module value
- uint16_t buzzer_flag = 1; // Enables and disables the buzzer
- void dbounce() {
- _delay_ms(400);
- GIFR = _BV(INTF1);
- }
- void set_buzzer() {
- if (buzzer_flag == 1) {
- buzzer_on_off = 1;
- buzzer_flag = 0;
- }
- }
- void reset_buzzer() {
- buzzer_flag = 1;
- buzzer_on_off = 0;
- PORTB = _BV(7);
- }
- void warning_smoke() {
- lcd_clrscr();
- lcd_gotoxy(2, 0);
- lcd_puts("UPOZORENJE!");
- lcd_gotoxy(3, 1);
- lcd_puts("LOS ZRAK!");
- }
- void warning_temp() {
- lcd_clrscr();
- lcd_gotoxy(2, 0);
- lcd_puts("UPOZORENJE!");
- lcd_gotoxy(0, 1);
- lcd_puts("PREVISOKA TEMP!");
- }
- void warning_moist() {
- lcd_clrscr();
- lcd_gotoxy(2, 0);
- lcd_puts("UPOZORENJE!");
- lcd_gotoxy(0, 1);
- lcd_puts("VLAGA U APARATU!");
- }
- void display_temp(int16_t I_Temp, int16_t D_temp) {
- char data[5];
- lcd_clrscr();
- lcd_gotoxy(0,0);
- lcd_puts("Temperatura:");
- itoa(I_Temp, data, 10);
- lcd_gotoxy(0,1);
- lcd_puts(data);
- lcd_puts(".");
- itoa(D_temp, data, 10);
- lcd_puts(data);
- lcd_putc(223);
- lcd_puts("C ");
- }
- void display_pressure(int16_t I_Pres, int16_t D_Pres) {
- char data[5];
- lcd_clrscr();
- lcd_gotoxy(0, 0);
- lcd_puts("Tlak:");
- itoa(I_Pres, data, 10);
- lcd_gotoxy(0, 1);
- lcd_puts(data);
- lcd_puts(".");
- itoa(D_Pres, data, 10);
- lcd_puts(data);
- lcd_puts(" Hg");
- }
- void display_smoke(uint16_t adc) {
- lcd_clrscr();
- lcd_gotoxy(0, 0);
- lcd_puts("Kvaliteta zraka:");
- lcd_gotoxy(0, 1);
- if (adc < 65) {
- lcd_puts("Dobra");
- } else if (adc < 85) {
- lcd_puts("Povisen CO2");
- }
- }
- void display_moist(uint16_t adc) {
- lcd_clrscr();
- lcd_gotoxy(0, 0);
- lcd_puts("Vlaga u aparatu:");
- lcd_gotoxy(0, 1);
- if (adc < 300) {
- lcd_puts("Nema");
- }
- }
- void display_warning(int16_t I_Temp, uint16_t air_value, uint16_t moisture_value) {
- if (air_value >= 85) {
- set_buzzer();
- warning_smoke();
- } else if (I_Temp >= 40) {
- set_buzzer();
- warning_temp();
- } else if (moisture_value >= 250) {
- set_buzzer();
- warning_moist();
- } else reset_buzzer();
- if (buzzer_on_off == 1) PORTB &= 0b01111111; //Set PB7 to 0 (turns on the buzzer)
- }
- ISR(INT1_vect) {
- display = (display + 1) % 4;
- dbounce();
- }
- ISR(INT0_vect) {
- buzzer_on_off = 0;
- buzzer_flag = 0;
- PORTB = _BV(7);
- }
- ISR(TIMER0_COMP_vect) {}
- ISR(ADC_vect) {}
- int main(void) {
- DDRA = 0b11011110; //Enabled PIN0 for soil-moisture detector and PIN5 for air-quality detector
- PORTA = _BV(5) | _BV(0);
- DDRB = _BV(7); //Enabled buzzer for warnings
- PORTB = _BV(7);
- //Setting contrast for the LCD display
- DDRD = _BV(4);
- TCCR1A = _BV(COM1B1) | _BV(WGM10);
- TCCR1B = _BV(WGM12) | _BV(CS11);
- OCR1B = 24;
- //Setting frequency for ADC
- TCCR0 = _BV(WGM01) | _BV(CS02) | _BV(CS00);
- OCR0 = 72;
- //Enabling external interrupts
- MCUCR = _BV(ISC01) | _BV(ISC11);
- GICR = _BV(INT0) | _BV(INT1);
- //Enabling internal interrupts
- TIMSK = _BV(OCIE0);
- //Settings for ADC
- ADMUX = _BV(REFS0);
- ADCSRA = _BV(ADEN) | _BV(ADATE) | _BV(ADIE) | _BV(ADPS2) | _BV(ADPS1);
- SFIOR = _BV(ADTS1) | _BV(ADTS0);
- lcd_init(LCD_DISP_ON);
- lcd_clrscr();
- bmp280_init();
- sei();
- uint16_t moisture_value = 0, air_value = 0;
- while(1) {
- bmp280_measure();
- //Get temperature value
- uint16_t temperature = bmp280_gettemperature();
- int16_t I_Temp = temperature / 100;
- int16_t D_Temp = temperature % 10;
- //Get pressure value
- uint16_t pressure = bmp280_getpressure();
- int16_t I_Pres = pressure / 1000;
- int16_t D_Pres = pressure % 10;
- //Multiplexing between PA0 (soil-moisture detector) and PA5 (air-quality detector)
- if (ADMUX == _BV(REFS0)) {
- moisture_value = ADC;
- ADMUX = _BV(REFS0) | _BV(MUX0) | _BV(MUX2);
- } else {
- air_value = ADC;
- ADMUX = _BV(REFS0);
- }
- if (display == 0) display_temp(I_Temp, D_Temp);
- else if (display == 1) display_pressure(I_Pres, D_Pres);
- else if (display == 2) display_smoke(air_value);
- else if (display == 3) display_moist(moisture_value);
- display_warning(I_Temp, air_value, moisture_value);
- _delay_ms(500);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement