Compare commits
9 Commits
a3453e1fdb
...
409b2b8afb
| Author | SHA1 | Date | |
|---|---|---|---|
| 409b2b8afb | |||
| f89ea0bde9 | |||
| c4fe5df4af | |||
| 858fcd1547 | |||
| 456731d2f0 | |||
| 30f1ec733d | |||
| 28942ab232 | |||
| e20bdb3e8a | |||
| 33b01ee2d8 |
106
main.c
106
main.c
@@ -2,6 +2,9 @@
|
||||
#include <avr/interrupt.h>
|
||||
#include <avr/wdt.h> // WatchDog
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include "Ethernet/socket.h"
|
||||
#include "Ethernet/wizchip_conf.h"
|
||||
@@ -17,10 +20,9 @@
|
||||
|
||||
#include "util/delay.h"
|
||||
|
||||
#include "modbus.h"
|
||||
#include "modbus_master.h"
|
||||
|
||||
#define PLC_MQTT_ENABLED 1
|
||||
#include "kraftsensor.h"
|
||||
#include "taenzer.h"
|
||||
#include "abzug.h"
|
||||
|
||||
Client mqtt_client;
|
||||
|
||||
@@ -39,12 +41,9 @@ void get_mcusr(void)
|
||||
}
|
||||
//***********Prologue for fast WDT disable & and save reason of reset/power-up: END
|
||||
|
||||
|
||||
//FUNC headers
|
||||
static void avr_init(void);
|
||||
void modbus_master_init(void);
|
||||
void timer0_init(void);
|
||||
void timer2_init(void);
|
||||
void timer3_init(void);
|
||||
void print_network_information(void);
|
||||
|
||||
@@ -60,12 +59,6 @@ void IO_LIBRARY_Init(void) {
|
||||
//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()
|
||||
{
|
||||
// Initialize device here.
|
||||
@@ -82,22 +75,16 @@ static void avr_init()
|
||||
return;
|
||||
}
|
||||
|
||||
void do_kraftsensor(){
|
||||
uint16_t m_data[4];
|
||||
char msg[64];
|
||||
void send_abzug_speed(void){
|
||||
char msg[4];
|
||||
sprintf(msg, "%d", abzug_speed);
|
||||
mqtt_pub(&mqtt_client, "/Filamentanlage/05_Abzug/state/abzug/speed", msg, strlen(msg));
|
||||
|
||||
readReg(1,0,2);
|
||||
if(wait_receive(2, m_data, 10))
|
||||
sprintf(msg, "n/a");
|
||||
else{
|
||||
int32_t tmp = (uint32_t)m_data[0]<<16;
|
||||
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
|
||||
if(TCCR3B & (1<<CS31))
|
||||
sprintf(msg, "True");
|
||||
else
|
||||
sprintf(msg, "False");
|
||||
mqtt_pub(&mqtt_client, "/Filamentanlage/05_Abzug/state/abzug/onoff", msg, strlen(msg));
|
||||
}
|
||||
|
||||
void do_notaus(){
|
||||
@@ -165,9 +152,11 @@ int main()
|
||||
// INIT MCU
|
||||
avr_init();
|
||||
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");
|
||||
timer3_init();
|
||||
timer4_init();
|
||||
|
||||
ioHelperInitBuffer();
|
||||
ioHelperIoConf();
|
||||
@@ -207,7 +196,7 @@ int main()
|
||||
else
|
||||
{
|
||||
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/#";
|
||||
@@ -227,12 +216,14 @@ int main()
|
||||
{
|
||||
wdt_reset(); // WDT reset at least every sec
|
||||
|
||||
//if (flag_refresh_inStates) {
|
||||
#if PLC_MQTT_ENABLED
|
||||
if(millis() < 10)
|
||||
send_abzug_speed();
|
||||
#endif
|
||||
|
||||
ioHelperReadPins();
|
||||
ioHelperDebounce();
|
||||
ioHelperEdgeDetector();
|
||||
// flag_refresh_inStates = 0;
|
||||
//}
|
||||
|
||||
// Toggle all outs which are set to blinking
|
||||
if(millis() - timer_blink_outs > 500){
|
||||
@@ -245,19 +236,29 @@ int main()
|
||||
|
||||
#if PLC_MQTT_ENABLED
|
||||
// send misc info
|
||||
if(millis() - timer_send_uptime > 5000){
|
||||
timer_send_uptime += 5000;
|
||||
char msg[64];
|
||||
sprintf(msg, "%ld", millis()/1000);
|
||||
mqtt_pub(&mqtt_client, "/Filamentanlage/04_Messmodul/state/uptime", msg, strlen(msg));
|
||||
}
|
||||
//if(millis() - timer_send_uptime > 5000){
|
||||
// timer_send_uptime += 5000;
|
||||
// char msg[64];
|
||||
// sprintf(msg, "%ld", millis()/1000);
|
||||
// mqtt_pub(&mqtt_client, "/Filamentanlage/04_Messmodul/state/uptime", msg, strlen(msg));
|
||||
//}
|
||||
#endif
|
||||
if(millis() - timer_modbus_poll > 1000){
|
||||
timer_modbus_poll += 1000;
|
||||
if(millis() - timer_modbus_poll > 200){
|
||||
timer_modbus_poll += 200;
|
||||
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_rolle();
|
||||
do_taenzer();
|
||||
|
||||
#if PLC_MQTT_ENABLED
|
||||
ioHelperSetBit(outStates, LED_BUS_OK, 1);
|
||||
@@ -270,25 +271,6 @@ int main()
|
||||
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()
|
||||
{
|
||||
|
||||
@@ -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]);
|
||||
}
|
||||
|
||||
ISR(TIMER2_COMPA_vect) { //this ISR is called 9765.625 times per second
|
||||
modbusTickTimer();
|
||||
}
|
||||
|
||||
|
||||
11
millis.c
11
millis.c
@@ -4,6 +4,17 @@
|
||||
|
||||
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)
|
||||
{
|
||||
_millis++; // INC millis tick
|
||||
|
||||
1
millis.h
1
millis.h
@@ -3,5 +3,6 @@
|
||||
|
||||
#define TICK_PER_SEC 1000UL
|
||||
unsigned long millis(void);
|
||||
void timer0_init(void);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
33
taenzer.c
33
taenzer.c
@@ -8,19 +8,20 @@ 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;
|
||||
//}
|
||||
/* 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;
|
||||
taenzer_state.pos = 0;
|
||||
}
|
||||
|
||||
/* Force regualtion */
|
||||
if(kraftsensor_valid /* && taenzer_state.homed */){
|
||||
if(kraftsensor_value < 0){
|
||||
if(kraftsensor_valid && taenzer_state.homed){
|
||||
if(kraftsensor_value < 9000){
|
||||
set_Output(MOTOR_TAENZER_DIR, 0); // direction: down
|
||||
TCCR4B |= _BV(CS41); //TURN ON
|
||||
}
|
||||
@@ -38,7 +39,7 @@ void timer4_init()
|
||||
TCCR4A |= (1<<COM4A1);
|
||||
TCCR4B |= _BV(WGM43);
|
||||
|
||||
// TIMSK4 |= 1<<TOIE4;
|
||||
TIMSK4 |= 1<<TOIE4;
|
||||
// TIMSK4 |= 1<<OCIE4A;
|
||||
|
||||
ICR4 = 100;
|
||||
@@ -47,8 +48,12 @@ void timer4_init()
|
||||
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) {
|
||||
//}
|
||||
|
||||
Reference in New Issue
Block a user