clean up main, disable some broken code

This commit is contained in:
2021-04-29 01:51:28 +02:00
parent c944c12fc1
commit e4b0ff8721
2 changed files with 33 additions and 19 deletions

48
main.c
View File

@@ -1,15 +1,15 @@
#include <avr/io.h> #include <avr/io.h>
#include <util/delay.h> #include <util/delay.h>
#include <modbus.h>
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include "buffer.h" #include "modbus.h"
#include "pid.h" #include "pid.h"
#include "adc.h" #include "adc.h"
#include "i2cmaster.h" #include "i2cmaster.h"
void modbusGet(void); void modbusGet(void);
volatile uint16_t holdingRegisters[40];
volatile float output; volatile float output;
volatile struct pid controller; volatile struct pid controller;
@@ -46,9 +46,9 @@ int main(){
sei(); sei();
_delay_ms(1000); _delay_ms(1000);
PORTD &= ~(1 << 4);
init_pid(&controller, 5, 0.2, 0); init_pid(&controller, 5, 0.2, 0);
// timer for regler // timer for regler
TCCR1B|=(1<<CS12) | (1<<CS10); TCCR1B|=(1<<CS12) | (1<<CS10);
TIMSK1|=(1<<TOIE1)|(1<<OCIE1A); TIMSK1|=(1<<TOIE1)|(1<<OCIE1A);
@@ -58,25 +58,35 @@ int main(){
} }
} }
void set_ADC_Channel(uint8_t adr)
{
if (adr < 11)
{
ADMUX &= (0b11110000); //Clear MUX-Address
ADMUX |= adr; //Set new MUX-Address
}
}
void modbusGet(void) { void modbusGet(void) {
if (modbusGetBusState() & (1<<ReceiveCompleted)) if (modbusGetBusState() & (1<<ReceiveCompleted))
{ {
switch(rxbuffer[1]) { switch(rxbuffer[1]) {
case fcReadHoldingRegisters: case fcReadHoldingRegisters:
modbusExchangeRegisters(holdingRegisters,0,40);
break;
case fcPresetSingleRegister: case fcPresetSingleRegister:
case fcPresetMultipleRegisters: case fcPresetMultipleRegisters:
modbusExchangeRegisters((uint16_t *)&OCR2B,0,1); if(modbusRequestedAmount()==1) {
modbusExchangeRegisters(holdingRegisters,0,40);
if(modbusIsInRange(0))
/* analog out for Motor VFD */
OCR2B = holdingRegisters[0] & 0xFF;
if(modbusIsInRange(1)){
/* i2c scanner */
for(int i = 0x0; i < 0xFF; i++){
if(!i2c_start(i)){
holdingRegisters[1] = i;
i2c_stop();
break;
}
i2c_stop();
}
}
} else {
modbusSendException(ecIllegalDataValue);
}
break; break;
case fcReadCoilStatus: case fcReadCoilStatus:
case fcForceSingleCoil: case fcForceSingleCoil:
case fcForceMultipleCoils: case fcForceMultipleCoils:
@@ -101,10 +111,12 @@ ISR(TIMER0_OVF_vect) { //this ISR is called 9765.625 times per second
modbusTickTimer(); modbusTickTimer();
} }
/* Heater PID control */
ISR(TIMER1_OVF_vect) { ISR(TIMER1_OVF_vect) {
output = pid_step(&controller, 1, setpoint_1 - adc_avrg[1]); //output = pid_step(&controller, 1, setpoint_1 - adc_avrg[1]);
output = 1;
PORTD &= ~(1 << 4); //PORTD &= ~(1 << 4);
if(output > 128) if(output > 128)
output = 128; output = 128;
@@ -115,7 +127,7 @@ ISR(TIMER1_OVF_vect) {
tmp = 0xFFFE; tmp = 0xFFFE;
OCR1A = tmp; OCR1A = tmp;
PORTB |= 0x0E; PORTB |= 0x0F;
} }
else else
OCR1A = 1000; OCR1A = 1000;
@@ -123,6 +135,6 @@ ISR(TIMER1_OVF_vect) {
ISR(TIMER1_COMPA_vect) { ISR(TIMER1_COMPA_vect) {
/* turn off outputs */ /* turn off outputs */
PORTB &= ~(0x0E); PORTB &= ~(0x0F);
} }

4
pid.c
View File

@@ -2,7 +2,9 @@
#include "pid.h" #include "pid.h"
float pid_step(volatile struct pid* controller, float dt, float error) { float pid_step(volatile struct pid* controller, float dt, float error) {
// Calculate p term //TODO convert to integer; floats cause trouble ...
// Calculate p term
float p = error * controller->kP; float p = error * controller->kP;
// Calculate i term // Calculate i term