rewrite with minimal hd444780/modbus example
This commit is contained in:
155
main.c
155
main.c
@@ -1,140 +1,57 @@
|
||||
/*
|
||||
* LCD_HD44780.c
|
||||
*
|
||||
* Created: 20.01.2018 12:51:11
|
||||
* Author: Ulrich
|
||||
*/
|
||||
|
||||
#include <avr/io.h>
|
||||
#include <util/delay.h>
|
||||
#include <avr/interrupt.h>
|
||||
#include <util/delay.h>
|
||||
|
||||
#include "lcd.h"
|
||||
#include "i2c.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;
|
||||
|
||||
volatile float setpoint_1 = 130;
|
||||
volatile float setpoint_2 = 150;
|
||||
volatile float setpoint_3 = 150;
|
||||
|
||||
int main(){
|
||||
DDRD |= (1 << 4); // LED
|
||||
DDRD |= (1 << 3); // FU PWM
|
||||
DDRD |= (1 << 2); // 485 DE
|
||||
|
||||
PORTD |= 1 << 4;
|
||||
|
||||
DDRB |= 0x0F; // out channels
|
||||
|
||||
PORTD|=(1<<0); // RX
|
||||
|
||||
// FU PWM on PD3
|
||||
TCCR2A |= (1 << COM2B1) | (1 << COM2B0);
|
||||
TCCR2A |= (1 << WGM21) | (1 << WGM20);
|
||||
TCCR2B |= (1 << CS21);
|
||||
|
||||
modbusSetAddress(1);
|
||||
modbusInit();
|
||||
|
||||
// Modbus Tick Timer
|
||||
TCCR0B|=(1<<CS01); //prescaler 8
|
||||
TIMSK0|=(1<<TOIE0);
|
||||
|
||||
initADC();
|
||||
i2c_init();
|
||||
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);
|
||||
|
||||
while(1){
|
||||
modbusGet();
|
||||
}
|
||||
}
|
||||
volatile uint16_t holdingRegisters[4];
|
||||
|
||||
void modbusGet(void) {
|
||||
if (modbusGetBusState() & (1<<ReceiveCompleted))
|
||||
{
|
||||
switch(rxbuffer[1]) {
|
||||
switch(rxbuffer[1]) {
|
||||
case fcReadHoldingRegisters:
|
||||
modbusExchangeRegisters(holdingRegisters,0,40);
|
||||
break;
|
||||
case fcPresetSingleRegister:
|
||||
case fcPresetMultipleRegisters:
|
||||
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);
|
||||
}
|
||||
case fcPresetMultipleRegisters:
|
||||
modbusExchangeRegisters(holdingRegisters,0,4);
|
||||
lcd_home();
|
||||
lcd_write("test %x", holdingRegisters[0]);
|
||||
break;
|
||||
case fcReadCoilStatus:
|
||||
case fcForceSingleCoil:
|
||||
case fcForceMultipleCoils:
|
||||
modbusExchangeBits(&PORTB,0,4);
|
||||
break;
|
||||
|
||||
case fcReadInputRegisters:
|
||||
if(modbusRequestedAddress()<10)
|
||||
modbusExchangeRegisters((uint16_t *)&adc_avrg,0,8);
|
||||
else
|
||||
modbusExchangeRegisters((uint16_t *)&output,10,4);
|
||||
break;
|
||||
|
||||
default:
|
||||
modbusSendException(ecIllegalFunction);
|
||||
modbusSendException(ecIllegalFunction);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
i2c_init();
|
||||
lcd_init();
|
||||
|
||||
lcd_clear();
|
||||
|
||||
sei();
|
||||
modbusSetAddress(1);
|
||||
modbusInit();
|
||||
|
||||
TCCR0B|=(1<<CS01); //prescaler 8
|
||||
TIMSK0|=(1<<TOIE0);
|
||||
|
||||
while(1){
|
||||
modbusGet();
|
||||
}
|
||||
}
|
||||
|
||||
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 = 1;
|
||||
|
||||
//PORTD &= ~(1 << 4);
|
||||
|
||||
if(output > 128)
|
||||
output = 128;
|
||||
|
||||
if(output >= 1){
|
||||
uint32_t tmp = output * 512;
|
||||
if(tmp >= 0x10000)
|
||||
tmp = 0xFFFE;
|
||||
|
||||
OCR1A = tmp;
|
||||
PORTB |= 0x0F;
|
||||
}
|
||||
else
|
||||
OCR1A = 1000;
|
||||
}
|
||||
|
||||
ISR(TIMER1_COMPA_vect) {
|
||||
/* turn off outputs */
|
||||
PORTB &= ~(0x0F);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user