View Single Post
Old 24-10-2005, 02:08 AM   #14
mafd_47a
Đệ tử 3 túi
 
Tham gia ngày: Sep 2005
Bài gửi: 54
:
Phan 2( cac module ve I2C va LCD )

PHP Code:
;================================== Cac chuong trinh con ======================
CONVERT
        MOVLW   START_RAM
        MOVWF   FSR
        MOVF    RAM_HOU
,W         Xu ly du lieu gio
        ANDLW   
B'00010000'
        
MOVWF   INDF
        SWAPF   INDF
,F            Luu vao trong Ram
        INCF    FSR
,F
        MOVF    RAM_HOU
,W    
        ANDLW   
B'00001111'
        
MOVWF   INDF              Ket thuc qua trinh xu ly hour
       
        INCF    FSR
,F             Bat dau xu ly minute
        MOVF    RAM_MIN
,W         Xy ly bien High truoc
        ANDLW   
B'01110000'
        
MOVWF   INDF
        SWAPF   INDF
,F
        INCF    FSR
,F
        MOVF    RAM_MIN
,W
        ANDLW   
B'00001111'
        
MOVWF   INDF
    
        INCF    FSR
,F             Bat dau xu ly SECOND
        MOVF    RAM_SEC
,W         Xy ly bien High truoc
        ANDLW   
B'01110000'
        
MOVWF   INDF
        SWAPF   INDF
,F
        INCF    FSR
,F
        MOVF    RAM_SEC
,W
        ANDLW   
B'00001111'
        
MOVWF   INDF
       
RETURN
WRITE_TIMER_LCD
        MOVLW   START_RAM
        MOVWF   FSR
        MOVLW   0x02
        MOVWF   R7
        MOVLW   0x05
        CALL    GOTOLINE1
LOOP_WTL1
        MOVF    INDF
,W            Qua trinh hien thi giophutgiay len dong 1 cua LCD
        ADDLW   0x30
        CALL    WRITE_TEXT
        INCF    FSR
,F
        MOVF    INDF
,W
        ADDLW   0x30
        CALL    WRITE_TEXT
        MOVLW   
':'
        
CALL    WRITE_TEXT
        INCF    FSR
,F
        DECFSZ  R7
,F
        
GOTO    LOOP_WTL1        Ket thuc qua trinh hien thi timer
        MOVF    INDF
,W            Qua trinh hien thi giophutgiay len dong 1 cua LCD
        ADDLW   0x30
        CALL    WRITE_TEXT
        INCF    FSR
,F
        MOVF    INDF
,W
        ADDLW   0x30
        CALL    WRITE_TEXT
        
RETURN

Write_RTC                    write W register to address Adr_Lo
        movwf    DAT_VAL            
save W
        call    I2C_Start
        call    I2C_Set_Write

; If ACK error display 'e'

        
btfsc    I2Cflags0
        call    Error_Routine

;            call    I2C_Hi_Adr        used for extendd addressing (24L64/5)

        
call    I2C_Lo_Adr

; If ACK error display 'f'

        
btfsc    I2Cflags0
        call    Error_Routine1

           movf    DAT_VAL
,     W    send the actual data
           movwf    OutputByte
           call    I2C_Out
           call    I2C_NAK

; If ACK error display 'g'

        
btfsc    I2Cflags0
        call    Error_Routine2

           call    I2C_Stop 
        call    WaitForWrite
           
return

Read_RTC                    reads data at location specified in Adr_Lo (& Adr_Hi for 24LC64)                    
        
call     I2C_Start         ; and page specified in Data_Page
        call    I2C_Set_Write     
returns result in W

; If ACK error display 'e'

        
btfsc    I2Cflags0
        call    Error_Routine
        call    I2C_Lo_Adr

; If ACK error display 'f'

        
btfsc    I2Cflags0
        call    Error_Routine1

           call     I2C_Start        
note there is no STOP
        call    I2C_Set_Read

; If ACK error display 'g'

        
btfsc    I2Cflags0
        call    Error_Routine2

           call     I2C_Read        
fetch the byte
           call     I2C_Send_NAK        
send no acknowledgement

           call     I2C_Stop
        movf     InputByte
,     W    ; return the byte in W
           
return


The following routines are low level I2C routines applicable to most
interfaces with I2C devices.

I2C_Read                    read byte on i2c bus
        clrf     InputByte
        movlw     0x08
        movwf     _N            
set index to 8    
        call    HIGH_SDA        
be sure SDA is configured as input
In_Bit
        call     HIGH_SCL        
clock high
        btfss     I2C_PORT
,    SDA    test SDA bit
        
goto    In_Zero
        
goto    In_One

In_Zero
        bcf     STATUS
,     C    clear carry
        rlf     InputByte
,     f    i_byte i_byte << 0
        
goto     Cont_In

In_One
        bsf    STATUS
,     C    set carry
        rlf     InputByte
,     f

Cont_In
        call    LOW_SCL            
bring clock low
        decfsz     _N
,     F        decrement index
        
goto     In_Bit
        
return

I2C_Out:                    ; send w register on I2C bus
           movwf     OutputByte
        movlw     0x08
        movwf     _N
Out_Bit
:
        
bcf     STATUS,        C    clear carry
        rlf     OutputByte
,     f    left shiftmost sig bit is now in carry
        btfss     STATUS
,     C    ; if onesend a one
        
goto     Out_Zero
        
goto     Out_One

Out_Zero
:
        
call     LOW_SDA            SDA at zero
        call     Clock_Pulse    
        call     HIGH_SDA
        
goto     Out_Cont

Out_One
:
        
call     HIGH_SDA        SDA at logic one
        call     Clock_Pulse
Out_Cont
:
        
decfsz     _N,        F    decrement index
        
goto     Out_Bit
        
return    

;;;;;;
        
I2C_NAK:                    ; bring SDA high and clock
        call     HIGH_SDA
        call     HIGH_SCL

        clrf    count            
wait for ACK
WaitForACK    
        incf    count
f        increase timeout counter each time ACK is not received
        btfsc    STATUS
Z
        
goto    No_ACK_Rec
        btfsc    I2C_PORT
,    SDA    test pin. If clearRTC is pulling SDA low for ACK
        
goto    WaitForACK        ; ...otherwise, continue to wait
        bcf        I2Cflags
0        clear flag bit (ACK received)
        
call     LOW_SCL
        
return

I2C_Send_NAK:                    ; bring SDA high and clock
        call     HIGH_SDA
        call     Clock_Pulse
        
return

WaitForWrite                    poll ACK for write timing
        call     I2C_Start
        call    I2C_Set_Write
        btfsc    I2Cflags
0
        
goto    WaitForWrite
        
return


;------ 
No ACK received from slave (must use "return" from here)
;; 
Typicallyset a flag bit to indicate failed write and check for it upon return.
No_ACK_Rec
        bsf    I2Cflags
0        set flag bit
        
return    

I2C_ACK:
        
call     LOW_SDA
        call     Clock_Pulse
        
return

I2C_Start:                
        
call     LOW_SCL
        call     HIGH_SDA
        call     HIGH_SCL
        call     LOW_SDA            
bring SDA low while SCL is high
        call     LOW_SCL
        
return

I2C_Stop:
        
call     LOW_SCL
        call     LOW_SDA
        call     HIGH_SCL
        call     HIGH_SDA        
bring SDA high while SCL is high
        call     LOW_SCL
        
return
 
I2C_Set_Write
        MOVLW     Chip_Write
        call     I2C_Out
        call     I2C_NAK
        
return

I2C_Set_Read
        MOVLW    Chip_Read
        call     I2C_Out
        call     I2C_NAK
        
return

I2C_Lo_Adr      movf     Adr_Lo,     W    send low byte of address
        call     I2C_Out
        call     I2C_NAK
        
return

Clock_Pulse:                    ; SCL momentarily to logic one
        call     HIGH_SCL
        call     LOW_SCL
        
return        

HIGH_SDA:                    ; high impedance by making SDA an input
        bsf     STATUS
,     RP0    bank 1
        bsf     I2C_TRIS
,     SDA    make SDA pin an input
        bcf     STATUS
,     RP0    back to bank 0
        
return

LOW_SDA:
        
bcf     I2C_PORT,     SDA    
        bsf     STATUS
,     RP0    bank 1
        bcf     I2C_TRIS
,     SDA    make SDA pin an output
        bcf     STATUS
,     RP0    back to bank 0
        
return

HIGH_SCL:
        
bsf     STATUS,     RP0    bank 1
        bsf     I2C_TRIS
,     SCL    make SCL pin an input
        bcf     STATUS
,     RP0    back to bank 0
        
return

LOW_SCL:
        
bcf        I2C_PORT,     SCL
        bsf     STATUS
,     RP0    bank 1
        bcf     I2C_TRIS
,     SCL    make SCL pin an output
        bcf     STATUS
,     RP0    back to bank 0
        
return

;------ 
No ACK received from slave.  This is the error handler.
Error_Routine
Output error messageetchere
        MOVLW   0x04
        CALL    GOTOLINE2
        movlw    
'e'
        
call    WRITE_TEXT
        
return                ; returns to INITIAL calling routine

Error_Routine1
Output error messageetchere
        MOVLW   0x05
        CALL    GOTOLINE2
        movlw    
'f'
        
call    WRITE_TEXT
        
return

Error_Routine2
Output error messageetchere
        MOVLW   0x06
        CALL    GOTOLINE2
        movlw    
'g'
        
call    WRITE_TEXT
        
return

End of I2C routines

;======================== MODULE LCD ====================================
WRITE_2_NIBBLES
        MOVWF   ACC
        BANKSEL TRISB
        MOVLW   
B'11110000'       Chon data lam dau ra
        ANDWF   TRIS_DAT
,F
        BANKSEL PORTB
        MOVLW   0x0F
        IORWF   PORT_DAT
,F
        SWAPF   ACC
,W
        IORLW   0xF0
        ANDWF   PORT_DAT
,F        Dua 4 bit CAO ra Port_CON
        BSF     PORT_CON
,EN
        NOP
        BCF     PORT_CON
,EN
        MOVLW   0x0F
        IORWF   PORT_DAT
,F
        MOVF    ACC
,W
        IORLW   0xF0
        ANDWF   PORT_DAT
,F        Dua 4 bit THAP ra Port_CON
        BSF     PORT_CON
,EN
        NOP
        BCF     PORT_CON
,EN
        
RETURN


        
INIT_LCD
        CALL    DELAY100
        BANKSEL TRISA
        MOVLW   
B'00000'
        
MOVWF   TRIS_DAT
        BCF     TRIS_CON
,EN
        BCF     TRIS_CON
,RS
        BCF     TRIS_CON
,RW
        BANKSEL PORTA    
        BCF     PORT_CON
,RS
        BCF     PORT_CON
,RW
        BCF     PORT_CON
,EN
        BSF     PORT_CON
,EN
        MOVLW   0x82
        MOVWF   PORT_DAT
        BCF     PORT_CON
,EN
        CALL    DELAY5
        MOVLW   0x28
        CALL    WRITE_2_NIBBLES   
Write A as two separate nibbles to LCD
        CALL    DELAY5
        MOVLW   0x0E
        CALL    WRITE_2_NIBBLES
        CALL    DELAY5
        MOVLW   0x06
        CALL    WRITE_2_NIBBLES
        CALL    DELAY5
        
RETURN

CLEAR_LCD
        BCF     PORT_CON
,RS
        MOVLW   0x01
        CALL    WRITE_2_NIBBLES
        CALL    DELAY50
        
RETURN

WRITE_TEXT
        BSF    PORT_CON
,RS
        CALL   WRITE_2_NIBBLES
        CALL   DELAY5
        
RETURN  

GOTOLINE1
        BCF    PORT_CON
,RS
        ADDLW  0x80
        CALL   WRITE_2_NIBBLES
        CALL   DELAY5
        
RETURN 

GOTOLINE2
        BCF    PORT_CON
,RS
        ADDLW  0xC0
        CALL   WRITE_2_NIBBLES
        CALL   DELAY5
        
RETURN 




DELAY255    movlw    0xff        ;delay 255 mS
        
goto    D0
DELAY100    movlw    d
'100'        ;delay 100mS
        
goto    D0
DELAY50        movlw    d
'50'        ;delay 50mS
        
goto    D0
DELAY20        movlw    d
'20'        ;delay 20mS
        
goto    D0
DELAY5        movlw    0x05        
;delay 5.000 ms (4 MHz clock)
D0        movwf    R2
D1        movlw    0xC7            
;delay 1mS
        movwf    R3
        movlw    0x01
        movwf    R4
Delay_0
        decfsz    R3
f
        
goto    $+2
        decfsz    R4
f
        
goto    Delay_0

        decfsz    R2    
,f
        
goto    D1
        retlw    0x00
            

        END 

Luu y voi cac ban la: cac ky tu f,g,e trong chuong trinh tren la cac ky tu bao loi duong truyen
File Kèm Theo
File Type: doc sodokhoi.doc (25.5 KB, 55 lần tải)

thay đổi nội dung bởi: mafd_47a, 26-10-2005 lúc 01:39 AM. Lý do: Quen Up len so do khoi
mafd_47a vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn