 |
 |
| View previous topic :: View next topic |
| Author |
Message |
RckRllRfg
Joined: 20 Jul 2011 Posts: 60
|
| Revisiting the SPI |
Posted: Wed Nov 09, 2011 2:18 pm |
|
|
Hi everyone -
I spent some time to revisit the SPI on the 16F1947. I was able to get things up and running using the setup_spi command:
| Code: |
#define SPI_DO PIN_C5
#define SPI_DI PIN_C4
#define SPI_CLK PIN_C3
#define SPI_CS PIN_C2
#define SPI_LOAD PIN_A5
unsigned int16 xcount = 0;
unsigned int16 ycount = 0;
unsigned int8 lobyte = 0;
unsigned int8 lobyte2 = 0;
unsigned int8 hibyte = 0;
unsigned int8 hibyte2 = 0;
setup_spi(SPI_MASTER | SPI_CLK_DIV_4 | SPI_L_TO_H | SPI_XMIT_L_TO_H);
output_low(SPI_LOAD); //Keeping this permanently low. No need to toggle
|
In the while loop of the main:
| Code: |
lobyte = xcount;
lobyte2 = ycount;
hibyte = xcount>>8; // Push the high byte of xcount into hibyte
hibyte2 = ycount>>8; // Push the high byte of ycount into hibyte
hibyte = hibyte & 0x0F; // Clear the upper nibble
hibyte = hibyte | 0x10; // Write command '1' to enable DACa, 2x Voltage
// and preserve the rest of the word
hibyte2 = hibyte2 & 0x0F; // Clear the upper nibble
hibyte2 = hibyte2 | 0x90; // Write command '1' to enable DACb, 2x Voltage
output_low(SPI_CS);
spi_write(hibyte);
spi_write(lobyte);
output_high(SPI_CS); // needs to be toggled for Channel B
output_low(SPI_CS);
spi_write(hibyte2);
spi_write(lobyte2);
output_high(SPI_CS);
xcount++;
ycount--;
|
The time for all 32 bits is 18us. This equates to approximately 200ns per clock. I wish that I could trim some of dead time between the CS rersponse; however, that would only buy me 1us or 2. I did play with the Timer2 option that is allowed for the SPI; however, the fastest I could push it was approximately 40us. The timer2 is divided by 2 regardless of any additional divide requests in the setup function. Any values I entered in the function that were lower than the code presented below prevented the clock from running or generated an error.
| Code: |
setup_timer_2(T2_DIV_BY_1,0x01,1);
setup_spi(SPI_MASTER | SPI_CLK_T2 | SPI_L_TO_H | SPI_XMIT_L_TO_H);
|
I am good with these results for now. I may ask the designer to increase the clock to 25MHz since the 16F1947 can handle up to 32MHz. But that's another story.
I need to move on to the DACs....
Regards -
RckRllRfg |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20161
|
|
Posted: Wed Nov 09, 2011 4:27 pm |
|
|
20MHz.
TMR2/2 - timer2 clocks off Fosc/4. So this will be slower than running off the standard Fosc/4.
Fosc/4 is fastest clock available -> 5Mhz. 200nSec/bit.
Remember you can halve the instructions used to actually drop/raise CS, by using fast_io mode.
Use make8, to get the high/low byte.
Best Wishes |
|
 |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Fri Nov 11, 2011 9:44 am |
|
|
| Ttelmah wrote: | | Remember you can halve the instructions used to actually drop/raise CS, by using fast_io mode. | Actually, the improvement is even better because you go from 4 instructions down to 1.
With the CS-line being modified 4 times inside the loop this results in an improvement of 4 x 3 instructions = 12 instructions.
At 20MHz that saves you 12 * 0.2us = 2.4us
| RckRllRfg wrote: | | I may ask the designer to increase the clock to 25MHz since the 16F1947 can handle up to 32MHz. | Probably there is a reason for your designer to have choosen 20MHz, this is the highest allowed crystal frequency in HS mode. For higher clock speeds you will have to use a slower crystal + 4x-PLL fuse, for example 32MHz is achievable by using an 8 MHz crystal + HSPLL fuse. |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|