split code into more files

master
Eggert Jung 4 years ago
parent b5f588bdf6
commit 34ee74c185

@ -0,0 +1,83 @@
#include <avr/io.h>
#include "avrIOhelper/io-helper.h"
uint16_t abzug_speed = 100;
void timer3_init()
{
TCCR3A |= (1<<COM3B1);
TCCR3B |= _BV(WGM33);
ICR3 = 100;
OCR3B = 50;
DDRE |= 1 << 4;
}
void send_abzug_speed(void);
void do_rolle(){
if (read_Input(BTN_ABZUG_EIN, RISING)) {
TCCR3B |= _BV(CS31);
set_Output(LED_ABZUG, ON);
#if PLC_MQTT_ENABLED
send_abzug_speed();
#endif
}
if (read_Input(BTN_ABZUG_AUS, RISING)) {
TCCR3B &= ~(_BV(CS31));
set_Output(LED_ABZUG, OFF);
#if PLC_MQTT_ENABLED
send_abzug_speed();
#endif
}
if (read_Input(BTN_ABZUG_PLUS, RISING)) {
if(abzug_speed <= 900)
abzug_speed += 100;
else
abzug_speed = 1000;
#if PLC_MQTT_ENABLED
send_abzug_speed();
#endif
}
if (read_Input(BTN_ABZUG_MINUS, RISING)) {
if(abzug_speed >= 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;
}

@ -0,0 +1,8 @@
#ifndef _ABZUG_H
#define _ABZUG_H
extern uint16_t abzug_speed;
void do_rolle(void);
#endif

@ -0,0 +1 @@
#define PLC_MQTT_ENABLED 1

@ -0,0 +1,92 @@
#include <avr/io.h>
#include <util/delay.h>
#include <stdio.h>
#include <avr/interrupt.h>
#include "kraftsensor.h"
#include "modbus.h"
#include "common.h"
int32_t kraftsensor_value;
uint8_t kraftsensor_valid;
void timer2_init()
{
TCCR2A = (1<<WGM21); //TIMER0 SET-UP: CTC MODE
TCCR2B|=(1<<CS21); //prescaler 8
OCR2A = 200;
TIMSK2|=(1<<OCIE2A);
}
void kraftsensor_init(){
modbusSetAddress(1); //better set this to sth.
modbusInit();
timer2_init(); // modbus tick timer
}
void do_kraftsensor(){
uint16_t m_data[4];
readReg(1,0,2);
if(wait_receive(2, m_data, 10))
kraftsensor_valid = 0;
else{
kraftsensor_valid = 1;
int32_t tmp = (uint32_t)m_data[0]<<16;
tmp |= m_data[1];
kraftsensor_value = tmp;
}
}
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);
}
ISR(TIMER2_COMPA_vect) { //this ISR is called 9765.625 times per second
modbusTickTimer();
}

@ -0,0 +1,18 @@
#ifndef _MODBUS_MASTER_H_
#define _MODBUS_MASTER_H_
#include <stdint.h>
#define receiveOkay (modbusGetBusState() & (1<<ReceiveCompleted))
extern int32_t kraftsensor_value;
extern uint8_t kraftsensor_valid;
void timer2_init();
void kraftsensor_init();
void do_kraftsensor(void);
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

@ -0,0 +1,54 @@
#include <avr/io.h>
#include <avr/interrupt.h>
#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<<COM4A1);
TCCR4B |= _BV(WGM43);
// TIMSK4 |= 1<<TOIE4;
// TIMSK4 |= 1<<OCIE4A;
ICR4 = 100;
OCR4A = 50;
DDRH |= 1 << 3;
}
//ISR(TIMER4_OVF_vect) {
//}
//ISR(TIMER4_OVF_vect) {
//}

@ -0,0 +1,12 @@
#ifndef _TAENZER_H_
#define _TAENZER_H
typedef struct {
uint8_t homed;
uint32_t pos;
} taenzer_state_t;
void do_taenzer(void);
void timer4_init(void);
#endif