[ บทความ : ตัวอย่างโปรแกรมของ CP-PIC877 V1/V2 ] ตอนที่ 4 การติดต่อกับ 2416 (EEPROM) ด้วย PIC16F877 |
ตัวอย่างต่อไปนี้เป็น โปรแกรมที่เขียนขึ้นเพื่อใช้งานกับบอร์ด CP-PIC877 V1 หรือ V2 ของบริษัท ETT ตัวอย่าง นี้เป็นการติดต่อหน่วยความจำ EEPROM เบอร์ 2416 ด้วย PIC16F877 นั้นเราสามารถเขียนเป็นภาษาแอสเซมบลีได้ดังนี้
;********************************************** ; Test write and read EEPROM 2416 (24XX) ; Hardware : CP-PIC877 V1.0 & V2.0 ; Assembler : mpasm.exe ; Programmer: Somkiat Siha ; Company : ETT CO.,LTD. ; Date : 3/3/2000 ;********************************************** LIST P=16f877 include#define SDA PORTC,4 #define SCL PORTC,3 dt1 EQU 0x20 dt2 EQU 0x21 dt3 EQU 0x22 XBUF EQU 0x23 count EQU 0x24 CONTROL EQU 0x25 ADD EQU 0x27 DAT EQU 0x28 flag EQU 0x29 DOUT EQU 0x30 ;********************************************* ORG 0x0000 movlw 0xff movwf PORTD ; Initial port d bsf STATUS,RP0 ; select bank 1 clrf TRISC ; port c all output clrf TRISD ; port d all output bcf STATUS,RP0 ; select bank 0 movlw 0x5 ; To be write data to address 9Ah movwf ADD movlw 0x66 ; Write 8E to address 9Ah movwf DAT call wr ; call write sub call SDel ; delay more than 10 mS movlw 0x5 ; Address to read data movwf ADD call rd ; call read sub movf DOUT,w ; Show data to port d movwf PORTD oo goto oo ;************************************ ; Write data ; Input : ADD,DAT ; Output : EEPROM ;************************************ wr bcf SDA bcf SCL call start movlw 0xA0 ; Control data & slave address 00H movwf XBUF call TB movlw TRISC movwf FSR bsf INDF,4 ; SDA input (hi) bsf SCL ; clock hi 9th btfsc SDA goto wr bcf SCL ; clock lo 9th call losda movf ADD,w movwf XBUF call TB movlw TRISC movwf FSR bsf INDF,4 ; SDA input (hi) bsf SCL ; clock hi 9th btfsc SDA goto wr bcf SCL ; clock lo 9th call losda movf DAT,w movwf XBUF call TB movlw TRISC movwf FSR bsf INDF,4 ; SDA input (hi) bsf SCL ; clock hi 9th btfsc SDA goto wr bcf SCL ; clock lo 9th call losda bsf SCL nop nop nop nop bsf SDA ; stop bit return ;************************************ ; Read data ; Input : ADD ; Output : DOUT ;************************************ rd clrf DOUT ; DOUT = 0 bcf SDA bcf SCL call start movlw 0xA0 ; Control data & slave address 00H movwf XBUF call TB movlw TRISC movwf FSR bsf INDF,4 ; SDA input (hi) bsf SCL ; clock hi 9th btfsc SDA goto rd bcf SCL ; clock lo 9th call losda movf ADD,w movwf XBUF call TB movlw TRISC movwf FSR bsf INDF,4 ; SDA input (hi) bsf SCL ; clock hi 9th btfsc SDA goto rd bcf SCL ; clock lo 9th call losda bsf SCL bsf SDA ; stop bit bcf SDA bcf SCL ; start bit movlw 0xA1 ; Control read movwf XBUF call TB movlw TRISC movwf FSR bsf INDF,4 ; SDA input (hi) bsf SCL ; clock hi 9th btfsc SDA goto rd bcf SCL ; clock lo 9th call losda bcf STATUS,C ; clear carry flag movlw 8 movwf count ; counter = 8 (bits) movlw TRISC movwf FSR bsf INDF,4 ; SDA input ;************* Read bit ***************************** N4 bsf SCL ; clock hi btfss TRISC,4 goto N5 ; Din = 0 bsf DOUT,0 ; Din = 1 N5 rlf DOUT,f ; shift left 1 bit bcf SCL ; clock lo decfsz count,f goto N4 rrf DOUT,f ; shift data to right 1 bit bcf INDF,4 ; SDA output (lo) bsf INDF,4 ; SDA hi bsf SCL ; clock hi bcf SCL ; clock lo bsf SCL ; clock hi bsf INDF,4 ; SDA hi return ;******************************** ; SDA low ;******************************** losda bcf SDA movlw TRISC movwf FSR bcf INDF,4 ; SDA lo return ;****************************** ; Send bit ; Input : XBUF ; Output : EEPROM ;****************************** TB bcf STATUS,C ; clear carry flag movlw 8 movwf count ; counter = 0000 1000 N1 rlf XBUF,f ; Shift data to Carry flag btfsc STATUS,C goto N2 ; data bit = 1 goto N3 ; data bit = 0 N2 movlw TRISC movwf FSR bsf INDF,4 ; SDA hi N3 bsf SCL bcf SCL bcf SDA ; SDA lo movlw TRISC movwf FSR bcf INDF,4 ; SDA output decfsz count,f ; Dec count 1, skip next instruction if count=0 goto N1 ; count <> 0 return ;****************************** ; Start bit ;****************************** start bsf STATUS,RP0 ; select bank 1 bsf TRISC,3 ; SDA = hi bsf TRISC,4 ; SCL = hi bcf STATUS,RP0 ; select bank 0 bsf STATUS,RP0 ; select bank 1 bcf TRISC,4 ; SDA lo bcf TRISC,3 ; SCL lo bcf STATUS,RP0 ; select bank 0 return ;****************************** ; Short delay ;****************************** SDel movlw 0x00 movwf dt1 sd2 movlw 0x00 movwf dt2 sd1 decfsz dt2 goto sd1 decfsz dt1 goto sd2 return end ;****************** END OF PROGRAM ************************ ส่วนใครที่ใช้ ภาษา BASIC ก็สามารถติดต่อกับ EEPROM ได้เช่นกันครับ ... ต่อไปนี้เป็นตัวอย่าง สำหรับภาษา BASIC
'********************************************* 'Test write and read EEPROM 2416 or 24XX 'Hardware : CP-PIC877 V1.0 & V2.0 'file name : PicBasic Pro compiler (PBP.EXE) 'Programmer : Somkiat Siha 'Company : ETT CO.,LTD. ' Date : 29/2/2000 '********************************************* ' After down load this program complete. ' 1. Connect RS232 cable between PC & board. ' 2. Run program terminal such as Procom Plus and set parameters follow ' 9600 Baud, 8 bits data, 1 stop ,No parity ' 3. Then press Reset key on board CP-PIC877, you will see data on screen INCLUDE "BS2DEFS.BAS" DAT_IN var BYTE DAT_OUT var BYTE INST var BYTE ADD var word DAT_IN=29 INST=$A0 ADD=$100 I2CWRITE PORTC.4,PORTC.3,INST,ADD,[DAT_IN] pause 20 I2CREAD PORTC.4,PORTC.3,INST,ADD,[DAT_OUT] serout PORTC.6,T9600,[#DAT_IN,10,13] endLiNK :
CP-PIC877 V1
CP-PIC877 V2