• slider image 442
  • slider image 483
  • slider image 484
  • slider image 485
  • slider image 486
  • slider image 487
:::

論壇索引


Board index » All Posts (Yomen)




PIC16F818 I2C 問題
#1
新會員
新會員


各位先進,小弟目前使用PIC16F818,當作SLAVE,利用51去讀PIC的資料,可是很奇怪,當我送出ADDRESS後,PIC並沒有回應ACK,這是啥原因?是我ADDRESS的設定錯了嗎?請指教,謝謝!
ORG 0x000 ; processor reset vector
goto main ; go to beginning of program


ORG 0x004 ; interrupt vector location
movwf w_temp ; save off current W register contents
movf STATUS,w ; move status register into W register
movwf status_temp ; save off contents of STATUS register


; isr code can go here or be located as a call subroutine elsewhere
banksel SSPSTAT
btfsc SSPSTAT,3
call Start_condition
btfsc SSPSTAT,4
call Start_stop

banksel PIR1
btfss PIR1,SSPIF ; Is this a SSP interrupt?
goto endint ; No, just trap here.
call ISR ; Yes, service SSP interrupt.
endint
movf status_temp,w ; retrieve copy of STATUS register
movwf STATUS ; restore pre-isr STATUS register contents
swapf w_temp,f
swapf w_temp,w ; restore pre-isr W register contents
retfie ; return from interrupt


main
call SetupSlaveI2C
banksel PORTB
bsf PORTB,2
bsf PORTB,3
mainloop
clrwdt
goto mainloop


; remaining code goes here

;-------------------------------------------------------------------------------------------------
SetupSlaveI2C
;-------------------------------------------------------------------------------------------------
bcf STATUS,RP0

movlw B'00000011'
banksel PCON
movwf PCON

movlw B'00110110' ;I2C SLAVE, 7-BIT ADDRESS
banksel SSPCON
movwf SSPCON
banksel PIR1
clrf PIR1 ;CLEAR SSPIF
;
banksel STATUS
bsf STATUS,RP0
movlw H'12' ;I2C ADDRESS, 可以是其它值
banksel SSPADD
movwf SSPADD
;
movlw B'11000000' ;GIE, PEIE = 1
banksel INTCON
movwf INTCON
;
movlw B'00001000' ;SSPIE = 1
banksel PIE1
movwf PIE1
;
banksel OPTION_REG
bcf OPTION_REG,NOT_RBPU
banksel PORTB
clrf PORTB
movlw B'00010010' ;將SCL及SDA腳的TRIS值設為1
banksel TRISB
movwf TRISB
;
return
;-------------------------------------------------------------------------------------------------
ISR
;-------------------------------------------------------------------------------------------------
bsf STATUS,RP0
;
btfss SSPSTAT,BF ;使用 BF 來分辨READ 或 WRITE
goto WAS_READ
;-------------------------------------------------------------------------------------------------
WAS_WRITE
;-------------------------------------------------------------------------------------------------
btfsc SSPSTAT,D ;再使用 D 來分辨 DATA 或ADDRESS
goto WAS_DATA
;-------------------------------------------------------------------------------------------------
WAS_ADDRESS
;-------------------------------------------------------------------------------------------------
bcf STATUS,RP0 ;因為之前的SSPSTAT是位於BANK1,
; ;所以切為BANK0比較好做事
;....
;.... ;在此做收到address時要做的事。
; ;也許是重置接收字串的指位器。
;
;-------------------------------------------------------------------------------------------------
WAS_DATA
;-------------------------------------------------------------------------------------------------
bcf STATUS,RP0
movfw SSPBUF ;利用讀取SSPBUF來清除BF旗標
;
; ;在此做收到address 或 data要做的事。
;
goto END_I2C_INT
;-------------------------------------------------------------------------------------------------
WAS_READ
;-------------------------------------------------------------------------------------------------
btfsc SSPSTAT,D
goto WAS_DATA1
;-------------------------------------------------------------------------------------------------
WAS_ADDRESS1
;-------------------------------------------------------------------------------------------------
bcf STATUS,RP0
movlw 0x90
; ;不論是收到address或data
; ;都要儘早填值到SSPBUF以供讀取
movwf SSPBUF ;因為SCL線會被拉低,直到
bsf SSPCON,CKP ;將CKP設為1才會將SCL線釋放
btfsc SSPCON,WCOL
bcf SSPCON,WCOL
;-------------------------------------------------------------------------------------------------
WAS_DATA1
;-------------------------------------------------------------------------------------------------
bcf STATUS,RP0
; ;
; ;
movwf SSPBUF
bsf SSPCON,CKP
;-------------------------------------------------------------------------------------------------
END_I2C_INT
;-------------------------------------------------------------------------------------------------
bcf PIR1,SSPIF ;一律要清除 SSPIF
return
;-------------------------------------------------------------------------------------------------
Start_condition
banksel PORTB
bcf PORTB,3
banksel SSPBUF
movf SSPBUF,W
return
Start_stop
banksel PORTB
bcf PORTB,2
return
; initialize eeprom locations

ORG 0x2100
DE 0x00, 0x01, 0x02, 0x03


END ; directive 'end of program'

發表於: 2004/7/22 18:21
頂部






:::

Microchip連結

https://www.facebook.com/microchiptechnologytaiwan/
http://www.microchip.com.tw/modules/tad_uploader/index.php?of_cat_sn=13
https://mu.microchip.com/page/tmu
http://elearning.microchip.com.tw/modules/tad_link/index.php?cate_sn=1
https://page.microchip.com/APAC-PrefCenters-TW.html
http://www.microchip.com/
http://www.microchip.com/treelink
http://www.microchipdirect.com/
http://www.microchip.com.cn/newcommunity/index.php?m=Video&a=index&id=103
http://www.microchip.com.tw/modules/tad_uploader/index.php?of_cat_sn=2
http://www.microchip.com.tw/Data_CD/eLearning/index.html
http://www.microchip.com.tw/RTC/RTC_DVD/
https://www.microchip.com/development-tools/
https://www.youtube.com/user/MicrochipTechnology
[ more... ]

教育訓練中心

!開發工具購買
辦法說明 [業界客戶] [教育單位]
----------------------------------
!校園樣品申請
辦法說明 [教師資格] [學生資格]
----------------------------------