Page 1 of 1

PIC18F25K42 problems with SPI

Posted: Wed Apr 20, 2022 1:42 pm
by Bruno
Hi at all
I have a configuration problem with the SPI module
of the PIC18F25K42 that I have chosen because it has the ADC with 12bit resolution.
The SPI, if configured as a hardware module does not work and blocks the cycle, if configured as software it works well.
I2C also has the same problems.
Is there anyone who has had the same problems?

Thank you

Re: PIC18F25K42 problems with SPI

Posted: Wed Apr 20, 2022 3:10 pm
by medelec35
Hi Bruno.
If you want to use both SPI and I2C in hardware mode then that would not be a possibility with PIC18FXXK42 range of devices.
This is because the I2C hardware was dropped due to the registers being completely different to all other devices.
The K42 range was only added as it was a user request.
As an alternative what about the K22 or K80 range?
If you want to sick to K42 then you will need to stay with I2C software mode.

Re: PIC18F25K42 problems with SPI

Posted: Wed Apr 20, 2022 3:51 pm
by Bruno
Hi Martin
Thanks for your reply, although I was hoping you would give better news !!!! :( :(
I need fifteen 12bit ADCs and unfortunately there aren't many with these features.
For now, I'll use the software SPI, then I'll try to figure out how to configure the hardware

best regards

Re: PIC18F25K42 problems with SPI

Posted: Wed Apr 20, 2022 6:04 pm
by medelec35
I did look up 12bit adc before replying. I have narrowed the search down to PIC18F25K80 or PIC18F26K80 as they both have 28pins and both have 12Bit ADC
There are more devices.
What I did was to go to MICROCHIP ADVANCED PART SELECTOR
Select CPU Type 8-bit MCU
Pin count 28 , 28
ADC Resolution 12,12
A total of 31 parts was found
After adding the second lot of 12 the K22 range disappeared, so could have been a bug.
I hope you find this more helpful?

Re: PIC18F25K42 problems with SPI

Posted: Wed Apr 20, 2022 9:34 pm
by Bruno
Thanks Martin
I visited the Microchip site, but the K80 series only has eight 12bit ADCs and I need sixteen ADCs.
The others, unfortunately, are not implemented in FC, such as Pic16F18456 or PIC18F25Q43

Re: PIC18F25K42 problems with SPI

Posted: Wed Apr 20, 2022 11:06 pm
by Bachman
C code, SPI example (check datasheet for explanation):

Setup SPI module:

Code: Select all

SPI1CON1 = 0b11000000;
SPI1CON2 = 0b00000011;
SPI1CLK = 0;
SPI1BAUD = 39;
SPI1CON0 = 0b10000011;
Using SPI module:

Code: Select all

cs = 0;
SPI1TXB = 0;
while (!PIR3bits.SPI1RXIF);
data_0 = SPI1RXB;
SPI1TXB = 0;
while (!PIR3bits.SPI1RXIF);
data_1 = SPI1RXB;
cs = 1;
cs: Chip select, active low. Use a simple output for it. Before setup, make sure to set the pin to input or output, set the PPS registers.

Re: PIC18F25K42 problems with SPI

Posted: Thu Apr 21, 2022 7:32 am
by Bruno
Hi Bachman
Thank you for your suggestion
I would like to understand if it is possible to integrate the C code in the PIC_CAL_SPI :D :D
best regards

Re: PIC18F25K42 problems with SPI

Posted: Mon May 02, 2022 10:03 am
by Bruno
Hi at all
I think I have solved the problem of the "Standard" configuration of the SPI module of the Pic 18Fxx42 series, thanks also to Bachman's suggestion. The only problem is the configuration of the prescaler which has a different setting from the other PICs.
I forced the default values that are set in the SPI configuration mask: for example, the value to pass to the variable PR_SCALE, is 15 to have a x32 division factor, as also shown in the photo.
It would be nice if the FC team modified the PIC_CAL_SPI.c file to add the few instructions.
Below is the C code to add at the beginning of the program.

best regards

/ *
Set_SPI_1
* /
SPI1CON2 = 0x03; // 0b00000011
SPI1CLK = 0x00;
SPI1CON0 = 0x83; // 0b10000011