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
This commit is contained in:
parent
5e65b9156a
commit
3c7a09479c
6
Makefile
6
Makefile
@ -4,7 +4,7 @@
|
|||||||
# --- control global project settings
|
# --- control global project settings
|
||||||
# RELEASE=1 -> enable optimisation, then disable debug
|
# RELEASE=1 -> enable optimisation, then disable debug
|
||||||
# RELEASE=0 -> disbale optimisation, then enable debug
|
# RELEASE=0 -> disbale optimisation, then enable debug
|
||||||
RELEASE=1
|
RELEASE=0
|
||||||
|
|
||||||
# --- project architecture
|
# --- project architecture
|
||||||
# program name
|
# program name
|
||||||
@ -52,12 +52,10 @@ DADEFS=-D__ASSEMBLY__
|
|||||||
|
|
||||||
# --- deduce file names
|
# --- deduce file names
|
||||||
MAIN_C_FILES=${wildcard ${SRC}/${strip ${EXE_PREFIX}}*.c}
|
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}}}
|
${foreach dir,${SUBFOLDERS},${wildcard ${SRC}/${dir}/*.c}}}
|
||||||
#${wildcard ${TC}*.c}}
|
|
||||||
COMMON_ASM_FILES=${filter-out ${MAIN_CXX_FILES},${wildcard *.s} \
|
COMMON_ASM_FILES=${filter-out ${MAIN_CXX_FILES},${wildcard *.s} \
|
||||||
${foreach dir,${SUBFOLDERS},${wildcard ${SRC}/${dir}/*.s}}}
|
${foreach dir,${SUBFOLDERS},${wildcard ${SRC}/${dir}/*.s}}}
|
||||||
#${wildcard ${TC}*.s}}
|
|
||||||
MAIN_OBJECT_FILES=${sort ${patsubst ${SRC}/%.c,${OBJ}/%.o,${MAIN_C_FILES}}}
|
MAIN_OBJECT_FILES=${sort ${patsubst ${SRC}/%.c,${OBJ}/%.o,${MAIN_C_FILES}}}
|
||||||
COMMON_OBJECT_FILES=${sort ${patsubst ${SRC}/%.c,${OBJ}/%.o,${COMMON_C_FILES}} \
|
COMMON_OBJECT_FILES=${sort ${patsubst ${SRC}/%.c,${OBJ}/%.o,${COMMON_C_FILES}} \
|
||||||
${patsubst ${SRC}/%.s,${OBJ}/%.o,${COMMON_ASM_FILES}}}
|
${patsubst ${SRC}/%.s,${OBJ}/%.o,${COMMON_ASM_FILES}}}
|
||||||
|
|||||||
@ -19,7 +19,7 @@ int adc_init(ADC_TypeDef* adc) {
|
|||||||
// set trigger to manual
|
// set trigger to manual
|
||||||
adc->CR1 |= 0x7 << 3;
|
adc->CR1 |= 0x7 << 3;
|
||||||
|
|
||||||
adc->SMPR2 |= 0x7;
|
adc->SMPR2 |= 0x3FFFFFFF;
|
||||||
|
|
||||||
// calibrate
|
// calibrate
|
||||||
adc->CR2 |= 0x1 << 2;
|
adc->CR2 |= 0x1 << 2;
|
||||||
@ -37,6 +37,6 @@ uint16_t adc_read(ADC_TypeDef* adc, uint8_t channel) {
|
|||||||
adc->CR2 |= 0x1;
|
adc->CR2 |= 0x1;
|
||||||
while(!((adc->SR >> 1) & 0x1)); //waiting for convertion
|
while(!((adc->SR >> 1) & 0x1)); //waiting for convertion
|
||||||
|
|
||||||
return adc->DR & 0xFFFF;
|
return adc->DR & 0xFFF;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,6 +11,7 @@ static TIM_TypeDef* timer = 0;
|
|||||||
static uint8_t mode = UNDEFINED;
|
static uint8_t mode = UNDEFINED;
|
||||||
static uint8_t rows = 0;
|
static uint8_t rows = 0;
|
||||||
static uint8_t columns = 0;
|
static uint8_t columns = 0;
|
||||||
|
static uint8_t rows_offset[4] = {};
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
// internal functions
|
// internal functions
|
||||||
@ -86,6 +87,10 @@ int lcd_init(TIM_TypeDef* tim, uint8_t col, uint8_t row) {
|
|||||||
timer = tim;
|
timer = tim;
|
||||||
columns = col;
|
columns = col;
|
||||||
rows = row;
|
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
|
// disable JTAG, as it utilise needed pins, SWD remains usable in
|
||||||
// synchronous mode
|
// synchronous mode
|
||||||
@ -163,6 +168,17 @@ void lcd_print(const char* txt) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void lcd_set_cursor(uint8_t col, uint8_t row) {
|
void lcd_print_c(char c) {
|
||||||
lcd_send_cmd(LCD_DDRAM_ADDR | col);
|
// 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]));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -85,6 +85,8 @@ void lcd_send_cmd(uint8_t cmd);
|
|||||||
*/
|
*/
|
||||||
void lcd_print(const char* txt);
|
void lcd_print(const char* txt);
|
||||||
|
|
||||||
|
void lcd_print_c(char c);
|
||||||
|
|
||||||
void lcd_set_cursor(uint8_t col, uint8_t row);
|
void lcd_set_cursor(uint8_t col, uint8_t row);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
76
src/main.c
76
src/main.c
@ -1,3 +1,7 @@
|
|||||||
|
// standard headers
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
// driver includes
|
||||||
#include "drivers/rcc.h"
|
#include "drivers/rcc.h"
|
||||||
#include "drivers/io.h"
|
#include "drivers/io.h"
|
||||||
#include "drivers/lcd.h"
|
#include "drivers/lcd.h"
|
||||||
@ -6,48 +10,82 @@
|
|||||||
Clock_t sysclks;
|
Clock_t sysclks;
|
||||||
#include "drivers/timer.h"
|
#include "drivers/timer.h"
|
||||||
|
|
||||||
|
// project headers
|
||||||
|
#include "ui.h"
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
/* static variables */
|
||||||
int val = 0;
|
int val = 0;
|
||||||
uint16_t data = 0;
|
uint16_t data = 0;
|
||||||
|
int16_t voltage = 0;
|
||||||
|
int16_t temp = 0;
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
/* Timer IRQ */
|
||||||
static void timeout_cb(void) {
|
static void timeout_cb(void) {
|
||||||
io_write(GPIOC, val, PIN_13);
|
io_write(GPIOC, val, PIN_13);
|
||||||
val = !val;
|
val = !val;
|
||||||
data = adc_read(ADC1, 2);
|
|
||||||
data -= adc_read(ADC1, 3);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
/* main function */
|
||||||
int main(void) {
|
int main(void) {
|
||||||
|
|
||||||
rcc_config_clock(CLOCK_CONFIG_PERFORMANCE, &sysclks);
|
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))
|
if(io_configure(GPIOC, PIN_13, IO_MODE_OUTPUT | IO_OUT_PUSH_PULL, 0))
|
||||||
return 0;
|
return 0;
|
||||||
io_write(GPIOC, 1, PIN_13);
|
io_write(GPIOC, 1, PIN_13);
|
||||||
|
|
||||||
if(io_configure(GPIOA, PIN_2 | PIN_3, IO_MODE_INPUT | IO_IN_ANALOG, 0))
|
// configure GPIOS for temperature sensors
|
||||||
return 0;
|
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;
|
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_tick_init(TIM2, 1000, timeout_cb);
|
||||||
timer_start(TIM2);
|
timer_start(TIM2);
|
||||||
|
|
||||||
lcd_init(TIM1);
|
// main loop
|
||||||
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);
|
|
||||||
|
|
||||||
|
|
||||||
while(1){
|
while(1){
|
||||||
lcd_print("loop");
|
// update T1
|
||||||
lcd_print("loop2");
|
data = adc_read(ADC1, 0);
|
||||||
lcd_print("loop3");
|
data -= adc_read(ADC1, 1);
|
||||||
lcd_print("loop4");
|
|
||||||
//voltage = data*4.0f/4095.0f;
|
voltage = ((data*4) << 8)/4095;
|
||||||
//temp = (voltage-0.45)/0.04f;
|
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);
|
timer_wait_ms(TIM1, 200, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
38
src/ui.c
Normal file
38
src/ui.c
Normal file
@ -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);
|
||||||
|
}
|
||||||
37
src/ui.h
Normal file
37
src/ui.h
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
#ifndef UI_H
|
||||||
|
#define UI_H
|
||||||
|
|
||||||
|
// drivers headers
|
||||||
|
#include "drivers/lcd.h"
|
||||||
|
|
||||||
|
// standard headers
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
// 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
|
||||||
|
|
||||||
30
src/utils.c
Normal file
30
src/utils.c
Normal file
@ -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<cnt/2; ++i) {
|
||||||
|
tmp = p[i];
|
||||||
|
p[i] = p[cnt-i-1];
|
||||||
|
p[cnt-i-1] = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
return cnt; //number of caracters (excluding '\0')
|
||||||
|
}
|
||||||
10
src/utils.h
Normal file
10
src/utils.h
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#ifndef UTILS_H
|
||||||
|
#define UTILS_H
|
||||||
|
|
||||||
|
// standard headers
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
uint32_t num2str(char *s, int number, uint8_t base);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
Reference in New Issue
Block a user