#include #include #include #include #include #include #include "pinout.h" #include "lcd.h" /* PINOUT Keypad - UART RX - receives symbols from STM8 LCD - HD44780 14 - RS (H-data, L-command) 15 - EN (CLK) 21,20,19,18 - D7,D6,D5,D4 RW tied low (always write) TODO backlight switching I2C 16 - SDA0 17 - SCL0 EEPROM 24C32 - addr 1010_000 RTC DS3231 - addr 1101_000 Relay 5, 4, 22, 26 - active high Moisture 28 - ADC2 0-3V */ void setup_output(uint num) { gpio_set_dir(num, GPIO_OUT); gpio_put(num, 0); gpio_set_function(num, GPIO_FUNC_SIO); } // RX interrupt handler void irq_uart() { while (uart_is_readable(uart0)) { uint8_t ch = uart_getc(uart0); // TODO do something useful if (uart_is_writable(uart0)) { uart_putc(uart0, ch); } } } void set_relays(bool one, bool two, bool three, bool four) { gpio_put(PIN_RE1, one); gpio_put(PIN_RE2, two); gpio_put(PIN_RE3, three); gpio_put(PIN_RE1, four); } void open_valve(uint num) { gpio_put(PIN_RE1, num == 1); gpio_put(PIN_RE2, num == 2); gpio_put(PIN_RE3, num == 3); gpio_put(PIN_RE4, num == 4); } uint16_t moisture_read() { adc_select_input(ADC_NUM_MOISTURE); return adc_read(); } int main() { // picotool binary info bi_decl(bi_1pin_with_name(PIN_LED, "LED")); bi_decl(bi_1pin_with_name(PIN_RE1, "RE1")); bi_decl(bi_1pin_with_name(PIN_RE2, "RE2")); bi_decl(bi_1pin_with_name(PIN_RE3, "RE3")); bi_decl(bi_1pin_with_name(PIN_RE4, "RE4")); bi_decl(bi_1pin_with_name(PIN_LCD_RS, "LCD RS")); bi_decl(bi_1pin_with_name(PIN_LCD_E, "LCD CLK")); bi_decl(bi_1pin_with_name(PIN_LCD_D7, "LCD D7")); bi_decl(bi_1pin_with_name(PIN_LCD_D6, "LCD D6")); bi_decl(bi_1pin_with_name(PIN_LCD_D5, "LCD D5")); bi_decl(bi_1pin_with_name(PIN_LCD_D4, "LCD D4")); bi_decl(bi_2pins_with_func(PIN_I2C_SDA, PIN_I2C_SCL, GPIO_FUNC_I2C)); bi_decl(bi_1pin_with_name(PIN_MOISTURE, "ADC (moisture)")); bi_decl(bi_program_description("Zavlaha Kuchynka")); setup_default_uart(); irq_set_exclusive_handler(UART0_IRQ, irq_uart); irq_set_enabled(UART0_IRQ, true); uart_set_irq_enables(uart0, 1, 0); /* On-board LED */ setup_output(PIN_LED); /* Valve relays */ setup_output(PIN_RE1); setup_output(PIN_RE2); setup_output(PIN_RE3); setup_output(PIN_RE4); /* LCD */ setup_output(PIN_LCD_RS); setup_output(PIN_LCD_RW); setup_output(PIN_LCD_E); setup_output(PIN_LCD_D7); setup_output(PIN_LCD_D6); setup_output(PIN_LCD_D5); setup_output(PIN_LCD_D4); /* I2C */ i2c_init(i2c0, 100 * 1000); // 100 kbps - play it safe gpio_set_function(PIN_I2C_SDA, GPIO_FUNC_I2C); gpio_set_function(PIN_I2C_SCL, GPIO_FUNC_I2C); gpio_pull_up(PIN_I2C_SDA); gpio_pull_up(PIN_I2C_SCL); /* ADC for moisture */ adc_init(); adc_gpio_init(PIN_MOISTURE); lcd_init(); lcd_puts("HELLO WORLD!"); uint cnt = 0; char buf[16]; while (1) { lcd_clear(); lcd_puts("HELLO WORLD!"); gpio_put(PIN_LED, 1); // LED on uint16_t moisture = moisture_read(); sprintf(buf, "ADC = %lu", moisture); lcd_xy(0, 1); lcd_puts(buf); lcd_xy(15, 3); lcd_putc('0' + cnt); sleep_ms(100); gpio_put(PIN_LED, 0); // LED off sleep_ms(100); // demo that valve control works cnt += 1; if (cnt == 5) { cnt = 0; } open_valve(cnt); } }