problem with spi port

For C and ASSEMBLY users to post questions and code snippets for programming in C and ASSEMBLY. And for any other C or ASM course related questions.

Moderators: Benj, Mods

Post Reply
David Halliday
Posts: 9
Joined: Tue Jan 10, 2006 4:36 pm
Location: Hampshire
Contact:

problem with spi port

Post by David Halliday »

Hi all

Does someone have a simple piece of code written in C on how to operate the SPI port. I have downloaded the data manual for the PIC16F877A but being a bit simple. I am still confused.

Here is what I've done so far


#include<system.h>

void main()
{

Trisc= 11000111; \\ setup port c
sspstat= 11000000; \\ set sspstat as manual show
sspcon= 00110010; \\ Enable serial port and set clock speed to fosc/64.

sspbuf=10101010; \\ Test data to be sent.

}

This is as far as I could work out . I know I need to send the data out of RC5 somehow but am not sure how to go about this.
I have tried the bit banging method which you helped me with before which was greatly appreciated(which work well). But would really like to learn more about spi port.



All help greatly received Dave Halliday :?:

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Post by Benj »

Hi David

Sorry to take so long getting back to you. I have this piece of C code for the SPI port that I think you should find useful. It is designed for use with a PIC16F877A so if you intend to use a different PIC then you will probably need to configure the TRIS differently etc.

/*********************
//Initialise SPI Module
//*********************
void FCD_SPI0_SPI_Init()
{
clear_bit(trisc, 2); //CS MMC Enable
clear_bit(trisc, 3); //SCK Output bit3
set_bit (trisc, 4); //SDI Input bit4
clear_bit(trisc, 5); //SDO Output bit5

portc = 0x04;

sspstat = 0x00; // Input In the middle of the clock
sspcon = 0x12; // Passive High f/64(307.2kHz), Master
set_bit (sspcon, 5); // SPI Enable
}


//**************************
//Send Character through SPI
//**************************
char SPI(char d) // send character over SPI
{
sspbuf = d; // send character
while ((sspstat & 0x01) == 0); // wait until sent
return sspbuf; // and return the received character
}

Let me know if this information has been useful.

David Halliday
Posts: 9
Joined: Tue Jan 10, 2006 4:36 pm
Location: Hampshire
Contact:

thankyou still appreciated

Post by David Halliday »

:lol:

Hi ben

Thank for the reply. I was trying to program a serial D/A convertor. Although in the end I managed to send data out of the SPI port I still could not get it working properlty. In the end I decided to bit_bang the bits to the device and all worked as expected although the code is alot longer.
But I'm going to have to get to grips with it at some point to learn how to talk/and listen to other device from a master I.C. pretty scared already though. Need to learn a lot more about serial programming.


So thanks again Dave H

Post Reply