|
|
View previous topic :: View next topic |
Author |
Message |
uslimey
Joined: 27 Aug 2011 Posts: 22 Location: Baltimore, MD
|
#USE SPI Issue |
Posted: Tue Aug 09, 2016 7:52 am |
|
|
I have PIC 18F97J60... I am trying to use the SPI with a fixed baud rate, however, when I add BAUD= to the USE SPI statement the code gets stuck in a loop in the USE SPI statement.
My USE SPI statement is as follows...
Code: |
#USE SPI(MASTER, SPI2, BITS=16, BAUD=500000, MODE=1, stream=DLP) |
The code I am trying to execute is as follows:
Code: |
unsigned int32 data=0x40070000;
spi_xfer(DLP,data); |
If I remove the BAUD statement it executes, but is too fast...
It gets stuck in an endless loop here:
.................... #use spi(MASTER, SPI2, BITS=16, BAUD=100000, MODE=1, stream=DLP)
*
00D18: MOVF F66,W
00D1A: MOVFF 2B3,F66
00D1E: RRCF F64,W
00D20: BNC 0D1E
00D22: MOVF F66,W
00D24: MOVWF 01
00D26: MOVFF 2B2,F66
00D2A: RRCF F64,W
00D2C: BNC 0D2A
00D2E: MOVFF F66,00
00D32: RETURN 0
Any thoughts? |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
|
uslimey
Joined: 27 Aug 2011 Posts: 22 Location: Baltimore, MD
|
|
Posted: Tue Aug 09, 2016 5:54 pm |
|
|
So I believe the spi_xfer function takes either an int8 or an int32... If you want to send a different number of bits - either 16 or 24 then you specify that in the #USE_SPI statement. So I don't think that is a problem.
I did a little more investigating today, and I can set a baud rate if I use the force_sw directive.
I also looked in the datasheet for the 18F97J60 and it says that the hardware baud rate is generated with timer2... I wonder, although I haven't checked yet, whether this is the same timer used by the Ethernet Stack for tick timing... I suspect it might be, which would explain the issue...
BTW... latest version of the compiler... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Wed Aug 10, 2016 12:47 am |
|
|
No.
You have to specify how many bits it is to send. The maximum (and default) is the value in the #use statement.
Currently your code is set to clock out 16bits from the int32.
Code: |
#use spi(MASTER, SPI2, BITS=32, BAUD=100000, MODE=1, stream=DLP)
//or omit the bits - it will default to 32
unsigned int32 data=0x40070000;
spi_xfer(DLP,data); //will now clock 32bits. To send 8, use
spi_xfer(DLP, 8bitval, 8); //will send 8bits
|
As asmboy has pointed out, you need to give us _data_.
It is likely that timer2 may be an issue, but it actually depends on your clock rate.
The chip can generate SPI from the master clock, but only at specific divisions of this clock. Otherwise it has to use timer2 as the BRG.
Code: |
0011 = SPI Master mode, Clock = TMR2 output/2
0010 = SPI Master mode, Clock = FOSC/64
0001 = SPI Master mode, Clock = FOSC/16
0000 = SPI Master mode, Clock = FOSC/4
|
If (for instance) your CPU clock is 20MHz, then it can generate SPI at 1250000bps or 312500bps without using Timer 2. The default will be Fosc/4. So with a 20MHz master clock, 5MHz. |
|
|
|
|
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
|