Hi Chris,
These are the I2c routines that I use.
This is not the whole firmware. These routines are called when the PIC receive trough RS232 some commands.
To read the encoder I use a comand that starts the reading routine once for every register, so the routine works for 17 times ( see the routine GlobalStat ).
After the total register reading the pda calculate a new engines speed that is sent to MD25 trough the I2C writing routine.
The speed is updated every 200 ms. I've tried also a 500/1000 msec with no better results.
This system is stopped when the encoders value reach the given setpoint.
As I wrote everything works fine when apply this procedure to a single motor ( MD25 mode set to 0 ).
The I2C frequency is about 100 KHz. The pull up resistors are 10K.
What about the MD25 hex firmware? Do you think it is possible find it?
Thank you very much.
You will find some italian words in the code because.... I'm italian
- The total register reading routine:
GlobalStat ; Read all registers
movlw 0xff
movwf ContaI2C
Scorri:
incf ContaI2C ;Starts from 0 register
movf ContaI2C,W
sublw 0x11 ; check for 17 registers read
btfsc STATUS,Z
goto $+8
movf ContaI2C,W
movwf Indirizzo ; Send the actual register
call LeggiI2C ; Read the register
movlw 0x3B
movwf TXREG
call TransWt ;Send RS232 the ";" symbol
Goto Scorri ; Go back for the next register
- The I2C Read routine
LeggiI2C ; I2C reading routine
call StartI2C ; Set SSPCON2.SEN
call WaitI2C ; Wait PIR1,SSPIF
BANKSEL SSPBUF
movlw B'10110000' ; Write Address in Write mode
movwf SSPBUF
call WaitI2C
BANKSEL SSPBUF
movf Indirizzo,W ; Write register
movwf SSPBUF
call WaitI2C
call RstartI2C ; Set SSPCON2.SEN
call WaitI2C ; Wait
BANKSEL SSPBUF
movlw B'10110001' ; Write Address in read mode
movwf SSPBUF
call WaitI2C
call RecI2C ; Enable I2C Receive
call WaitI2C ; Wait Until Buffer Received
BANKSEL SSPBUF
movf SSPBUF,W ; Save to I2C_Data First !!
BANKSEL I2C_Data
movwf I2C_Data
;
call StopI2C ; Initial STOP Condition
call WaitI2C ; Wait Until STOP Condition Terminated
- The I2C write routine
ScriviI2C
call RiceviRS232
call StartI2C ; Set SSPCON2.SEN
call WaitI2C ; Wait PIR1,SSPIF
BANKSEL SSPBUF
movlw B'10110000' ; Write address
movwf SSPBUF
call WaitI2C
BANKSEL SSPBUF
movf Indirizzo,W ; Write register
movwf SSPBUF
call WaitI2C
BANKSEL SSPBUF
movf DatoRicevuto,W ; Write data
movwf SSPBUF
call WaitI2C
call StopI2C ; Initial STOP Condition
call WaitI2C ; Wait Until STOP Condition Terminated
- The Wait I2C routine
WaitI2C ; Poll for SSPIF
banksel PIR1
FLoop btfss PIR1,SSPIF
goto FLoop
bcf PIR1,SSPIF
return
- The Start/Stop routines
StartI2C ; Initiate the I2C START condition.
banksel SSPCON2
bsf SSPCON2,SEN
return
StopI2C ; Initiate the I2C STOP condition.
banksel SSPCON2
bsf SSPCON2,PEN
return
- The restart routine
RstartI2C ; Initiate the I2C restart condition.
banksel SSPCON2
bsf SSPCON2,RSEN
return
- The I2C Init routine
InitI2C: ; The subroutine of I2C Initialization
movlw 0x08 ; This gives 100KHz I2C clock @ 4MHz
banksel SSPADD
movwf SSPADD
movlw b'10000000' ; Disable slew rate control.
banksel SSPSTAT
movwf SSPSTAT
movlw b'00000000' ;
movwf SSPCON2 ; Setup MSSP for continuous reception.
movlw b'00101000' ; Enable MSSP and setup for I2C master
banksel SSPCON ; mode.
movwf SSPCON
return