Compare commits

..

9 Commits

Author SHA1 Message Date
409b2b8afb call do_* functions 2022-02-11 18:22:59 +01:00
f89ea0bde9 change poll rate on modbus 2022-02-11 18:22:32 +01:00
c4fe5df4af disable uptime msg 2022-02-11 18:21:43 +01:00
858fcd1547 send speed val on init 2022-02-11 18:19:51 +01:00
456731d2f0 make taenzer status available in header 2022-02-11 18:17:48 +01:00
30f1ec733d uncomment homing and send position 2022-02-11 18:17:02 +01:00
28942ab232 change include names etc 2022-02-11 18:12:18 +01:00
e20bdb3e8a move timer inits to files where it is used 2022-02-11 18:11:08 +01:00
33b01ee2d8 remove old filename 2022-02-11 18:06:44 +01:00
7 changed files with 76 additions and 144 deletions

106
main.c
View File

@@ -2,6 +2,9 @@
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include <avr/wdt.h> // WatchDog #include <avr/wdt.h> // WatchDog
#include <string.h> #include <string.h>
#include <stdlib.h>
#include "common.h"
#include "Ethernet/socket.h" #include "Ethernet/socket.h"
#include "Ethernet/wizchip_conf.h" #include "Ethernet/wizchip_conf.h"
@@ -17,10 +20,9 @@
#include "util/delay.h" #include "util/delay.h"
#include "modbus.h" #include "kraftsensor.h"
#include "modbus_master.h" #include "taenzer.h"
#include "abzug.h"
#define PLC_MQTT_ENABLED 1
Client mqtt_client; Client mqtt_client;
@@ -39,12 +41,9 @@ void get_mcusr(void)
} }
//***********Prologue for fast WDT disable & and save reason of reset/power-up: END //***********Prologue for fast WDT disable & and save reason of reset/power-up: END
//FUNC headers //FUNC headers
static void avr_init(void); static void avr_init(void);
void modbus_master_init(void);
void timer0_init(void); void timer0_init(void);
void timer2_init(void);
void timer3_init(void); void timer3_init(void);
void print_network_information(void); void print_network_information(void);
@@ -60,12 +59,6 @@ void IO_LIBRARY_Init(void) {
//wizchip_setinterruptmask(IK_SOCK_0); //wizchip_setinterruptmask(IK_SOCK_0);
} }
void modbus_master_init(){
modbusSetAddress(1); //better set this to sth.
modbusInit();
timer2_init(); // modbus tick timer
}
static void avr_init() static void avr_init()
{ {
// Initialize device here. // Initialize device here.
@@ -82,22 +75,16 @@ static void avr_init()
return; return;
} }
void do_kraftsensor(){ void send_abzug_speed(void){
uint16_t m_data[4]; char msg[4];
char msg[64]; sprintf(msg, "%d", abzug_speed);
mqtt_pub(&mqtt_client, "/Filamentanlage/05_Abzug/state/abzug/speed", msg, strlen(msg));
readReg(1,0,2); if(TCCR3B & (1<<CS31))
if(wait_receive(2, m_data, 10)) sprintf(msg, "True");
sprintf(msg, "n/a"); else
else{ sprintf(msg, "False");
int32_t tmp = (uint32_t)m_data[0]<<16; mqtt_pub(&mqtt_client, "/Filamentanlage/05_Abzug/state/abzug/onoff", msg, strlen(msg));
tmp |= m_data[1];
sprintf(msg, "%ld", tmp);
}
#if PLC_MQTT_ENABLED
mqtt_pub(&mqtt_client, "/Filamentanlage/05_Abzug/state/kraft", msg, strlen(msg));
#endif
} }
void do_notaus(){ void do_notaus(){
@@ -165,9 +152,11 @@ int main()
// INIT MCU // INIT MCU
avr_init(); avr_init();
spi_init(); //SPI Master, MODE0, 4Mhz(DIV4), CS_PB.3=HIGH - suitable for WIZNET 5x00(1/2/5) spi_init(); //SPI Master, MODE0, 4Mhz(DIV4), CS_PB.3=HIGH - suitable for WIZNET 5x00(1/2/5)
modbus_master_init(); kraftsensor_init();
printf("moin!\n\r"); printf("moin!\n\r");
timer3_init();
timer4_init();
ioHelperInitBuffer(); ioHelperInitBuffer();
ioHelperIoConf(); ioHelperIoConf();
@@ -207,7 +196,7 @@ int main()
else else
{ {
printf("--MQTT Connected ERROR: %ld\r\n", mqtt_rc); printf("--MQTT Connected ERROR: %ld\r\n", mqtt_rc);
//while(1); //Reboot the board while(1); //Reboot the board
} }
char SubString[] = "/Filamentanlage/04_Messmodul/set/#"; char SubString[] = "/Filamentanlage/04_Messmodul/set/#";
@@ -227,12 +216,14 @@ int main()
{ {
wdt_reset(); // WDT reset at least every sec wdt_reset(); // WDT reset at least every sec
//if (flag_refresh_inStates) { #if PLC_MQTT_ENABLED
if(millis() < 10)
send_abzug_speed();
#endif
ioHelperReadPins(); ioHelperReadPins();
ioHelperDebounce(); ioHelperDebounce();
ioHelperEdgeDetector(); ioHelperEdgeDetector();
// flag_refresh_inStates = 0;
//}
// Toggle all outs which are set to blinking // Toggle all outs which are set to blinking
if(millis() - timer_blink_outs > 500){ if(millis() - timer_blink_outs > 500){
@@ -245,19 +236,29 @@ int main()
#if PLC_MQTT_ENABLED #if PLC_MQTT_ENABLED
// send misc info // send misc info
if(millis() - timer_send_uptime > 5000){ //if(millis() - timer_send_uptime > 5000){
timer_send_uptime += 5000; // timer_send_uptime += 5000;
char msg[64]; // char msg[64];
sprintf(msg, "%ld", millis()/1000); // sprintf(msg, "%ld", millis()/1000);
mqtt_pub(&mqtt_client, "/Filamentanlage/04_Messmodul/state/uptime", msg, strlen(msg)); // mqtt_pub(&mqtt_client, "/Filamentanlage/04_Messmodul/state/uptime", msg, strlen(msg));
} //}
#endif #endif
if(millis() - timer_modbus_poll > 1000){ if(millis() - timer_modbus_poll > 200){
timer_modbus_poll += 1000; timer_modbus_poll += 200;
do_kraftsensor(); do_kraftsensor();
#if PLC_MQTT_ENABLED
send_abzug_speed();
char msg[10];
ltoa(kraftsensor_value, msg, 10);
mqtt_pub(&mqtt_client, "/Filamentanlage/05_Abzug/state/kraft", msg, strlen(msg));
ltoa(taenzer_state.pos, msg, 10);
mqtt_pub(&mqtt_client, "/Filamentanlage/05_Abzug/state/speicher/pos", msg, strlen(msg));
#endif
} }
do_notaus(); do_notaus();
do_rolle();
do_taenzer();
#if PLC_MQTT_ENABLED #if PLC_MQTT_ENABLED
ioHelperSetBit(outStates, LED_BUS_OK, 1); ioHelperSetBit(outStates, LED_BUS_OK, 1);
@@ -270,25 +271,6 @@ int main()
return 0; return 0;
} }
// Timer0
// 1ms IRQ
// Used for millis() timing
void timer0_init()
{
TCCR0A = (1<<WGM01); //TIMER0 SET-UP: CTC MODE
TCCR0B = (1<<CS01)|(1<<CS00); // PS 1:64
OCR0A = 249; // 1ms reach for clear (16mz:64=>250kHz:250-=>1kHz)
TIMSK0 |= 1<<OCIE0A; //IRQ on TIMER0 output compareA
}
void timer2_init()
{
TCCR2A = (1<<WGM21); //TIMER0 SET-UP: CTC MODE
TCCR2B|=(1<<CS21); //prescaler 8
OCR2A = 200; // 1ms reach for clear (16mz/8/200=>10kHz)
TIMSK2|=(1<<OCIE2A);
}
void print_network_information() void print_network_information()
{ {
@@ -311,6 +293,4 @@ void print_network_information()
printf("DNS Server : %d.%d.%d.%d\n\r",gWIZNETINFO.dns[0],gWIZNETINFO.dns[1],gWIZNETINFO.dns[2],gWIZNETINFO.dns[3]); printf("DNS Server : %d.%d.%d.%d\n\r",gWIZNETINFO.dns[0],gWIZNETINFO.dns[1],gWIZNETINFO.dns[2],gWIZNETINFO.dns[3]);
} }
ISR(TIMER2_COMPA_vect) { //this ISR is called 9765.625 times per second
modbusTickTimer();
}

View File

@@ -4,6 +4,17 @@
volatile unsigned long _millis; // for millis tick !! Overflow every ~49.7 days volatile unsigned long _millis; // for millis tick !! Overflow every ~49.7 days
// Timer0
// 1ms IRQ
// Used for millis() timing
void timer0_init()
{
TCCR0A = (1<<WGM01); //TIMER0 SET-UP: CTC MODE
TCCR0B = (1<<CS01)|(1<<CS00); // PS 1:64
OCR0A = 249; // 1ms reach for clear (16mz:64=>250kHz:250-=>1kHz)
TIMSK0 |= 1<<OCIE0A; //IRQ on TIMER0 output compareA
}
ISR (TIMER0_COMPA_vect) ISR (TIMER0_COMPA_vect)
{ {
_millis++; // INC millis tick _millis++; // INC millis tick

View File

@@ -3,5 +3,6 @@
#define TICK_PER_SEC 1000UL #define TICK_PER_SEC 1000UL
unsigned long millis(void); unsigned long millis(void);
void timer0_init(void);
#endif #endif

View File

@@ -1,55 +0,0 @@
#include "modbus_master.h"
#include "modbus.h"
#include <util/delay.h>
#include <stdio.h>
uint8_t wait_receive(uint8_t len, uint16_t dest[], uint8_t timeout){
uint8_t breaker = timeout;
while(!receiveOkay && breaker) { //wait for client response, time out after 1s
breaker--;
_delay_ms(10);
}
if(receiveOkay) { //if this fails, there was either no response or a crc error
if(rxbuffer[1]&0x80) { //client responded with an error code
//handle the error
printf("error\n\r");
return -1;
}
else {
for(uint8_t x=0;x<len;x++) { //rxbuffer[2] should be 8 (4 registers => 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);
}

View File

@@ -1,12 +0,0 @@
#ifndef _MODBUS_MASTER_H_
#define _MODBUS_MASTER_H_
#include <stdint.h>
#define receiveOkay (modbusGetBusState() & (1<<ReceiveCompleted))
uint8_t wait_receive(uint8_t len, uint16_t dest[], uint8_t timeout);
void readReg(uint8_t slaveid, uint16_t address, uint8_t amount);
void writeReg(uint8_t slaveid, uint16_t address, uint16_t value);
#endif

View File

@@ -8,19 +8,20 @@ taenzer_state_t taenzer_state;
void do_taenzer(){ void do_taenzer(){
///* Homing */ /* Homing */
//if(!taenzer_state.homed){ if(!taenzer_state.homed){
// set_Output(MOTOR_TAENZER_DIR, 1); // direction: up set_Output(MOTOR_TAENZER_DIR, 1); // direction: up
// TCCR4B |= _BV(CS41); //TURN ON TCCR4B |= _BV(CS41); //TURN ON
//} }
//if(read_Input(IN_TAENZER_HOME, LEVEL)){ if(read_Input(IN_TAENZER_HOME, LEVEL)){
// TCCR4B &= ~_BV(CS41); //TURN OFF TCCR4B &= ~_BV(CS41); //TURN OFF
// taenzer_state.homed = 1; taenzer_state.homed = 1;
//} taenzer_state.pos = 0;
}
/* Force regualtion */ /* Force regualtion */
if(kraftsensor_valid /* && taenzer_state.homed */){ if(kraftsensor_valid && taenzer_state.homed){
if(kraftsensor_value < 0){ if(kraftsensor_value < 9000){
set_Output(MOTOR_TAENZER_DIR, 0); // direction: down set_Output(MOTOR_TAENZER_DIR, 0); // direction: down
TCCR4B |= _BV(CS41); //TURN ON TCCR4B |= _BV(CS41); //TURN ON
} }
@@ -38,7 +39,7 @@ void timer4_init()
TCCR4A |= (1<<COM4A1); TCCR4A |= (1<<COM4A1);
TCCR4B |= _BV(WGM43); TCCR4B |= _BV(WGM43);
// TIMSK4 |= 1<<TOIE4; TIMSK4 |= 1<<TOIE4;
// TIMSK4 |= 1<<OCIE4A; // TIMSK4 |= 1<<OCIE4A;
ICR4 = 100; ICR4 = 100;
@@ -47,8 +48,12 @@ void timer4_init()
DDRH |= 1 << 3; DDRH |= 1 << 3;
} }
//ISR(TIMER4_OVF_vect) { ISR(TIMER4_OVF_vect) {
//} if(ioHelperReadBit(outStates, MOTOR_TAENZER_DIR))
taenzer_state.pos -= 1;
else
taenzer_state.pos += 1;
}
//ISR(TIMER4_OVF_vect) { //ISR(TIMER4_OVF_vect) {
//} //}

View File

@@ -6,6 +6,8 @@ typedef struct {
uint32_t pos; uint32_t pos;
} taenzer_state_t; } taenzer_state_t;
extern taenzer_state_t taenzer_state;
void do_taenzer(void); void do_taenzer(void);
void timer4_init(void); void timer4_init(void);