romel_emperado
Advanced Member level 2
- Joined
- Jul 23, 2009
- Messages
- 606
- Helped
- 45
- Reputation
- 132
- Reaction score
- 65
- Trophy points
- 1,318
- Location
- philippines
- Activity points
- 6,061
this is very generic to pic16 but concept is almost same...
**broken link removed**
**broken link removed**
When CKE=0, transmit occurs on transition from idle to active clock state, andwhen CKE=1, transmit occurs on transition from active to idle clock state. Bit 0 (BF) isthe buffer full status bit. When BF=1, receive is complete (i.e., SSPBUF is full), and whenBF=0, receive is not complete (i.e., SSPBUF is empty)
no I was wrong.. it does not need to toggle manually the clock 8 times.. I verified it using oscilloscope of proteus..what you said is correct about toggling the clock..
check the second link i have posted... i was searching it for you rom a long time... one the the most informative SPI document...
toggling cannot be seen in proteus... they are quite fast..
You can also use a digital graph with a voltage probe at the clock, you can view it very well there
Alex
Well, at least you know your routines were written correctly.
Or did you use the Hi-Tech PIC18 Libraries?
What was the first device you attempted to interface with SPI?
#include<htc.h>
unsigned char msb , lsb;
unsigned int x,y;
void spi_init();
unsigned char read_sensor();
void spi_init();
void main()
{
spi_init();
TRISD = 0;
while(1)
{
PORTD = read_sensor();
for(y=0; y<5000; y++);
}
}
void spi_init()
{
SMP = 1; //Input data sampled at end of data output time
CKE = 0; //Transmit occurs on transition from Idle to active clock state
CKP = 0; //Idle state for clock is a low level
SSPEN = 1; //Enables serial port and configures SCK, SDO, SDI and SS as serial port pins
SSPCON1 |= 0b0000; // SPI Master mode, clock = FOSC/4
TRISCbits.RC7 = 0; //Serial Data Out (SDO)
TRISBbits.RB0 = 1; //Serial Data In (SDI)
TRISBbits.RB1 = 0; //Serial Clock (SCK)
TRISCbits.RC0 = 0; //chip select
}
void spi_send(unsigned char data)
{
SSPBUF = data;
while(!BF);
}
unsigned char read_sensor()
{
char dummy;
RC0 = 1; // Enable sensor
spi_send(0x80);
// dummy = SSPBUF;
spi_send(0x11);
RC0 = 0;
// dummy = SSPBUF;
for(x=0; x<3000; x++);
RC0 = 1;
spi_send(0x02); //Read MSB temperature address
// dummy = SSPBUF;
spi_send(0x00); //Read temperature high
msb = SSPBUF;
spi_send(0x00);
lsb = SSPBUF;
RC0 = 0;
return msb;
}
I write my own functions based on my understanding how SPI works as described in the datasheet.
My code is very basic... take a look.. I interface it using TCP72 SPI temperature sensor
I doubt that my previous code doesn't work because I did not test it with other device.. hmmm anyway I have my next target.. I will try to write/read in SDcard..
With live hardware or simulation? How about trying a SPI EEPROM?
I think you might find the 18F4550 a little more challenging in actual hardware, but we'll see.
Proteus is very forgiving as far as the little details, compared to implementing an actual circuit in physical hardware.
Its hard to simulate SPI protocol without the actual hardware.. so you need an external interface to get the communication and see the output.........
With live hardware or simulation? How about trying a SPI EEPROM?
There is an SPI debugger in proteus , connect the Din, Dout, SCK, SS and you can easily see the spi bytes that were transmitted and if the timings were valid, you can also use it while there are other spi devices connected to the mcu.
There are also many eeprom models, you can connect any of them and watch the internal eeprom memory as it is written (from the debug menu show spi memory of the chip)
To clear the eeprom contents (because they are persistent in the eeprom case) go to debug menu -> reset persistent model data
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?