/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
GPIO_InitTypeDef GPIO_InitStruct;
void gpio_set_input (void)
{
GPIO_InitStruct.Pin = GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
}
void gpio_set_output (void)
{
GPIO_InitStruct.Pin = GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
}
uint8_t check =2, temp_l, temp_h;
uint16_t temp;
float temperature;
uint8_t ds18b20_init (void)
{
gpio_set_output (); // set the pin as output
HAL_GPIO_WritePin (GPIOE, GPIO_PIN_3, 0); // pull the pin low
DWT_Delay_us (480); // delay according to datasheet
gpio_set_input (); // set the pin as input
DWT_Delay_us (80); // delay according to datasheet
if (!(HAL_GPIO_ReadPin (GPIOE, GPIO_PIN_3))){ // if the pin is low i.e the presence pulse is there
DWT_Delay_us (400); // wait for 400 us
return 0;
}
else{
DWT_Delay_us (400);
return 1;
}
}
void write (uint8_t data){
gpio_set_output (); // set as output
for (int i=0; i<8; i++){
if ((data & (1<<i))!=0){ // if the bit is high
// write 1
gpio_set_output (); // set as output
HAL_GPIO_WritePin (GPIOE, GPIO_PIN_3, 0); // pull the pin LOW
DWT_Delay_us (1); // wait for us
gpio_set_input (); // set as input
DWT_Delay_us (60); // wait for 60 us
}
else{ // if the bit is low
// write 0
gpio_set_output ();
HAL_GPIO_WritePin (GPIOE, GPIO_PIN_3, 0); // pull the pin LOW
DWT_Delay_us (60); // wait for 60 us
gpio_set_input ();
}
}
}
uint8_t read (void){
uint8_t value=0;
gpio_set_input ();
for (int i=0;i<8;i++){
gpio_set_output (); // set as output
HAL_GPIO_WritePin (GPIOE, GPIO_PIN_3, 0); // pull the data pin LOW
DWT_Delay_us (2); // wait for 2 us
gpio_set_input (); // set as input
if (HAL_GPIO_ReadPin (GPIOE, GPIO_PIN_3)){ // if the pin is HIGH
value |= 1<<i; // read = 1
}
DWT_Delay_us (60); // wait for 60 us
}
return value;
}
int main(void) {
char line[41]; // To build lines for the lcd
// STM32F7xx HAL library initialization
if (HAL_Init() != HAL_OK) {
blink_error_code(ERROR_HAL_INIT);
}
// Configure the system clock
SystemClock_Config();
lcdInit();
DWT_Delay_Init ();
// Verificar si es un reset de iwdg
if (__HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST) != RESET) {
__HAL_RCC_CLEAR_RESET_FLAGS();
lcdPutsCenter(0, "RESET POR");
lcdPutsCenter(1, "WATCHDOG");
HAL_Delay(2000);
}
// Inicializar iwdg
IwdgHandle.Instance = IWDG;
IwdgHandle.Init.Prescaler = IWDG_PRESCALER_128;
IwdgHandle.Init.Reload = 0x8ff;
HAL_IWDG_Init(&IwdgHandle);
////// Main loop //////
while (1) {
// Refrescar watch dog
HAL_IWDG_Refresh(&IwdgHandle);
//JUEGO DE LUCES//
if (TEST_IN_STATE == IS_TEST_IN_ON) {
LED_R1_OFF;
LED_R2_OFF;
HAL_Delay(200);
LED_V1_ON;
LED_V2_ON;
}
if (CAMBIO_IN_STATE == IS_CAMBIO_IN_ON) {
LED_ERR_ON;
HAL_Delay(1000);
if ((DM1_IN_STATE == IS_DM1_IN_ON)||(DM2_IN_STATE == IS_DM2_IN_ON)||(DM3_IN_STATE == IS_DM3_IN_ON)){
LED_ERR_OFF;
}
else{
LED_ERR_ON;
}
}
else if (CO2_IN_STATE == IS_CO2_IN_ON) {
sprintf(line, "SIN CO2");
lcdPutsCenter(1, line);
LED_ERR_ON;
if (CERO_IN_STATE == IS_CERO_IN_ON){
LED_ERR_OFF;
}
else{
sprintf(line, "NAME");
lcdPutsCenter(1, line);
}
}
if ((DM1_IN_STATE == IS_DM1_IN_ON)&&(DM2_IN_STATE == IS_DM2_IN_ON)&&(DM3_IN_STATE == IS_DM3_IN_ON)) {
if(CERO_IN_STATE == IS_CERO_IN_OFF){
LED_ERR_ON;
}
else{
LED_ERR_OFF;
}
if (((HAL_GetTick() - tiempo) > 30000)){
check = ds18b20_init ();
write (0xCC); // skip ROM
write (0x44); // convert t
HAL_Delay (94);
ds18b20_init ();
write (0xCC); // skip ROM
write (0xBE); // Read Scratchpad
temp_l = read();
temp_h = read();
temp = (temp_h<<8)|temp_l;
temperature = (float)temp/16;
sprintf(line, "TEMP6:");
lcdPutsCenter(1, line);
HAL_Delay(400);
check = ds18b20_init ();
write (0xCC); // skip ROM
write (0x44); // convert t
HAL_Delay (94);
ds18b20_init ();
write (0xCC); // skip ROM
write (0xBE); // Read Scratchpad
temp_l = read();
temp_h = read();
temp = (temp_h<<8)|temp_l;
temperature = (float)temp/16;
sprintf(line, "TEMP6: %d \337C ",(int)(temperature));
lcdPutsCenter(1, line);
tiempo = HAL_GetTick();
}
}
else if ((DM1_IN_STATE == IS_DM1_IN_ON)&&(DM2_IN_STATE == IS_DM2_IN_ON)&&(DM3_IN_STATE == IS_DM3_IN_OFF)) {
if(SW3_IN_STATE == IS_SW3_IN_OFF){
LED_ERR_ON;
}
else{
LED_ERR_OFF;
HAL_Delay(1000);
}
if (((HAL_GetTick() - tiempo) > 30000)){
check = ds18b20_init ();
write (0xCC); // skip ROM
write (0x44); // convert t
HAL_Delay (94);
ds18b20_init ();
write (0xCC); // skip ROM
write (0xBE); // Read Scratchpad
temp_l = read();
temp_h = read();
temp = (temp_h<<8)|temp_l;
temperature = (float)temp/16;
sprintf(line, "TEMP6:");
lcdPutsCenter(1, line);
HAL_Delay(400);
check = ds18b20_init ();
write (0xCC); // skip ROM
write (0x44); // convert t
HAL_Delay (94);
ds18b20_init ();
write (0xCC); // skip ROM
write (0xBE); // Read Scratchpad
temp_l = read();
temp_h = read();
temp = (temp_h<<8)|temp_l;
temperature = (float)temp/16;
sprintf(line, "TEMP6: %d \337C ",(int)(temperature));
lcdPutsCenter(1, line);
tiempo = HAL_GetTick();
}
}
else if ((DM1_IN_STATE == IS_DM1_IN_ON)&&(DM2_IN_STATE == IS_DM2_IN_OFF)&&(DM3_IN_STATE == IS_DM3_IN_ON)) {
if(SW2_IN_STATE == IS_SW2_IN_OFF){
LED_ERR_ON;
}
else{
LED_ERR_OFF;
HAL_Delay(1000);
}
if (((HAL_GetTick() - tiempo) > 30000)){
check = ds18b20_init ();
write (0xCC); // skip ROM
write (0x44); // convert t
HAL_Delay (94);
ds18b20_init ();
write (0xCC); // skip ROM
write (0xBE); // Read Scratchpad
temp_l = read();
temp_h = read();
temp = (temp_h<<8)|temp_l;
temperature = (float)temp/16;
sprintf(line, "TEMP6:");
lcdPutsCenter(1, line);
HAL_Delay(400);
check = ds18b20_init ();
write (0xCC); // skip ROM
write (0x44); // convert t
HAL_Delay (94);
ds18b20_init ();
write (0xCC); // skip ROM
write (0xBE); // Read Scratchpad
temp_l = read();
temp_h = read();
temp = (temp_h<<8)|temp_l;
temperature = (float)temp/16;
sprintf(line, "TEMP6: %d \337C ",(int)(temperature));
lcdPutsCenter(1, line);
tiempo = HAL_GetTick();
}
}
else if ((DM1_IN_STATE == IS_DM1_IN_OFF)&&(DM2_IN_STATE == IS_DM2_IN_ON)&&(DM3_IN_STATE == IS_DM3_IN_ON)) {
if(SW1_IN_STATE == IS_SW1_IN_OFF){
LED_ERR_ON;
}
else{
LED_ERR_OFF;
HAL_Delay(1000);
}
if (((HAL_GetTick() - tiempo) > 30000)){
check = ds18b20_init ();
write (0xCC); // skip ROM
write (0x44); // convert t
HAL_Delay (94);
ds18b20_init ();
write (0xCC); // skip ROM
write (0xBE); // Read Scratchpad
temp_l = read();
temp_h = read();
temp = (temp_h<<8)|temp_l;
temperature = (float)temp/16;
sprintf(line, "TEMP6:");
lcdPutsCenter(1, line);
HAL_Delay(400);
check = ds18b20_init ();
write (0xCC); // skip ROM
write (0x44); // convert t
HAL_Delay (94);
ds18b20_init ();
write (0xCC); // skip ROM
write (0xBE); // Read Scratchpad
temp_l = read();
temp_h = read();
temp = (temp_h<<8)|temp_l;
temperature = (float)temp/16;
sprintf(line, "TEMP6: %d \337C ",(int)(temperature));
lcdPutsCenter(1, line);
tiempo = HAL_GetTick();
}
}
}
}
}