// ローテーターコントローラー // ROTE7-A7 2021-1-1 ソフトバージョン 7 // // ATmega88p // ------------------------------------------------- // 内部 Xtal =8M H=DF L=F2 // ヒューズビット  L=E2 // PC1=7SEG PC2=7SEG PC3=7SEG // PD0-PD7 =OUT 7SEG // // PC0 = ADC // // PB6, PB7= LED 動作 //---------------------------------------------------- // 2021-1-1 // ホームは スタートボタンに移行して // 黒SW= 北向け // 白SW= HFの270℃ 9エリア方向へ // //--------------------------------------------- #include #define F_CPU 8000000UL //内部8M #include #include #include // 擬似命令 #define LK1_off PORTC |= _BV(PC3) #define LK1_on PORTC &=~_BV(PC3) #define LK2_off PORTC |= _BV(PC2) #define LK2_on PORTC &=~_BV(PC2) #define LK3_off PORTC |= _BV(PC1) #define LK3_on PORTC &=~_BV(PC1) #define LEFT_OFF PORTC |= _BV(PC4) #define LEFT_ON PORTC &=~_BV(PC4) #define RIGHT_OFF PORTC |= _BV(PC5) #define RIGHT_ON PORTC &=~_BV(PC5) #define a2_ON PORTD |= _BV(PD0) //H #define a2_OFF PORTD &=~_BV(PD0) //L #define b2_ON PORTD |= _BV(PD1) //H #define b2_OFF PORTD &=~_BV(PD1) //L #define c2_ON PORTD |= _BV(PD2) //H #define c2_OFF PORTD &=~_BV(PD2) //L #define d2_ON PORTD |= _BV(PD3) //H #define d2_OFF PORTD &=~_BV(PD3) //L #define e2_ON PORTD |= _BV(PD4) //H #define e2_OFF PORTD &=~_BV(PD4) //L #define f2_ON PORTD |= _BV(PD5) //H #define f2_OFF PORTD &=~_BV(PD5) //L #define g2_ON PORTD |= _BV(PD6) //H #define g2_OFF PORTD &=~_BV(PD6) //L #define LED_GON PORTB |= _BV(PB7) //RIGHT動作用 #define LED_GOFF PORTB &=~_BV(PB7) #define LED_RON PORTB |= _BV(PB6) //LEFT動作用 #define LED_ROFF PORTB &=~_BV(PB6) #define k_left_off bit_is_set (PINB,PB2) #define k_left bit_is_clear (PINB,PB2) #define k_right_off bit_is_set (PINB,PB0) #define k_right bit_is_clear (PINB,PB0) #define k_stop_off bit_is_set (PINB,PB1) #define k_stop bit_is_clear (PINB,PB1) #define k_240_off bit_is_set (PINB,PB3) #define k_240 bit_is_clear (PINB,PB3) #define k_30_off bit_is_set (PINB,PB4) #define k_30 bit_is_clear (PINB,PB4) #define k_set_off bit_is_set (PINB,PB5) #define k_set bit_is_clear (PINB,PB5) uint8_t sidx[] = {0,0,0,0,0,0,0,0,0,0,0,0,0}; // 初期化 sidx[0]=0,sidx[1]=0,sidx[2]=0,sidx[3]=0 uint8_t n_k1; uint8_t n_k2; uint8_t n_k3; volatile uint8_t o_data[12]; //温度データ volatile uint8_t r_data[12]; //読込データ volatile uint8_t vram[10]; uint16_t count; uint16_t count1; uint8_t set_buf; uint16_t adc_dat; //ADC_IN; uint16_t houi_dat; // 0--360 uint16_t shoui_dat; uint8_t houi3; // 3** uint8_t houi2; // *6* uint8_t houi1; // **0 uint16_t dat_off; // オフセット uint16_t dat_240; //240 set uint16_t dat_30; //30 set //------------------------------------------------ enum eSAVE { MP1,MP2,MP3,MP4,MP5,MP6, }; // 7セグ用LEDテーブル -------------------------- unsigned char SEGCHAR[]= { // 0b0GFEDCBA 0b00111111, //0 0b00000110, //1 0b01011011, //2 0b01001111, //3 0b01100110, //4 0b01101101, //5 0b01111101, //6 0b00100111, //7 0b01111111, //8 0b01101111, //9 0b00000000, //null=10 // 0b0GFEDCBA 0b00110111, //M('U'の逆さで) 0b01011100, //o 0b01010100, //n 0b00110001, //T('「'の形で) 0b00011100, //u 0b01111011, //e 0b01111110, //W('A'の逆さで) 0b01011110, //d 0b01110100, //h 0b01110001, //F 0b01010000, //r 0b00010000, //i 0b01101101, //S('5'と同じだが構わない) 0b01011111, //a 0b01111000, //t // 0b0GFEDCBA 0b00100000, //AMのドット 0b00010000, //PMのドット 0b00000001, //A 0b01000000, //- =25(i) }; enum _SIDX { SIDX_NULL = 10, SIDX_M, SIDX_o, SIDX_n, SIDX_T, SIDX_u, SIDX_e, SIDX_W, SIDX_d, SIDX_h, SIDX_F, SIDX_r, SIDX_i, SIDX_S, SIDX_a, SIDX_t, SIDX_AM, SIDX_PM, SIDX_A, SIDX_B, }; //*---------------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_busy_wait(); eeprom_write_byte((uint8_t*)MP1,cou1); // // 例 //if(KEY_RET){ // eeprom_busy_wait(); eeprom_write_byte((uint8_t*)MP1,dat_off); // break;} // //---------- EEPROM 読み出し ------------- void read_rom(void) { eeprom_busy_wait(); dat_off=eeprom_read_word((uint16_t*)MP1); eeprom_busy_wait(); dat_240=eeprom_read_word((uint16_t*)MP3); eeprom_busy_wait(); dat_30=eeprom_read_word((uint16_t*)MP5); } // ---------- ADC ------------------------ void ADC_IN (void) { ADCSRA |= _BV(ADSC); //変換開始要ループ内 loop_until_bit_is_set(ADCSRA,ADIF); //変換終了時ADIFがセットされる // a = ADC; //AD変換結果 10bit:0-1024 adc_dat = ADC; //AD変換結果 4bitシフト= 6bit:0-64 //shoui_dat = (adc_dat*36)/102.4; shoui_dat = (adc_dat*35)/102.4; //補正 houi_dat = shoui_dat+dat_off; if(houi_dat >= 360){houi_dat=houi_dat-360;} SET_7SEG(); // houi_dat にいれて飛ばす } // --------------SET_7SEG--------------- // houi_dat に入れる // void SET_7SEG(void) { houi3=houi_dat/100; houi2=(houi_dat-(houi3*100))/10; houi1=houi_dat-(houi3*100)-(houi2*10); sidx[0] = houi1; sidx[1] = houi2; sidx[2] = houi3; } // 7SEG 表示 --------------------- void HYOU_7SEG(void) { PORTD = SEGCHAR[sidx[0]]; LK1_on; wait_ms(5); LK1_off; PORTD = SEGCHAR[sidx[1]]; LK2_on; wait_ms(5); LK2_off; PORTD = SEGCHAR[sidx[2]]; LK3_on; wait_ms(5); LK3_off; } // Data set ------------------------ void OFFSET_SET(void) { //dat_off OFFSET-data while (1){ LED_GON;LED_RON; // OFFSET houi_dat=dat_off;SET_7SEG(); HYOU_7SEG(); if(k_right){dat_off++; if(dat_off >= 360){dat_off=0;} houi_dat=dat_off;SET_7SEG(); HYOU_7SEG(); HYOU_7SEG(); HYOU_7SEG(); HYOU_7SEG(); HYOU_7SEG(); HYOU_7SEG(); } if(k_left){dat_off--; if(dat_off <= 0){dat_off=360;} houi_dat=dat_off;SET_7SEG(); HYOU_7SEG(); HYOU_7SEG(); HYOU_7SEG(); HYOU_7SEG(); HYOU_7SEG(); HYOU_7SEG(); } if (k_stop){LED_GOFF;LED_ROFF; eeprom_busy_wait(); eeprom_write_word((uint16_t*)MP1,dat_off); wait_ms(1000); LED_GON;LED_RON; wait_ms(1000); LED_GOFF;LED_ROFF; break;; } } //while end //------- 白キーセット------------------------ // while(1){ LED_RON;// 240set houi_dat=dat_240;SET_7SEG(); HYOU_7SEG(); if(k_right){dat_240++; if(dat_240 >= 360){dat_240=0;} houi_dat=dat_240;SET_7SEG(); HYOU_7SEG(); HYOU_7SEG(); HYOU_7SEG(); HYOU_7SEG(); HYOU_7SEG(); HYOU_7SEG(); } if(k_left){dat_240--; if(dat_240 <= 0){dat_240=0;} houi_dat=dat_240;SET_7SEG(); HYOU_7SEG(); HYOU_7SEG(); HYOU_7SEG(); HYOU_7SEG(); HYOU_7SEG(); HYOU_7SEG(); } if (k_stop){LED_GOFF;LED_ROFF; eeprom_busy_wait(); eeprom_write_word((uint16_t*)MP3,dat_240); wait_ms(1000); LED_GON;LED_RON; wait_ms(1000); LED_GOFF;LED_ROFF; break; } } //while end // //---黒キーセット------------------- // while(1){ LED_GON; // 30set houi_dat=dat_30;SET_7SEG(); HYOU_7SEG(); if(k_right){dat_30++; if(dat_30 >= 0){dat_30=0;} houi_dat=dat_30;SET_7SEG(); HYOU_7SEG(); HYOU_7SEG(); HYOU_7SEG(); HYOU_7SEG(); HYOU_7SEG(); HYOU_7SEG(); } if(k_left){dat_30--; if(dat_30 <= 0){dat_30=360;} houi_dat=dat_30;SET_7SEG(); HYOU_7SEG(); HYOU_7SEG(); HYOU_7SEG(); HYOU_7SEG(); HYOU_7SEG(); HYOU_7SEG(); } if (k_stop){LED_GOFF;LED_ROFF; eeprom_busy_wait(); eeprom_write_word((uint16_t*)MP5,dat_30); wait_ms(1000); LED_GON;LED_RON; wait_ms(1000); LED_GOFF;LED_ROFF; break; } } //while end LED_GON; wait_ms(200); LED_GOFF; wait_ms(200); LED_GON; wait_ms(200); LED_GOFF; wait_ms(200); LED_GON; wait_ms(200); LED_GOFF; wait_ms(200); LED_GON; wait_ms(200); LED_GOFF; wait_ms(200); LED_RON; wait_ms(200); LED_ROFF; wait_ms(200); LED_RON; wait_ms(200); LED_ROFF; wait_ms(200); LED_RON; wait_ms(200); LED_ROFF; wait_ms(200); LED_RON; wait_ms(200); LED_ROFF; wait_ms(200); }//offset_set end //----------------------------------------------------------- int main () { DDRB = 0xC0; // 0b1100-0000 DDRC = 0xFE; // 0b0011-1110 DDRD = 0x7F; // 0b0111-1111 PORTD = 0x00; // L 出力 PORTC = 0x3E; // H 出力 //--------------------------------------------- ADCSRA = 0b10000011; // ADEN,ADSC,ADATE,ADIF,ADIE,ADPS2,ADPS1,ADPS0 // ADEN許可:1 ADSC開始:0 ADATE自動起動:0 ADIF完了割込:0 // ADIE割込許可:0 分周ck/8 ADMUX = 0x40; //AVCC基準電圧、入力PC0 データ右揃え ADCSRB = 0x00; // 連続変換動作 =0 LED_GON; wait_ms(500); LED_GOFF; wait_ms(500); LED_RON; wait_ms(500); LED_ROFF; count1=0; // setkey 長押し set_buf=0; // 0= offset data 1= 270set 2=30set read_rom(); //dat_off=0;dat_30=120;dat_240=120; houi_dat=7; //ソフトバージョン 7 HYOU_7SEG(); wait_ms(2000); ADC_IN(); HYOU_7SEG(); while(1){ if(k_right){RIGHT_ON;LED_RON; HYOU_7SEG(); HYOU_7SEG(); HYOU_7SEG(); HYOU_7SEG(); HYOU_7SEG(); HYOU_7SEG(); HYOU_7SEG(); RIGHT_OFF;LED_ROFF; } if(k_left){LEFT_ON;LED_GON; HYOU_7SEG(); HYOU_7SEG(); HYOU_7SEG(); HYOU_7SEG(); HYOU_7SEG(); HYOU_7SEG(); HYOU_7SEG(); LEFT_OFF;LED_GOFF; } if(k_stop){LEFT_OFF;RIGHT_OFF;LED_GOFF; LED_GOFF;LED_ROFF;wait_ms(100); } count++; if(count == 50){ADC_IN();count=0;} HYOU_7SEG(); if (k_set){count1++; if(count1 == 100){count1=0; OFFSET_SET();} } // shoui_dat 生の方位 // houi_dat 生の方位+オフセット if(k_240){ //白キー if(shoui_dat >= dat_240){LEFT_ON;LED_RON;set_buf=10;} if(shoui_dat <= dat_240){RIGHT_ON;LED_RON;set_buf=11;} } if(set_buf == 10){ if(shoui_dat <= dat_240){LEFT_OFF;LED_GOFF;LED_ROFF;set_buf=0;} } if(set_buf == 11){ if(shoui_dat >= dat_240){RIGHT_OFF;LED_GOFF;LED_ROFF;set_buf=0;} } if(k_30){ //黒キー  if(shoui_dat >= dat_30){LEFT_ON;LED_GON;set_buf=20;} if(shoui_dat <= dat_30){RIGHT_ON;LED_GON;set_buf=21;} } if(set_buf == 20){ if(shoui_dat <= dat_30){LEFT_OFF;LED_GOFF;LED_ROFF;set_buf=0;} } if(set_buf == 21){ if(shoui_dat >= dat_30){RIGHT_OFF;LED_GOFF;LED_ROFF;set_buf=0;} } } //while(1)終了 nn return 0; }