View previous topic :: View next topic |
Author |
Message |
Sigma
Joined: 03 May 2004 Posts: 48 Location: Sg
|
SPI speed question |
Posted: Mon Sep 20, 2004 11:08 pm |
|
|
Hi, all,
I am new with SPI. I am now interfacing with a part with SPI interface.
According to the datasheet, its recommended speed is 115khz. How to set this speed using setup_spi()? Is it using spi_clk_div_16... these options? I did not see any clarification of the usage of these options on the PICC manual. Can anyone help?
Thanks.
Sigma |
|
|
Sigma
Joined: 03 May 2004 Posts: 48 Location: Sg
|
|
Posted: Tue Sep 21, 2004 1:09 am |
|
|
By the way, my mcu freq. is 4Mhz. And also in the datasheet, it mentions something like 'The interval between the host lowering /SS and then generating the first SCK pulse should be a minimum of 3us.' How can this be achieved?
Sigma |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Sep 21, 2004 3:00 am |
|
|
You didn't mention which processor you are using, but as most PICs are using the same hardware modules I will continue with the most common type.
The frequency generated for the SPI-bus is selected in the function setup_spi(). Possible options are: SPI_CLK_DIV_4, SPI_CLK_DIV_16,
SPI_CLK_DIV_64 and SPI_CLK_T2.
At a clock speed of 4MHz this will give you the following SPI-frequencies:
Code: | SPI_CLK_DIV_4: 1MHz
SPI_CLK_DIV_16: 250kHz
SPI_CLK_DIV_64: 62,5kHz
SPI_CLK_T2: configurable 15Hz to 1MHz |
Quote: | 'The interval between the host lowering /SS and then generating the first SCK pulse should be a minimum of 3us.' How can this be achieved? |
I guess /SS is the chip-select pin for your device? This is than connected to an I/O pin on the PIC, so you have total control over it. Just after lowering this output you will have to add a call to delay_us(3). |
|
|
Sigma
Joined: 03 May 2004 Posts: 48 Location: Sg
|
|
Posted: Tue Sep 21, 2004 4:33 am |
|
|
Hi, ckielstra,
How to calculate SPI_CLK_T2 for 115khz using PIC16F76?
I saw some sample code as followed for 1khz.
What is the t2div?
Is it the '16' inside setup_timer_2(T2_DIV_BY_16,249,1)'s T2_DIV_BY_16?
Quote: |
setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_CLK_T2);
// System 1ms tick.
// The cycle time will be (1/clock)*4*t2div*(period+1)
// In this program clock=16,000,000 and period=249
// (1/16000000)*4*16*250=1000 us or 1kHz)
setup_timer_2(T2_DIV_BY_16,249,1);
|
Thanks.
Sigma |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Sep 21, 2004 5:39 am |
|
|
What kind of device are you using? I think it is very strange to be talking about a 'recommended' speed of 115kHz, most datasheets will specify a 'maximum' bus speed instead.
I would recommend to make life easier on yourself and forget about using timer2. Use the lower bus speed of 62kHz instead as this will make your application less time critical and you can use the timer2 for other functions in your program or turn it of to save power. Later when your application is working stable and you decide you need the extra performance you can always do the speed optimization. |
|
|
Sigma
Joined: 03 May 2004 Posts: 48 Location: Sg
|
|
Posted: Tue Sep 21, 2004 7:11 pm |
|
|
It is some kind of RFID module. Actually i tried spi_clk_div_64, yet not working. |
|
|
|