Best practice, writting software based SPI and I2C

Thread Starter

sairfan1

Joined May 24, 2012
89
I'm going to write software SPI and I2C for PIC 8 bit MCU where I need to write command and read sensor data. I want to know what is the best practice to write such library.
As we can use delay statement for clock high and low state but its not a good practice (sounds to me) because delay statement blocks the execution and I belive it should be interrupt deriven for example I can use TMR0 to manage SCL/SCK state high/sow and on each Timer interrupt I can send and receive data.
I would appriciate if someone could help me on this, what sould be taken care while I'm writing this code for a low power project. If possible give me a small example just to understand the idea.
 

nsaspook

Joined Aug 27, 2009
12,329
Why would you do that instead of using a PIC with the hardware peripheral?
That's what they learned (or read about it in the nearly infinite number of old articles doing it that way usually), bit-banging on 30 year old 8-bit PIC controllers it seems. I can understand it if you're stuck on a existing product that just needs a little more but there are ton's of modern chips for new projects that handle those functions in hardware at lower power with better software efficiency and complexity.
 

John P

Joined Oct 14, 2008
2,014
This would be much easier if you just replace the processor with a modern one that has I2C and SPI built in. But if you want to write your own, SPI is easy and I2C is difficult.
 

Ian0

Joined Aug 7, 2020
8,948
Microchip's is one of the better IIC interfaces. Having dealt recently with Renesas's and NXP's in both cases, I found it easier to write a "bit-banging" routine to do it, and in each case the bit-banging software took less code than the code required to run the built-in interface.
IIC is not a difficult routine to write.
 

BobTPH

Joined Jun 5, 2013
8,120
If it is an old project with an 8-bit micro, I would be surprised if there is enough code space left over to implement SPI and IIC.

Edited to add: And Microchip is very good about keeping pin compatibility across generations of their chips.
 

drjohsmith

Joined Dec 13, 2021
816
I'm going to write software SPI and I2C for PIC 8 bit MCU where I need to write command and read sensor data. I want to know what is the best practice to write such library.
As we can use delay statement for clock high and low state but its not a good practice (sounds to me) because delay statement blocks the execution and I belive it should be interrupt deriven for example I can use TMR0 to manage SCL/SCK state high/sow and on each Timer interrupt I can send and receive data.
I would appriciate if someone could help me on this, what sould be taken care while I'm writing this code for a low power project. If possible give me a small example just to understand the idea.
How much resources do yiu have free?
There are quiet a few libraries out there that have efficient code fir this,
The i2c , I'd look at what do yiu have to support , if it's a known peripheral, at one frequency, and your the master i2c can be simplified quiet a bit,,
 

Thread Starter

sairfan1

Joined May 24, 2012
89
I was able to write SPI that was not very tricky, actually i have lots of devices to update hardware is ready and they have smd components soldered, if i start taking off pic uc and resolder again those PCBs will get messy, so I decided to write software SPI

I did not use timer based interrupt and wanting if someone can advise still i can update my code to use interrupts for data tx/rx and stable timing.
 

nsaspook

Joined Aug 27, 2009
12,329
I was able to write SPI that was not very tricky, actually i have lots of devices to update hardware is ready and they have smd components soldered, if i start taking off pic uc and resolder again those PCBs will get messy, so I decided to write software SPI

I did not use timer based interrupt and wanting if someone can advise still i can update my code to use interrupts for data tx/rx and stable timing.
It's going to be hard to say without you posting the code so others can look at and try it. Interrupts are unlikely to improve much on a simple controller with a typical shift-register SPI emulation as the send and receive are synchronous with the generated clock.
 
Last edited:
Top