diff --git a/README.md b/README.md index 6113ab9..ba2b6e8 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,89 @@ +--- + # ArmBookCurso2024 -Primer ejemplo básico para aprender a usar las herramientas +## **Título:** Control de Sistema de Tratamiento de Agua + +### **Alumno:** Jesús García + +### **Objetivo:** +Desarrollar un sistema de monitoreo y control para un proceso de tratamiento de agua. + +### **Descripción:** + +Este sistema controla el suministro y la producción de agua ultrapura mediante un tratamiento por ósmosis inversa. Utiliza diferentes módulos y periféricos para monitorear la presión y gestionar las bombas, optimizando el proceso de tratamiento de agua y garantizando su seguridad y eficiencia. + +### **Componentes del sistema:** + +1. **Módulo `control_system`:** + Inicializa el panel de control y actualiza el sistema constantemente. + +2. **Módulo `panel_control`:** + Interactúa con los siguientes módulos: + - `buttons`: Gestiona la interacción con el usuario mediante botones. + - `display`: Muestra la información del sistema en la pantalla. + - `matrix_keypad`: Permite la entrada de datos mediante un teclado 4x3. + - `pressure_sensor`: Monitorea la presión de entrada y salida del sistema. + - `pumps`: Controla el funcionamiento de las bombas. + - `serial_communication`: Comunica información sobre el sistema mediante UART. + +### **Funcionamiento:** + +El sistema monitorea dos presiones clave: +- **P1:** Presión de entrada (antes de los filtros). +- **P2:** Presión de salida (después de los filtros). + +En función de estas presiones, el sistema controla el encendido y apagado de las bombas para evitar condiciones peligrosas como la falta de agua o sobrepresión. + +### **Interacción con el usuario:** + +El usuario puede interactuar con el sistema mediante un botón que, al ser presionado de manera extendida, despliega un menú serial con las siguientes opciones: + +1. **Ajustar Frecuencia de la Bomba:** + Permite ingresar la frecuencia de operación de las bombas mediante el teclado de 4x3. + +2. **Verificar Estado del Sistema:** + Imprime las variables de estado del sistema en la pantalla. + +3. **Detener Sistema:** + Detiene el funcionamiento de las bombas de manera segura. + +### **Operación del sistema:** + +- **Bombas P1 y P2:** + Funcionan dentro de los rangos de presión mínima y máxima. + La bomba P1 está controlada por la presión de entrada, mientras que P2 se controla por la presión de salida. + +- **Pantalla LCD:** + Muestra el estado de cada variable del sistema, actualizándose periódicamente. + +### **Plataforma de desarrollo:** +- **STM32 Nucleo-144** + +### **Periféricos utilizados:** + +- **USER BUTTON:** + Inicia o apaga el sistema. + +- **LED 1:** + Indica un cambio de estado para iniciar el modo menú. + +- **LED 2:** + Indica el estado de la bomba P1. + +- **LED 3:** + Indica el estado de la bomba P2. + +- **ANALOG IN 1:** + Simula un sensor de presión. + +- **UART:** + Comunica información sobre el estado del sistema a un PC. + +- **TECLADO:** + Permite navegar en el menú serial e ingresar la frecuencia de las bombas. + +- **LCD:** + Visualiza los estados de las variables periódicamente. + +--- diff --git a/README.md.bak b/README.md.bak new file mode 100644 index 0000000..6113ab9 --- /dev/null +++ b/README.md.bak @@ -0,0 +1,3 @@ +# ArmBookCurso2024 + +Primer ejemplo básico para aprender a usar las herramientas diff --git a/arm_book_lib.h b/arm_book_lib.h new file mode 100644 index 0000000..5c941a7 --- /dev/null +++ b/arm_book_lib.h @@ -0,0 +1,32 @@ +//=====[#include guards - begin]=============================================== + +#ifndef _ARM_BOOK_LIB_H_ +#define _ARM_BOOK_LIB_H_ + +//=====[Libraries]============================================================= + +#include + +//=====[Declaration of public defines]========================================= + +// Functional states +#ifndef OFF +#define OFF 0 +#endif +#ifndef ON +#define ON (!OFF) +#endif + +// Electrical states +#ifndef LOW +#define LOW 0 +#endif +#ifndef HIGH +#define HIGH (!LOW) +#endif + +#define delay(ms) thread_sleep_for( ms ) + +//=====[#include guards - end]================================================= + +#endif // _ARM_BOOK_LIB_H_ \ No newline at end of file diff --git a/main.cpp b/main.cpp index 21eabd5..9cdc9ba 100644 --- a/main.cpp +++ b/main.cpp @@ -1,12 +1,13 @@ -#include "mbed.h" +//=====[Libraries]============================================================= -int main() -{ - DigitalIn B1_USER(BUTTON1); +#include "control_system.h" - DigitalOut LD1(LED1); +//=====[Main function, the program entry point after power on or reset]======== +int main() +{ + controlSystemInit(); while (true) { - LD1 = B1_USER; + controlSystemUpdate(); } -} +} \ No newline at end of file diff --git a/mbed-os.lib b/mbed-os.lib index 27e1338..91bb263 100644 --- a/mbed-os.lib +++ b/mbed-os.lib @@ -1 +1 @@ -https://github.com/ARMmbed/mbed-os.git#26606218ad9d1ee1c8781aa73774fd7ea3a7658e +https://github.com/ARMmbed/mbed-os.git#17dc3dc2e6e2817a8bd3df62f38583319f0e4fed \ No newline at end of file diff --git a/mbed_app.json b/mbed_app.json new file mode 100644 index 0000000..9cde318 --- /dev/null +++ b/mbed_app.json @@ -0,0 +1,7 @@ +{ + "target_overrides": { + "*": { + "target.printf_lib": "std" + } + } +} \ No newline at end of file diff --git a/modules/buttons/buttons.cpp b/modules/buttons/buttons.cpp new file mode 100644 index 0000000..cb2730a --- /dev/null +++ b/modules/buttons/buttons.cpp @@ -0,0 +1,104 @@ +//=====[Libraries]============================================================= + +#include "buttons.h" +#include "mbed.h" +#include "arm_book_lib.h" + +#include "serial_communication.h" + + +//=====[Declaration of private defines]======================================== +#define DEBOUNCE_KEY_TIME_MS 400 + + +//=====[Declaration of private data types]===================================== +typedef enum { + BUTTON_UP, + BUTTON_DEBOUNCE, + BUTTON_DOWN, +} buttonState_t; + + + +//=====[Declaration and initialization of public global objects]=============== +DigitalIn menuButton(BUTTON1); +DigitalOut Led_1(LED1); + +//=====[Declaration of external public global variables]======================= +bool pressedButton = false; + +//=====[Declaration and initialization of public global variables]============= + +//=====[Declaration and initialization of private global variables]============ +static buttonState_t buttonState = BUTTON_UP; +static int timeIncrement_ms = 0; + +//=====[Declarations (prototypes) of private functions]======================== +static void buttonReset(); +static void buttonsUpdate(); + + +//=====[Implementations of public functions]=================================== +void buttonsInit( int updateTime_ms ) +{ + timeIncrement_ms = updateTime_ms; + menuButton.mode(PullDown); + Led_1=0; + buttonState= BUTTON_UP; +} + +bool statusButton(){ + buttonsUpdate(); + return pressedButton; +} + + + +//=====[Implementations of private functions]================================== + +static void buttonsUpdate() +{ + static int accumulatedDebouncebuttonTime = 0; + switch( buttonState ) { + + case BUTTON_UP: + pressedButton = false; + if( menuButton == true) { + buttonState = BUTTON_DEBOUNCE; + accumulatedDebouncebuttonTime=0; + } + break; + + case BUTTON_DEBOUNCE: + if( accumulatedDebouncebuttonTime >= + DEBOUNCE_KEY_TIME_MS ) { + if( menuButton == true ) { + Led_1=1; + buttonState = BUTTON_DOWN; + } else { + buttonState = BUTTON_UP; + } + } + accumulatedDebouncebuttonTime = + accumulatedDebouncebuttonTime + timeIncrement_ms; + break; + + case BUTTON_DOWN: + if( menuButton == false ) { + pressedButton = true; + Led_1=0; + buttonState = BUTTON_UP; + } + break; + + default: + buttonReset(); + break; + + } +} + +static void buttonReset() +{ + buttonState = BUTTON_UP; +} \ No newline at end of file diff --git a/modules/buttons/buttons.h b/modules/buttons/buttons.h new file mode 100644 index 0000000..641dd41 --- /dev/null +++ b/modules/buttons/buttons.h @@ -0,0 +1,17 @@ +//=====[#include guards - begin]=============================================== + +#ifndef _BUTTONS_H_ +#define _BUTTONS_H_ + +//=====[Declaration of public defines]========================================= + +//=====[Declaration of public data types]====================================== + +//=====[Declarations (prototypes) of public functions]========================= + +void buttonsInit( int updateTime_ms ); +bool statusButton(); + +//=====[#include guards - end]================================================= + +#endif \ No newline at end of file diff --git a/modules/control_system/control_system.cpp b/modules/control_system/control_system.cpp new file mode 100644 index 0000000..7ad6b09 --- /dev/null +++ b/modules/control_system/control_system.cpp @@ -0,0 +1,38 @@ +//=====[Libraries]============================================================= + +#include "arm_book_lib.h" + +#include "control_system.h" +#include "control_panel.h" +#include "serial_communication.h" + +//pendiente de agregar mas modulos con los cuales interactua + + +//=====[Declaration of private defines]======================================== + +//=====[Declaration of private data types]===================================== + +//=====[Declaration and initialization of public global objects]=============== + +//=====[Declaration of external public global variables]======================= + +//=====[Declaration and initialization of public global variables]============= + +//=====[Declaration and initialization of private global variables]============ + +//=====[Declarations (prototypes) of private functions]======================== + +//=====[Implementations of public functions]=================================== + +void controlSystemInit(){ + controlPanelInit(); +} + +void controlSystemUpdate() +{ + controlPanelUpdate(); + delay(SYSTEM_TIME_INCREMENT_MS); +} + +//=====[Implementations of private functions]================================== \ No newline at end of file diff --git a/modules/control_system/control_system.h b/modules/control_system/control_system.h new file mode 100644 index 0000000..422e0e4 --- /dev/null +++ b/modules/control_system/control_system.h @@ -0,0 +1,18 @@ +//=====[#include guards - begin]=============================================== + +#ifndef _SMART_HOME_SYSTEM_H_ +#define _SMART_HOME_SYSTEM_H_ + +//=====[Declaration of public defines]========================================= +#define SYSTEM_TIME_INCREMENT_MS 10 + +//=====[Declaration of public data types]====================================== + +//=====[Declarations (prototypes) of public functions]========================= + +void controlSystemInit(); +void controlSystemUpdate(); + +//=====[#include guards - end]================================================= + +#endif // _SMART_HOME_SYSTEM_H_ \ No newline at end of file diff --git a/modules/display/display.cpp b/modules/display/display.cpp new file mode 100644 index 0000000..3ab8e63 --- /dev/null +++ b/modules/display/display.cpp @@ -0,0 +1,357 @@ +//=====[Libraries]============================================================= + +#include "mbed.h" +#include "arm_book_lib.h" +#include "display.h" + +//=====[Declaration of private defines]======================================== + +#define DISPLAY_IR_CLEAR_DISPLAY 0b00000001 +#define DISPLAY_IR_ENTRY_MODE_SET 0b00000100 +#define DISPLAY_IR_DISPLAY_CONTROL 0b00001000 +#define DISPLAY_IR_FUNCTION_SET 0b00100000 +#define DISPLAY_IR_SET_DDRAM_ADDR 0b10000000 + +#define DISPLAY_IR_ENTRY_MODE_SET_INCREMENT 0b00000010 +#define DISPLAY_IR_ENTRY_MODE_SET_DECREMENT 0b00000000 +#define DISPLAY_IR_ENTRY_MODE_SET_SHIFT 0b00000001 +#define DISPLAY_IR_ENTRY_MODE_SET_NO_SHIFT 0b00000000 + +#define DISPLAY_IR_DISPLAY_CONTROL_DISPLAY_ON 0b00000100 +#define DISPLAY_IR_DISPLAY_CONTROL_DISPLAY_OFF 0b00000000 +#define DISPLAY_IR_DISPLAY_CONTROL_CURSOR_ON 0b00000010 +#define DISPLAY_IR_DISPLAY_CONTROL_CURSOR_OFF 0b00000000 +#define DISPLAY_IR_DISPLAY_CONTROL_BLINK_ON 0b00000001 +#define DISPLAY_IR_DISPLAY_CONTROL_BLINK_OFF 0b00000000 + +#define DISPLAY_IR_FUNCTION_SET_8BITS 0b00010000 +#define DISPLAY_IR_FUNCTION_SET_4BITS 0b00000000 +#define DISPLAY_IR_FUNCTION_SET_2LINES 0b00001000 +#define DISPLAY_IR_FUNCTION_SET_1LINE 0b00000000 +#define DISPLAY_IR_FUNCTION_SET_5x10DOTS 0b00000100 +#define DISPLAY_IR_FUNCTION_SET_5x8DOTS 0b00000000 + +#define DISPLAY_20x4_LINE1_FIRST_CHARACTER_ADDRESS 0 +#define DISPLAY_20x4_LINE2_FIRST_CHARACTER_ADDRESS 64 +#define DISPLAY_20x4_LINE3_FIRST_CHARACTER_ADDRESS 20 +#define DISPLAY_20x4_LINE4_FIRST_CHARACTER_ADDRESS 84 + +#define DISPLAY_RS_INSTRUCTION 0 +#define DISPLAY_RS_DATA 1 + +#define DISPLAY_RW_WRITE 0 +#define DISPLAY_RW_READ 1 + +#define DISPLAY_PIN_RS 4 +#define DISPLAY_PIN_RW 5 +#define DISPLAY_PIN_EN 6 +#define DISPLAY_PIN_D0 7 +#define DISPLAY_PIN_D1 8 +#define DISPLAY_PIN_D2 9 +#define DISPLAY_PIN_D3 10 +#define DISPLAY_PIN_D4 11 +#define DISPLAY_PIN_D5 12 +#define DISPLAY_PIN_D6 13 +#define DISPLAY_PIN_D7 14 + +#define DISPLAY_PIN_A_PCF8574 3 + +#define I2C1_SDA PB_9 +#define I2C1_SCL PB_8 + +#define PCF8574_I2C_BUS_8BIT_WRITE_ADDRESS 78 + +//=====[Declaration of private data types]===================================== + +typedef struct{ + int address; + char data; + bool displayPinRs; + bool displayPinRw; + bool displayPinEn; + bool displayPinA; + bool displayPinD4; + bool displayPinD5; + bool displayPinD6; + bool displayPinD7; +} pcf8574_t; + +//=====[Declaration and initialization of public global objects]=============== + +DigitalOut displayD0( D0 ); +DigitalOut displayD1( D1 ); +DigitalOut displayD2( D2 ); +DigitalOut displayD3( D3 ); +DigitalOut displayD4( D4 ); +DigitalOut displayD5( D5 ); +DigitalOut displayD6( D6 ); +DigitalOut displayD7( D7 ); +DigitalOut displayRs( D8 ); +DigitalOut displayEn( D9 ); + +I2C i2cPcf8574( I2C1_SDA, I2C1_SCL ); + +//=====[Declaration of external public global variables]======================= + +//=====[Declaration and initialization of public global variables]============= + +//=====[Declaration and initialization of private global variables]============ + +static display_t display; +static pcf8574_t pcf8574; +static bool initial8BitCommunicationIsCompleted; + +//=====[Declarations (prototypes) of private functions]======================== + +static void displayPinWrite( uint8_t pinName, int value ); +static void displayDataBusWrite( uint8_t dataByte ); +static void displayCodeWrite( bool type, uint8_t dataBus ); + +//=====[Implementations of public functions]=================================== + +void displayInit( displayConnection_t connection ) +{ + display.connection = connection; + + if( display.connection == DISPLAY_CONNECTION_I2C_PCF8574_IO_EXPANDER) { + pcf8574.address = PCF8574_I2C_BUS_8BIT_WRITE_ADDRESS; + pcf8574.data = 0b00000000; + i2cPcf8574.frequency(100000); + displayPinWrite( DISPLAY_PIN_A_PCF8574, ON ); + } + + initial8BitCommunicationIsCompleted = false; + + delay( 50 ); + + displayCodeWrite( DISPLAY_RS_INSTRUCTION, + DISPLAY_IR_FUNCTION_SET | + DISPLAY_IR_FUNCTION_SET_8BITS ); + delay( 5 ); + + displayCodeWrite( DISPLAY_RS_INSTRUCTION, + DISPLAY_IR_FUNCTION_SET | + DISPLAY_IR_FUNCTION_SET_8BITS ); + delay( 1 ); + + displayCodeWrite( DISPLAY_RS_INSTRUCTION, + DISPLAY_IR_FUNCTION_SET | + DISPLAY_IR_FUNCTION_SET_8BITS ); + delay( 1 ); + + switch( display.connection ) { + case DISPLAY_CONNECTION_GPIO_8BITS: + displayCodeWrite( DISPLAY_RS_INSTRUCTION, + DISPLAY_IR_FUNCTION_SET | + DISPLAY_IR_FUNCTION_SET_8BITS | + DISPLAY_IR_FUNCTION_SET_2LINES | + DISPLAY_IR_FUNCTION_SET_5x8DOTS ); + delay( 1 ); + break; + + case DISPLAY_CONNECTION_GPIO_4BITS: + case DISPLAY_CONNECTION_I2C_PCF8574_IO_EXPANDER: + displayCodeWrite( DISPLAY_RS_INSTRUCTION, + DISPLAY_IR_FUNCTION_SET | + DISPLAY_IR_FUNCTION_SET_4BITS ); + delay( 1 ); + + initial8BitCommunicationIsCompleted = true; + + displayCodeWrite( DISPLAY_RS_INSTRUCTION, + DISPLAY_IR_FUNCTION_SET | + DISPLAY_IR_FUNCTION_SET_4BITS | + DISPLAY_IR_FUNCTION_SET_2LINES | + DISPLAY_IR_FUNCTION_SET_5x8DOTS ); + delay( 1 ); + break; + } + + displayCodeWrite( DISPLAY_RS_INSTRUCTION, + DISPLAY_IR_DISPLAY_CONTROL | + DISPLAY_IR_DISPLAY_CONTROL_DISPLAY_OFF | + DISPLAY_IR_DISPLAY_CONTROL_CURSOR_OFF | + DISPLAY_IR_DISPLAY_CONTROL_BLINK_OFF ); + delay( 1 ); + + displayCodeWrite( DISPLAY_RS_INSTRUCTION, + DISPLAY_IR_CLEAR_DISPLAY ); + delay( 1 ); + + displayCodeWrite( DISPLAY_RS_INSTRUCTION, + DISPLAY_IR_ENTRY_MODE_SET | + DISPLAY_IR_ENTRY_MODE_SET_INCREMENT | + DISPLAY_IR_ENTRY_MODE_SET_NO_SHIFT ); + delay( 1 ); + + displayCodeWrite( DISPLAY_RS_INSTRUCTION, + DISPLAY_IR_DISPLAY_CONTROL | + DISPLAY_IR_DISPLAY_CONTROL_DISPLAY_ON | + DISPLAY_IR_DISPLAY_CONTROL_CURSOR_OFF | + DISPLAY_IR_DISPLAY_CONTROL_BLINK_OFF ); + delay( 1 ); +} + +void displayCharPositionWrite( uint8_t charPositionX, uint8_t charPositionY ) +{ + switch( charPositionY ) { + case 0: + displayCodeWrite( DISPLAY_RS_INSTRUCTION, + DISPLAY_IR_SET_DDRAM_ADDR | + ( DISPLAY_20x4_LINE1_FIRST_CHARACTER_ADDRESS + + charPositionX ) ); + delay( 1 ); + break; + + case 1: + displayCodeWrite( DISPLAY_RS_INSTRUCTION, + DISPLAY_IR_SET_DDRAM_ADDR | + ( DISPLAY_20x4_LINE2_FIRST_CHARACTER_ADDRESS + + charPositionX ) ); + delay( 1 ); + break; + + case 2: + displayCodeWrite( DISPLAY_RS_INSTRUCTION, + DISPLAY_IR_SET_DDRAM_ADDR | + ( DISPLAY_20x4_LINE3_FIRST_CHARACTER_ADDRESS + + charPositionX ) ); + delay( 1 ); + break; + + case 3: + displayCodeWrite( DISPLAY_RS_INSTRUCTION, + DISPLAY_IR_SET_DDRAM_ADDR | + ( DISPLAY_20x4_LINE4_FIRST_CHARACTER_ADDRESS + + charPositionX ) ); + delay( 1 ); + break; + } +} + +void displayStringWrite( const char * str ) +{ + while (*str) { + displayCodeWrite(DISPLAY_RS_DATA, *str++); + } +} + +//=====[Implementations of private functions]================================== + +static void displayCodeWrite( bool type, uint8_t dataBus ) +{ + if ( type == DISPLAY_RS_INSTRUCTION ) + displayPinWrite( DISPLAY_PIN_RS, DISPLAY_RS_INSTRUCTION); + else + displayPinWrite( DISPLAY_PIN_RS, DISPLAY_RS_DATA); + displayPinWrite( DISPLAY_PIN_RW, DISPLAY_RW_WRITE ); + displayDataBusWrite( dataBus ); +} + +static void displayPinWrite( uint8_t pinName, int value ) +{ + switch( display.connection ) { + case DISPLAY_CONNECTION_GPIO_8BITS: + switch( pinName ) { + case DISPLAY_PIN_D0: displayD0 = value; break; + case DISPLAY_PIN_D1: displayD1 = value; break; + case DISPLAY_PIN_D2: displayD2 = value; break; + case DISPLAY_PIN_D3: displayD3 = value; break; + case DISPLAY_PIN_D4: displayD4 = value; break; + case DISPLAY_PIN_D5: displayD5 = value; break; + case DISPLAY_PIN_D6: displayD6 = value; break; + case DISPLAY_PIN_D7: displayD7 = value; break; + case DISPLAY_PIN_RS: displayRs = value; break; + case DISPLAY_PIN_EN: displayEn = value; break; + case DISPLAY_PIN_RW: break; + default: break; + } + break; + case DISPLAY_CONNECTION_GPIO_4BITS: + switch( pinName ) { + case DISPLAY_PIN_D4: displayD4 = value; break; + case DISPLAY_PIN_D5: displayD5 = value; break; + case DISPLAY_PIN_D6: displayD6 = value; break; + case DISPLAY_PIN_D7: displayD7 = value; break; + case DISPLAY_PIN_RS: displayRs = value; break; + case DISPLAY_PIN_EN: displayEn = value; break; + case DISPLAY_PIN_RW: break; + default: break; + } + break; + case DISPLAY_CONNECTION_I2C_PCF8574_IO_EXPANDER: + if ( value ) { + switch( pinName ) { + case DISPLAY_PIN_D4: pcf8574.displayPinD4 = ON; break; + case DISPLAY_PIN_D5: pcf8574.displayPinD5 = ON; break; + case DISPLAY_PIN_D6: pcf8574.displayPinD6 = ON; break; + case DISPLAY_PIN_D7: pcf8574.displayPinD7 = ON; break; + case DISPLAY_PIN_RS: pcf8574.displayPinRs = ON; break; + case DISPLAY_PIN_EN: pcf8574.displayPinEn = ON; break; + case DISPLAY_PIN_RW: pcf8574.displayPinRw = ON; break; + case DISPLAY_PIN_A_PCF8574: pcf8574.displayPinA = ON; break; + default: break; + } + } + else { + switch( pinName ) { + case DISPLAY_PIN_D4: pcf8574.displayPinD4 = OFF; break; + case DISPLAY_PIN_D5: pcf8574.displayPinD5 = OFF; break; + case DISPLAY_PIN_D6: pcf8574.displayPinD6 = OFF; break; + case DISPLAY_PIN_D7: pcf8574.displayPinD7 = OFF; break; + case DISPLAY_PIN_RS: pcf8574.displayPinRs = OFF; break; + case DISPLAY_PIN_EN: pcf8574.displayPinEn = OFF; break; + case DISPLAY_PIN_RW: pcf8574.displayPinRw = OFF; break; + case DISPLAY_PIN_A_PCF8574: pcf8574.displayPinA = OFF; break; + default: break; + } + } + pcf8574.data = 0b00000000; + if ( pcf8574.displayPinRs ) pcf8574.data |= 0b00000001; + if ( pcf8574.displayPinRw ) pcf8574.data |= 0b00000010; + if ( pcf8574.displayPinEn ) pcf8574.data |= 0b00000100; + if ( pcf8574.displayPinA ) pcf8574.data |= 0b00001000; + if ( pcf8574.displayPinD4 ) pcf8574.data |= 0b00010000; + if ( pcf8574.displayPinD5 ) pcf8574.data |= 0b00100000; + if ( pcf8574.displayPinD6 ) pcf8574.data |= 0b01000000; + if ( pcf8574.displayPinD7 ) pcf8574.data |= 0b10000000; + i2cPcf8574.write( pcf8574.address, &pcf8574.data, 1); + break; + } +} + +static void displayDataBusWrite( uint8_t dataBus ) +{ + displayPinWrite( DISPLAY_PIN_EN, OFF ); + displayPinWrite( DISPLAY_PIN_D7, dataBus & 0b10000000 ); + displayPinWrite( DISPLAY_PIN_D6, dataBus & 0b01000000 ); + displayPinWrite( DISPLAY_PIN_D5, dataBus & 0b00100000 ); + displayPinWrite( DISPLAY_PIN_D4, dataBus & 0b00010000 ); + switch( display.connection ) { + case DISPLAY_CONNECTION_GPIO_8BITS: + displayPinWrite( DISPLAY_PIN_D3, dataBus & 0b00001000 ); + displayPinWrite( DISPLAY_PIN_D2, dataBus & 0b00000100 ); + displayPinWrite( DISPLAY_PIN_D1, dataBus & 0b00000010 ); + displayPinWrite( DISPLAY_PIN_D0, dataBus & 0b00000001 ); + break; + + case DISPLAY_CONNECTION_GPIO_4BITS: + case DISPLAY_CONNECTION_I2C_PCF8574_IO_EXPANDER: + if ( initial8BitCommunicationIsCompleted == true) { + displayPinWrite( DISPLAY_PIN_EN, ON ); + delay( 1 ); + displayPinWrite( DISPLAY_PIN_EN, OFF ); + delay( 1 ); + displayPinWrite( DISPLAY_PIN_D7, dataBus & 0b00001000 ); + displayPinWrite( DISPLAY_PIN_D6, dataBus & 0b00000100 ); + displayPinWrite( DISPLAY_PIN_D5, dataBus & 0b00000010 ); + displayPinWrite( DISPLAY_PIN_D4, dataBus & 0b00000001 ); + } + break; + + } + displayPinWrite( DISPLAY_PIN_EN, ON ); + delay( 1 ); + displayPinWrite( DISPLAY_PIN_EN, OFF ); + delay( 1 ); +} \ No newline at end of file diff --git a/modules/display/display.h b/modules/display/display.h new file mode 100644 index 0000000..63106d4 --- /dev/null +++ b/modules/display/display.h @@ -0,0 +1,31 @@ +//=====[#include guards - begin]=============================================== +#include +#ifndef _DISPLAY_H_ +#define _DISPLAY_H_ + +//=====[Declaration of public defines]========================================= + +//=====[Declaration of public data types]====================================== + +typedef enum { + DISPLAY_CONNECTION_GPIO_4BITS, + DISPLAY_CONNECTION_GPIO_8BITS, + DISPLAY_CONNECTION_I2C_PCF8574_IO_EXPANDER, +} displayConnection_t; + +typedef struct { + displayConnection_t connection; +} display_t; + +//=====[Declarations (prototypes) of public functions]========================= + +void displayInit( displayConnection_t connection ); + +void displayCharPositionWrite( uint8_t charPositionX, uint8_t charPositionY ); + +void displayStringWrite( const char * str ); + +//=====[#include guards - end]================================================= + +#endif // _DISPLAY_H_ + diff --git a/modules/matrix_keypad/matrix_keypad.cpp b/modules/matrix_keypad/matrix_keypad.cpp new file mode 100644 index 0000000..8378d87 --- /dev/null +++ b/modules/matrix_keypad/matrix_keypad.cpp @@ -0,0 +1,137 @@ +//=====[Libraries]============================================================= + +#include "mbed.h" +#include "arm_book_lib.h" + +#include "matrix_keypad.h" + + +//=====[Declaration of private defines]======================================== +#define MATRIX_KEYPAD_NUMBER_OF_ROWS 4 +#define MATRIX_KEYPAD_NUMBER_OF_COLS 3 +#define DEBOUNCE_KEY_TIME_MS 30 + + +//=====[Declaration of private data types]===================================== +typedef enum { + MATRIX_KEYPAD_SCANNING, + MATRIX_KEYPAD_DEBOUNCE, + MATRIX_KEYPAD_KEY_HOLD_PRESSED +} matrixKeypadState_t; + + +//=====[Declaration and initialization of public global objects]=============== +DigitalOut keypadRowPins[MATRIX_KEYPAD_NUMBER_OF_ROWS] = {PE_11, PF_3, PF_15, PF_11}; +DigitalIn keypadColPins[MATRIX_KEYPAD_NUMBER_OF_COLS] = {PE_0, PG_8, PG_5}; + +//=====[Declaration of external public global variables]======================= + +//=====[Declaration and initialization of public global variables]============= + +//=====[Declaration and initialization of private global variables]============ +static matrixKeypadState_t matrixKeypadState; +static int timeIncrement_ms = 0; + + +//=====[Declarations (prototypes) of private functions]======================== +static char matrixKeypadScan(); +static void matrixKeypadReset(); + + +//=====[Implementations of public functions]=================================== +void matrixKeypadInit( int updateTime_ms ) +{ + timeIncrement_ms = updateTime_ms; + matrixKeypadState = MATRIX_KEYPAD_SCANNING; + int pinIndex = 0; + for( pinIndex=0; pinIndex= + DEBOUNCE_KEY_TIME_MS ) { + keyDetected = matrixKeypadScan(); + if( keyDetected == matrixKeypadLastKeyPressed ) { + matrixKeypadState = MATRIX_KEYPAD_KEY_HOLD_PRESSED; + } else { + matrixKeypadState = MATRIX_KEYPAD_SCANNING; + } + } + accumulatedDebounceMatrixKeypadTime = + accumulatedDebounceMatrixKeypadTime + timeIncrement_ms; + break; + + case MATRIX_KEYPAD_KEY_HOLD_PRESSED: + keyDetected = matrixKeypadScan(); + if( keyDetected != matrixKeypadLastKeyPressed ) { + if( keyDetected == '\0' ) { + keyReleased = matrixKeypadLastKeyPressed; + } + matrixKeypadState = MATRIX_KEYPAD_SCANNING; + } + break; + + default: + matrixKeypadReset(); + break; + } + return keyReleased; +} + + +//=====[Implementations of private functions]================================== +static char matrixKeypadScan() +{ + int row = 0; + int col = 0; + int i = 0; + + char matrixKeypadIndexToCharArray[] = { + '1', '2', '3', + '4', '5', '6', + '7', '8', '9', + '*', '0', '#', + }; + + for( row=0; row 1) { + int length = snprintf(buffer, sizeof(buffer), + " Hz \nPara confirmar frecuencia presione # \n"); + serialComWrite(buffer, length); + length = snprintf(buffer, sizeof(buffer), + "Para cancelar frecuencia presione * \n"); + serialComWrite(buffer, length); + options_menu=OPTIONS; + ajustFrequency = CONFIRM; + timesKeyPressed = 0; + } + break; + + case STATUS_SYSTEM: + length = snprintf(buffer, sizeof(buffer), + "\nPresión 1: %.2f \n", readPressureM1()); + serialComWrite(buffer, length); + length = snprintf(buffer, sizeof(buffer), + "\nPresión 2: %.2f \n", readPressureM2()); + serialComWrite(buffer, length); + length = snprintf(buffer, sizeof(buffer), + "\nBomba 1: %s \n", StatePump1Read()? "Encendida" : "Apagada"); + serialComWrite(buffer, length); + length = snprintf(buffer, sizeof(buffer), + "\nBomba 2: %s \n", StatePump2Read()? "Encendida" : "Apagada"); + serialComWrite(buffer, length); + options_menu=OPTIONS; + lcdState = PRESSURE_SP1; + break; + + case STOP_SYSTEM: + runPumps=false; + pumpsInitUpdate(runPumps); + options_menu=OPTIONS; + lcdState = PRESSURE_SP1; + break; + + } +}; \ No newline at end of file diff --git a/modules/panel_control/control_panel.h b/modules/panel_control/control_panel.h new file mode 100644 index 0000000..ea274fa --- /dev/null +++ b/modules/panel_control/control_panel.h @@ -0,0 +1,17 @@ +//=====[#include guards - begin]=============================================== + +#ifndef _CONTROL_PANEL_H_ +#define _CONTROL_PANEL_H_ + +//=====[Declaration of public defines]========================================= + +//=====[Declaration of public data types]====================================== + +//=====[Declarations (prototypes) of public functions]========================= +void controlPanelInit(); +void controlPanelUpdate(); + + +//=====[#include guards - end]================================================= + +#endif // _CONTROL_PANEL_H_ \ No newline at end of file diff --git a/modules/pressure_sensor/pressure_sensor.cpp b/modules/pressure_sensor/pressure_sensor.cpp new file mode 100644 index 0000000..ad2ce0c --- /dev/null +++ b/modules/pressure_sensor/pressure_sensor.cpp @@ -0,0 +1,68 @@ +//=====[Libraries]============================================================= + +#include "arm_book_lib.h" + +//=====[Declaration of private defines]======================================== +#define TIME_CHECK_PRESSURE 2000 +#define TIME_INCREMENT_MS 10 + +//=====[Declaration of private data types]===================================== + +//=====[Declaration and initialization of public global objects]=============== +AnalogIn potentiometer(A0); + +//=====[Declaration of external public global variables]======================= + +//=====[Declaration and initialization of public global variables]============= +float valuePressureM1 = 0.0; +float valuePressureM2 = 0.0; + +//=====[Declaration and initialization of private global variables]============ +static int timeCheckPressure = 0; +static int timeAccumulatedCheckPressure=0; + +//=====[Declarations (prototypes) of private functions]======================== +float readPressureM1(); +float readPressureM2(); + +//=====[Implementations of public functions]=================================== + +void pressureSensorInit() {} + + +void pressureSensorUpdate(){ + /* + //verifica presion cada 2 seg + if (timeAccumulatedCheckPressure <= TIME_CHECK_PRESSURE){ + timeAccumulatedCheckPressure=timeAccumulatedCheckPressure+TIME_INCREMENT_MS; + } + else{ + timeAccumulatedCheckPressure = 0; + } + */ +}; + +void strPressureM1(char* buffer, size_t bufferSize){ + snprintf(buffer, bufferSize, "%.2f", readPressureM1()); // Convertir float a char +} + +void strPressureM2(char* buffer, size_t bufferSize){ + snprintf(buffer, bufferSize, "%.2f", readPressureM2()); // Convertir float a char +} + +float readPressureM1() { + float analogValue = potentiometer.read(); + float voltagePsi= (analogValue * 3.3)*30; + float pressureM1=voltagePsi; + return pressureM1; +} + +float readPressureM2() { + float analogValue = potentiometer.read(); + float voltagePsi= ((analogValue * 3.3)*30)-5; + float pressureM2=voltagePsi; + return pressureM2; +} + +//=====[Implementations of private functions]================================== + diff --git a/modules/pressure_sensor/pressure_sensor.h b/modules/pressure_sensor/pressure_sensor.h new file mode 100644 index 0000000..d239f2e --- /dev/null +++ b/modules/pressure_sensor/pressure_sensor.h @@ -0,0 +1,23 @@ +//=====[#include guards - begin]=============================================== +#include +#include + +#ifndef _PRESSURE_SENSOR_H_ +#define _PRESSURE_SENSOR_H_ + +//=====[Declaration of public defines]========================================= + +//=====[Declaration of public data types]====================================== + +//=====[Declarations (prototypes) of public functions]========================= + +void pressureSensorInit(); +void pressureSensorUpdate(); +void strPressureM1(char* buffer, size_t bufferSize); +void strPressureM2(char* buffer, size_t bufferSize); +float readPressureM1(); +float readPressureM2(); + +//=====[#include guards - end]================================================= + +#endif // _PRESSURE_SENSOR_H_ \ No newline at end of file diff --git a/modules/pumps/pumps.cpp b/modules/pumps/pumps.cpp new file mode 100644 index 0000000..b23aef2 --- /dev/null +++ b/modules/pumps/pumps.cpp @@ -0,0 +1,95 @@ +//=====[Libraries]============================================================= +#include "mbed.h" +#include "arm_book_lib.h" +#include "pressure_sensor.h" +#include "control_system.h" + +//=====[Declaration of private defines]======================================== +#define TIME_CHECK_PRESSURE 200 +#define PRESSURE_CUT_IN_P1 20 +#define PRESSURE_CUT_OFF_P1 80 +#define TIME_INCREMENT_MS 10 + +//=====[Declaration of private data types]===================================== + +//=====[Declaration and initialization of public global objects]=============== +DigitalOut pump1(LED2); +DigitalOut pump2(LED3); + +//=====[Declaration of external public global variables]======================= + +//=====[Declaration and initialization of public global variables]============= +float valuePressure1=0; +float valuePressure2=0; +bool statePump1 = false; +bool statePump2 = false; + +//=====[Declaration and initialization of private global variables]============ +static int timeAccumulatedCheckPressure=0; + +//=====[Declarations (prototypes) of private functions]======================== +static bool statusPump1(); +static bool statusPump2(); +static void checkPressure(); + +//=====[Implementations of public functions]=================================== +void pumpsInit(){ + pump1=false; + pump2=false; +} + +void pumpsInitUpdate(bool run){ + if (run){ + checkPressure(); + }else{ + pump1=false; + pump2=false; + } +} + + +bool StatePump1Read() +{ + return statePump1; +} + +bool StatePump2Read() +{ + return statePump2; +} +//=====[Implementations of private functions]================================== + +static void checkPressure() { + if (timeAccumulatedCheckPressure <= TIME_CHECK_PRESSURE){ + timeAccumulatedCheckPressure=timeAccumulatedCheckPressure+TIME_INCREMENT_MS; + } + else{ + statePump1=statusPump1(); + statePump2=statusPump2(); + } +} + +static bool statusPump1(){ + valuePressure1 = readPressureM1(); + if (valuePressure1 <= PRESSURE_CUT_IN_P1) { + pump1 = false; + pump2 = false; + } else if (valuePressure1 > PRESSURE_CUT_IN_P1 && valuePressure1 <= PRESSURE_CUT_OFF_P1) { + pump1 = true; + } else { + pump1 = false; + } + return pump1; +} + +static bool statusPump2(){ + valuePressure2 = readPressureM1(); + if (valuePressure2 <= PRESSURE_CUT_IN_P1) { + pump2 = false; + } else if (valuePressure2 > PRESSURE_CUT_IN_P1 && valuePressure2 <= PRESSURE_CUT_OFF_P1) { + pump2 = true; + } else { + pump2 = false; + } + return pump2; +} diff --git a/modules/pumps/pumps.h b/modules/pumps/pumps.h new file mode 100644 index 0000000..7d5814c --- /dev/null +++ b/modules/pumps/pumps.h @@ -0,0 +1,19 @@ +//=====[#include guards - begin]=============================================== + +#ifndef _PUMPS_ +#define _PUMPS_ + +//=====[Declaration of public defines]========================================= + +//=====[Declaration of public data types]====================================== + +//=====[Declarations (prototypes) of public functions]========================= + +void pumpsInit(); +void pumpsInitUpdate(bool run); +bool StatePump1Read(); +bool StatePump2Read(); + +//=====[#include guards - end]================================================= + +#endif // _PUMPS_ \ No newline at end of file diff --git a/modules/serial_communication/serial_communication.cpp b/modules/serial_communication/serial_communication.cpp new file mode 100644 index 0000000..9194385 --- /dev/null +++ b/modules/serial_communication/serial_communication.cpp @@ -0,0 +1,55 @@ +//=====[Libraries]============================================================= + +#include "mbed.h" +#include "arm_book_lib.h" + +#include "serial_communication.h" +#include "matrix_keypad.h" + + +//=====[Declaration of private defines]======================================== + +//=====[Declaration of private data types]===================================== + +//=====[Declaration and initialization of public global objects]=============== + +UnbufferedSerial uartUsb(USBTX, USBRX, 115200); + +//=====[Declaration of external public global variables]======================= + +//=====[Declaration and initialization of public global variables]============= + + +//=====[Declaration and initialization of private global variables]============ + + +//=====[Declarations (prototypes) of private functions]======================== +void static serialComRead(); + +//=====[Implementations of public functions]=================================== + +void serialComInit() +{ +} + +void serialMenu() +{ + uartUsb.write( "\n",1); + uartUsb.write( "MENU DE CONFIGURACION \n",24); + uartUsb.write( "\n",1); + uartUsb.write( "Seleccione el item que desea configurar: \n",43); + uartUsb.write( "1. Ajustar Frecuencia de la bomba \n",39); + uartUsb.write( "2. Verificar estado del sistema \n",39); + uartUsb.write( "3. Detener sistema \n",39); + uartUsb.write( "\n",1); +} + +void serialComWrite( char* message, int length) +{ + uartUsb.write(message, length ); +} + + +//=====[Implementations of private functions]================================== + + diff --git a/modules/serial_communication/serial_communication.h b/modules/serial_communication/serial_communication.h new file mode 100644 index 0000000..6997139 --- /dev/null +++ b/modules/serial_communication/serial_communication.h @@ -0,0 +1,19 @@ +//=====[#include guards - begin]=============================================== + +#ifndef _SERIAL_COMMUNICATION_ +#define _SERIAL_COMMUNICATION_ + +//=====[Declaration of public defines]========================================= + +//=====[Declaration of public data types]====================================== + +//=====[Declarations (prototypes) of public functions]========================= + +void serialComInit(); +void serialComKeypadUpdate(); +void serialMenu(); +void serialComWrite( char* message, int length); + +//=====[#include guards - end]================================================= + +#endif // _PC_SERIAL_COM_H_ \ No newline at end of file