Compare commits
6 Commits
6c18ba3708
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f878693e6e | ||
| 55641ce8cb | |||
| d732306e02 | |||
| 9c52c27a6f | |||
| 0f29264da5 | |||
| 77a07b1aee |
@@ -81,6 +81,9 @@ void ioHelperEdgeDetector(void);
|
|||||||
#define LED_PLC_OK BitPL5
|
#define LED_PLC_OK BitPL5
|
||||||
#define LED_BUS_OK BitPL3
|
#define LED_BUS_OK BitPL3
|
||||||
|
|
||||||
|
#define LED_EXTR_ONOFF BitPL3
|
||||||
|
|
||||||
|
#define LIFT_MOTOR_DIR BitPL6
|
||||||
|
|
||||||
//Inputs
|
//Inputs
|
||||||
//Verknüpfen von Pin | Bit mit Bitposition (0...n) inStates[0...n/8].
|
//Verknüpfen von Pin | Bit mit Bitposition (0...n) inStates[0...n/8].
|
||||||
@@ -105,13 +108,24 @@ void ioHelperEdgeDetector(void);
|
|||||||
#define BitPinD7 16 //I16
|
#define BitPinD7 16 //I16
|
||||||
#define BitPinG2 17 //I17
|
#define BitPinG2 17 //I17
|
||||||
#define BitPinG1 18 //I18
|
#define BitPinG1 18 //I18
|
||||||
#define BitPinD3 10 //INO
|
#define BitPinD3 19 //INO
|
||||||
#define BitPinD2 11 //IN1
|
#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_ANLAGE_EIN_INV BitPinF7
|
||||||
#define IN_NOTAUS_ANLAGE_R BitPinK5
|
#define IN_NOTAUS_ANLAGE_R BitPinK5
|
||||||
#define IN_NOTAUS_ANLAGE BitPinK4
|
#define IN_NOTAUS_ANLAGE BitPinK4
|
||||||
#define IN_NOTAUS_SCHRANK BitPinK6
|
#define IN_NOTAUS_SCHRANK BitPinK6
|
||||||
|
|
||||||
|
#define MOD_OUT_HEIZEN_LED 1
|
||||||
|
#define MOD_OUT_EXTR_LED 6
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
--target=avr
|
||||||
-D
|
-D
|
||||||
__AVR_ATmega2560__
|
__AVR_ATmega2560__
|
||||||
-D
|
-D
|
||||||
|
|||||||
40
gefran_gtf.c
Normal file
40
gefran_gtf.c
Normal 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
15
gefran_gtf.h
Normal 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);
|
||||||
@@ -18,7 +18,7 @@ void do_kraftsensor(){
|
|||||||
|
|
||||||
/* read 2 16bit values and merge to 32bit signed integer */
|
/* read 2 16bit values and merge to 32bit signed integer */
|
||||||
readReg(1,0,2);
|
readReg(1,0,2);
|
||||||
if(wait_receive(2, m_data, 10)){
|
if(wait_receive(2, m_data, 100)){
|
||||||
kraftsensor_valid = 0;
|
kraftsensor_valid = 0;
|
||||||
printf("modbus error\n\r");
|
printf("modbus error\n\r");
|
||||||
}
|
}
|
||||||
|
|||||||
171
main.c
171
main.c
@@ -2,6 +2,7 @@
|
|||||||
#include <avr/interrupt.h>
|
#include <avr/interrupt.h>
|
||||||
#include <avr/wdt.h> // WatchDog
|
#include <avr/wdt.h> // WatchDog
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "Ethernet/socket.h"
|
#include "Ethernet/socket.h"
|
||||||
@@ -18,10 +19,12 @@
|
|||||||
#include "mqtt.h"
|
#include "mqtt.h"
|
||||||
#include "modbus.h"
|
#include "modbus.h"
|
||||||
#include "modbus-master.h"
|
#include "modbus-master.h"
|
||||||
|
#include "modbus_io_slave.h"
|
||||||
|
#include "gefran_gtf.h"
|
||||||
|
|
||||||
#include "util/delay.h"
|
#include "util/delay.h"
|
||||||
|
|
||||||
#define PLC_MQTT_ENABLED 1
|
#define PLC_MQTT_ENABLED 0
|
||||||
|
|
||||||
Client mqtt_client;
|
Client mqtt_client;
|
||||||
|
|
||||||
@@ -135,76 +138,10 @@ void send_values(void){
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void modbus_set_Output(uint8_t slave_adr, uint8_t nr, uint8_t state) {
|
|
||||||
switch (state) {
|
|
||||||
case BLINK:
|
|
||||||
//ioHelperSetBit(outStatesBlinking, nr, ON);
|
|
||||||
writeCoil(slave_adr,100+nr,1);
|
|
||||||
wait_write(10);
|
|
||||||
break;
|
|
||||||
//case TOGGLE:
|
|
||||||
// ioHelperSetBit(outStatesBlinking, nr, OFF);
|
|
||||||
// if (ioHelperReadBit(outStates, nr)) {
|
|
||||||
// ioHelperSetBit(outStates, nr, OFF);
|
|
||||||
// } else {
|
|
||||||
// ioHelperSetBit(outStates, nr, ON);
|
|
||||||
// }
|
|
||||||
// break;
|
|
||||||
case ON:
|
|
||||||
//ioHelperSetBit(outStates, nr, ON);
|
|
||||||
//ioHelperSetBit(outStatesBlinking, nr, OFF);
|
|
||||||
writeCoil(slave_adr,nr,1);
|
|
||||||
wait_write(10);
|
|
||||||
break;
|
|
||||||
case OFF:
|
|
||||||
writeCoil(slave_adr,nr,0);
|
|
||||||
wait_write(10);
|
|
||||||
//ioHelperSetBit(outStates, nr, OFF);
|
|
||||||
//ioHelperSetBit(outStatesBlinking, nr, OFF);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t modbus_read_Input(uint8_t nr, uint8_t type) {
|
|
||||||
uint8_t state = 0;
|
|
||||||
switch (type) {
|
|
||||||
case LEVEL:
|
|
||||||
state = ioHelperReadBit(inStates, nr);
|
|
||||||
break;
|
|
||||||
case EDGE:
|
|
||||||
state = ioHelperReadBit(inStatesBothEdges, nr);
|
|
||||||
ioHelperSetBit(inStatesBothEdges, nr, 0);
|
|
||||||
break;
|
|
||||||
case RISING:
|
|
||||||
state = ioHelperReadBit(inStatesRisingEdge, nr);
|
|
||||||
ioHelperSetBit(inStatesRisingEdge, nr, 0);
|
|
||||||
break;
|
|
||||||
case FALLING:
|
|
||||||
state = ioHelperReadBit(inStatesFallingEdge, nr);
|
|
||||||
ioHelperSetBit(inStatesFallingEdge, nr, 0);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
//reset edges after being read once
|
|
||||||
// if (state) {
|
|
||||||
// if (type == EDGE)
|
|
||||||
// ioHelperSetBit(inStatesBothEdges, nr, OFF);
|
|
||||||
// if (type == RISING)
|
|
||||||
// ioHelperSetBit(inStatesRisingEdge, nr, OFF);
|
|
||||||
// if (type == FALLING)
|
|
||||||
// ioHelperSetBit(inStatesFallingEdge, nr, OFF);
|
|
||||||
// }
|
|
||||||
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int main(){
|
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)
|
||||||
//spi_speed_tst(); / Here on SPI pins: MOSI 400Khz freq out, on SCLK 3.2MhzOUT (Witk SPI CLK 4Mhz)
|
|
||||||
|
|
||||||
ioHelperInitBuffer();
|
ioHelperInitBuffer();
|
||||||
ioHelperIoConf();
|
ioHelperIoConf();
|
||||||
@@ -258,12 +195,17 @@ int main(){
|
|||||||
ioHelperSetBit(outStates, BitPG5, 1);
|
ioHelperSetBit(outStates, BitPG5, 1);
|
||||||
|
|
||||||
uint32_t timer_blink_outs = millis();
|
uint32_t timer_blink_outs = millis();
|
||||||
uint32_t timer_send_uptime = millis();
|
uint32_t timer_read_slave = millis();
|
||||||
|
#ifdef PLC_MQTT_ENABLED
|
||||||
uint32_t timer_send_info = millis();
|
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));
|
//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)
|
while(1)
|
||||||
{
|
{
|
||||||
wdt_reset(); // WDT reset at least every sec
|
wdt_reset(); // WDT reset at least every sec
|
||||||
@@ -282,28 +224,65 @@ int main(){
|
|||||||
|
|
||||||
do_kraftsensor();
|
do_kraftsensor();
|
||||||
|
|
||||||
|
#if PLC_MQTT_ENABLED
|
||||||
char msg[64];
|
char msg[64];
|
||||||
sprintf(msg, "%d", kraftsensor_value);
|
sprintf(msg, "%ld", kraftsensor_value);
|
||||||
mqtt_pub(&mqtt_client, "/Filamentanlage/01_Extruder/kraft", msg, strlen(msg));
|
mqtt_pub(&mqtt_client, "/Filamentanlage/01_Extruder/kraft", msg, strlen(msg));
|
||||||
|
#endif
|
||||||
|
|
||||||
//tgl ^= 0x01;
|
printf("gefran 54: 0x%02X\n", gtf_read_register(5, 54));
|
||||||
//modbus_set_Output(4, 0, OFF);
|
printf("gefran 55: 0x%02X\n", gtf_read_register(5, 55));
|
||||||
//modbus_set_Output(4, 1, ON);
|
printf("gefran 56: 0x%02X\n", gtf_read_register(5, 56));
|
||||||
//modbus_set_Output(4, 2, BLINK);
|
|
||||||
//modbus_set_Output(4, 3, tgl);
|
|
||||||
|
|
||||||
//readInputReg(4, 0, 1);
|
printf("gefran pv: 0x%02X\n", gtf_read_register(5, GTF_PV));
|
||||||
//uint16_t inp;
|
printf("gefran oup: 0x%02X\n", gtf_read_register(5, GTF_OuP));
|
||||||
//wait_receive(1, &inp, 100);
|
printf("\n");
|
||||||
//printf("read inputs: 0x%X\n", inp);
|
}
|
||||||
|
|
||||||
|
if(millis() - timer_read_slave > 200){
|
||||||
|
timer_read_slave = millis();
|
||||||
|
modbus_io_read();
|
||||||
|
|
||||||
|
if(modbus_io_read_Input(16, RISING))
|
||||||
|
modbus_io_set_Output(4, 3, ON);
|
||||||
|
if(modbus_io_read_Input(17, RISING))
|
||||||
|
modbus_io_set_Output(4, 3, OFF);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if(read_Input(BTN_HEIZEN_AN, RISING)){
|
||||||
|
modbus_io_set_Output(4, MOD_OUT_HEIZEN_LED, 1);
|
||||||
|
}
|
||||||
|
if(read_Input(BTN_HEIZEN_AUS, RISING)){
|
||||||
|
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
|
#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 = millis();
|
||||||
char msg[64];
|
char msg[64];
|
||||||
sprintf(msg, "%ld", millis()/1000);
|
//sprintf(msg, "%ld", millis()/1000);
|
||||||
mqtt_pub(&mqtt_client, "/Filamentanlage/01_Extruder/uptime", msg, strlen(msg));
|
mqtt_pub(&mqtt_client, "/Filamentanlage/01_Extruder/uptime", msg, strlen(msg));
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -317,14 +296,6 @@ int main(){
|
|||||||
}
|
}
|
||||||
#endif
|
#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();
|
do_notaus();
|
||||||
|
|
||||||
#if PLC_MQTT_ENABLED
|
#if PLC_MQTT_ENABLED
|
||||||
@@ -335,7 +306,6 @@ int main(){
|
|||||||
#endif
|
#endif
|
||||||
ioHelperSetOuts();
|
ioHelperSetOuts();
|
||||||
}
|
}
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Timer0
|
// Timer0
|
||||||
@@ -351,16 +321,13 @@ void timer0_init(void)
|
|||||||
|
|
||||||
void timer1_init()
|
void timer1_init()
|
||||||
{
|
{
|
||||||
TCCR3A |= (1<<COM3B1);
|
TCCR1A |= (1<<COM1A1);
|
||||||
TCCR3B |= _BV(WGM33);
|
TCCR1B |= _BV(WGM13);
|
||||||
|
|
||||||
ICR3 = 100;
|
ICR1 = 100;
|
||||||
OCR3A = 50;
|
OCR1A = 50;
|
||||||
|
|
||||||
DDRE |= 1 << 4;
|
DDRB |= 1 << 5;
|
||||||
TIMSK3 |= 1<<TOIE3;
|
|
||||||
|
|
||||||
TCCR3B |= _BV(CS31);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void timer3_init()
|
void timer3_init()
|
||||||
@@ -368,11 +335,10 @@ void timer3_init()
|
|||||||
TCCR3A |= (1<<COM3B1);
|
TCCR3A |= (1<<COM3B1);
|
||||||
TCCR3B |= _BV(WGM33);
|
TCCR3B |= _BV(WGM33);
|
||||||
|
|
||||||
ICR3 = 100;
|
ICR3 = 60;
|
||||||
OCR3B = 50;
|
OCR3B = 30;
|
||||||
|
|
||||||
DDRE |= 1 << 4;
|
DDRE |= 1 << 4;
|
||||||
TCCR3B |= _BV(CS31);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void initADC(void)
|
void initADC(void)
|
||||||
@@ -391,7 +357,8 @@ static void avr_init(void)
|
|||||||
wdt_reset(); // wdt reset ~ every <2000ms
|
wdt_reset(); // wdt reset ~ every <2000ms
|
||||||
|
|
||||||
timer0_init();// Timer0 millis engine init
|
timer0_init();// Timer0 millis engine init
|
||||||
//timer3_init();
|
timer1_init();
|
||||||
|
timer3_init();
|
||||||
DDRL |= 1<<6;
|
DDRL |= 1<<6;
|
||||||
uart_init();
|
uart_init();
|
||||||
|
|
||||||
|
|||||||
@@ -97,6 +97,18 @@ void readInputReg(uint8_t slaveid, uint16_t address, uint16_t amount) {
|
|||||||
modbusSendMessage(5);
|
modbusSendMessage(5);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void readInputState(uint8_t slaveid, uint16_t address, uint16_t amount) {
|
||||||
|
_delay_ms(2);
|
||||||
|
rxbuffer[0]=slaveid;
|
||||||
|
modbusSetAddress(slaveid);
|
||||||
|
rxbuffer[1]=0x02;
|
||||||
|
rxbuffer[2]=(address>>8)&0xFF;
|
||||||
|
rxbuffer[3]=address&0xFF;
|
||||||
|
rxbuffer[4]=0;
|
||||||
|
rxbuffer[5]=amount;
|
||||||
|
modbusSendMessage(5);
|
||||||
|
}
|
||||||
|
|
||||||
void writeCoil(uint8_t slaveid, uint16_t address, uint16_t value) {
|
void writeCoil(uint8_t slaveid, uint16_t address, uint16_t value) {
|
||||||
_delay_ms(2);
|
_delay_ms(2);
|
||||||
rxbuffer[0]=slaveid;
|
rxbuffer[0]=slaveid;
|
||||||
|
|||||||
@@ -7,5 +7,5 @@ int8_t wait_write(uint8_t timeout);
|
|||||||
void readReg(uint8_t slaveid, uint16_t address, uint8_t amount);
|
void readReg(uint8_t slaveid, uint16_t address, uint8_t amount);
|
||||||
void writeReg(uint8_t slaveid, uint16_t address, uint16_t value);
|
void writeReg(uint8_t slaveid, uint16_t address, uint16_t value);
|
||||||
void readInputReg(uint8_t slaveid, uint16_t address, uint16_t amount);
|
void readInputReg(uint8_t slaveid, uint16_t address, uint16_t amount);
|
||||||
void readInputStatus(uint8_t slaveid, uint16_t address, uint16_t amount);
|
void readInputState(uint8_t slaveid, uint16_t address, uint16_t amount);
|
||||||
void writeCoil(uint8_t slaveid, uint16_t address, uint16_t value);
|
void writeCoil(uint8_t slaveid, uint16_t address, uint16_t value);
|
||||||
|
|||||||
2
modbus.h
2
modbus.h
@@ -52,7 +52,7 @@ THE POSSIBILITY OF SUCH DAMAGE.
|
|||||||
|
|
||||||
/* define baudrate of modbus */
|
/* define baudrate of modbus */
|
||||||
#ifndef BAUD
|
#ifndef BAUD
|
||||||
#define BAUD 38400L
|
#define BAUD 19200L
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
63
modbus_io_slave.c
Normal file
63
modbus_io_slave.c
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
#include "avrIOhelper/io-helper.h"
|
||||||
|
#include "modbus-master.h"
|
||||||
|
|
||||||
|
#define IO_SLAVE_ADDR 0x04
|
||||||
|
|
||||||
|
volatile uint8_t modbus_io_inStates[4];
|
||||||
|
volatile uint8_t modbus_io_inStatesBothEdges[4];
|
||||||
|
volatile uint8_t modbus_io_inStatesRisingEdge[4];
|
||||||
|
volatile uint8_t modbus_io_inStatesFallingEdge[4];
|
||||||
|
|
||||||
|
void modbus_io_read(){
|
||||||
|
readInputReg(IO_SLAVE_ADDR, 0, 2);
|
||||||
|
wait_receive(2, (void*)modbus_io_inStates, 100);
|
||||||
|
|
||||||
|
readInputReg(IO_SLAVE_ADDR, 100, 2);
|
||||||
|
wait_receive(2, (void*)modbus_io_inStatesBothEdges, 100);
|
||||||
|
|
||||||
|
readInputReg(IO_SLAVE_ADDR, 200, 2);
|
||||||
|
wait_receive(2, (void*)modbus_io_inStatesFallingEdge, 100);
|
||||||
|
|
||||||
|
readInputReg(IO_SLAVE_ADDR, 300, 2);
|
||||||
|
wait_receive(2, (void*)modbus_io_inStatesRisingEdge, 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
void modbus_io_set_Output(uint8_t slave_adr, uint8_t nr, uint8_t state) {
|
||||||
|
switch (state) {
|
||||||
|
case BLINK:
|
||||||
|
writeCoil(slave_adr,100+nr,1);
|
||||||
|
wait_write(10);
|
||||||
|
break;
|
||||||
|
case TOGGLE:
|
||||||
|
writeCoil(slave_adr,200+nr,1);
|
||||||
|
wait_write(10);
|
||||||
|
break;
|
||||||
|
case ON:
|
||||||
|
writeCoil(slave_adr,nr,1);
|
||||||
|
wait_write(10);
|
||||||
|
break;
|
||||||
|
case OFF:
|
||||||
|
writeCoil(slave_adr,nr,0);
|
||||||
|
wait_write(10);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t modbus_io_read_Input(uint8_t nr, uint8_t type) {
|
||||||
|
uint8_t state = 0;
|
||||||
|
switch (type) {
|
||||||
|
case LEVEL:
|
||||||
|
state = ioHelperReadBit(modbus_io_inStates, nr);
|
||||||
|
break;
|
||||||
|
case EDGE:
|
||||||
|
state = ioHelperReadBit(modbus_io_inStatesBothEdges, nr);
|
||||||
|
break;
|
||||||
|
case RISING:
|
||||||
|
state = ioHelperReadBit(modbus_io_inStatesFallingEdge, nr);
|
||||||
|
break;
|
||||||
|
case FALLING:
|
||||||
|
state = ioHelperReadBit(modbus_io_inStatesRisingEdge, nr);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return state;
|
||||||
|
}
|
||||||
10
modbus_io_slave.h
Normal file
10
modbus_io_slave.h
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#ifndef _MODBUS_IO_SLAVE_H_
|
||||||
|
#define _MODBUS_IO_SLAVE_H_
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
void modbus_io_read(void);
|
||||||
|
void modbus_io_set_Output(uint8_t slave_adr, uint8_t nr, uint8_t state);
|
||||||
|
uint8_t modbus_io_read_Input(uint8_t nr, uint8_t type);
|
||||||
|
|
||||||
|
#endif
|
||||||
2
mqtt.c
2
mqtt.c
@@ -35,7 +35,7 @@ void messageArrived(MessageData* md)
|
|||||||
|
|
||||||
void mqtt_pub(Client* mqtt_client, char * mqtt_topic, char * mqtt_msg, int mqtt_msg_len)
|
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;
|
static uint8_t mqtt_err_cnt = 0;
|
||||||
int32_t mqtt_rc;
|
int32_t mqtt_rc;
|
||||||
|
|
||||||
|
|||||||
10
uart.c
10
uart.c
@@ -8,8 +8,14 @@ FILE uart_output = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
|
|||||||
void uart_init()
|
void uart_init()
|
||||||
{
|
{
|
||||||
DDRD |= 1 << 1; // TX
|
DDRD |= 1 << 1; // TX
|
||||||
UART_BAUD_REGH = (BAUDRATE>>8);
|
UART_BAUD_REGH = UBRRH_VALUE;//(BAUDRATE>>8);
|
||||||
UART_BAUD_REGL = BAUDRATE; // set baud rate
|
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)
|
UART_CTRL_REGB |= (1<<UART_TXEN_BM)
|
||||||
//|(1<<UART_RXEN_BM)
|
//|(1<<UART_RXEN_BM)
|
||||||
|
|||||||
6
uart.h
6
uart.h
@@ -1,9 +1,11 @@
|
|||||||
#ifndef _UART_H_
|
#ifndef _UART_H_
|
||||||
#define _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 BAUDRATE ((F_CPU)/(BAUD*16UL)-1)
|
||||||
|
|
||||||
#define UART_BAUD_REGH UBRR0H
|
#define UART_BAUD_REGH UBRR0H
|
||||||
|
|||||||
Reference in New Issue
Block a user