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.
80 lines
1.7 KiB
C
80 lines
1.7 KiB
C
#include <avr/io.h>
|
|
#include <avr/interrupt.h>
|
|
|
|
#include "millis.h"
|
|
#include "modbus.h"
|
|
#include "avrIOhelper/io-helper.h"
|
|
|
|
uint16_t holdingregister;
|
|
|
|
void timer0_init()
|
|
{
|
|
TCCR0A = (1<<WGM01); //TIMER0 SET-UP: CTC MODE
|
|
TCCR0B = (1<<CS01)|(1<<CS00); // PS 1:64
|
|
OCR0A = 249; // 1ms reach for clear (16mz:64=>250kHz:250-=>1kHz)
|
|
TIMSK0 |= 1<<OCIE0A; //IRQ on TIMER0 output compareA
|
|
}
|
|
|
|
void timer2_init()
|
|
{
|
|
TCCR2A = (1<<WGM21); //TIMER0 SET-UP: CTC MODE
|
|
TCCR2B|=(1<<CS21); //prescaler 8
|
|
OCR2A = 200;
|
|
TIMSK2|=(1<<OCIE2A);
|
|
}
|
|
|
|
void modbusGet(void) {
|
|
if (modbusGetBusState() & (1<<ReceiveCompleted))
|
|
{
|
|
switch(rxbuffer[1]) {
|
|
case fcPresetSingleRegister:
|
|
case fcPresetMultipleRegisters:
|
|
case fcReadHoldingRegisters:
|
|
modbusExchangeRegisters(outStates,0,1);
|
|
break;
|
|
default:
|
|
modbusSendException(ecIllegalFunction);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
int main()
|
|
{
|
|
ioHelperInitBuffer();
|
|
ioHelperIoConf();
|
|
timer0_init();
|
|
|
|
modbusSetAddress(4);
|
|
modbusInit();
|
|
timer2_init();
|
|
sei();
|
|
|
|
set_Output(LED_EXTR_FEHLER, BLINK);
|
|
uint32_t timer_blink_outs = millis();
|
|
|
|
while(1)
|
|
{
|
|
ioHelperReadPins();
|
|
ioHelperDebounce();
|
|
ioHelperEdgeDetector();
|
|
|
|
if(millis() - timer_blink_outs > 500){
|
|
outStates[0] ^= outStatesBlinking[0];
|
|
outStates[1] ^= outStatesBlinking[1];
|
|
outStates[2] ^= outStatesBlinking[2];
|
|
outStates[2] ^= outStatesBlinking[3];
|
|
timer_blink_outs = millis();
|
|
}
|
|
|
|
ioHelperSetOuts();
|
|
|
|
modbusGet();
|
|
}
|
|
}
|
|
|
|
ISR(TIMER2_COMPA_vect) { //this ISR is called 9765.625 times per second
|
|
modbusTickTimer();
|
|
}
|
|
|