changes ...
This commit is contained in:
138
main.c
138
main.c
@@ -22,6 +22,7 @@ uint16_t bcd;
|
||||
uint8_t display[4];
|
||||
volatile uint16_t holdingRegisters[4];
|
||||
volatile uint8_t outstate = 0;
|
||||
volatile uint8_t ejector_timer = 0;
|
||||
|
||||
void timer0_init()
|
||||
{
|
||||
@@ -31,15 +32,15 @@ void timer0_init()
|
||||
TIMSK |= 1<<OCIE0; //IRQ on TIMER0 output compareA
|
||||
}
|
||||
|
||||
void timer2_init(void) {
|
||||
//TCCR2 = (1<<WGM21); //TIMER0 SET-UP: CTC MODE
|
||||
//TCCR2 = (1<<CS21)|(1<<CS20); // PS 1:64
|
||||
//OCR2 = 25; // 1ms reach for clear (16mz:64=>250kHz:250-=>1kHz)
|
||||
//TIMSK |= 1<<OCIE2; //IRQ on TIMER0 output compareA
|
||||
void timer1_init(void) {
|
||||
TCCR1B|= (1<<CS12) | (1<<WGM12); //prescaler 8
|
||||
OCR1A = 62500;
|
||||
TIMSK|=(1<<OCIE1A);
|
||||
}
|
||||
|
||||
void timer2_init(void) {
|
||||
TCCR2|=(1<<CS21); //prescaler 8
|
||||
TIMSK|=(1<<TOIE2);
|
||||
|
||||
}
|
||||
|
||||
unsigned long bin2bcd (uint32_t n) // convert 16 bit binary n to 5 digit BCD (packed)
|
||||
@@ -47,45 +48,67 @@ unsigned long bin2bcd (uint32_t n) // convert 16 bit binary n to 5 digit BCD
|
||||
return n + 6 * (n/10 + 16*(n/100) + 256*(n/1000) + 4096*(n/10000));
|
||||
}
|
||||
|
||||
void update_display(){
|
||||
uint8_t h = holdingRegisters[0] / 3600;
|
||||
uint8_t m = (holdingRegisters[0] / 60) % 60;
|
||||
uint8_t s = holdingRegisters[0] % 60;
|
||||
|
||||
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];
|
||||
display[2] = font[(bcd&0x0f00) >> 8];
|
||||
display[3] = bcd&0xF000 ? font[(bcd&0xf000) >> 12] : 0;
|
||||
}
|
||||
|
||||
void eject(){
|
||||
PORTA |= _BV(7);
|
||||
ejector_timer = 200;
|
||||
//outstate |= _BV(1);
|
||||
}
|
||||
|
||||
void reset_timer(){
|
||||
outstate &= ~0x01;
|
||||
holdingRegisters[0] = 17*60;
|
||||
update_display();
|
||||
}
|
||||
|
||||
void modbusGet(void) {
|
||||
if (modbusGetBusState() & (1<<ReceiveCompleted))
|
||||
{
|
||||
switch(rxbuffer[1]) {
|
||||
case fcReadHoldingRegisters:
|
||||
holdingRegisters[1] = bcd;
|
||||
holdingRegisters[1] = PINA & 0x0F;
|
||||
holdingRegisters[2] = bcd;
|
||||
modbusExchangeRegisters(holdingRegisters,0,4);
|
||||
break;
|
||||
case fcPresetMultipleRegisters:
|
||||
modbusExchangeRegisters(holdingRegisters,0,1);
|
||||
|
||||
uint8_t h = holdingRegisters[0] / 3600;
|
||||
uint8_t m = (holdingRegisters[0] / 60) % 60;
|
||||
uint8_t s = holdingRegisters[0] % 60;
|
||||
|
||||
if(h)
|
||||
bcd = bin2bcd(m) | bin2bcd(h)<<8;
|
||||
else
|
||||
bcd = bin2bcd(s) | bin2bcd(m)<<8;
|
||||
|
||||
update_display();
|
||||
break;
|
||||
case fcReadCoilStatus:
|
||||
modbusExchangeBits(&outstate,0,8);
|
||||
modbusExchangeBits(&outstate,0,8);
|
||||
if(modbusIsInRange(3))
|
||||
outstate &= ~_BV(3);
|
||||
break;
|
||||
case fcForceMultipleCoils:
|
||||
case fcForceSingleCoil:
|
||||
;
|
||||
uint8_t oldstates = outstate;
|
||||
modbusExchangeBits(&outstate,0,8);
|
||||
|
||||
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;
|
||||
if(!(oldstates & _BV(0)) && outstate & _BV(0)){
|
||||
//reset_timer();
|
||||
update_display();
|
||||
outstate |= _BV(0);
|
||||
}
|
||||
|
||||
if(outstate & 0x02)
|
||||
eject();
|
||||
|
||||
break;
|
||||
default:
|
||||
modbusSendException(ecIllegalFunction);
|
||||
@@ -102,27 +125,51 @@ int main(void){
|
||||
DDRC = 0xFE; // uln output 2
|
||||
DDRA |= _BV(7); // ejector out
|
||||
|
||||
PORTA |= _BV(0) | _BV(1) | _BV(2); // pullups on button inputs
|
||||
|
||||
modbusSetAddress(24);
|
||||
modbusInit();
|
||||
|
||||
timer0_init();
|
||||
timer1_init();
|
||||
timer2_init();
|
||||
|
||||
outstate |= _BV(2);
|
||||
holdingRegisters[0] = 17*60;
|
||||
|
||||
sei();
|
||||
|
||||
uint8_t old_A = PINA;
|
||||
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();
|
||||
|
||||
if(!(PINA & _BV(2))){
|
||||
holdingRegisters[0] += 5;
|
||||
update_display();
|
||||
_delay_ms(10);
|
||||
}
|
||||
if(!(PINA & _BV(1))){
|
||||
if(holdingRegisters[0]>5){
|
||||
holdingRegisters[0] -= 5;
|
||||
update_display();
|
||||
_delay_ms(10);
|
||||
}
|
||||
}
|
||||
if(!(PINA & _BV(0)) && (old_A & _BV(0))){
|
||||
if(!(outstate & 0x01)){
|
||||
reset_timer();
|
||||
}
|
||||
outstate ^= 0x01;
|
||||
_delay_ms(100);
|
||||
}
|
||||
old_A = PINA;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
ISR(TIMER0_COMP_vect) {
|
||||
if(outstate & 0x01){
|
||||
PORTB|=1<<4; // dots
|
||||
if(PORTB & 0x01){
|
||||
PORTB = (PORTB & 0xF0) | 1<<1 | 1<<3;
|
||||
PORTD = display[3] & 0xFC;
|
||||
@@ -137,8 +184,33 @@ ISR(TIMER0_COMP_vect) {
|
||||
}
|
||||
}
|
||||
else{
|
||||
PORTB&=~(1<<4); // dots
|
||||
PORTB &= ~0x0F;
|
||||
}
|
||||
|
||||
if(ejector_timer > 0 && ejector_timer < 0xFF){
|
||||
ejector_timer--;
|
||||
}
|
||||
else if(ejector_timer == 0){
|
||||
PORTA &= ~_BV(7);
|
||||
outstate &= ~0x02;
|
||||
ejector_timer = 0xFF;
|
||||
}
|
||||
}
|
||||
|
||||
ISR(TIMER1_COMPA_vect) {
|
||||
if(outstate & 0x01){
|
||||
holdingRegisters[0]--;
|
||||
update_display();
|
||||
}
|
||||
|
||||
if(holdingRegisters[0] == 0){
|
||||
reset_timer();
|
||||
if(outstate & _BV(2)){
|
||||
eject();
|
||||
outstate |= _BV(3);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ISR(TIMER2_OVF_vect) { //this ISR is called 9765.625 times per second
|
||||
|
||||
Reference in New Issue
Block a user