////* // MFJ962D 駆動コントローラー // MFJ-7MAIN3 2021-7-5〜 // ATmega1284p // CLOCK=8MHz。内蔵RC発振器を使用 // ヒューズビット  L=E2 H=DF // ------------------------------------------------- // //---------------------------------------------------- // // #include #define F_CPU 8000000UL //内部8M #include #include #include #include // 擬似命令 #define k_menu bit_is_clear (PINA,PA5) #define k_ret bit_is_clear (PINA,PA4) #define k_fwd bit_is_clear (PINB,PB6) #define k_rev bit_is_clear (PINB,PB7) #define k_in bit_is_set (PINB,PB0) #define k_in9 bit_is_clear (PINA,PA3) #define k_a0l bit_is_clear (PINA,PA0) #define k_a0h bit_is_set (PINA,PA0) #define k_a1l bit_is_clear (PINA,PA1) #define k_a1h bit_is_set (PINA,PA1) #define k_a2l bit_is_clear (PINA,PA2) #define k_a2h bit_is_set (PINA,PA2) #define enc_off bit_is_set (PINA,PA7) #define enc_on bit_is_clear (PINA,PA7) #define inisw_off bit_is_set (PINA,PA6) #define inisw_on bit_is_clear (PINA,PA6) #define LEDR_ON PORTC |= _BV(PC4) #define LEDR_OFF PORTC &=~_BV(PC4) #define LEDG_ON PORTC |= _BV(PC5) #define LEDG_OFF PORTC &=~_BV(PC5) #define LED_M1ON PORTC |= _BV(PC0) #define LED_M1OFF PORTC |= _BV(PC1) #define OUT1_H PORTC |= _BV(PC7) // ukeori motor #define OUT1_L PORTC &=~_BV(PC7) #define OUT2_H PORTC |= _BV(PC6) // ukeori motor #define OUT2_L PORTC &=~_BV(PC6) #define CRT_B1OFF PORTB |= _BV(PB1) #define CRT_B1ON PORTB &=~_BV(PB1) #define CRT_B2OFF PORTB |= _BV(PB2) #define CRT_B2ON PORTB &=~_BV(PB2) #define TEST_ON PORTB |= _BV(PB3) #define TEST_OFF PORTB &=~_BV(PB3) #define CRT_SETOFF PORTB |= _BV(PB4) #define CRT_SETON PORTB &=~_BV(PB4) uint16_t count; //カウンタ uint8_t set_flg; //enc data uint8_t motor_flg; // motor FWD=1 REV=2 STOP=0 uint8_t time_flg; //割り込みスタート 1=start uint8_t keyin_flg; //割り込みスタート 1=start uint16_t c_count; //カウンタ計算 uint16_t buf_count; //カウンタ計算 uint8_t keyin_pa; //Aport uint8_t keysuu; uint8_t kk; uint16_t m1_count; //周波数 M1 uint16_t m2_count; //周波数 ? uint16_t m3_count; //周波数 M1 uint16_t m4_count; //周波数 ? uint16_t m5_count; //周波数 M1 uint16_t m6_count; //周波数 M1 uint16_t m7_count; //周波数 ? uint16_t m8_count; //周波数 M1 uint16_t m9_count; //周波数 M1 uint16_t equ_count; // 一致カウント uint16_t end_count; // 電源切る前の最終カウント uint8_t data_set; //回数 uint8_t data_end; uint8_t wari_flg; // 割り込みのflg uint16_t off_time; //タイマー uint16_t off_count; //タイマーの長さ uint16_t key_dat; //最終のきースイッチ記憶 #define MP1 0x0000 #define MP2 0x0002 #define MP3 0x0004 #define MP4 0x0006 #define MP5 0x0008 #define MP6 0x000A #define MP7 0x000C #define MP8 0x000E #define MP9 0x0010 #define MP10 0x0012 #define MP11 0x0014 //*---------------timer----------------------------- void wait_ms(uint16_t m ) //1=1ms { while(m--) _delay_ms(1); } //----- void wait_us(uint16_t m ) //1=1us { while(m--) _delay_us(1); } //---------- EEPROM 読み出し ------------- void read_rom(void) { eeprom_busy_wait(); m1_count=eeprom_read_word((uint16_t*)MP1); eeprom_busy_wait(); m2_count=eeprom_read_word((uint16_t*)MP2); eeprom_busy_wait(); m3_count=eeprom_read_word((uint16_t*)MP3); eeprom_busy_wait(); m4_count=eeprom_read_word((uint16_t*)MP4); eeprom_busy_wait(); m5_count=eeprom_read_word((uint16_t*)MP5); eeprom_busy_wait(); m6_count=eeprom_read_word((uint16_t*)MP6); eeprom_busy_wait(); m7_count=eeprom_read_word((uint16_t*)MP7); eeprom_busy_wait(); m8_count=eeprom_read_word((uint16_t*)MP8); eeprom_busy_wait(); m9_count=eeprom_read_word((uint16_t*)MP9); } // ******* 書き込み 方法 ********************* void write_rom(void) { eeprom_busy_wait(); eeprom_write_word((uint16_t*)MP1,m1_count); eeprom_busy_wait(); eeprom_write_word((uint16_t*)MP2,m2_count); eeprom_busy_wait(); eeprom_write_word((uint16_t*)MP3,m3_count); eeprom_busy_wait(); eeprom_write_word((uint16_t*)MP4,m4_count); eeprom_busy_wait(); eeprom_write_word((uint16_t*)MP5,m5_count); eeprom_busy_wait(); eeprom_write_word((uint16_t*)MP6,m6_count); eeprom_busy_wait(); eeprom_write_word((uint16_t*)MP7,m7_count); eeprom_busy_wait(); eeprom_write_word((uint16_t*)MP8,m8_count); eeprom_busy_wait(); eeprom_write_word((uint16_t*)MP9,m9_count); } // void read_endrom(void) { eeprom_busy_wait(); end_count=eeprom_read_word((uint16_t*)MP10); eeprom_busy_wait(); key_dat=eeprom_read_word((uint16_t*)MP11); } // ******* 書き込み 方法 ********************* void write_endrom(void) { eeprom_busy_wait(); eeprom_write_word((uint16_t*)MP10,end_count); eeprom_busy_wait(); eeprom_write_word((uint16_t*)MP11,key_dat); } //---------------------------------------------------------- SIGNAL( TIMER1_COMPA_vect) // コンペアマッチA割込が発生したときの処理を行います。 { wari_flg=1; } // メモリタイマー void TIME_START(void) { off_count=off_time; TCCR1B = 0b00001101;OCR1A = 8000; } // WGM12=1 CTCモード  分周=1024} 1秒 void TIME_COUNT(void) { // off_count が 0ならば ストップする wari_flg=0; off_count=off_count-1; if(off_count == 0){ TCCR1B = 0b00000000; end_count=count; key_dat=keyin_flg; write_endrom(); LEDG_ON;LEDR_ON;wait_ms(2000); LEDG_OFF;LEDR_OFF; wari_flg=0; } //stop } void TIME_STOP(void) { off_count=off_time; TCCR1B = 0b00000000; wari_flg=0; } // WGM12=1 CTCモード  分周=1024} 1秒 //--------モータ FWD REV コントロール---------------- void mot_stop(void) { OUT1_L;OUT2_L; }; void mot_fwd(void) { OUT1_L;OUT2_H; }; void mot_rev(void) { OUT1_H;OUT2_L; }; //----- エンコーダー カウント-------------------- void mem_up(void) { while(enc_off){wait_ms(1);} wait_ms(1); while(enc_on){wait_ms(1);} wait_ms(1); while(enc_off){wait_ms(1);} count++; while(enc_on){wait_ms(1);} wait_ms(1); while(enc_off){wait_ms(1);} wait_ms(1); while(enc_on){wait_ms(1);} TEST_ON; crt_count(); TEST_OFF; } void mem_down(void) { while(enc_off){wait_ms(1);} wait_ms(1); while(enc_on){wait_ms(1);} wait_ms(1); while(enc_off){wait_ms(1);} count--; while(enc_on){wait_ms(1);} wait_ms(1); while(enc_off){wait_ms(1);} wait_ms(1); while(enc_on){wait_ms(1);} crt_count(); } //---------------------------------------------------- void crt_count(void) { c_count=count; buf_count=count; if(c_count >= 768){CRT_B1ON;CRT_B2ON;c_count=c_count-768;} if(c_count >= 512){CRT_B1OFF;CRT_B2ON;c_count=c_count-512;} if(c_count >= 256){CRT_B1ON;CRT_B2OFF;c_count=c_count-256;} if(count < 256){CRT_B1OFF;CRT_B2OFF;} PORTD=c_count; } //--------------------------------------------------- void set_menu(void) { data_set=1; CRT_SETON; wait_ms(500); while(1){ if(data_set == 1){ PORTC=data_set; key_in(); if(keysuu == 1){ m1_count=count;LEDG_ON; write_rom(); wait_ms(500); break; } } if(data_set == 2){ PORTC=data_set; key_in(); if(keysuu == 2){ m2_count=count;LEDR_ON; write_rom(); wait_ms(500); break; } } if(data_set == 3){ PORTC=data_set; key_in(); if(keysuu == 3){ m3_count=count;LEDR_ON; write_rom(); wait_ms(500); break; } } if(data_set == 4){ PORTC=data_set; key_in(); if(keysuu == 4){ m4_count=count;LEDR_ON; write_rom(); wait_ms(500); break; } } if(data_set == 5){ PORTC=data_set; key_in(); if(keysuu == 5){ m5_count=count;LEDR_ON; write_rom(); wait_ms(500); break; } } if(data_set == 6){ PORTC=data_set; key_in(); if(keysuu == 6){ m6_count=count;LEDR_ON; write_rom(); wait_ms(500); break; } } if(data_set == 7){ PORTC=data_set; key_in(); if(keysuu == 7){ m7_count=count;LEDR_ON; write_rom(); wait_ms(500); break; } } if(data_set == 8){ PORTC=data_set; key_in(); if(keysuu == 8){ m8_count=count;LEDR_ON; write_rom(); wait_ms(500); break; } } if(data_set == 9){ PORTC=data_set; key_in(); if(keysuu == 9){ m9_count=count;LEDR_ON; write_rom(); wait_ms(500); break; } } if(data_set == 10){ PORTC=data_set; LEDR_ON;LEDG_ON; wait_ms(250); if(k_ret){ while(1){ mot_rev();LEDR_ON; mem_down(); if(inisw_on){count=0; mot_stop(); data_set=0;break; } if(k_menu){mot_stop();count=0;break;} } //while }//k_ret }//data_set10 if(k_menu){while(k_menu){wait_ms(10);} wait_ms(200); data_set++; if(data_set >10){data_set=1;} } if(k_ret){break;} } //while CRT_SETOFF; data_end=0; wait_ms(500); } void equ_mem(void) { while(1){ if(equ_count > count){ mot_fwd();LEDG_ON; mem_up(); } if(k_ret){break;} if(equ_count == count){mot_stop();break;} if(equ_count < count){ mot_rev();LEDR_ON; mem_down(); } if(inisw_on){count=0; mot_stop(); break; } } } //74148 ninsiki key_in //count=0xF2; //kk=count & 0x07; //count=kk ^ 0x07; // void key_in(void) { keysuu=0; if(k_in){ wait_ms(100); if(k_a2l){if(k_a1l){if(k_a0l){keysuu=8;}}} if(k_a2l){if(k_a1l){if(k_a0h){keysuu=7;}}} if(k_a2l){if(k_a1h){if(k_a0l){keysuu=6;}}} if(k_a2l){if(k_a1h){if(k_a0h){keysuu=5;}}} if(k_a2h){if(k_a1l){if(k_a0l){keysuu=4;}}} if(k_a2h){if(k_a1l){if(k_a0h){keysuu=3;}}} if(k_a2h){if(k_a1h){if(k_a0l){keysuu=2;}}} if(k_a2h){if(k_a1h){if(k_a0h){keysuu=1;}}} } if(k_in9){keysuu=9;} wait_ms(200); } /*-----------------------------------------------------------*/ int main () { //初期化 出力=1 入力=0 DDRB = 0x1E; // 0b0001-1110 DDRC = 0xFF; // 0b1111-1111 =out DDRD = 0xFF; // 0b1111-1111 DDRA = 0x00; // IN PORTC = 0x00; // L 出力 PORTB = 0x1E; // PORTD = 0xFF; // L 出力 LEDG_ON; LEDR_OFF; wait_ms(500); LEDG_OFF; LEDR_ON; wait_ms(500); //-----------割り込み --------------------- //タイマ0,CTC,割り込み用、比較A一致で割り込み //TCCR1B |= (1 << WGM12); // タイマ1をCTCモードに設定 OCR1A = 8000; // コンペアマッチAレジスタに8000(1秒)を設定 TIMSK1 |= (1 << OCIE1A); // コンペアマッチA一致で割込を許可します。 TCCR1B = 0b00000000; // 8分周でタイマをストップ。 //TCCR1B = 0b00001101; // WGM12=1 CTCモード  分周=1024 スタート //--------------------------------------- off_time=3; time_flg=0; read_rom(); read_endrom(); count=end_count; // 最終のカウントを思い出す keyin_flg=key_dat; //最終のダイレクトスイッチ PORTC=key_dat; wait_ms(1000); sei(); while(1){ crt_count(); while(k_fwd){ TIME_STOP(); mot_fwd();LEDG_ON; mem_up(); TIME_START(); } //1=FWD mot_stop(); motor_flg=0; LEDG_OFF;LEDR_OFF; while(k_rev){ TIME_STOP(); mot_rev();LEDR_ON; mem_down(); if(inisw_on){count=0; mot_stop(); wait_ms(500);LEDR_OFF; } TIME_START(); } //2=REV mot_stop(); motor_flg=0; LEDG_OFF;LEDR_OFF; if(k_menu){set_menu();} key_in(); keyin_flg=keysuu; if(keyin_flg == 1){ equ_count=m1_count;PORTC=keyin_flg;key_dat=keyin_flg; equ_mem();end_count=count;write_endrom();keyin_flg=0; } if(keyin_flg == 2){ equ_count=m2_count;PORTC=keyin_flg;key_dat=keyin_flg; equ_mem();end_count=count;write_endrom();keyin_flg=0; } if(keyin_flg == 3){ equ_count=m3_count;PORTC=keyin_flg;key_dat=keyin_flg; equ_mem();end_count=count;write_endrom();keyin_flg=0; } if(keyin_flg == 4){ equ_count=m4_count;PORTC=keyin_flg;key_dat=keyin_flg; equ_mem();end_count=count;write_endrom();keyin_flg=0; } if(keyin_flg == 5){ equ_count=m5_count;PORTC=keyin_flg;key_dat=keyin_flg; equ_mem();end_count=count;write_endrom();keyin_flg=0; } if(keyin_flg == 6){ equ_count=m6_count;PORTC=keyin_flg;key_dat=keyin_flg; equ_mem();end_count=count;write_endrom();keyin_flg=0; } if(keyin_flg == 7){ equ_count=m7_count;PORTC=keyin_flg;key_dat=keyin_flg; equ_mem();end_count=count;write_endrom();keyin_flg=0; } if(keyin_flg == 8){ equ_count=m8_count;PORTC=keyin_flg;key_dat=keyin_flg; equ_mem();end_count=count;write_endrom();keyin_flg=0; } if(keyin_flg == 9){ equ_count=m9_count;PORTC=keyin_flg;key_dat=keyin_flg; equ_mem();end_count=count;write_endrom();keyin_flg=0; } if(wari_flg == 1){TIME_COUNT();} } //while(1)終了 return 0; }