Page 30 - The Journal of the Royal Society of Thailand
P. 30

วารสารราชบััณฑิิตยสภา
                                          ปีที� ๔๙ ฉบับที� ๑ มกราคม-เมษายน ๒๕๖๗

             20                การต่่อประสานแผงวงจรไมโครคอนโทรลเลอร์เพื่่�อการเรียนร้�กับหน่วยความจำอีอีพื่ร็อมแบบอนุกรมต่ามเกณฑ์์วิธีี I2C



                     call delay1                 ;wait 0.16 sec
                     call chknak                 ;check NAK error after write
                     inc  si                     ;next byte
                     loop odat0                  ;loop 64 times

                     call i2cstp                 ;stop sequence
                     call delay2                 ;wait 0.6 sec for ser EEPROM to work

              ;//write 36 more data bytes to EEPROM
                     ;use si from previous write cycle
                     mov  ax,0040h               ;new start addr of ser EEPROM to write to
                     call prewr                  ;prepare ser EEPROM for new write cycle
                     mov  cx,36                  ;36 bytes to write
              odat1:
                     mov  al,byte ptr[si]  ;use data from obuf
                     out  ppi_port2b,al   ;display data byte in al
                     call i2c_wr                 ;write byte to ser EEPROM
                     call delay1                 ;wait 0.16 sec
                     call chknak                 ;check NAK error after write
                     inc  si                     ;next byte
                     loop odat1                  ;loop 36 times

                     call i2cstp                 ;stop sequence
                     call delay2                 ;wait 0.6 sec

              ;//read first 99 data bytes from ser EEPROM
                     mov  ax,0000h               ;start addr of ser EEPROM to read from
                     call prerd                  ;prepare ser EEPROM for read
                     mov  cx,99                  ;first 99 bytes to read
                     mov  di,ibuf                ;addr of input buffer
              idat:  mov  ah,0                   ;ACK=0 => acknowledge received byte
                     call i2c_rd                 ;read byte from ser EEPROM in to al
                     mov  byte ptr[di],al  ;store data in ibuf
                     inc  di                     ;next byte
                     loop idat                   ;loop  99 times

              ;//read 100th (last) data byte
                     mov  ah,1                   ;NACK=1 => no more acknowledge
                     call i2c_rd                 ;read byte from ser EEPROM in to al
                     mov  byte ptr[di],al  ;store data in ibuf
                     call i2cstp                 ;stop sequence
                     call delay2                 ;wait 0.6 sec
                     call delay2

              ;//display all read-back bytes
                     mov  cx,99                  ;for first 99 bytes
                     mov  si,ibuf          ;addr of input buffer
              dspl:
                     mov  al,byte ptr[si]  ;get data byte in to al
                     out  ppi_port2b,al    ;show data on Port 2B LEDs
                     call delay1           ;wait 0.16 sec
   25   26   27   28   29   30   31   32   33   34   35