You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
116 lines
2.3 KiB
C
116 lines
2.3 KiB
C
/*
|
|
* LCD_HD44780.c
|
|
*
|
|
* Created: 20.01.2018 12:51:11
|
|
* Author: Ulrich
|
|
*/
|
|
|
|
#include <avr/io.h>
|
|
#include <avr/interrupt.h>
|
|
#include <util/delay.h>
|
|
|
|
#include "lcd.h"
|
|
#include "i2c.h"
|
|
#include "modbus.h"
|
|
#include "menu.h"
|
|
#include "adc.h"
|
|
|
|
volatile uint16_t holdingRegisters[10];
|
|
|
|
volatile uint16_t temp_values[4];
|
|
volatile uint16_t temp_setpoints[4];
|
|
|
|
void modbusGet(void) {
|
|
if (modbusGetBusState() & (1<<ReceiveCompleted))
|
|
{
|
|
switch(rxbuffer[1]) {
|
|
case fcReadHoldingRegisters:
|
|
holdingRegisters[0] = menu_state;
|
|
case fcPresetSingleRegister:
|
|
case fcPresetMultipleRegisters:
|
|
modbusExchangeRegisters(holdingRegisters,0,4);
|
|
menu_state = holdingRegisters[0];
|
|
break;
|
|
default:
|
|
modbusSendException(ecIllegalFunction);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void do_buttons(){
|
|
i2c_start(0x71);
|
|
uint8_t buttons = i2c_readNak();
|
|
i2c_stop();
|
|
//TODO error handling
|
|
|
|
if(buttons & (1 << 0))
|
|
set_item(1);
|
|
else if(buttons & (1 << 1))
|
|
set_item(2);
|
|
else if(buttons & (1 << 2))
|
|
set_item(3);
|
|
else if(buttons & (1 << 3))
|
|
set_item(4);
|
|
else if(buttons & (1 << 4))
|
|
set_item(5);
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
i2c_init();
|
|
lcd_init();
|
|
adc_init();
|
|
|
|
lcd_clear();
|
|
|
|
sei();
|
|
modbusSetAddress(1);
|
|
modbusInit();
|
|
|
|
TCCR0B|=(1<<CS01); //prescaler 8
|
|
TIMSK0|=(1<<TOIE0);
|
|
|
|
PORTD |= (1<<6) | (1 << 7);
|
|
PCICR |= (1<<PCIE2);
|
|
PCMSK2 |= (1<<PCINT23);
|
|
|
|
draw_menu();
|
|
|
|
uint16_t x = 0;
|
|
while(1){
|
|
modbusGet();
|
|
do_buttons();
|
|
if(x==0){
|
|
write_temps();
|
|
write_setpoints();
|
|
update_cursor();
|
|
}
|
|
x++;
|
|
if(x>=2024)
|
|
x=0;
|
|
}
|
|
}
|
|
|
|
ISR(TIMER0_OVF_vect) { //this ISR is called 9765.625 times per second
|
|
modbusTickTimer();
|
|
}
|
|
|
|
ISR(PCINT2_vect){
|
|
//TODO good quadrature reading code
|
|
if((PIND & (1<<7)) && (menu_state >= 1) && (menu_state <= 3)){
|
|
if(PIND & (1<<6))
|
|
temp_setpoints[menu_state-1]--;
|
|
else
|
|
temp_setpoints[menu_state-1]++;
|
|
}
|
|
|
|
//if(PIND & (1<<7))
|
|
// if(PIND & (1<<6)){
|
|
// if(temp_setpoints[menu_state] > 0)
|
|
// }
|
|
// else{
|
|
// if(temp_setpoints[menu_state] < 300)
|
|
// }
|
|
}
|