Compare commits

...

3 Commits

Author SHA1 Message Date
agsler
f878693e6e gtf testing:wq 2022-12-16 09:40:41 +01:00
55641ce8cb add gefran and stuff 2022-11-09 17:17:40 +01:00
d732306e02 lift motor 2022-10-26 18:36:07 +02:00
10 changed files with 134 additions and 44 deletions

View File

@@ -81,6 +81,9 @@ void ioHelperEdgeDetector(void);
#define LED_PLC_OK BitPL5
#define LED_BUS_OK BitPL3
#define LED_EXTR_ONOFF BitPL3
#define LIFT_MOTOR_DIR BitPL6
//Inputs
//Verknüpfen von Pin | Bit mit Bitposition (0...n) inStates[0...n/8].
@@ -105,17 +108,24 @@ void ioHelperEdgeDetector(void);
#define BitPinD7 16 //I16
#define BitPinG2 17 //I17
#define BitPinG1 18 //I18
#define BitPinD3 10 //INO
#define BitPinD2 11 //IN1
#define BitPinD3 19 //INO
#define BitPinD2 20 //IN1
#define BTN_HEIZEN_AN BitPinK2
#define BTN_HEIZEN_AUS BitPinK3
#define BTN_LIFT_UP BitPinD7
#define BTN_LIFT_DOWN BitPinG2
#define BTN_EXTR_ON 8
#define BTN_EXTR_OFF 9
#define IN_ANLAGE_EIN_INV BitPinF7
#define IN_NOTAUS_ANLAGE_R BitPinK5
#define IN_NOTAUS_ANLAGE BitPinK4
#define IN_NOTAUS_SCHRANK BitPinK6
#define MOD_OUT_HEIZEN_LED 1
#define MOD_OUT_EXTR_LED 6
#endif

View File

@@ -1,3 +1,4 @@
--target=avr
-D
__AVR_ATmega2560__
-D

40
gefran_gtf.c Normal file
View File

@@ -0,0 +1,40 @@
#include "modbus-master.h"
#include "gefran_gtf.h"
#include <stdint.h>
uint16_t gtf_firmware_version(uint8_t address){
return gtf_read_register(address, GTF_FIRMWARE_VERSION);
}
uint16_t gtf_read_register(uint8_t address, uint8_t reg){
uint16_t val = 0;
readReg(address, reg, 1);
int8_t res = wait_receive(1, &val, 30);
if(!res)
return val;
else
return res;
}
int8_t gtf_write_register(uint8_t address, uint8_t reg, uint16_t value){
writeReg(address, reg, value);
return wait_write(100);
}
int8_t gtf_read_coil(uint8_t address, uint8_t coil){
uint16_t val=0;
readCoil(address, coil);
uint8_t res = wait_receive_coil(20);
if(res)
return -1;
return val;
}
int8_t gtf_write_coil(uint8_t address, uint8_t coil, uint8_t val){
writeCoil(address, coil, val);
uint8_t res = wait_write(100);
if(res)
return -1;
return val;
}

15
gefran_gtf.h Normal file
View File

@@ -0,0 +1,15 @@
#include <stdio.h>
#define GTF_PV 0
#define GTF_OuP 3
#define GTF_FIRMWARE_VERSION 122
#define GTF_OPERATING_HOURS 161
#define GTF_BAUDRATE 24
#define GTF_SETPOINT 27
#define GTF_POWER 106
uint16_t gtf_firmware_version(uint8_t address);
uint16_t gtf_read_register(uint8_t address, uint8_t reg);
int8_t gtf_write_register(uint8_t address, uint8_t reg, uint16_t value);
int8_t gtf_read_coil(uint8_t address, uint8_t coil);
int8_t gtf_write_coil(uint8_t address, uint8_t coil, uint8_t val);

View File

@@ -18,7 +18,7 @@ void do_kraftsensor(){
/* read 2 16bit values and merge to 32bit signed integer */
readReg(1,0,2);
if(wait_receive(2, m_data, 10)){
if(wait_receive(2, m_data, 100)){
kraftsensor_valid = 0;
printf("modbus error\n\r");
}

84
main.c
View File

@@ -2,6 +2,7 @@
#include <avr/interrupt.h>
#include <avr/wdt.h> // WatchDog
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "Ethernet/socket.h"
@@ -19,10 +20,11 @@
#include "modbus.h"
#include "modbus-master.h"
#include "modbus_io_slave.h"
#include "gefran_gtf.h"
#include "util/delay.h"
#define PLC_MQTT_ENABLED 1
#define PLC_MQTT_ENABLED 0
Client mqtt_client;
@@ -140,7 +142,6 @@ int main(){
// INIT MCU
avr_init();
spi_init(); //SPI Master, MODE0, 4Mhz(DIV4), CS_PB.3=HIGH - suitable for WIZNET 5x00(1/2/5)
//spi_speed_tst(); / Here on SPI pins: MOSI 400Khz freq out, on SCLK 3.2MhzOUT (Witk SPI CLK 4Mhz)
ioHelperInitBuffer();
ioHelperIoConf();
@@ -194,13 +195,17 @@ int main(){
ioHelperSetBit(outStates, BitPG5, 1);
uint32_t timer_blink_outs = millis();
uint32_t timer_send_uptime = millis();
uint32_t timer_send_info = millis();
uint32_t timer_read_slave = millis();
#ifdef PLC_MQTT_ENABLED
uint32_t timer_send_info = millis();
uint32_t timer_send_uptime = millis();
#endif
//printf("anlage: %x\n\r", read_Input(IN_ANLAGE_EIN_INV, LEVEL));
gtf_write_register(5, 54, 1);
gtf_write_register(5, 55, _BV(4));
gtf_write_register(5, 56, 100);
uint16_t tgl = 0;
while(1)
{
wdt_reset(); // WDT reset at least every sec
@@ -221,16 +226,21 @@ int main(){
#if PLC_MQTT_ENABLED
char msg[64];
sprintf(msg, "%d", kraftsensor_value);
sprintf(msg, "%ld", kraftsensor_value);
mqtt_pub(&mqtt_client, "/Filamentanlage/01_Extruder/kraft", msg, strlen(msg));
#endif
modbus_io_set_Output(4, 0, TOGGLE);
printf("gefran 54: 0x%02X\n", gtf_read_register(5, 54));
printf("gefran 55: 0x%02X\n", gtf_read_register(5, 55));
printf("gefran 56: 0x%02X\n", gtf_read_register(5, 56));
printf("gefran pv: 0x%02X\n", gtf_read_register(5, GTF_PV));
printf("gefran oup: 0x%02X\n", gtf_read_register(5, GTF_OuP));
printf("\n");
}
if(millis() - timer_read_slave > 100){
if(millis() - timer_read_slave > 200){
timer_read_slave = millis();
//readInputState(4, 200, 32);
modbus_io_read();
if(modbus_io_read_Input(16, RISING))
@@ -238,8 +248,6 @@ int main(){
if(modbus_io_read_Input(17, RISING))
modbus_io_set_Output(4, 3, OFF);
//printf("read inputs: 0x%02X 0x%02X 0x%02X 0x%02X\n", inp[3], inp[2], inp[1], inp[0]);
}
if(read_Input(BTN_HEIZEN_AN, RISING)){
@@ -249,12 +257,32 @@ int main(){
modbus_io_set_Output(4, MOD_OUT_HEIZEN_LED, 0);
}
if(read_Input(BTN_LIFT_UP, LEVEL)){
set_Output(LIFT_MOTOR_DIR, 1);
TCCR3B |= _BV(CS30);
}
else if(read_Input(BTN_LIFT_DOWN, LEVEL)){
set_Output(LIFT_MOTOR_DIR, 0);
TCCR3B |= _BV(CS30);
}
else
TCCR3B &= ~_BV(CS30);
if(read_Input(BTN_EXTR_ON, RISING)){
TCCR1B |= _BV(CS11);
modbus_io_set_Output(4, MOD_OUT_EXTR_LED, 1);
}
if(read_Input(BTN_EXTR_OFF, RISING)){
TCCR1B &= ~_BV(CS11);
modbus_io_set_Output(4, MOD_OUT_EXTR_LED, 0);
}
#if PLC_MQTT_ENABLED
// send misc info
if(millis() - timer_send_uptime > 5000){
timer_send_uptime += 5000;
timer_send_uptime = millis();
char msg[64];
sprintf(msg, "%ld", millis()/1000);
//sprintf(msg, "%ld", millis()/1000);
mqtt_pub(&mqtt_client, "/Filamentanlage/01_Extruder/uptime", msg, strlen(msg));
}
#endif
@@ -268,14 +296,6 @@ int main(){
}
#endif
//if(read_Input(IN_ANLAGE_EIN_INV, FALLING)){
// printf("anlage ein\n\r");
//}
// ioHelperSetBit(outStates, LED_GRN_NOTAUS_SCHRANK, 1);
//}
//else{
// ioHelperSetBit(outStates, LED_GRN_NOTAUS_SCHRANK, 0);
//}
do_notaus();
#if PLC_MQTT_ENABLED
@@ -286,7 +306,6 @@ int main(){
#endif
ioHelperSetOuts();
}
return 0;
}
// Timer0
@@ -302,16 +321,13 @@ void timer0_init(void)
void timer1_init()
{
TCCR3A |= (1<<COM3B1);
TCCR3B |= _BV(WGM33);
TCCR1A |= (1<<COM1A1);
TCCR1B |= _BV(WGM13);
ICR3 = 100;
OCR3A = 50;
ICR1 = 100;
OCR1A = 50;
DDRE |= 1 << 4;
TIMSK3 |= 1<<TOIE3;
TCCR3B |= _BV(CS31);
DDRB |= 1 << 5;
}
void timer3_init()
@@ -319,11 +335,10 @@ void timer3_init()
TCCR3A |= (1<<COM3B1);
TCCR3B |= _BV(WGM33);
ICR3 = 100;
OCR3B = 50;
ICR3 = 60;
OCR3B = 30;
DDRE |= 1 << 4;
TCCR3B |= _BV(CS31);
}
void initADC(void)
@@ -342,7 +357,8 @@ static void avr_init(void)
wdt_reset(); // wdt reset ~ every <2000ms
timer0_init();// Timer0 millis engine init
//timer3_init();
timer1_init();
timer3_init();
DDRL |= 1<<6;
uart_init();

View File

@@ -52,7 +52,7 @@ THE POSSIBILITY OF SUCH DAMAGE.
/* define baudrate of modbus */
#ifndef BAUD
#define BAUD 38400L
#define BAUD 19200L
#endif
/*

2
mqtt.c
View File

@@ -35,7 +35,7 @@ void messageArrived(MessageData* md)
void mqtt_pub(Client* mqtt_client, char * mqtt_topic, char * mqtt_msg, int mqtt_msg_len)
{
static uint32_t mqtt_pub_count = 0;
//static uint32_t mqtt_pub_count = 0;
static uint8_t mqtt_err_cnt = 0;
int32_t mqtt_rc;

10
uart.c
View File

@@ -8,8 +8,14 @@ FILE uart_output = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
void uart_init()
{
DDRD |= 1 << 1; // TX
UART_BAUD_REGH = (BAUDRATE>>8);
UART_BAUD_REGL = BAUDRATE; // set baud rate
UART_BAUD_REGH = UBRRH_VALUE;//(BAUDRATE>>8);
UART_BAUD_REGL = UBRRL_VALUE;//BAUDRATE; // set baud rate
#if USE_2X
UART_CTRL_REGA |= (1 << USE_2X);
#else
UART_CTRL_REGA &= ~(1 << USE_2X);
#endif
UART_CTRL_REGB |= (1<<UART_TXEN_BM)
//|(1<<UART_RXEN_BM)

6
uart.h
View File

@@ -1,9 +1,11 @@
#ifndef _UART_H_
#define _UART_H_
#include <stdio.h>
#define BAUD 115200
#include <stdio.h>
#include <util/setbaud.h>
#define BAUD 9600
#define BAUDRATE ((F_CPU)/(BAUD*16UL)-1)
#define UART_BAUD_REGH UBRR0H