News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Hbus_Bitrate change inside the program?

Started by trastikata, May 26, 2021, 10:52 PM

Previous topic - Next topic

trastikata

Hi,

I was wondering if the "Declare Hbus_Bitrate=xxx" can be used inside the program to change the bit rate dynamically?

Thus, I have a device using slow I2C memory (100KHz) and fast I2C OLED that can go up to 800KHz and I want to refresh the screen with the maximum speed to minimize flicker. So I was wondering if I issue the "Declare Hbus_Bitrate=xxx" command inside the program will change the bit rate or I have to change the SSPxADD register as part of the OLED subroutines I wrote?

Regards

trastikata

#1
To reply myself, I found the answer ... SSPADD is set only once at the beginning ignoring in-program declarations and has to be modified according this: I2C Master mode Clock = FOSC/(4 * (SSPxADD + 1))

_compiler_main_start_
    movlw 64
    movwf _FP_FLAGS
    clrf BPF
    movlb 0
    movlw 40
    movwf SSPCON1
    movlw 29
    movwf SSPADD
    movlw 128
    movwf SSPSTAT

top204

#2
For that type of use, simply create a few $define meta-macros with the bit-rate value to place into the appropriate SFRs for a required bit-rate. Then issue the $define when the bit-rate is to be altered:

$define I2C_BitRate_100() SSPxADD = {the correct value to place into the SSPxADD SFR for 100KHz operation}

$define I2C_BitRate_800() SSPxADD = {the correct value to place into the SSPxADD SFR for 800KHz operation}


Then in the program, use I2C_BitRate_100() or I2C_BitRate_800() before the I2C commands are used.