SMBus Library
The SMBus I/O interface is a two-wire, bi-directional serial bus. The System Management Bus is compatible with the I2C serial bus.
SMBus module is available with a number of Silicon Laboratories 8051 MCU models. mikroBasic PRO for 8051 provides library which supports the master SMBus mode.
Library Routines
- SMBus1_Init
- SMBus1_Start
- SMBus1_Repeated_Start
- SMBus1_Busy
- SMBus1_Read
- SMBus1_Write
- SMBus1_Status
- SMBus1_Stop
- SMBus1_Close
SMBus1_Init
Prototype |
sub procedure SMBus1_Init(dim clock as longword) |
---|---|
Returns |
Nothing. |
Description |
Initializes SMBus with desired |
Requires |
Library requires SMBus module. |
Example |
SMBus1_Init(100000) |
SMBus1_Start
Prototype |
sub function SMBus1_Start() as byte |
---|---|
Returns |
If there is no error function returns 0, otherwise returns 1. |
Description |
Determines if SMBus bus is free and issues START signal. |
Requires |
SMBus must be configured before using this function. See SMBus1_Init. |
Example |
if (SMBus1_Start = 1) begin ... end. |
SMBus1_Repeated_Start
Prototype |
sub function SMBus1_Repeated_Start() as byte |
---|---|
Returns |
If there is no error function returns 0, otherwise returns 1. |
Description |
Issues repeated START signal. |
Requires |
SMBus must be configured before using this function. See SMBus1_Init. |
Example |
SMBus1_Repeated_Start() |
SMBus1_Busy
Prototype |
sub function SMBus1_Busy() as byte |
---|---|
Returns |
Returns 0 if SMBus start sequence is finished, 1 if SMBus start sequence is not finished. |
Description |
Signalizes the status of SMBus bus. |
Requires |
SMBus must be configured before using this function. See SMBus1_Init. |
Example |
if (SMBus1_Busy = 1) begin ... end. |
SMBus1_Read
Prototype |
sub function SMBus1_Read(dim nack_ as byte) as byte |
---|---|
Returns |
Returns one byte from the slave. |
Description |
Reads one byte from the slave, and sends acknowledge signal if parameter |
Requires |
SMBus must be configured before using this function. See SMBus1_Init. Also, START signal needs to be issued in order to use this function. See SMBus1_Start. |
Example |
Read data and send not acknowledge signal: tmp = SMBus1_Read(0) |
SMBus1_Write
Prototype |
sub procedure SMBus1_Write(dim data_ as byte) |
---|---|
Returns |
Nothing. |
Description |
Sends data byte (parameter |
Requires |
SMBus must be configured before using this function. See SMBus1_Init. Also, START signal needs to be issued in order to use this function. See SMBus1_Start. |
Example |
SMBus1_Write(0xA3) |
SMBus1_Status
Prototype |
sub function SMBus1_Status() as byte |
---|---|
Returns |
Returns value of status register. |
Description |
Returns status of SMBus. |
Requires |
SMBus must be configured before using this function. See SMBus1_Init. |
Example |
status = SMBus1_Status() |
SMBus1_Stop
Prototype |
sub procedure SMBus1_Stop() |
---|---|
Returns |
Nothing. |
Description |
Issues STOP signal to SMBus operation. |
Requires |
SMBus must be configured before using this function. See SMBus1_Init. |
Example |
SMBus1_Stop() |
SMBus1_Close
Prototype |
sub procedure SMBus1_Close() |
---|---|
Returns |
Nothing. |
Description |
Closes SMBus connection. |
Requires |
SMBus must be configured before using this function. See SMBus1_Init. |
Example |
SMBus1_Close() |
Library Example
This code demonstrates use of SMBus Library sub procedures and functions. 8051 MCU is connected (SCL, SDA pins ) to 24c02 EEPROM. Program sends data to EEPROM (data is written at address 2). Then, we read data via SMBus from EEPROM and send its value to PORTA, to check if the cycle was successful. Check the figure below.
program I2C_Simple dim ee_data as byte main: WDTCN = 0xDE ' Sequence for WDTCN = 0xAD ' disabling the watchdog timer OSCICN = 0x83 ' Enable internal oscillator (24.5MHz divided by 1) P0MDOUT = P0MDOUT or %00001101 ' Configure P0.3(SCK), P0.2(SDA) ' and P0.0(TX) pin as push-pull UART1_Init(4800) ' Initialize UART module at 4800 bps Delay_ms(100) ' Wait for UART module to stabilize UART1_Write_Text("Start ") SMBus1_Init(32000) ' initialize I2C communication SMBus1_Start() ' issue I2C start signal SMBus1_Write(0xA2) ' send byte via I2C (device address + W) SMBus1_Write(2) ' send byte (address of EEPROM location) SMBus1_Write(0xFF) ' send data (data to be written) SMBus1_Stop() ' issue I2C stop signal Delay_100ms() SMBus1_Start() ' issue I2C start signal SMBus1_Write(0xA2) ' send byte via I2C (device address + W) SMBus1_Write(2) ' send byte (data address) SMBus1_Repeated_Start() ' issue I2C signal repeated start SMBus1_Write(0xA3) ' send byte (device address + R) ee_data = SMBus1_Read(0) ' Read the data (NO acknowledge) UART1_Write(ee_data) SMBus1_Stop() ' issue I2C stop signal end. end.
HW Connection
Interfacing 24c02 to 8051 via SMBus
What do you think about this topic ? Send us feedback!