From 3c7a09479c5afb9521f4dbbd6638fba413fdcbb0 Mon Sep 17 00:00:00 2001 From: Steins7 Date: Sat, 1 Feb 2020 14:54:47 +0100 Subject: [PATCH] Got the display of all temps to work + added function libraries to keep main.c clean + moved temprature calculus to fixed-point * fixed ADC driver messed up during dma_dev branch creation --- Makefile | 6 ++-- src/drivers/adc.c | 4 +-- src/drivers/lcd.c | 20 +++++++++++-- src/drivers/lcd.h | 2 ++ src/main.c | 76 +++++++++++++++++++++++++++++++++++------------ src/ui.c | 38 ++++++++++++++++++++++++ src/ui.h | 37 +++++++++++++++++++++++ src/utils.c | 30 +++++++++++++++++++ src/utils.h | 10 +++++++ 9 files changed, 196 insertions(+), 27 deletions(-) create mode 100644 src/ui.c create mode 100644 src/ui.h create mode 100644 src/utils.c create mode 100644 src/utils.h diff --git a/Makefile b/Makefile index a6913fd..64dd3c9 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ # --- control global project settings # RELEASE=1 -> enable optimisation, then disable debug # RELEASE=0 -> disbale optimisation, then enable debug -RELEASE=1 +RELEASE=0 # --- project architecture # program name @@ -52,12 +52,10 @@ DADEFS=-D__ASSEMBLY__ # --- deduce file names MAIN_C_FILES=${wildcard ${SRC}/${strip ${EXE_PREFIX}}*.c} -COMMON_C_FILES=${filter-out ${MAIN_C_FILES},${wildcard *.c} \ +COMMON_C_FILES=${filter-out ${MAIN_C_FILES},${wildcard ${SRC}/*.c} \ ${foreach dir,${SUBFOLDERS},${wildcard ${SRC}/${dir}/*.c}}} - #${wildcard ${TC}*.c}} COMMON_ASM_FILES=${filter-out ${MAIN_CXX_FILES},${wildcard *.s} \ ${foreach dir,${SUBFOLDERS},${wildcard ${SRC}/${dir}/*.s}}} - #${wildcard ${TC}*.s}} MAIN_OBJECT_FILES=${sort ${patsubst ${SRC}/%.c,${OBJ}/%.o,${MAIN_C_FILES}}} COMMON_OBJECT_FILES=${sort ${patsubst ${SRC}/%.c,${OBJ}/%.o,${COMMON_C_FILES}} \ ${patsubst ${SRC}/%.s,${OBJ}/%.o,${COMMON_ASM_FILES}}} diff --git a/src/drivers/adc.c b/src/drivers/adc.c index 214b1a9..c5382ce 100644 --- a/src/drivers/adc.c +++ b/src/drivers/adc.c @@ -19,7 +19,7 @@ int adc_init(ADC_TypeDef* adc) { // set trigger to manual adc->CR1 |= 0x7 << 3; - adc->SMPR2 |= 0x7; + adc->SMPR2 |= 0x3FFFFFFF; // calibrate adc->CR2 |= 0x1 << 2; @@ -37,6 +37,6 @@ uint16_t adc_read(ADC_TypeDef* adc, uint8_t channel) { adc->CR2 |= 0x1; while(!((adc->SR >> 1) & 0x1)); //waiting for convertion - return adc->DR & 0xFFFF; + return adc->DR & 0xFFF; } diff --git a/src/drivers/lcd.c b/src/drivers/lcd.c index c784193..f2638a2 100644 --- a/src/drivers/lcd.c +++ b/src/drivers/lcd.c @@ -11,6 +11,7 @@ static TIM_TypeDef* timer = 0; static uint8_t mode = UNDEFINED; static uint8_t rows = 0; static uint8_t columns = 0; +static uint8_t rows_offset[4] = {}; //------------------------------------------------------------------------------ // internal functions @@ -86,6 +87,10 @@ int lcd_init(TIM_TypeDef* tim, uint8_t col, uint8_t row) { timer = tim; columns = col; rows = row; + rows_offset[0] = 0x00; + rows_offset[1] = 0x40; + rows_offset[2] = 0x00 + columns; + rows_offset[3] = 0x40 + columns; // disable JTAG, as it utilise needed pins, SWD remains usable in // synchronous mode @@ -163,6 +168,17 @@ void lcd_print(const char* txt) { } } -void lcd_set_cursor(uint8_t col, uint8_t row) { - lcd_send_cmd(LCD_DDRAM_ADDR | col); +void lcd_print_c(char c) { + // wait for the screen + wait_for_ready(); + + // select data register + io_write(GPIOA, LCD_MODE_DATA, PIN_10); + + // send the caracter + write_byte(c); +} + +void lcd_set_cursor(uint8_t col, uint8_t row) { + lcd_send_cmd(LCD_DDRAM_ADDR | (col + rows_offset[row])); } diff --git a/src/drivers/lcd.h b/src/drivers/lcd.h index 5eba6e1..00fb737 100644 --- a/src/drivers/lcd.h +++ b/src/drivers/lcd.h @@ -85,6 +85,8 @@ void lcd_send_cmd(uint8_t cmd); */ void lcd_print(const char* txt); +void lcd_print_c(char c); + void lcd_set_cursor(uint8_t col, uint8_t row); #endif diff --git a/src/main.c b/src/main.c index 6349e2c..2a1dc71 100644 --- a/src/main.c +++ b/src/main.c @@ -1,3 +1,7 @@ +// standard headers +#include + +// driver includes #include "drivers/rcc.h" #include "drivers/io.h" #include "drivers/lcd.h" @@ -6,48 +10,82 @@ Clock_t sysclks; #include "drivers/timer.h" +// project headers +#include "ui.h" + +//------------------------------------------------------------------------------ +/* static variables */ int val = 0; uint16_t data = 0; +int16_t voltage = 0; +int16_t temp = 0; +//------------------------------------------------------------------------------ +/* Timer IRQ */ static void timeout_cb(void) { io_write(GPIOC, val, PIN_13); val = !val; - data = adc_read(ADC1, 2); - data -= adc_read(ADC1, 3); } +//------------------------------------------------------------------------------ +/* main function */ int main(void) { rcc_config_clock(CLOCK_CONFIG_PERFORMANCE, &sysclks); + // configure GPIO for LED if(io_configure(GPIOC, PIN_13, IO_MODE_OUTPUT | IO_OUT_PUSH_PULL, 0)) return 0; io_write(GPIOC, 1, PIN_13); - if(io_configure(GPIOA, PIN_2 | PIN_3, IO_MODE_INPUT | IO_IN_ANALOG, 0)) - return 0; + // configure GPIOS for temperature sensors + if(io_configure(GPIOA, PIN_0 | PIN_1 | PIN_2 | PIN_3 | PIN_4 | PIN_5, + IO_MODE_INPUT | IO_IN_ANALOG, 0)) return 0; if(adc_init(ADC1)) return 0; - //int test = 0; + // configure lcd + lcd_init(TIM1, 16, 2); + lcd_send_cmd(LCD_CUR_HOME); + lcd_print("SILO:1 Te: 18""\xDF""C"); + lcd_set_cursor(0,1); + lcd_print("1: 25""\xDF""C 2: 26""\xDF""C"); + lcd_send_cmd(LCD_DISP_CTRL | LCD_CTRL_DISP_ON | LCD_CTRL_CUR_OFF | + LCD_CTRL_BLINK_OFF); + // start timed interruption timer_tick_init(TIM2, 1000, timeout_cb); timer_start(TIM2); - lcd_init(TIM1); - lcd_print(" et paf"); - lcd_print("Hello world!"); - lcd_send_cmd(LCD_CUR_HOME); - lcd_send_cmd(LCD_CUR_HOME); - lcd_set_cursor(5, 1); - - + // main loop while(1){ - lcd_print("loop"); - lcd_print("loop2"); - lcd_print("loop3"); - lcd_print("loop4"); - //voltage = data*4.0f/4095.0f; - //temp = (voltage-0.45)/0.04f; + // update T1 + data = adc_read(ADC1, 0); + data -= adc_read(ADC1, 1); + + voltage = ((data*4) << 8)/4095; + temp = ((voltage - 0x73) << 8)/0x9; + + update_temp(T1, temp); + + // update T2 + data = adc_read(ADC1, 2); + data -= adc_read(ADC1, 3); + + voltage = ((data*4) << 8)/4095; + temp = ((voltage - 0x73) << 8)/0x9; + + update_temp(T2, temp); + + // update T_ext + data = adc_read(ADC1, 4); + data -= adc_read(ADC1, 5); + + voltage = ((data*4) << 8)/4095; + temp = ((voltage - 0x73) << 8)/0x9; + + update_temp(T_EXT, temp); + + // update every 0.2 seconds timer_wait_ms(TIM1, 200, 0); } diff --git a/src/ui.c b/src/ui.c new file mode 100644 index 0000000..6ac3e28 --- /dev/null +++ b/src/ui.c @@ -0,0 +1,38 @@ +#include "ui.h" + +void update_temp(uint8_t id, int16_t t) { + + if(id > 2) return; //protect from overflow + uint8_t* pos = temp_pos[id]; + + // prepare data + char str[16]; //longer, in case of error + t = t >> 8; + + // convert int into str + uint32_t nb = num2str(str, t, 10); + + // clear previous text + lcd_set_cursor(pos[0],pos[1]); + lcd_print(" "); + + // prepare lcd for write + switch(nb) { + case 1: + lcd_set_cursor(pos[0]+2,pos[1]); + break; + case 2: + lcd_set_cursor(pos[0]+1,pos[1]); + break; + case 3: + lcd_set_cursor(pos[0],pos[1]); + break; + default: // something went wrong + lcd_set_cursor(pos[0],pos[1]); + lcd_print("Err"); + return; + } + + // write value + lcd_print(str); +} diff --git a/src/ui.h b/src/ui.h new file mode 100644 index 0000000..9ae58e7 --- /dev/null +++ b/src/ui.h @@ -0,0 +1,37 @@ +#ifndef UI_H +#define UI_H + +// drivers headers +#include "drivers/lcd.h" + +// standard headers +#include + +// project headers +#include "utils.h" + +/** temp + * list of all the temperature ids on the sreen + */ +enum temp { + T_EXT = 0, + T1 = 1, + T2 = 2 +}; + +/** temp_pos + * coords of the temprature ids + */ +static uint8_t temp_pos[][2] = { + {11, 0}, + { 2, 1}, + {11, 1}}; + +/** update_temp + * update on the lcd the given value for the corresponding id + */ +void update_temp(uint8_t id, int16_t t_ext); + + +#endif + diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000..1d16eb4 --- /dev/null +++ b/src/utils.c @@ -0,0 +1,30 @@ +#include "utils.h" + +uint32_t num2str(char *s, int number, uint8_t base) { + + static char hexChars[] = "0123456789ABCDEF"; + char *p = s; + + // manage sign + uint32_t nb = (number < 0 ? -number : number); + + // get digits + do { + *s++ = hexChars[nb % base]; + } while (nb /= base); + + // finalize string + if(number < 0) *s++ = '-'; + *s='\0'; + + // reverse string + uint32_t cnt = s - p; + char tmp; + for(int i=0; i + +uint32_t num2str(char *s, int number, uint8_t base); + +#endif +