{"id":161951,"date":"2012-12-09T15:11:03","date_gmt":"2012-12-09T11:11:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=161951"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=161951","title":{"rendered":"<span class=\"post_title\">Ping-Pong \u043d\u0430 AVR<\/span>"},"content":{"rendered":"<div class=\"content html_format\">   \t\u0412 \u043e\u0447\u0435\u0440\u0435\u0434\u043d\u0443\u044e \u0443\u0431\u043e\u0440\u043a\u0443 \u0432 \u043c\u0430\u0441\u0442\u0435\u0440\u0441\u043a\u043e\u0439 \u0434\u043e\u0432\u0435\u043b\u043e\u0441\u044c \u043c\u043d\u0435 \u043d\u0430\u0442\u043a\u043d\u0443\u0442\u044c\u0441\u044f \u043d\u0430 \u0434\u0432\u0430 \u043c\u0430\u0442\u0440\u0438\u0447\u043d\u044b\u0445 \u0438\u043d\u0434\u0438\u043a\u0430\u0442\u043e\u0440\u0430 8\u04458, \u0442\u0430\u043c \u0436\u0435 \u0431\u044b\u043b\u0430 \u043d\u0430\u0439\u0434\u0435\u043d\u0430 Mega16, \u043a\u043d\u043e\u043f\u043a\u0438, \u043c\u0430\u043a\u0435\u0442\u043a\u0430. \u00ab\u041a\u0430\u043a\u043e\u0439 \u0436\u0435 \u0442\u044b \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442, \u0435\u0441\u043b\u0438 \u0437\u0430 \u0441\u0432\u043e\u044e \u0436\u0438\u0437\u043d\u044c \u043d\u0438 \u0440\u0430\u0437\u0443 \u043d\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u0422\u0435\u0442\u0440\u0438\u0441?\u00bb, \u0441\u043f\u0440\u043e\u0441\u0438\u043b \u043c\u043e\u0437\u0433.<br \/>  \u041a\u043e\u043d\u0435\u0446 \u0434\u043d\u044f \u0431\u044b\u043b \u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d \u0442\u043e\u043c\u0443, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0432\u0435\u0441\u043e\u043c \u0441\u043f\u0430\u044f\u0442\u044c \u044d\u0442\u043e \u0432\u0441\u0435 \u0445\u043e\u0437\u044f\u0439\u0441\u0442\u0432\u043e.<br \/>  <img decoding=\"async\" src=\"http:\/\/s020.radikal.ru\/i703\/1212\/00\/b63f8ffe5011.jpg\" alt=\"image\"\/><\/p>\n<p>  \u0421\u0445\u0435\u043c\u0430 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<br \/>  <img decoding=\"async\" src=\"http:\/\/s017.radikal.ru\/i440\/1212\/1a\/b3a60b057432.jpg\" alt=\"image\"\/><\/p>\n<p>  \u041a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e \u0440\u0443\u043a\u0438 \u0434\u043e \u0422\u0435\u0442\u0440\u0438\u0441\u0430 \u043f\u043e\u043a\u0430 \u043d\u0435 \u0434\u043e\u0448\u043b\u0438, \u0440\u0430\u0437\u043c\u044f\u043b\u0441\u044f \u0442\u0435\u043c, \u0447\u0442\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u041f\u0438\u043d\u0433-\u041f\u043e\u043d\u0433.<br \/>  \u0412\u0438\u0434\u0435\u043e \u0440\u0430\u0431\u043e\u0442\u044b \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0442\u0443\u0442. <a href=\"http:\/\/vk.com\/video14260049_163193346\">\u0412\u0438\u0434\u0435\u043e<\/a><br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<pre><code class=\"cpp\">\/***************************************************** Chip type               : ATmega16 Program type            : Application AVR Core Clock frequency: 8,000000 MHz Memory model            : Small External RAM size       : 0 Data Stack size         : 256 *****************************************************\/  #include &lt;mega16.h&gt; #include &lt;delay.h&gt; #include &lt;math.h&gt; #include &lt;stdlib.h&gt;  #include &lt;stdio.h&gt;  \/\/ Declare your global variables here int cnt = 0; char src = 0, start = 0, tmp = 0, cx = 3, cy = 14, mx = 2, my = 15, ex = 3, ey = 0, dx = 1, dy = 1, fld[16][8], speed = 200; char flds[16][8] = {{0,1,1,1,1,1,1,0},   \/\/ 0                    {1,0,0,0,0,0,0,1},   \/\/ 1                    {1,0,1,0,0,1,0,1},   \/\/ 2                    {1,0,0,0,0,0,0,1},   \/\/ 3                    {1,0,1,0,0,1,0,1},   \/\/ 4                    {1,0,0,1,1,0,0,1},   \/\/ 5                    {0,1,0,0,0,0,1,0},   \/\/ 6                    {0,0,1,1,1,1,0,0},   \/\/ 7                                        {0,0,0,0,0,0,0,0},   \/\/ 8                    {0,0,0,0,0,0,0,0},   \/\/ 9                    {0,0,0,0,0,0,0,0},   \/\/ 10                    {0,0,0,0,0,0,0,0},   \/\/ 11                    {0,0,0,0,0,0,0,0},   \/\/ 12                    {0,0,0,0,0,0,0,0},   \/\/ 13                    {0,0,0,0,0,0,0,0},   \/\/ 14                    {0,0,0,0,0,0,0,0}};  \/\/ 15                        void showsrc(void) { char x = 0, y = 0;     src = 0, start = 0, tmp = 0, cx = 3, cy = 14, mx = 2, my = 15, ex = 3, ey = 0, dx = 1, dy = 1, speed = 200;     while (y &lt; 16) {          x = 0;         while (x &lt; 8) {             fld[y][x] = flds[y][x];             x++;         }         y++;     } }                                \/\/ Timer 0 overflow interrupt service routine interrupt [TIM0_OVF] void timer0_ovf_isr(void) { \/\/ Place your code here     if (cnt &gt;= speed) {         if (start == 0)          {                    fld[9][5]  = 0; fld[10][5] = 0;             fld[11][5] = 0; fld[12][5] = 0;              fld[12][3] = 0; fld[12][7] = 0;             fld[13][3] = 0; fld[13][5] = 0;             fld[13][7] = 0; fld[13][4] = 0;             fld[13][6] = 0; fld[14][4] = 0;             fld[14][5] = 0; fld[14][6] = 0;             fld[15][5] = 0;              if (tmp == 0) {                  fld[10][5] = 1; fld[11][5] = 1;                 fld[12][5] = 1; fld[13][3] = 1;                 fld[13][5] = 1; fld[13][7] = 1;                 fld[14][4] = 1; fld[14][5] = 1;                 fld[14][6] = 1; fld[15][5] = 1;                        }             else {                 fld[9][5]  = 1; fld[10][5] = 1;                 fld[11][5] = 1; fld[12][3] = 1;                 fld[12][5] = 1; fld[12][7] = 1;                 fld[13][4] = 1; fld[13][5] = 1;                 fld[13][6] = 1; fld[14][5] = 1;                   };             tmp++;             if (tmp == 2) tmp = 0;          }         else \/\/game cycle         {                fld[cy][cx] = 0;             if (dy == 1) {                 if (cy &gt; 0) cy--;                  else {tmp = 0; start = 0; showsrc();}             }             else {                 if (cy &lt; 15) cy ++;                 else {tmp = 0; start = 0; showsrc();}             }                              if ((cy == 1) && (fld[0][cx] == 1)) dy = 2;             if ((cy == 1) && (fld[0][cx] == 0)) {tmp = 0; start = 0; showsrc();}               if ((cy == 14) && (fld[15][cx] == 1)) dy = 1;             if ((cy == 14) && (fld[0][cx] == 0)) {tmp = 0; start = 0; showsrc();}              if (start == 1) {                            if (dx == 1) {                     cx--;                     if (cx == 0) {dx = 2;}                  }                 else {                     cx++;                     if (cx == 7) {dx = 1;}                 }                 fld[cy][cx] = 1;                       if (PINC.7 == 0) {                     delay_ms(1);                     if (PINC.7 == 0) {                         if ((mx &gt; 0) && (mx != 0)) {mx--; fld[my][mx+3] = 0;}                     }                 }                     if (PINC.6 == 0) {                     delay_ms(1);                     if (PINC.6 == 0){                          if ((mx &lt; 5) && (mx != 5)) {mx++; fld[my][mx-1] = 0;}                     }                   }                  ex = cx;                 if (cx &gt; 5) ex = 5;                  if (ex &gt; 0) fld[ey][ex-1] = 0;                  if (ex &lt; 5) fld[ey][ex+3] = 0;                 fld[my][mx] = 1; fld[my][mx+1] = 1; fld[my][mx+2] = 1;                  fld[ey][ex] = 1; fld[ey][ex+1] = 1; fld[ey][ex+2] = 1;             }                              }      cnt = 0;         }     cnt++; }  void paint(void) { char x = 0;     while (x &lt;= 7) {         PORTA = 0b00000000;         PORTB = 0b00000000;         PORTD = 0b11111111;         if (src == 0) {             PORTA.0 = fld[0][x];  PORTA.1 = fld[1][x];             PORTA.2 = fld[2][x];  PORTA.3 = fld[3][x];             PORTA.4 = fld[4][x];  PORTA.5 = fld[5][x];             PORTA.6 = fld[6][x];  PORTA.7 = fld[7][x];         } else {                 PORTB.0 = fld[8][x];  PORTB.1 = fld[9][x];             PORTB.2 = fld[10][x]; PORTB.3 = fld[11][x];             PORTB.4 = fld[12][x]; PORTB.5 = fld[13][x];             PORTB.6 = fld[14][x]; PORTB.7 = fld[15][x];         }             switch (x) {             case 0: {PORTD = 0b11111110; break;} case 1: {PORTD = 0b11111101; break;}             case 2: {PORTD = 0b11111011; break;} case 3: {PORTD = 0b11110111; break;}             case 4: {PORTD = 0b11101111; break;} case 5: {PORTD = 0b11011111; break;}             case 6: {PORTD = 0b10111111; break;} case 7: {PORTD = 0b01111111; break;}         }             delay_us(50);         x++;     }     src++;     if (src == 2) src = 0;  }  void initgame(void) { char x = 0, y = 0;     #asm(&quot;cli&quot;)     while (y &lt; 16) {          x = 0;         while (x &lt; 8) {             fld[y][x] = 0;             x++;         }         y++;     }      tmp = 1;      speed = 50;     #asm(&quot;sei&quot;) }  void inkey(void){ char key = 0;       if (PINC.1 == 0) {         delay_ms(1);         if (PINC.1 == 0) key = 3; \/\/down      }      if (PINC.0 == 0) {         delay_ms(1);         if (PINC.0 == 0) key = 4; \/\/button      }        switch (key) {     case 1: {              break;}     case 2: {          break;}     case 3: {          break;}     case 4: {         if (start == 0) {             start = 1;             initgame();         }             break;}      default: {     }     };              }    void main(void) { \/\/ Declare your local variables here  \/\/ Input\/Output Ports initialization \/\/ Port A initialization \/\/ Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out  \/\/ State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0  PORTA=0x00; DDRA=0xFF;  \/\/ Port B initialization \/\/ Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In  \/\/ State7=P State6=P State5=P State4=P State3=P State2=P State1=P State0=P  PORTB=0x00; DDRB=0xFF;  \/\/ Port C initialization \/\/ Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out  \/\/ State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0  PORTC=0xFF; DDRC=0x00;  \/\/ Port D initialization \/\/ Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out  \/\/ State7=1 State6=1 State5=1 State4=1 State3=1 State2=1 State1=1 State0=1  PORTD=0x00; DDRD=0xFF; \/\/ Timer\/Counter 0 initialization \/\/ Clock source: System Clock \/\/ Clock value: 1000,000 kHz \/\/ Mode: Normal top=0xFF \/\/ OC0 output: Disconnected TCCR0=0x02; TCNT0=0x00; OCR0=0x00;  \/\/ Timer\/Counter 1 initialization \/\/ Clock source: System Clock \/\/ Clock value: Timer1 Stopped \/\/ Mode: Normal top=0xFFFF \/\/ OC1A output: Discon. \/\/ 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=0x00; TCCR1B=0x00; 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: Timer2 Stopped \/\/ Mode: Normal top=0xFF \/\/ OC2 output: Disconnected ASSR=0x00; TCCR2=0x00; TCNT2=0x00; OCR2=0x00;  \/\/ External Interrupt(s) initialization \/\/ INT0: Off \/\/ INT1: Off \/\/ INT2: Off MCUCR=0x00; MCUCSR=0x00;  \/\/ Timer(s)\/Counter(s) Interrupt(s) initialization TIMSK=0x01;   \/\/ 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; showsrc(); #asm(&quot;sei&quot;) while (1)       {       \/\/ Place your code here       paint();       inkey();                } } <\/code><\/pre>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/161951\/\"> http:\/\/habrahabr.ru\/post\/161951\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">   \t\u0412 \u043e\u0447\u0435\u0440\u0435\u0434\u043d\u0443\u044e \u0443\u0431\u043e\u0440\u043a\u0443 \u0432 \u043c\u0430\u0441\u0442\u0435\u0440\u0441\u043a\u043e\u0439 \u0434\u043e\u0432\u0435\u043b\u043e\u0441\u044c \u043c\u043d\u0435 \u043d\u0430\u0442\u043a\u043d\u0443\u0442\u044c\u0441\u044f \u043d\u0430 \u0434\u0432\u0430 \u043c\u0430\u0442\u0440\u0438\u0447\u043d\u044b\u0445 \u0438\u043d\u0434\u0438\u043a\u0430\u0442\u043e\u0440\u0430 8\u04458, \u0442\u0430\u043c \u0436\u0435 \u0431\u044b\u043b\u0430 \u043d\u0430\u0439\u0434\u0435\u043d\u0430 Mega16, \u043a\u043d\u043e\u043f\u043a\u0438, \u043c\u0430\u043a\u0435\u0442\u043a\u0430. \u00ab\u041a\u0430\u043a\u043e\u0439 \u0436\u0435 \u0442\u044b \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442, \u0435\u0441\u043b\u0438 \u0437\u0430 \u0441\u0432\u043e\u044e \u0436\u0438\u0437\u043d\u044c \u043d\u0438 \u0440\u0430\u0437\u0443 \u043d\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u0422\u0435\u0442\u0440\u0438\u0441?\u00bb, \u0441\u043f\u0440\u043e\u0441\u0438\u043b \u043c\u043e\u0437\u0433.<br \/>  \u041a\u043e\u043d\u0435\u0446 \u0434\u043d\u044f \u0431\u044b\u043b \u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d \u0442\u043e\u043c\u0443, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0432\u0435\u0441\u043e\u043c \u0441\u043f\u0430\u044f\u0442\u044c \u044d\u0442\u043e \u0432\u0441\u0435 \u0445\u043e\u0437\u044f\u0439\u0441\u0442\u0432\u043e.<br \/>  <img decoding=\"async\" src=\"http:\/\/s020.radikal.ru\/i703\/1212\/00\/b63f8ffe5011.jpg\" alt=\"image\"\/><\/p>\n<p>  \u0421\u0445\u0435\u043c\u0430 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<br \/>  <img decoding=\"async\" src=\"http:\/\/s017.radikal.ru\/i440\/1212\/1a\/b3a60b057432.jpg\" alt=\"image\"\/><\/p>\n<p>  \u041a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e \u0440\u0443\u043a\u0438 \u0434\u043e \u0422\u0435\u0442\u0440\u0438\u0441\u0430 \u043f\u043e\u043a\u0430 \u043d\u0435 \u0434\u043e\u0448\u043b\u0438, \u0440\u0430\u0437\u043c\u044f\u043b\u0441\u044f \u0442\u0435\u043c, \u0447\u0442\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u041f\u0438\u043d\u0433-\u041f\u043e\u043d\u0433.<br \/>  \u0412\u0438\u0434\u0435\u043e \u0440\u0430\u0431\u043e\u0442\u044b \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0442\u0443\u0442. <a href=\"http:\/\/vk.com\/video14260049_163193346\">\u0412\u0438\u0434\u0435\u043e<\/a>  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-161951","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/161951","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=161951"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/161951\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=161951"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=161951"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=161951"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}