From 34ee74c185ee4e0ac0699c256907e7d427dcfde4 Mon Sep 17 00:00:00 2001 From: Eggert Jung Date: Thu, 10 Feb 2022 16:41:27 +0100 Subject: [PATCH] split code into more files --- abzug.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++ abzug.h | 8 ++++++ common.h | 1 + kraftsensor.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ kraftsensor.h | 18 ++++++++++++ taenzer.c | 54 +++++++++++++++++++++++++++++++++++ taenzer.h | 12 ++++++++ 7 files changed, 268 insertions(+) create mode 100644 abzug.c create mode 100644 abzug.h create mode 100644 common.h create mode 100644 kraftsensor.c create mode 100644 kraftsensor.h create mode 100644 taenzer.c create mode 100644 taenzer.h diff --git a/abzug.c b/abzug.c new file mode 100644 index 0000000..e56aaf0 --- /dev/null +++ b/abzug.c @@ -0,0 +1,83 @@ +#include +#include "avrIOhelper/io-helper.h" + +uint16_t abzug_speed = 100; + +void timer3_init() +{ + TCCR3A |= (1<= 110) + abzug_speed -= 100; + else + abzug_speed = 10; +#if PLC_MQTT_ENABLED + send_abzug_speed(); +#endif + } + + if (read_Input(BTN_ABZUG_PLUS_FEIN, RISING)) { + if(abzug_speed <= 990) + abzug_speed += 10; + else + abzug_speed = 1000; +#if PLC_MQTT_ENABLED + send_abzug_speed(); +#endif + } + + if (read_Input(BTN_ABZUG_MINUS_FEIN, RISING)) { + if(abzug_speed >= 20) + abzug_speed -= 10; + else + abzug_speed = 10; +#if PLC_MQTT_ENABLED + send_abzug_speed(); +#endif + } + + // 16000000/8 + + ICR3 = ((1.0/abzug_speed)*3.14*42.0*(1.0/5.0))*2.0*500.0*(1/1.03); + OCR3B = ICR3/2; +} + + diff --git a/abzug.h b/abzug.h new file mode 100644 index 0000000..f7cc88e --- /dev/null +++ b/abzug.h @@ -0,0 +1,8 @@ +#ifndef _ABZUG_H +#define _ABZUG_H + +extern uint16_t abzug_speed; + +void do_rolle(void); + +#endif diff --git a/common.h b/common.h new file mode 100644 index 0000000..437ebae --- /dev/null +++ b/common.h @@ -0,0 +1 @@ +#define PLC_MQTT_ENABLED 1 diff --git a/kraftsensor.c b/kraftsensor.c new file mode 100644 index 0000000..84b282a --- /dev/null +++ b/kraftsensor.c @@ -0,0 +1,92 @@ +#include +#include +#include +#include + +#include "kraftsensor.h" +#include "modbus.h" +#include "common.h" + +int32_t kraftsensor_value; +uint8_t kraftsensor_valid; + +void timer2_init() +{ + TCCR2A = (1< 8 bytes). You might want to check this at this point. + dest[x]=(rxbuffer[3+x*2]<<8)+rxbuffer[4+x*2]; //do sth with the acquired data. + } + } + + } + return 0; +} + +void readReg(uint8_t slaveid, uint16_t address, uint8_t amount) { + _delay_ms(2); + rxbuffer[0]=slaveid; + modbusSetAddress(slaveid); + rxbuffer[1]=0x03; + rxbuffer[2]=(address>>8)&0xFF; + rxbuffer[3]=address&0xFF; + rxbuffer[4]=0x00; + rxbuffer[5]=amount; + modbusSendMessage(5); +} + +void writeReg(uint8_t slaveid, uint16_t address, uint16_t value) { + _delay_ms(2); + rxbuffer[0]=slaveid; + modbusSetAddress(slaveid); + rxbuffer[1]=0x06; + rxbuffer[2]=(address>>8)&0xFF; + rxbuffer[3]=address&0xFF; + rxbuffer[4]=(value>>8)&0xFF;; + rxbuffer[5]=value&0xFF; + modbusSendMessage(5); +} + +ISR(TIMER2_COMPA_vect) { //this ISR is called 9765.625 times per second + modbusTickTimer(); +} diff --git a/kraftsensor.h b/kraftsensor.h new file mode 100644 index 0000000..23e75ab --- /dev/null +++ b/kraftsensor.h @@ -0,0 +1,18 @@ +#ifndef _MODBUS_MASTER_H_ +#define _MODBUS_MASTER_H_ + +#include + +#define receiveOkay (modbusGetBusState() & (1< +#include +#include "taenzer.h" +#include "avrIOhelper/io-helper.h" +#include "kraftsensor.h" + +taenzer_state_t taenzer_state; + +void do_taenzer(){ + + ///* Homing */ + //if(!taenzer_state.homed){ + // set_Output(MOTOR_TAENZER_DIR, 1); // direction: up + // TCCR4B |= _BV(CS41); //TURN ON + //} + //if(read_Input(IN_TAENZER_HOME, LEVEL)){ + // TCCR4B &= ~_BV(CS41); //TURN OFF + // taenzer_state.homed = 1; + //} + + /* Force regualtion */ + if(kraftsensor_valid /* && taenzer_state.homed */){ + if(kraftsensor_value < 0){ + set_Output(MOTOR_TAENZER_DIR, 0); // direction: down + TCCR4B |= _BV(CS41); //TURN ON + } + else if(kraftsensor_value > 10000){ + set_Output(MOTOR_TAENZER_DIR, 1); // direction: up + TCCR4B |= _BV(CS41); //TURN ON + } + else + TCCR4B &= ~_BV(CS41); //TURN OFF + } +} + +void timer4_init() +{ + TCCR4A |= (1<