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

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:

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

Post 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

User avatar
Steve
Matrix Staff
Posts: 3422
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Post 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.

Post Reply