// ATtiny2313, Ext CLK=6MHz #include #include #include #include #define C500us 12 #define LeftHold 0x01 //F: 1:LeftHold #define RightHold 0x02 //F: 1:RightHold #define SSW 0x10 //F: Single SW 0:off, 1:on #define SW0 0x20 //F: SW0 0:off, 1:on #define SW1 0x40 //F: SW1 0:off, 1:on #define TO 0x80 //F: TimeOut 0:off, 1:on prog_uint8_t ctab[]={234,165,117}; // 10ms,7ms,5ms prog_uint8_t dirtab[]={ // direction 0<<2, 1<<2, 3<<2, 2<<2, // R 0<<4, 1<<4, 3<<4, 2<<4, // D 0<<2, 2<<2, 3<<2, 1<<2, // L 0<<4, 2<<4, 3<<4, 1<<4 // U }; uint8_t sw0cntr, sw1cntr, c500us, c10ms, S, F, dir, b; uint8_t t500us, t10ms, c, m, x, cxms, i, j, u; SIGNAL(SIG_OVERFLOW0){ // 0.167 * 256 = 42.6666 us if(++c10ms>=234){ t10ms++; c10ms=0; } // 10ms sync if(++c500us>=C500us){ // 500us sync c500us=0; if(b>0){ PORTD^=0x40; b--; } if(bit_is_clear(PINB,0))sw0cntr=100; // 50ms hold else if(sw0cntr>0) sw0cntr--; if(bit_is_clear(PINB,1))sw1cntr=100; // 50ms hold else if(sw1cntr>0) sw1cntr--; } if((S==14 || !(F&SSW) && F&SW1) && ++x>=cxms){ // xms sync x=0; u=pgm_read_byte(&dirtab[(uint8_t)(dir+m)]); m++; m&=0x03; PORTD=u|(PORTD&0x03); } } int main(void){ PORTB=0xfc; DDRB=0x00; // rrrrrrii PORTD=0x03; DDRD=0x7f; // xooooooo TCNT0=0; TCCR0B=1; // TCNT0=0 CLK=0.167 us TIMSK=1<2) eeprom_write_byte(0,1); cxms=pgm_read_byte(&ctab[eeprom_read_byte(0)]); // 10ms if(bit_is_clear(PINB,1)) F|=SSW; // SW1オンなら1個のSWで操作 sei(); for(;;){ F&=0x1f; if(sw0cntr>0) F|=SW0; if(sw1cntr>0) F|=SW1; if(t10ms>=100){ F|=TO; t10ms=0; } if(S==0){ if(F&SW0){ S=1; t10ms=0; b=100; } }else if(S==1){ if(F&TO){ S=2; b=100; F&=0xfc;} // Hold off else if(!(F&SW0)){ if(F&SSW){ S=12; t10ms=0; } else{ S=0; dir=(dir+4)&0x0c; } } }else if(S==2){ if(F&TO){ S=3; b=100; } else if(!(F&SW0)){S=5; t10ms=0; } }else if(S==3){ if(F&TO){ S=4; b=100; } else if(!(F&SW0)){S=7; t10ms=0; } }else if(S==4){ if(F&TO){ S=15; b=100; } else if(!(F&SW0)){S=9; t10ms=0; } }else if(S==5){ if(F&TO){ S=0; b=100; } else if(F&SW0){ S=6; t10ms=0; b=100; } }else if(S==6){ if(F&TO){ S=15; b=100; F|=LeftHold;} else if(!(F&SW0)){S=5; t10ms=0; } }else if(S==7){ if(F&TO){ S=0; b=100; } else if(F&SW0){ S=8; t10ms=0; b=100; } }else if(S==8){ if(!(F&SW0)){ S=7; t10ms=0; } }else if(S==9){ if(F&TO){ S=0; b=100; } else if(F&SW0){ S=10; t10ms=0; b=100; } }else if(S==10){ if(F&TO){ S=11; b=100; } else if(!(F&SW0)){S=0; t10ms=0; eeprom_write_byte(0,0); cxms=pgm_read_byte(&ctab[0]);} //低速 }else if(S==11){ if(F&TO){ S=15; b=100; eeprom_write_byte(0,2); cxms=pgm_read_byte(&ctab[2]);} //高速 else if(!(F&SW0)){S=0; t10ms=0; eeprom_write_byte(0,1); cxms=pgm_read_byte(&ctab[1]);} //標準 }else if(S==12){ if(F&TO){ S=0; b=100; } else if(F&SW0){ S=13; t10ms=0; b=100; } }else if(S==13){ if(F&TO){ S=14; b=100; x=0;} else if(!(F&SW0)){S=12; t10ms=0; dir=(dir+4)&0x0c; } }else if(S==14){ if(F&SSW){ if(!(F&SW0)){S=12; t10ms=0; } }else{ if(!(F&SW1))S=0; } }else if(S==15){ if(!(F&SW0)) S=0; } if(S==6 || F&LeftHold) PORTD&=0xfe; // LB on else PORTD|=0x01; // LB off if(S==8) PORTD&=0xfd; // RB on else PORTD|=0x02; // RB off } }