×
单片机 > 单片机程序设计 > 详情

PIC单片机软件模拟IIC读写例程

发布时间:2020-06-16 发布时间:
|

;I2C总线程序 
;this is eeprom read & write programm 
;the special store ram unit 
w    equ 0 
f0    equ 0 

rtCC    equ 1 
pc    equ 2 
f2    equ 2 
f3    equ 3 
f4    equ 4 
ra    equ 5 
rb    equ 6 
rc    equ 7 
pclath    equ 0ah 
iNTCon    equ 0bh 
pir1    equ 0ch 
tmr1l    equ 0eh 
tmr1h    equ 0fh 
t1con    equ 10h 
tmr2    equ 11h 
t2con    equ 12h 
sspbuf    equ 13h 
sspcon    equ 14h 
ccpr1l    equ 15h 
ccpr1h    equ 16h 
ccpcon    equ 17h 
adcon0    equ 1fh 

;in the bank1 
trISA    equ 85h 
trisb    equ 86h 
trisc    equ 87h 
pcon    equ 8eh 
pie1    equ 8ch 
pr2    equ 92h 
sspadd    equ 93h 
sspstat equ 94h 
adcon1    equ 9fh 

;-------------------------------------------------------- 
;the genaral store ram unit 

temp6     equ 76h 
temp7     equ 77h 

eepCTR     equ 6dh ;eeprom control words buffer 
eepaddr  equ 6eh ;eeprom address buffer 
eepdata  equ 6fh ;eeprom data buffer 

;-------------------------------------------------------- 
;the bits of ram unit 
scl    equ 3    ;rc 
sda    equ 4    ;rc 

pd    equ 3    ;status 
to    equ 4    ;status 

rbif    equ 0    ;intcon 
intif    equ 1    ;intcon 
rtif    equ 2    ;intcon 
rbie    equ 3    ;intcon 
intie    equ 4    ;intcon 
rtie    equ 5    ;intcon 
adie    equ 6    ;intcon 
gie    equ 7    ;intcon 

;---------------------------------------------------------- 
;The contact 
sda_in    equ    10h 
sda_out equ    00h 

;---------------------------------------------------------- 
    org 000h 
    goto main_program 
;-------------------------------------------------------- 
;     the son-program area 
;-------------------------------------------------------- 
    org 005h 
;-------------------------------------------------------- 
make_s 
    bsf rc,sda 
    bsf f3,5 
;    bsf rc,sda 
    MOVlw sda_out 
    MOVwf trisc 
    bcf f3,5 
    nop 
ack_check 
    bsf f3,5 
;    bcf rc,sda 
    MOVlw sda_in 
    MOVwf trisc 
    bcf f3,5 
    nop 
m_start 
    bsf rc,scl 
    nop 
    btfss rc,sda 
    goto make_s 
m_start0 
    bsf rc,scl 
    bcf rc,sda 
    bsf f3,5 
;    bsf rc,sda 
    MOVlw sda_out 
    MOVwf trisc 
    bcf f3,5 
    MOVlw 10100000b 
    btfsc sign,read_b 
    MOVlw 10100001b 
    MOVwf 6dh 
    MOVlw 6dh 
    MOVwf f4 
w_byte 
    bsf f3,5 
;    bsf rc,sda 
    MOVlw sda_out 
    MOVwf trisc 
    bcf f3,5 
    MOVlw 08h 
    MOVwf temp2 
bit_loop 
    nop 
    rlf f0 
    bcf rc,scl 
    btfss f3,0 
    goto ^+3 
    bsf rc,sda 
    goto ^+2 
    bcf rc,sda 
    nop 
    bsf rc,scl 
    decfsz temp2 
    goto bit_loop 
    nop 
    bcf rc,scl 
    bsf f3,5 
;    bcf rc,sda 
    MOVlw sda_in 
    MOVwf trisc 
    bcf f3,5 
    bsf rc,scl 
    nop 
    btfsc rc,sda 
    goto m_start0 
    bcf rc,scl 
    return 

w_stop 
m_stop 
    bcf rc,sda 
    bsf f3,5 
;    bsf rc,sda 
    MOVlw sda_out 
    MOVwf trisc 
    bcf f3,5 
    nop 
    bsf rc,scl 
    nop 
    bsf rc,sda 
    nop 
    bcf rc,scl 
    return 

rd_bytes 
    nop 
rnxtb 
    bcf rc,scl 
    bsf f3,5 
;    bcf rc,sda 
    MOVlw sda_in 
    MOVwf trisc 
    bcf f3,5 
    MOVlw 08h 
    MOVwf temp2 
rnx 
    bcf rc,scl 
    nop 
    bcf f3,0 
    btfsc rc,sda 
    bsf f3,0 
    rlf f0 
    bsf rc,scl 
    nop 
    decfsz temp2 
    goto rnx 
    incf f4,1 
    bsf f3,5 
;    bsf rc,sda 
    MOVlw sda_out 
    MOVwf trisc 
    bcf f3,5 
    bcf rc,scl 
    decfsz temp4 
    goto t_ackg 
r_stop 
    goto m_stop 
t_ackg 
    bcf rc,sda 
    nop 
    bsf rc,scl 
    nop 
    goto rnxtb 
;----------------------------- 
delay_1ms 
    MOVlw .1 
    MOVwf temp6 
delay_publIC 
    MOVlw .223 
    MOVwf temp7 
public_2 
    clrwdt 
    decfsz temp7,1 
    goto public_2 
    decfsz temp6,1 
    goto delay_public 
    return 
;----------------------------- 
;----------------------------- 
;    the mainprogram area 
;----------------------------- 
main_program 
    bsf f3,5 
    MOVlw 0fh 
    MOVwf 81h 
    MOVlw sda_in 
    MOVwf trisc 
    bcf f3,5       ;bank 0 
reset_delay 
    MOVlw 20h 
    MOVwf f4 
next 
    clrf f0 
    incf f4,1 
    MOVlw 80h        ;clr 20h--7fh ram 
    clrz 
    xorwf f4,w 
    skpz 
    goto next 
;----------------------- 
    clrf intcon 
    bsf f3,5 
    MOVlw 0fh 
    MOVwf 81h 
    bcf f3,5       ;bank 0 

;--------------------------- 
read_eeprom_repair 
    bcf rc,eneep 
    call delay_1ms 

    call ack_check 
    MOVf address,0 
    MOVwf 6eh 
    incf f4 
    call w_byte 
    bsf sign,read_b 
    call ack_check 
    bcf sign,read_b 
    MOVlw 20h 
    MOVwf f4 
    MOVlw .8 
    MOVwf temp4 
    call rd_bytes 
    nop 
    nop 
    nop 
    nop 
    call delay_1ms 
    bsf rc,eneep 

write_eeprom_repair 
    bcf rc,eneep 
    call delay_1ms 

    call ack_check 
    MOVf address,0 
    MOVwf 6eh 
    incf f4 
    call w_byte 
    MOVlw 40h 
    MOVwf f4 
    MOVlw .8 
    MOVwf temp4 
write_option 
    call w_byte 
    incf f4,1 
    decfsz temp4 
    goto write_option 
    call w_stop 
    nop 
    nop 
    nop 
    nop 
    call delay_1ms 
    bsf rc,eneep 

loop 
    goto loop 
end 


关键字:PIC单片机软件模拟IIC  读写 

『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
AVR M16实验之六 DS18B20测温