A modified Arduino library to let mbed boards to behave as modbus slave. See readme. Tested on Nucleo F401RE.
Dependencies: mbed mbed-rtos ModbusSlave232 MODSERIAL max6675 millis
Revision 5:cc12e1f4daf1, committed 2021-09-02
- Comitter:
- ascheeren
- Date:
- Thu Sep 02 14:46:39 2021 +0000
- Parent:
- 4:80f3ac4b1c8b
- Commit message:
- panela modbus
Changed in this revision
diff -r 80f3ac4b1c8b -r cc12e1f4daf1 ModbusSlave232.lib --- a/ModbusSlave232.lib Thu Jul 28 14:59:54 2016 +0000 +++ b/ModbusSlave232.lib Thu Sep 02 14:46:39 2021 +0000 @@ -1,1 +1,1 @@ -https://developer.mbed.org/users/AfdhalAtiffTan/code/ModbusSlave232/#35fdc7056f66 +https://os.mbed.com/users/ascheeren/code/ModbusSlave232/#b8d920737d9e
diff -r 80f3ac4b1c8b -r cc12e1f4daf1 ios.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ios.h Thu Sep 02 14:46:39 2021 +0000 @@ -0,0 +1,9 @@ + + + DigitalOut buzerOUT (PA_0); + DigitalOut bombaOUT (PA_1); + DigitalOut aquecimentoOUT(PA_2); + DigitalOut led1OUT (PA_3); + PwmOut res (PB_1); + +
diff -r 80f3ac4b1c8b -r cc12e1f4daf1 main.cpp --- a/main.cpp Thu Jul 28 14:59:54 2016 +0000 +++ b/main.cpp Thu Sep 02 14:46:39 2021 +0000 @@ -2,52 +2,193 @@ #include "rtos.h" #include "ModbusSlave232.h" //see readme #include "millis.h" //see readme - +#include "ios.h" +#include "max6675.h" ModbusSlave232 mbs; // Create new mbs instance -// Slave registers -enum { - MB_0, // Register 0 - MB_1, // Register 1 - MB_2, // Register 2 - MB_3, // Register 3 - MB_4, // Register 4 - MB_REGS // Dummy register. using 0 offset to keep size of array -}; +SPI spi(PB_5, PB_4, PB_3); // MOSI, MISO, SCLK +max6675 max(spi,PB_10); + + + +Ticker seg,media ; -DigitalOut led1(LED1); - -int regs[MB_REGS]; +// Slave registersr +enum { + MB_0, // temperatura + MB_1, // tempo restante + MB_2, // setpoin temperatura + MB_3, // aquecimento bit5 manual/auto bit 4 liga/deli(manual) bit0estado + MB_4, // set temp1 + MB_5, // set temp2 + MB_6, // set temp3 + MB_7, // set temp4 + MB_8, // set temp5 fervura + MB_9, // set tempo 1 + MB_10, // set tempo 2 + MB_11, // set tempo 3 + MB_12, // set tempo 4 + MB_13, // set tempo 5 fervura + MB_14, //pwm + MB_15, // + MB_16, // bomba + MB_17, // + MB_18, //adiantar + MB_19, //ok + MB_20, + MB_21, + MB_REGS // Dummy register. using 0 offset to keep size of array +}; +//Prototipos +void cozimento(void); +void PID(); +void segs(); +void passagem_zero(); +void atualiza(void); +void leitura(void); +void media_temp(); +// variaveis Globais -void another_thread(void const *argument) // do stuff -{ - while (true) { - Thread::wait(100); - led1 = !led1; - } -} + +float Kp=5.5, Ki=0.5, Kd=0.1, propo, integ, deriv,medida, setpoint[6], valor_medido, erro, erro_ant, pwm,temperatura, setpointAtual, periodo; + int regs[MB_REGS], etapa, minetapa[6], bomba, tempoEtapa, cont1, cont2, cont3, aquecimento; + + int main() { const unsigned char SLAVE = 1; - const long BAUD = 9600; + const long BAUD = 9600; const unsigned PARITY = 'n'; - + seg.attach(&segs, 1.0); + media.attach(&media_temp, 0.05); + etapa=0;; + res.period(5.f); + startMillis(); // milliseconds (arduino like) mbs.configure(SLAVE, BAUD, PARITY); - Thread thread(another_thread, NULL, osPriorityNormal, DEFAULT_STACK_SIZE); - - //test values (updatable) - regs[MB_0] = 0xCA1F; - regs[MB_1] = 0xFACE; - regs[MB_2] = 0xC0DE; - regs[MB_3] = 0x1234; - - while (true) //main thread - { + + + //test values (updatable) + + + while (true) { //main thread + if(aquecimento){ //se aquecimento estiver ligado periodo do duty cycle sera 1 div pwm + periodo=1.0f/pwm; + res.write(periodo); + } + + if(!aquecimento)res.write(0.0f); // se aquecimento estiver deligado duty sera 0 + + + + atualiza(); // atualiza holdregistres + + bombaOUT=bomba; + + + + + + switch(etapa){ + case 1: + {tempoEtapa=minetapa[0]; + setpointAtual=setpoint[0]; + } break; + + case 2: + {tempoEtapa=minetapa[1]; + setpointAtual=setpoint[1]; + } break; + + + case 3: + {tempoEtapa=minetapa[2]; + setpointAtual=setpoint[2]; + } break; + + + + + } + + mbs.update(regs, MB_REGS); // Pass current register values to mbs - Thread::wait(10); // not too sure if this is needed + + + + + + } -} \ No newline at end of file + +} +void atualiza() +{ + + +} +void leitura() +{ + int conta; + float cache; + cache+=max.read_temp(); + conta++; + if(conta>19){ + valor_medido=cache/conta; + cache=0; + conta=0;} + + + +} +void PID() +{ + erro= setpointAtual - valor_medido; + propo=erro*Kp; + integ+=(erro*Ki); + deriv=(erro-erro_ant)*Kd; + erro_ant=erro; + pwm=propo+integ+deriv; + if(pwm>=100)pwm=100; + else if(pwm<=0)pwm=0; + +} + + + + + + + + + + +void segs (void) +{ + + cont1++; + cont2++; + cont3++; + +} + + + + + +void media_temp(void) +{ + int contador; + float cache; + + cache+=max.read_temp(); + contador++; + if(contador>=20) { + temperatura =cache/contador; + contador=0; + cache=0; + } +}
diff -r 80f3ac4b1c8b -r cc12e1f4daf1 max6675.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/max6675.lib Thu Sep 02 14:46:39 2021 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/zelmic/code/max6675/#f0cdf38c7cfc