Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. #define F_CPU 7372800UL
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <avr/interrupt.h>
  5. #include <util/delay.h>
  6.  
  7. #include "adc.h"
  8. #include "I2C_Master_H_file.h"
  9. #include "bmp280.h"
  10. #include "lcd.h"
  11.  
  12. uint16_t buzzer_on_off = 1; // Sets buzzer on or off
  13. uint16_t display = 0; // Displays a certain module value
  14. uint16_t buzzer_flag = 1; // Enables and disables the buzzer
  15.  
  16. void dbounce() {
  17. _delay_ms(400);
  18. GIFR = _BV(INTF1);
  19. }
  20.  
  21. void set_buzzer() {
  22. if (buzzer_flag == 1) {
  23. buzzer_on_off = 1;
  24. buzzer_flag = 0;
  25. }
  26. }
  27.  
  28. void reset_buzzer() {
  29. buzzer_flag = 1;
  30. buzzer_on_off = 0;
  31. PORTB = _BV(7);
  32. }
  33.  
  34. void warning_smoke() {
  35. lcd_clrscr();
  36. lcd_gotoxy(2, 0);
  37. lcd_puts("UPOZORENJE!");
  38. lcd_gotoxy(3, 1);
  39. lcd_puts("LOS ZRAK!");
  40. }
  41.  
  42. void warning_temp() {
  43.  
  44. lcd_clrscr();
  45. lcd_gotoxy(2, 0);
  46. lcd_puts("UPOZORENJE!");
  47. lcd_gotoxy(0, 1);
  48. lcd_puts("PREVISOKA TEMP!");
  49. }
  50.  
  51. void warning_moist() {
  52. lcd_clrscr();
  53. lcd_gotoxy(2, 0);
  54. lcd_puts("UPOZORENJE!");
  55. lcd_gotoxy(0, 1);
  56. lcd_puts("VLAGA U APARATU!");
  57. }
  58.  
  59. void display_temp(int16_t I_Temp, int16_t D_temp) {
  60.  
  61. char data[5];
  62.  
  63. lcd_clrscr();
  64. lcd_gotoxy(0,0);
  65. lcd_puts("Temperatura:");
  66.  
  67. itoa(I_Temp, data, 10);
  68. lcd_gotoxy(0,1);
  69. lcd_puts(data);
  70. lcd_puts(".");
  71.  
  72. itoa(D_temp, data, 10);
  73. lcd_puts(data);
  74. lcd_putc(223);
  75. lcd_puts("C ");
  76.  
  77. }
  78.  
  79. void display_pressure(int16_t I_Pres, int16_t D_Pres) {
  80.  
  81. char data[5];
  82.  
  83. lcd_clrscr();
  84. lcd_gotoxy(0, 0);
  85. lcd_puts("Tlak:");
  86.  
  87. itoa(I_Pres, data, 10);
  88. lcd_gotoxy(0, 1);
  89. lcd_puts(data);
  90.  
  91. lcd_puts(".");
  92. itoa(D_Pres, data, 10);
  93. lcd_puts(data);
  94. lcd_puts(" Hg");
  95. }
  96.  
  97. void display_smoke(uint16_t adc) {
  98.  
  99. lcd_clrscr();
  100. lcd_gotoxy(0, 0);
  101. lcd_puts("Kvaliteta zraka:");
  102. lcd_gotoxy(0, 1);
  103.  
  104. if (adc < 65) {
  105. lcd_puts("Dobra");
  106. } else if (adc < 85) {
  107. lcd_puts("Povisen CO2");
  108. }
  109.  
  110. }
  111.  
  112. void display_moist(uint16_t adc) {
  113.  
  114. lcd_clrscr();
  115. lcd_gotoxy(0, 0);
  116. lcd_puts("Vlaga u aparatu:");
  117. lcd_gotoxy(0, 1);
  118.  
  119. if (adc < 300) {
  120. lcd_puts("Nema");
  121. }
  122.  
  123. }
  124.  
  125. void display_warning(int16_t I_Temp, uint16_t air_value, uint16_t moisture_value) {
  126.  
  127. if (air_value >= 85) {
  128.  
  129. set_buzzer();
  130. warning_smoke();
  131.  
  132. } else if (I_Temp >= 40) {
  133.  
  134. set_buzzer();
  135. warning_temp();
  136.  
  137. } else if (moisture_value >= 250) {
  138.  
  139. set_buzzer();
  140. warning_moist();
  141.  
  142. } else reset_buzzer();
  143.  
  144. if (buzzer_on_off == 1) PORTB &= 0b01111111; //Set PB7 to 0 (turns on the buzzer)
  145.  
  146. }
  147.  
  148. ISR(INT1_vect) {
  149. display = (display + 1) % 4;
  150. dbounce();
  151. }
  152.  
  153. ISR(INT0_vect) {
  154. buzzer_on_off = 0;
  155. buzzer_flag = 0;
  156. PORTB = _BV(7);
  157. }
  158.  
  159. ISR(TIMER0_COMP_vect) {}
  160. ISR(ADC_vect) {}
  161.  
  162. int main(void) {
  163. DDRA = 0b11011110; //Enabled PIN0 for soil-moisture detector and PIN5 for air-quality detector
  164. PORTA = _BV(5) | _BV(0);
  165.  
  166. DDRB = _BV(7); //Enabled buzzer for warnings
  167. PORTB = _BV(7);
  168.  
  169. //Setting contrast for the LCD display
  170. DDRD = _BV(4);
  171. TCCR1A = _BV(COM1B1) | _BV(WGM10);
  172. TCCR1B = _BV(WGM12) | _BV(CS11);
  173. OCR1B = 24;
  174.  
  175. //Setting frequency for ADC
  176. TCCR0 = _BV(WGM01) | _BV(CS02) | _BV(CS00);
  177. OCR0 = 72;
  178.  
  179. //Enabling external interrupts
  180. MCUCR = _BV(ISC01) | _BV(ISC11);
  181. GICR = _BV(INT0) | _BV(INT1);
  182.  
  183. //Enabling internal interrupts
  184. TIMSK = _BV(OCIE0);
  185.  
  186. //Settings for ADC
  187. ADMUX = _BV(REFS0);
  188. ADCSRA = _BV(ADEN) | _BV(ADATE) | _BV(ADIE) | _BV(ADPS2) | _BV(ADPS1);
  189. SFIOR = _BV(ADTS1) | _BV(ADTS0);
  190.  
  191. lcd_init(LCD_DISP_ON);
  192. lcd_clrscr();
  193. bmp280_init();
  194.  
  195. sei();
  196.  
  197. uint16_t moisture_value = 0, air_value = 0;
  198.  
  199. while(1) {
  200.  
  201. bmp280_measure();
  202.  
  203. //Get temperature value
  204. uint16_t temperature = bmp280_gettemperature();
  205. int16_t I_Temp = temperature / 100;
  206. int16_t D_Temp = temperature % 10;
  207.  
  208. //Get pressure value
  209. uint16_t pressure = bmp280_getpressure();
  210. int16_t I_Pres = pressure / 1000;
  211. int16_t D_Pres = pressure % 10;
  212.  
  213. //Multiplexing between PA0 (soil-moisture detector) and PA5 (air-quality detector)
  214. if (ADMUX == _BV(REFS0)) {
  215.  
  216. moisture_value = ADC;
  217. ADMUX = _BV(REFS0) | _BV(MUX0) | _BV(MUX2);
  218.  
  219. } else {
  220.  
  221. air_value = ADC;
  222. ADMUX = _BV(REFS0);
  223.  
  224. }
  225.  
  226. if (display == 0) display_temp(I_Temp, D_Temp);
  227. else if (display == 1) display_pressure(I_Pres, D_Pres);
  228. else if (display == 2) display_smoke(air_value);
  229. else if (display == 3) display_moist(moisture_value);
  230.  
  231. display_warning(I_Temp, air_value, moisture_value);
  232.  
  233. _delay_ms(500);
  234.  
  235. }
  236. return 0;
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement