View previous topic :: View next topic |
Author |
Message |
georpo
Joined: 18 Nov 2008 Posts: 281 Location: Athens, Greece.
|
PIC24FJ128GA204 SPI |
Posted: Fri May 22, 2020 12:56 am |
|
|
Hello!
I am using PIC24FJ128GA204 with SPI but I have some problems.
Code: |
#include <24FJ128GA204.h>
#fuses HS,NOWDT,DEBUG,NOPROTECT,NOPR,FRC_PLL,OSCIO
#use delay(clock=32M,int)
#pin_select U1TX = PIN_C9
#pin_select U1RX = PIN_C8
#pin_select SDO1 = PIN_B15
#pin_select SCK1OUT = PIN_B13
#pin_select SDI1 = PIN_B12
#use rs232(BAUD=115200,UART1)
#use fast_io(A)
#use fast_io(B)
#use fast_io(C)
#word PORTA=getenv("SFR:PORTA")
#word PORTB=getenv("SFR:PORTB")
#word PORTC=getenv("SFR:PORTC")
#word LATA=getenv("SFR:LATA")
#word LATB=getenv("SFR:LATB")
#word LATC=getenv("SFR:LATC")
#word TRISA=getenv("SFR:TRISA")
#word TRISB=getenv("SFR:TRISB")
#word TRISC=getenv("SFR:TRISC")
#word SPI1BRGL=getenv("SFR:SPI1BRGL")
void Main(){
TRISA=0b0000000000000000;
TRISB=0b0001000100000000;
TRISC=0b0000000100000000;
LATA=0;
LATB=0;
LATC=0;
setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_XMIT_L_TO_H);
SPI1BRGL=0;
while(1){
spi_read(85);
delay_us(100);
}
}
|
The code above works and the SPI clock is according to the SPI1BRGL register.
But if I try this:
Code: |
setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_XMIT_L_TO_H SPI_CLK_DIV_2);
|
No matter the SPI_CLK_DIV_ X value, I see no clock output!
Compiler version 5.049
Any ideas? _________________ George. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Fri May 22, 2020 1:01 am |
|
|
| 'or' missing in what you post:
georpo wrote: | setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_XMIT_L_TO_H SPI_CLK_DIV_2); |
Needs to be:
Code: | setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_XMIT_L_TO_H | SPI_CLK_DIV_2); |
Note the extra | |
|
|
georpo
Joined: 18 Nov 2008 Posts: 281 Location: Athens, Greece.
|
|
Posted: Fri May 22, 2020 1:11 am |
|
|
Yes of course.
I typed the code here as an example and made a typo.
Of course it is:
Code: |
setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_XMIT_L_TO_H | SPI_CLK_DIV_2);
|
It does not work. _________________ George. |
|
|
georpo
Joined: 18 Nov 2008 Posts: 281 Location: Athens, Greece.
|
|
Posted: Fri May 22, 2020 1:25 am |
|
|
Also just for the record this works:
Code: |
CNPU2|=0x20; //enable PIN_B9 (CN21) pull-up
|
This does not work.
Code: |
SET_PULLUP(PIN_B9); //enable PIN_B9 (CN21) pull-up
|
_________________ George. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Fri May 22, 2020 1:39 am |
|
|
OK.
The reason is simple. Those options don't exist....
If you look at the datasheet for that PIC, it doesn't have a prescaler
option for the SPI. The #defines were removed almost immediately
afterwards (5.053 doesn't have them). The bit patterns being loaded
by the incorrect entries do things like disable the clock output....
You can't use a prescaler on this clock.
The reason is that the chip has a 16bit BRG, so shouldn't need a
prescaler. The people setting up the files at CCS made a mistake
including this option, and realised it soon afterwards. |
|
|
georpo
Joined: 18 Nov 2008 Posts: 281 Location: Athens, Greece.
|
|
Posted: Fri May 22, 2020 1:52 am |
|
|
As always Ttelmah you know everything and I thank you for this
So for my case I should just use the SPI1BRGL register. Right? _________________ George. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Fri May 22, 2020 2:56 am |
|
|
Or just use setup_spi.
setup_spi(1000000);
Gives you 1Mbps on the SPI (if the hardware supports it).
Etc. etc., for whatever baud rate you want.
Means you don't have to be working out BRG values yourself. |
|
|
georpo
Joined: 18 Nov 2008 Posts: 281 Location: Athens, Greece.
|
|
Posted: Fri May 22, 2020 3:14 am |
|
|
This did not work. _________________ George. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Fri May 22, 2020 4:05 am |
|
|
Yes, just checked with your compiler, and this makes the same mistake
as for the SPI_CLK_DIV_2. It tried to write a prescaler to the SPI1CON1
register. A couple of versions later, both faults are fixed... |
|
|
|