|
|
|
|
@ -1,15 +1,15 @@
|
|
|
|
|
#include <avr/io.h>
|
|
|
|
|
#include <util/delay.h>
|
|
|
|
|
#include <modbus.h>
|
|
|
|
|
#include <avr/interrupt.h>
|
|
|
|
|
|
|
|
|
|
#include "buffer.h"
|
|
|
|
|
#include "modbus.h"
|
|
|
|
|
#include "pid.h"
|
|
|
|
|
#include "adc.h"
|
|
|
|
|
#include "i2cmaster.h"
|
|
|
|
|
|
|
|
|
|
void modbusGet(void);
|
|
|
|
|
|
|
|
|
|
volatile uint16_t holdingRegisters[40];
|
|
|
|
|
|
|
|
|
|
volatile float output;
|
|
|
|
|
volatile struct pid controller;
|
|
|
|
|
@ -46,9 +46,9 @@ int main(){
|
|
|
|
|
sei();
|
|
|
|
|
|
|
|
|
|
_delay_ms(1000);
|
|
|
|
|
PORTD &= ~(1 << 4);
|
|
|
|
|
init_pid(&controller, 5, 0.2, 0);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// timer for regler
|
|
|
|
|
TCCR1B|=(1<<CS12) | (1<<CS10);
|
|
|
|
|
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) {
|
|
|
|
|
if (modbusGetBusState() & (1<<ReceiveCompleted))
|
|
|
|
|
{
|
|
|
|
|
switch(rxbuffer[1]) {
|
|
|
|
|
case fcReadHoldingRegisters:
|
|
|
|
|
modbusExchangeRegisters(holdingRegisters,0,40);
|
|
|
|
|
break;
|
|
|
|
|
case fcPresetSingleRegister:
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
case fcReadCoilStatus:
|
|
|
|
|
case fcForceSingleCoil:
|
|
|
|
|
case fcForceMultipleCoils:
|
|
|
|
|
@ -101,10 +111,12 @@ ISR(TIMER0_OVF_vect) { //this ISR is called 9765.625 times per second
|
|
|
|
|
modbusTickTimer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Heater PID control */
|
|
|
|
|
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)
|
|
|
|
|
output = 128;
|
|
|
|
|
@ -115,7 +127,7 @@ ISR(TIMER1_OVF_vect) {
|
|
|
|
|
tmp = 0xFFFE;
|
|
|
|
|
|
|
|
|
|
OCR1A = tmp;
|
|
|
|
|
PORTB |= 0x0E;
|
|
|
|
|
PORTB |= 0x0F;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
OCR1A = 1000;
|
|
|
|
|
@ -123,6 +135,6 @@ ISR(TIMER1_OVF_vect) {
|
|
|
|
|
|
|
|
|
|
ISR(TIMER1_COMPA_vect) {
|
|
|
|
|
/* turn off outputs */
|
|
|
|
|
PORTB &= ~(0x0E);
|
|
|
|
|
PORTB &= ~(0x0F);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|