ΠΡΠΎΠ³ΡΠ°ΠΌΠΌΠΈΡΠΎΠ²Π°Π½ΠΈΠ΅ ΠΌΠΈΠΊΡΠΎΠΊΠΎΠ½ΡΡΠΎΠ»Π»Π΅ΡΠΎΠ². ΠΠΈΠΊΡΠΎΠΊΠΎΠ½ΡΡΠΎΠ»Π»Π΅Ρ: AVR ATMega. Π€ΡΠ½ΠΊΡΠΈΠΈ ΠΈ Π·Π°Π΄Π°ΡΠ° ΡΡΡΡΠΎΠΉΡΡΠ²Π°: ΠΠ±ΡΡΡΠ½ΠΈΡΡ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΡ. /***************************************************** Chip type : ATmega8 Program type : Application AVR Core Clock frequency: 8,000000 MHz Memory model : Small External RAM size : 0 Data Stack size : 256 *****************************************************/ #include #include #define ON 1 #define OFF 0 #define timer_ TCNT0 #define timer_tact TCCR0 #define OUT PORTB.0 #define enable PINB.3 #define up PINB.4 #define down PINB.5 #define pwm__ OCR1AL #define klapan PORTB.2 #define motor_stop {PORTB.1=0;} #define motor_tormoz {PORTD.4=1;} #define motor_tormoz_off {PORTD.4=0;} bit flags_button=0; volatile unsigned char sek_; volatile unsigned char timer_button, timer_butt; volatile unsigned char timer_shou; volatile unsigned char timer_stop; volatile unsigned char eep_timer; volatile unsigned char sek; volatile unsigned char timer; volatile unsigned char y,jamp2=0; volatile unsigned int pwm, start_; volatile unsigned char pwm_, data0, data1, data2, led_buff[3]; //--------------- 0-9 10 ........... 18 ΡΠΈΠΌΠ²ΠΎΠ»ΠΎΠ² //Π‘ΠΈΠΌΠ²ΠΎΠ»Ρ Π΄Π»Ρ LED (0-9, ,-,t,L,A,r,d,b,DP) flash unsigned char led_table[26]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf,0x87,0xC7,0x88,0xAF,0xA1,0x83,0x7F,0x8C,0xC6,0xC1,0x86,0x89,0x8E,0x9C}; // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 // [] - t L A r d b Dp P C U E H F Π³ΡΠ°Π΄. //volatile unsigned char y, jamp2=0; volatile unsigned char start=180; eeprom unsigned char eep_power, eep_jamp, eep_pwm, eep_sek; // External Interrupt 0 service routine interrupt [EXT_INT0] void ext_int0_isr(void) // ΠΏΡΠ΅ΡΡΠ²Π°Π½ΠΈΠ΅ Π΄Π»Ρ Π·Π°Ρ
Π²Π°ΡΠ° ΠΏΠ΅ΡΠ΅Ρ
ΠΎΠ΄Π° ΡΠ΅ΡΠ΅Π· Π½ΠΎΠ»Ρ. { // Place your code here OUT=OFF; timer_=start; } // Timer 0 overflow interrupt service routine interrupt [TIM0_OVF] void timer0_ovf_isr(void) // ΠΏΡΠ΅ΡΡΠ²Π°Π½ΠΈΠ΅ Π΄Π»Ρ Π²ΡΡ
ΠΎΠ΄Π° ΡΠΏΡΠ°Π²Π»Π΅Π½ΠΈΡ ΡΠΈΡΠΈΡΡΠΎΡΠ°ΠΌΠΈ { // Place your code here timer_=start; OUT=ON; } // Timer2 overflow interrupt service routine interrupt [TIM2_OVF] void timer2_ovf_isr(void) // ΠΏΡΠ΅ΡΡΠ²Π°Π½ΠΈΠ΅ Π΄Π»Ρ Π΄ΠΈΡΠΏΠ»Π΅Ρ { // Reinitialize Timer2 value TCNT2=0xCB; if (jamp2==0) { PORTD.5=PORTD.6=PORTD.7=0; // Π²ΡΠΊΠ»ΡΡΠ°Π΅ΠΌ Π²ΡΠ΅ ΡΠ°Π·ΡΡΠ΄Ρ switch (y) // ΠΎΠ±ΡΠΈΠΉ Π°Π½ΠΎΠ΄ { case 0: PORTC = 0x3F & led_buff[0]; PORTD.0 = 0x40 & led_buff[0]; PORTD.1 = 0x80 & led_buff[0]; PORTD.5=1; PORTD.6=0; PORTD.7=0; break; case 1: PORTC = 0x3F & led_buff[1]; PORTD.0 = 0x40 & led_buff[1]; PORTD.1 = 0x80 & led_buff[1]; PORTD.5=0; PORTD.6=1; PORTD.7=0; break; case 2: PORTC = 0x3F & led_buff[2]; PORTD.0 = 0x40 & led_buff[2]; PORTD.1 = 0x80 & led_buff[2]; PORTD.5=0; PORTD.6=0; PORTD.7=1; break; default: } } else // ΠΎΠ±ΡΠΈΠΉ ΠΊΠ°ΡΠΎΠ΄ { PORTD.5=PORTD.6=PORTD.7=1; // Π²ΡΠΊΠ»ΡΡΠ°Π΅ΠΌ Π²ΡΠ΅ ΡΠ°Π·ΡΡΠ΄Ρ data0 = ~led_buff[0]; data1 = ~led_buff[1]; data2 = ~led_buff[2]; switch (y) { case 0: PORTC = 0x3F & data0; PORTD.0 = 0x40 & data0; PORTD.1 = 0x80 & data0; PORTD.5=0; PORTD.6=1; PORTD.7=1; break; case 1: PORTC = 0x3F & data1; PORTD.0 = 0x40 & data1; PORTD.1 = 0x80 & data1; PORTD.5=1; PORTD.6=0; PORTD.7=1; break; case 2: PORTC = 0x3F & data2; PORTD.0 = 0x40 & data2; PORTD.1 = 0x80 & data2; PORTD.5=1; PORTD.6=1; PORTD.7=0; break; default: } } if (++y>2)y=0; } /////===========================================////// void enable_() { if (enable==0) // Π½Π°ΠΆΠΈΠΌΠ°Π΅ΠΌ Π½Π° ΠΊΠ½ΠΎΠΏΠΊΡ { klapan=1; pwm__=pwm_; // ΠΊΠΎΠΏΠΈΡΡΠ΅ΠΌ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ Π² ΡΠ΅Π³ΠΈΡΡΡ motor_tormoz_off; if (++timer > sek) { TCCR1B=0x02; // ΡΠ°ΡΡΠΎΡΠ° Π¨ΠΠ Π΄Π²ΠΈΠ³Π°ΡΠ΅Π»Ρ timer_tact=0x05; // Π²ΠΊΠ»ΡΡΠ°Π΅ΠΌ ΡΠ²Π°ΡΠΊΡ timer=sek+1; } else { TCCR1B=0; } timer_stop=0; // ΡΠ°ΠΉΠΌΠ΅Ρ ΡΠΎΡΠΌΠΎΠΆΠ΅Π½ΠΈΡ Π΄Π²ΠΈΠ³Π°ΡΠ΅Π»Ρ } if (enable==1) { pwm__=0; // ΡΠ±ΡΠ°ΡΡΠ²Π°Π΅ΠΌ ΡΠ΅Π³ΠΈΡΡΡ Π¨ΠΠ delay_ms(5); TCCR1B=OFF; motor_stop; timer=timer_tact=OFF; // ΠΎΡΡΠ°Π½Π°Π²Π»ΠΈΠ²Π°Π΅ΠΌ ΡΠ°ΠΉΠΌΠ΅Ρ OUT=OFF; // Π²ΡΠΊΠ»ΡΡΠ°Π΅ΠΌ Π²ΡΡ
ΠΎΠ΄ timer=0; klapan=0; if (++timer_stop > 3) // ΡΠ°ΠΉΠΌΠ΅Ρ ΡΠΎΡΠΌΠΎΠΆΠ΅Π½ΠΈΡ Π΄Π²ΠΈΠ³Π°ΡΠ΅Π»Ρ. { motor_tormoz; timer_stop=3; } } } void power_control() { start_ = start; //start_ *= 100; //start_ /= 177; start_ -= 154; if(start_ < 10) { led_buff[0]=led_table[start_]; // led_buff[1]=led_table[10]; led_buff[2]=led_table[10]; } if(start_ > 9 && start_ < 100) { led_buff[1]=led_table[start_%10]; // start_ = start_/10; led_buff[0]=led_table[start_%10]; led_buff[2]=led_table[10]; } if (start_ > 99 && start_ <= 255) { led_buff[2]=led_table[start_%10]; // start_ = start_/10; led_buff[1]=led_table[start_%10]; start_ = start_/10; led_buff[0]=led_table[start_%10]; } if (up==0 && down && timer_butt<1) // ΡΠ²Π΅Π»ΠΈΡΠΈΠ²Π°Π΅ΠΌ ΡΠΎΠΊ Π½Π° ΠΎΠ΄ΠΈΠ½ ΡΠ°Π³ { if(start<254) { start++; } } if (up==0 && down && ++timer_butt>8) // Π±ΡΡΡΡΠΎΠ΅ ΡΠ²Π΅Π»ΠΈΡΠ΅Π½ΠΈΠ΅ ΡΠΎΠΊΠ° { if(start<254) { start++; } timer_butt=9; } if (down==0 && up && ++timer_butt>8) // Π±ΡΡΡΡΠΎΠ΅ ΡΠΌΠ΅Π½ΡΡΠ΅Π½ΠΈΠ΅ ΡΠΎΠΊΠ° { if (start>154) { start--; } timer_butt=9; } if (down==0 && up && timer_butt<2) // ΡΠΌΠ΅Π½ΡΡΠ°Π΅ΠΌ ΡΠΎΠΊ Π½Π° ΠΎΠ΄ΠΈΠ½ ΡΠ°Π³ { if (start>154) { start--; } } } void pwm_control() { pwm = pwm_; pwm = pwm * 99; pwm = pwm / 255; if(pwm < 10) { led_buff[1]=led_table[pwm]; // led_buff[0]=led_table[19]; led_buff[2]=led_table[10]; } if(pwm > 9 && pwm < 100) { led_buff[2]=led_table[pwm%10]; // pwm = pwm/10; led_buff[1]=led_table[pwm%10]; led_buff[0]=led_table[19]; } if (up==0 && down && timer_button==0) { pwm_+=2; eep_timer=0; timer_shou=80; flags_button=1; } if (up==0 && down) { if (++timer_button > 8) { pwm_+=2; eep_timer=0; delay_ms(75); timer_button=9; flags_button=1; } timer_shou=80; } if (up && down==0 && timer_button==0) { pwm_-=2; eep_timer=0; flags_button=1; timer_shou=80; } if (up && down==0) { if (++timer_button > 8) { pwm_-=2; eep_timer=0; delay_ms(75); timer_button=9; flags_button=1; } timer_shou=80; } } void motor_delay() // ΡΡΠ½ΠΊΡΠΈΡ Π·Π°Π΄Π΅ΡΠΆΠΊΠΈ ΠΏΠΎΠ΄Π°ΡΠΈ ΠΏΡΠΎΠ²ΠΎΠ»ΠΎΠΊΠΈ. { sek_ = sek; if(sek_ < 10) { led_buff[0]=led_table[12]; // led_buff[1]=led_table[sek_]; led_buff[2]=led_table[10]; } if(sek_ > 9 && sek_ < 100) { led_buff[2]=led_table[sek_%10]; // sek_ = sek_/10; led_buff[1]=led_table[sek_%10]; led_buff[0]=led_table[12]; } if (up==0 && down && timer_button==0) { if(sek<99) sek++; else sek=2; timer_shou=100; eep_timer=0; flags_button=1; timer_shou=160; } if (up==0 && down) { if(++timer_button > 8) { if(sek<99) sek++; else sek=2; timer_shou=100; delay_ms(75); eep_timer=0; timer_button=9; flags_button=1; } timer_shou=160; } if (up && down==0 && timer_button==0) { sek--; timer_shou=100; if (sek<2) sek=99; eep_timer=0; flags_button=1; timer_shou=160; } if (up && down==0) { if (++timer_button > 8) { sek--; timer_shou=100; if (sek<2) sek=99; eep_timer=0; delay_ms(75); timer_button=9; flags_button=1; } timer_shou=160; } } void button_enable() { if (up && down) { flags_button=0; timer_button=0; timer_butt=0; } if (up==0 && down==0 && flags_button==0 && timer_shou==0) // ΠΊΠ½ΠΎΠΏΠΊΠ° ΠΌΠ΅Π½Ρ { timer_shou=80; flags_button=1; } if (up==0 && down==0 && flags_button==0 && (timer_shou>0 && timer_shou<81)) // ΠΏΠ΅ΡΠ΅Ρ
ΠΎΠ΄ Π² ΠΌΠ΅Π½Ρ ΡΠ΅Π³ΡΠ»ΠΈΡΠΎΠ²ΠΊΠΈ ΠΏΠΎΠ΄Π°ΡΠΈ ΠΏΡΠΎΠ²ΠΎΠ»ΠΎΠΊΠΈ { timer_shou=160; flags_button=1; } if (up==0 && down==0 && flags_button==0 && (timer_shou>80 && timer_shou<161)) // Π²ΠΎΠ·Π²ΡΠ°Ρ Π² Π³Π»Π°Π²Π½ΠΎΠ΅ ΠΌΠ΅Π½Ρ { timer_shou=0; flags_button=1; } if (timer_shou>0) // Π²ΠΎΠ·Π²ΡΠ°Ρ Π² Π³Π»Π°Π²Π½ΠΎΠ΅ ΠΌΠ΅Π½Ρ { timer_shou--; } if (timer_shou == 81) // Π²ΠΎΠ·Π²ΡΠ°Ρ Π² Π³Π»Π°Π²Π½ΠΎΠ΅ ΠΌΠ΅Π½Ρ { timer_shou=0; } if (sek > 100) sek=99; delay_ms(50); } void eep_save() { if (eep_timer < 200) { eep_timer++; } if (eep_timer > 197 && eep_timer < 199) { #asm (“cli“); delay_ms(2); eep_sek = sek; eep_pwm = pwm_; eep_power = start; #asm (“sei“); } } /////===========================================////// // Declare your global variables here void main(void) { // Declare your local variables here // Input/Output Ports initialization // Port B initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=Out Func1=Out Func0=Out // State7=T State6=T State5=P State4=P State3=P State2=0 State1=0 State0=0 PORTB=0x38; DDRB=0x07; // Port C initialization // Func6=In Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out // State6=T State5=0 State4=0 State3=0 State2=0 State1=0 State0=0 PORTC=0x00; DDRC=0x3F; // Port D initialization // Func7=Out Func6=Out Func5=Out Func4=In Func3=Out Func2=In Func1=Out Func0=Out // State7=0 State6=0 State5=0 State4=T State3=0 State2=P State1=0 State0=0 PORTD=0x04; DDRD=0xFB; // Timer/Counter 0 initialization // Clock source: System Clock // Clock value: 7,813 kHz TCCR0=0x00; // ΡΠ°ΠΉΠΌΠ΅Ρ Π΄Π»Ρ ΡΠΏΡΠ°Π²Π»Π΅Π½ΠΈΡ Π²ΡΡ
ΠΎΠ΄ΠΎΠΌ Π½Π° ΡΠΈΡΠΈΡΡΠΎΡΡ TCNT0=0x00; // Timer/Counter 1 initialization // Clock source: System Clock // Clock value: 1000,000 kHz // Mode: Fast PWM top=OCR1A // OC1A output: Toggle // OC1B output: Discon. // Noise Canceler: Off // Input Capture on Falling Edge // Timer1 Overflow Interrupt: Off // Input Capture Interrupt: Off // Compare A Match Interrupt: Off // Compare B Match Interrupt: Off TCCR1A=0x81; // ΡΠ°ΠΉΠΌΠ΅Ρ1 Π΄Π»Ρ ΡΠΎΡΠΌΠΈΡΠΎΠ²Π°Π½ΠΈΡ Π¨ΠΠ ΠΏΠΎΠ΄Π°ΡΠΈ ΠΏΡΠΎΠ²ΠΎΠ»ΠΎΠΊΠΈ TCCR1B=0x03; TCNT1H=0x00; TCNT1L=0x00; ICR1H=0x00; ICR1L=0x00; OCR1AH=0x00; OCR1AL=0x00; OCR1BH=0x00; OCR1BL=0x00; // Timer/Counter 2 initialization // Clock source: System Clock // Clock value: 7,813 kHz // Mode: Normal top=0xFF // OC2 output: Disconnected ASSR=0x00; TCCR2=0x07; TCNT2=0xCB; OCR2=0x00; // External Interrupt(s) initialization // INT0: On // INT0 Mode: Falling Edge // INT1: On // INT1 Mode: Low level GICR|=0x70; MCUCR=0x01; GIFR=0xC0; // Timer(s)/Counter(s) Interrupt(s) initialization TIMSK=0x41; // USART initialization // USART disabled UCSRB=0x00; // Analog Comparator initialization // Analog Comparator: Off // Analog Comparator Input Capture by Timer/Counter 1: Off ACSR=0x80; SFIOR=0x00; // ADC initialization // ADC disabled ADCSRA=0x00; // SPI initialization // SPI disabled SPCR=0x00; // TWI initialization // TWI disabled TWCR=0x00; // Global enable interrupts #asm(“sei“) jamp2 = eep_jamp; led_buff[0]=led_table[11]; // led_buff[1]=led_table[10]; led_buff[2]=led_table[10]; delay_ms(1000); led_buff[0]=led_table[11]; // led_buff[1]=led_table[11]; led_buff[2]=led_table[10]; delay_ms(1000); led_buff[0]=led_table[11]; // led_buff[1]=led_table[11]; led_buff[2]=led_table[11]; delay_ms(1000); led_buff[0]=led_table[10]; // led_buff[1]=led_table[10]; led_buff[2]=led_table[10]; delay_ms(1000); if (up==0 && down) { led_buff[0]=led_table[19]; // led_buff[1]=led_table[11]; led_buff[2]=led_table[20]; jamp2 = 1; eep_jamp = jamp2; delay_ms(3000); } if (up==0 && down==0) { led_buff[0]=led_table[19]; // led_buff[1]=led_table[11]; led_buff[2]=led_table[14]; jamp2 = 0; eep_jamp = jamp2; delay_ms(3000); } #asm (“cli“); delay_ms(2); pwm_ = eep_pwm; if (eep_sek > 100) { eep_sek=10; } if (eep_power < 154 || eep_power > 254) { eep_power=155; } sek = eep_sek; start = eep_power; #asm(“sei“) while (1) { // Place your code here enable_(); if (timer_shou==0) { power_control(); } if (timer_shou > 0 && timer_shou < 81) { pwm_control(); } if (timer_shou > 80 && timer_shou < 161) { motor_delay(); } button_enable(); eep_save(); } }.