pic_therm

PIC16F877A and BME280 thermometer
git clone git://git.margiolis.net/pic_therm.git
Log | Files | Refs | README | LICENSE

main.c (2551B)


      1 #include "extern.h"
      2 #include "bme280.h"
      3 #include "i2c.h"
      4 #include "lcd.h"
      5 #include "tmr0.h"
      6 #include "util.h"
      7 
      8 static __code uint16_t __at (_CONFIG) __configword =
      9     _FOSC_HS & _WDTE_OFF & _PWRTE_ON & _LVP_OFF & _WRT_OFF & _BOREN_ON &
     10     _CPD_OFF & _CP_OFF;
     11 
     12 /*
     13  * TODO: turn off unneeded modules (e.g tmr1..) to minimize consumption.
     14  */
     15 
     16 static void	ctx_main(void);
     17 static void	ctx_uptime_maxtp(void);
     18 static void	print_tp(int32_t);
     19 static void	led_blink(void);
     20 static void	button_debounce(void);
     21 
     22 static uint32_t	timecnt = 0;	/* Seconds passed since start */
     23 static int	f_ctx = 0;	/* Change context */
     24 static uint32_t	humid;		/* Current humidity */
     25 static int32_t	tp;		/* Current temperature */
     26 static int32_t	maxtp = -99999;	/* Max temperature */
     27 static char	buf[BUFSIZ+1] = {0}; /* Generic buffer */
     28 
     29 #define LCD_PUTS_INT(buf, v) do {		\
     30 	memset(buf, 0, sizeof(buf));		\
     31 	lcd_puts(itoa(&buf[sizeof(buf)-1], v));	\
     32 } while (0)
     33 
     34 static void
     35 ctx_main(void)
     36 {
     37 	lcd_cmd(LCD_CURS_ROW1);
     38 	lcd_puts("Temp: ");
     39 	print_tp(tp);
     40 
     41 	lcd_cmd(LCD_CURS_ROW2);
     42 	lcd_puts("Humid: ");
     43 	LCD_PUTS_INT(buf, humid / 1024);
     44 	lcd_putc('.');
     45 	LCD_PUTS_INT(buf, ((humid * 100) / 1024) % 100);
     46 	lcd_putc('%');
     47 }
     48 
     49 static void
     50 ctx_uptime_maxtp(void)
     51 {
     52 	lcd_cmd(LCD_CURS_ROW1);
     53 	lcd_puts("T: ");
     54 	LCD_PUTS_INT(buf, timecnt);
     55 
     56 	lcd_cmd(LCD_CURS_ROW2);
     57 	lcd_puts("Max: ");
     58 	print_tp(maxtp);
     59 }
     60 
     61 static void
     62 print_tp(int32_t tp)
     63 {
     64 	if (tp < 0) {
     65 		tp = -tp;
     66 		lcd_putc('-');
     67 	}
     68 	LCD_PUTS_INT(buf, tp / 100);
     69 	lcd_putc('.');
     70 	LCD_PUTS_INT(buf, tp % 100);
     71 	lcd_puts("\337C   ");
     72 }
     73 
     74 #undef LCD_PUTS_INT
     75 
     76 static void
     77 led_blink(void)
     78 {
     79 	LED_PORT ^= 1;
     80 	/*
     81 	 * Increment here since this function is called every 1 sec.
     82 	 * No need to create another timer callback.
     83 	 */
     84 	timecnt++;
     85 }
     86 
     87 static void
     88 button_debounce(void)
     89 {
     90 	static uint8_t cnt = 0;
     91 
     92 	/* Button is pressed */
     93 	if (BTN_PORT == 0) {
     94 		if (cnt == 0) {
     95 			/* Actual button functionality goes here. */
     96 			f_ctx = 1;
     97 			cnt++;
     98 		}
     99 		cnt = BTN_DEBOUNCE_TIME_MS;
    100 	} else if (cnt != 0) {
    101 		f_ctx = 0;
    102 		cnt--;
    103 	}
    104 }
    105 
    106 void
    107 main(void)
    108 {
    109 	tmr0_init();
    110 	lcd_init();
    111 	tmr0_set_event(&led_blink, 1000);
    112 	tmr0_set_event(&button_debounce, 1);
    113 	i2c_init(I2C_MASTER, I2C_SLEW_OFF, I2C_CLK_1MHZ);
    114 	if (bme280_init() < 0) {
    115 		lcd_puts("BME280 error");
    116 		for (;;);
    117 	}
    118 
    119 	BTN_TRIS = INPUT;
    120 	LED_PORT = 1; /* LED on */
    121 	LED_TRIS = OUTPUT;
    122 
    123 	for (;;) {
    124 		if ((tp = bme280_read_temp()) > maxtp)
    125 			maxtp = tp;
    126 		humid = bme280_read_humid();
    127 		lcd_cmd(LCD_CLEAR);
    128 		if (!f_ctx)
    129 			ctx_main();
    130 		else
    131 			ctx_uptime_maxtp();
    132 		tmr0_delay_ms(1000);
    133 	}
    134 }