Help speed up my SPI

MMcLaren

Joined Feb 14, 2010
861
Just had a few hours trying to get hardware SPI working on a PIC18LF26K22. If anyone is interested, here is how you write to the SPI bus:

Rich (BB code):
    SCE = 0;            // Chip enable

    PIR1bits.SSP1IF = 0;            // clear the SSP transmit finished interrupt flag
    SSP1CON1bits.WCOL = 0;          // clear write collision bit SSP1
    SSP1BUF = data;                 // Load data into buffer SSP1
    while(!PIR1bits.SSP1IF);        // wait for SSP interrupt flag to say transmit is finished

    SCE = 1;            // Chip enable
The while (!BF) doesn't seem to work on the 18F PIC I'm using, so here I clear the 'transmit finished' interrupt flag, load the buffer, then wait for the flag to set before continuing. Then clear it again.
Craig,

May I ask what speed you're using for SPI, please? It seems that if you're using Fosc/4 that each byte could be sent in something like eight instruction cycles (plus a few cycles to check flags) which would have to be faster than your bit-banged SPI routine.

Also, what SPI mode did you use? John_P's example looks like mode 0 (CKP = 0, CKE = 1).

Regards, Mike

 
Last edited:

Thread Starter

portreathbeach

Joined Mar 7, 2010
143
Hi Mike. I'm using FOSC/4 but I think the speed could be increased if I check for the transfer complete before I load the buffer, then it can transfer the data while my program is getting the next byte from an array, rather than loading the buffer and editing for transfer complete.
 

Markd77

Joined Sep 7, 2009
2,806
Looks nice. I wonder if you could make a variable temperature version, seems like it might be useful for that sort of thing. You could turn the power off for a few milliseconds every now and then and measure the resistance, if you have the resistance / temperature curve for the heating element you can work out how hot it is.
 

Thread Starter

portreathbeach

Joined Mar 7, 2010
143
Yeah, that could be done, but all the heating coils that are used in these devices are home made. They are just a few wraps of kanthol heating wire. I suppose, you could take a cold reading and then when it's being fired, keep reading it and work out the temperature. Trouble is, as it's a coil, there would be inductance to think about too.
 

Markd77

Joined Sep 7, 2009
2,806
I just realised that I failed to spot the obvious. Because you are constantly measuring current and voltage, you already know the hot resistance. To get the cold resistance you just need to be able to make those measurements quickly before the coil heats up. I doubt that the inductance of a few coils of wire without a magnetic core would make any difference after a few microseconds. It could have the benefits of faster heat up time (can be run at full power until the coil is hot) and you could swap coils without the resistance change making a big difference.
 

THE_RB

Joined Feb 11, 2008
5,438
Why the need for a cold reading? Just measure the hot resistance (with current and voltage data) and use a lookup table to show what temperature it is at.
 

Thread Starter

portreathbeach

Joined Mar 7, 2010
143
Hi,

I actually only read the cold resistance. I blip it for 50ms and take the current. It's not that critical really. Even after the coil has heated up, it's usually only about 0.1 ohms different anyway.
 
Top