From 03d7fa45137b90ce115d991c56ac0ef58e8aa9b4 Mon Sep 17 00:00:00 2001 From: Eggert Jung Date: Sun, 9 Oct 2022 23:39:10 +0200 Subject: [PATCH] add coils for onoff and ejector --- main.c | 71 +++++++++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 49 insertions(+), 22 deletions(-) diff --git a/main.c b/main.c index 44e15e6..cb80d39 100644 --- a/main.c +++ b/main.c @@ -18,8 +18,10 @@ uint8_t font[] = { 0xF6, }; +uint16_t bcd; uint8_t display[4]; volatile uint16_t holdingRegisters[4]; +volatile uint8_t outstate = 0; void timer0_init() { @@ -50,6 +52,7 @@ void modbusGet(void) { { switch(rxbuffer[1]) { case fcReadHoldingRegisters: + holdingRegisters[1] = bcd; modbusExchangeRegisters(holdingRegisters,0,4); break; case fcPresetMultipleRegisters: @@ -59,17 +62,30 @@ void modbusGet(void) { uint8_t m = (holdingRegisters[0] / 60) % 60; uint8_t s = holdingRegisters[0] % 60; - uint16_t bcd; if(h) bcd = bin2bcd(m) | bin2bcd(h)<<8; else bcd = bin2bcd(s) | bin2bcd(m)<<8; - display[0] = font[(bcd&0x000f)]; - display[1] = font[(bcd&0x00f0) >> 4]; + break; + case fcReadCoilStatus: + modbusExchangeBits(&outstate,0,8); + break; + case fcForceMultipleCoils: + case fcForceSingleCoil: + modbusExchangeBits(&outstate,0,8); - display[2] = font[(bcd&0x0f00) >> 8]; - display[3] = bcd&0xF000 ? font[(bcd&0xf000) >> 12] : 0; + if(outstate & 0x01) + PORTB|=1<<4; + else + PORTB&=~(1<<4); + + if(outstate & 0x02){ + PORTA |= _BV(7); + _delay_ms(250); // well ... + PORTA &= ~_BV(7); + outstate &= ~0x02; + } break; default: modbusSendException(ecIllegalFunction); @@ -79,12 +95,14 @@ void modbusGet(void) { } int main(void){ - DDRB |= _BV(4) | _BV(3) | _BV(2) | _BV(1) | _BV(0); - DDRD = 0xFC; - DDRA = 0x03 << 4; - DDRC = 0xFE; - - modbusSetAddress(24); //better set this to sth. + DDRB |= _BV(3) | _BV(2) | _BV(1) | _BV(0); // led anodes + DDRB |= _BV(4); // dots + DDRD = 0xFC; // uln output 1 + DDRA |= _BV(5) | _BV(4); + DDRC = 0xFE; // uln output 2 + DDRA |= _BV(7); // ejector out + + modbusSetAddress(24); modbusInit(); timer0_init(); @@ -93,24 +111,33 @@ int main(void){ sei(); while(1){ + display[0] = font[(bcd&0x000f)]; + display[1] = font[(bcd&0x00f0) >> 4]; + display[2] = font[(bcd&0x0f00) >> 8]; + display[3] = bcd&0xF000 ? font[(bcd&0xf000) >> 12] : 0; + modbusGet(); } } -ISR(TIMER0_COMP_vect) -{ - if(PORTB & 0x01){ - PORTB = (PORTB & 0xF0) | 1<<1 | 1<<3; - PORTD = display[3] & 0xFC; - PORTA = (PORTA & 0xCF) | (display[3] & 0x03) << 4; - PORTC = display[1]; +ISR(TIMER0_COMP_vect) { + if(outstate & 0x01){ + if(PORTB & 0x01){ + PORTB = (PORTB & 0xF0) | 1<<1 | 1<<3; + PORTD = display[3] & 0xFC; + PORTA = (PORTA & 0xCF) | (display[3] & 0x03) << 4; + PORTC = display[1]; + } + else{ + PORTB = (PORTB & 0xF0) | 1<<0 | 1<<2; + PORTD = display[2] & 0xFC; + PORTA = (PORTA & 0xCF) | (display[2] & 0x03) << 4; + PORTC = display[0]; + } } else{ - PORTB = (PORTB & 0xF0) | 1<<0 | 1<<2; - PORTD = display[2] & 0xFC; - PORTA = (PORTA & 0xCF) | (display[2] & 0x03) << 4; - PORTC = display[0]; + PORTB &= ~0x0F; } }