;================ Main loop==================================
Main
call hello
call beep
call del_1s
call lcd_clr
read ;loop
btg porta,5
call READ_COMPASS
movff XHI,numh
movff XLO,numl
putstr "X = "
call convert
call LCD_Decimal
call Del_2S
call lcd_clr
putstr "Z = "
movff ZHI,numh
movff ZLO,numl
call convert
call LCD_Decimal
call Del_2S
call lcd_clr
putstr "Y = "
movff YHI,numh
movff YLO,numl
call convert
call LCD_Decimal
call Del_2S
call lcd_clr
goto read
READ_COMPASS ;SINGLE-Measurement mode
call beep
call s_start
MOVLW slave_write ;0x3c control in (write)
CALL send_i2c_byte
call chk_ack
movlw 0x02 ;Mode register
call send_i2c_byte
call CHK_ACK
movlw 0x01 ;(single-measurement mode)
call send_i2c_byte
call CHK_ACK
delaycy (100*msecs)
call s_restart
movlw slave_write ;0x3c control in (write)
call send_i2c_byte
call chk_ack
;read Xhi
movlw XHIREG ;point to XHI register
call send_i2c_byte
call CHK_ACK
;restart and send control byte (READ)
call s_restart
movlw slave_read ;0x3d control out (read)
call SEND_I2C_BYTE
call chk_ack
;Configure MSSP for reading
bsf sspcon2,rcen ;enable i2c read
call waitMSSP ;waits for the last I2C operation to complete
bcf sspcon2,ackdt
bsf sspcon2,acken ;send ack to slave
movff sspbuf,XHI
delaycy (10*msecs)
;Configure MSSP for reading
bsf sspcon2,rcen ;enable i2c read
call waitMSSP ;waits for the last I2C operation to complete
bcf sspcon2,ackdt
bsf sspcon2,acken ;send ack to slave
; bsf porta,1
movff sspbuf,XLO
;read ZHI
delaycy (10*msecs)
;Configure MSSP for reading
bsf sspcon2,rcen ;enable i2c read
call waitMSSP ;waits for the last I2C operation to complete
bcf sspcon2,ackdt
bsf sspcon2,acken ;send ack to slave
movff sspbuf,ZHI
;read ZLO
delaycy (10*msecs)
;Configure MSSP for reading
bsf sspcon2,rcen ;enable i2c read
call waitMSSP ;waits for the last I2C operation to complete
bcf sspcon2,ackdt
bsf sspcon2,acken ;send ack to slave
movff sspbuf,ZLO
;read YHI
delaycy (10*msecs)
;Configure MSSP for reading
bsf sspcon2,rcen ;enable i2c read
call waitMSSP ;waits for the last I2C operation to complete
bcf sspcon2,ackdt
bsf sspcon2,acken ;send ack to slave
movff sspbuf,YHI
;Configure MSSP for reading
bsf sspcon2,rcen ;enable i2c read
call waitMSSP ;waits for the last I2C operation to complete
bsf sspcon2,acken ;send ack to slave
movff sspbuf,YLO
call S_STOP
return