Page 253 - Journal451
P. 253

วารสารราชบััณฑิิตยสภา
                                          ปีีที่่� ๔๕ ฉบัับัที่่� ๑ มกราคม-เมษายน ๒๕๖๓
             ศาสตราจารย์์ ดร.มงคล เดชนครินทร์                                               241



                  //Page 3/4 of Fig. 7                 //Page 4/4 of Fig. 7

                  //Wait for upld signal from uC 8088 (XmtFlg=1)   //Read RB1 pin for 2 consec. pulse times
                     cnt1 = 0; //clear counter of wait time      ch3 = PORTB & 0x02; //pin RB1 logic value #1
                     while(XmtFlg == 0)                   Delay_ms(1); //1-ms delay=>2nd bit after SP
                     { Delay_ms(10);                      ch4 = PORTB & 0x02; //pin RB1 logic value #2
                       cnt1++; //count time
                       if (cnt1 == 100) break; //quit after 1 sec  //Check for data/control bit
                     } //end while                        if (ch3 != 0x00) //=> "1" bit or start bit
                                                          {  if (ch4 != 0x00) //it's start bit
                     if (XmtFlg != 0) //start sending data bytes         { if (IntFlg == 0) {IntFlg = 1;};}
                     {  XmtFlg = 0; //reset flag => no repeat           //IntFlg=0 => 1st-time INT from 8088
                        PORTB.F3 = 1; //pin RB3 LED on         else if (ch4 == 0x00) //it's "1" bit
                        Delay_ms(500);                       {  if (IntFlg == 0) {IntFlg = 1;}
                                                                //1st-time INT from 8088
                  //STORE DATA BYTES 0x01-0x80 in buffer            else if (IntFlg != 0)
                     buf[0]=0x01; buf[1]=0x02; buf[2]=0x04;             //not 1st-time INT from 8088
                     buf[3]=0x08;                               {  ch2 = ch2 << 1; //regular data bit
                     buf[4]=0x10; buf[5]=0x20; buf[6]=0x40;                ch2++; //add 1 to LSb
                     buf[7]=0x80;                               };
                                                             }; //end if ch4
                  //send 8 bytes to uC 8088, last byte first      }
                        for (cnt=7; cnt >= 0; cnt--)      else if (ch3 == 0x00)  //=> "0"/stop bit
                        {  ch0 = buf[cnt];                {  if (ch4 == 0x00)    //it's "0" bit
                        //display and send data byte         {  if (IntFlg == 0)
                           ShwSnd(ch0, 1);                      //first-time INT from 8088
                        } //end for                             {  XmtFlg = 1;   //set xmit flag
                                                                   IntFlg = 1;   //set INT flag
                  //send 5 consec. 0xFF's to conclude sending            } //end if IntFlg
                        for (cnt=0; cnt<5; cnt++)               else if (IntFlg != 0) //later INT
                        { ShwSnd(0xFF, 1); };                   { ch2 = ch2 << 1; }
                                                                  //shift ch2 but no increase
                     } //end if XmtFlg                       }
                                                             else if (ch4 != 0x00) //it's stop bit
                  //Loop until reset                         {  buf[dcnt] = ch2; //store char in buf
                     while(1) {};                               dcnt++; //prepare to store next char
                  } //end main
                  //*******************************************         //display complete data byte (for debug)
                                                                PORTA = (ch2 & 0x0F) << 1;
                  void senbyt(unsigned char ch)                 PORTB = ch2 & 0xF0;
                  {//send data byte in ch                       Delay_ms(250); //On-time
                     unsigned char mask, ch1;                   PORTA = 0x00; PORTB = 0x00;
                     int i;                                     Delay_ms(64);  //Off-time
                   //send start bit                          //watch for 5 consec. 0xFF's
                     senstrt();                                 if (ch2 == 0xFF) {ecnt++;}
                                                                else if (ch2 != 0xFF) {ecnt = 0;};
                   //send 8 regular data bits in ch, MSb first            //reset end counter
                     mask = 0x80; //take MSb first              if (ecnt == 5)  //end of receive
                     for (i=0; i<8; i++)                        {
                     {                                             dcnt1 = dcnt; ecnt1 = ecnt;
                        ch1 = ch & mask; //select bit to send               //save counters
                        if (ch1 != 0x00)                           dcnt = 0; ecnt = 0;
                        { onebit(); }  //send hi via RA0               //reset counters
                        else if (ch1 == 0x00)                   } //end if ecnt
                        { zrobit(); }; //send lo via RA0            ch2 = 0x00; //prepare for new dat byte
                        mask = mask >> 1; //select next lower bit        }; //end else if ch4
                     } //end for i                        }; //end else if ch3

                   //send stop bit                        INTCON = 0B10010000;
                     senstop();                           //GIE=1 + INTE=1 => enable INT
                  } //end senbyt                          OPTION_REG = 0B01000000;
                  //*******************************************      //INTEDG=1 => rising edge trig.

                  void interrupt(void) //ext. INT via RB0 (pin 6)  } //end interrupt
                  {//receive data byte in ch2          //*******************************************
                     unsigned char ch3, ch4;
                     INTCON = 0x00; //prevent further interrupt
                   //Identify bit 'start,' 'regular,' or 'stop'
                     Delay_us(500); //0.5-ms delay, skip sync pls
                     Delay_ms(1); //1-ms delay=>1st bit after SP

             ภาพท่� ๗  ซึ่อฟตั์แวร์การแลกเปล้�ยนี้ข้้อมูลแบบข้ัดจังหวะข้องไมโครคอนี้โทรลเลอร์ PIC16F627A (ตั่อ)
   248   249   250   251   252   253   254   255   256   257   258