Page 1 of 1

Sending 8-bits of serial data for D/A convertor

Posted: Wed Jun 07, 2006 9:21 am
by David Halliday
Hi
Does anyone have any examples on how to send serial data out of a port in C. In the manual it tells you to use
SPI mode (SD0)RC5 to send serial data out of a port but I am not sure how you do this in 'C' ?

I assumed that you would need a loop routine simliar to what I have done but how do you shift the bit out of the port
with each clock cycles falling edge ?




#include <system.h>

int serial;
int i;

char main()
{

set_tris_b(0x00);

for(i=0;i==8;i++)
{
serial=10101010b; //set 8-bits to be outputed



}


any help greatly recieved

Dave

Posted: Wed Jun 07, 2006 4:16 pm
by Steve
If you require SPI comms, then it's best to use the inbuilt SPI module of the micro - look in the PICmicro datasheet for an idea of how to drive SPI comms (the ASM examples should translate ok into C).

If you need to bitbang (LSB first), then here's a quick, untested answer:

Code: Select all

char cDataToSend = 10101010b;

char cMask = 0x01;
while (cMask != 0)
{
  if (cDataToSend & cMask)
  {
    //output high
    set_bit(portc, 5);
  } else {
    //output low
    clear_bit(portc, 5);
  }

  //shift the mask to the next bit
  cMask = cMask << 1;

  //you will also need to add a delay here, 
  //depending on the required speed of 
  //communication...

}
But remember that with SPI, you also need to output a "clock" signal and probably also a "chip select" signal.