Converting a PIC C code to the mbed

08 Dec 2010 . Edited: 08 Dec 2010

Hi All,

I have found a C code working with a PIC processor – 12C508A. I want that code to use with my mbed.
Since I am still learning mbed and C++, I need some help. if someone who familiar with that code syntax and convert it to the mbed syntax or point me a right direction how to make it by myself, I'll be really appreciated.

        void sendbit(int thebit)
        {
            output_high(PICPIN); delay_us(400);
            output_low(PICPIN); delay_us(700);
            if (thebit == 0) { delay_us(1100); }
            return;
        }
        void sendbyte(int thebyte)
        {
            int8 i;
            for (i = 0; i < 8; i++)
            {
                sendbit(shift_left(&thebyte, 1, 0));
            }
            return;
        }

        void senddata() 
       {
            int ufcbyte;
            char hc;

           hc=0b00000011;
           hc=hc&0b10011000;

           sendbyte(hc); 
           sendbyte(~hc);

           ufcbyte=0b10000111;
           ufcbyte=ufcbyte|0b01000000;

           sendbyte(ufcbyte); 
           sendbyte(~ufcbyte);

           sendbit(1);
    
           return;
       }

Sener

 

08 Dec 2010

for send data and send byte, you dont need to change much.  not sure on the shift left function. worse thing, just implent the maths part of left shifting.

 

for the send bit,

delay_us needs to be wait_us  http://mbed.org/projects/libraries/api/mbed/trunk/wait_api is the handbook page dealing with the wait function

for the output pin, use the digital out class(in the handbook)

08 Dec 2010
user Christopher Hasler wrote:

for send data and send byte, you dont need to change much.  not sure on the shift left function. worse thing, just implent the maths part of left shifting.

 

for the send bit,

delay_us needs to be wait_us  http://mbed.org/projects/libraries/api/mbed/trunk/wait_api is the handbook page dealing with the wait function

for the output pin, use the digital out class(in the handbook)

Thanks for your input Christopher. I am also doing some comparison with my findings and more or less I have started to figure out how to replace correspending pieces in C++.

But as you also noticed one of my concerns is;

       shift_left(&thebyte, 1, 0)  ===> ???

and another one is;

           hc=0b00000011;
           hc=hc&0b10011000

I am not sure 0b00000011 value can be used like it is.

Sener

 

09 Dec 2010

intresting, I can't work out how to input binary values. convert to hex, 0x...

left shift is y<<x

y shifted left x times. I think it defaults to using 0's

09 Dec 2010 . Edited: 09 Dec 2010

Hi Christopher,

Here is my findings about the shift_left():

http://ccsinfo.com/downloads/CReferenceManual.pdf

shift_left( )

Syntax: shift_left (address, bytes, value)
Parameters: address is a pointer to memory, bytes is a count of the number of bytes to work with, value is a 0 to 1 to be shifted in.
Returns: 0 or 1 for the bit shifted out
Function: Shifts a bit into an array or structure. The address may be an array identifier or an address to a structure (such as &data). Bit 0 of the lowest byte in RAM is treated as the LSB.

If so, the line <sendbit(shift_left(&thebyte, 1, 0));> would be sendbit(thebyte << 1); is that right?
and how about the <&> before the variable name? what is the function of it?

I think I might live with the above explanation.

And,

for these two lines;

hc=0b00000011;
hc=hc&0b10011000;

I think I can do this conversion (is that correct?)

char hc; ==> int hc; // I think <int> better match than <char> instead?!

hc = 0x03; //0b00000011

then;

hc = hc & 0x98;// 0x98 is for 0b10011000

Thanks,
Sener

09 Dec 2010

Going by the above doc, sendbit(shift_left(&thebyte, 1, 0)) would be:

 // calculate MSB (bit to be shifted out)
 int bit = 0;
 if ( thebyte & 0x80 )  // is MSB set?
    bit = 1;
 // send it
 sendbit(bit);
 // shift the byte one position to the left
 thebyte = thebyte << 1;

However, the whole function can be rewritten in another way:

// send a byte, starting from MSB
void sendbyte(int thebyte)
{
  for (int i = 7; i >= 0; i--)
  {
    sendbit(thebyte & (1<<i));
  }
}
09 Dec 2010

Hi Igor,

It is very neat explanation that you made. I'll give it a try.

to make somethings clear in my mind could you tell me what is byte shift for real? 

In this code snippet you've written above, it appears to be you are just walking in the byte bit by bit.
It is always confusing me the byte shifting. Isn't it just parsing a string e.g. <10011000> into characters like 1-0-01-1-0-0-0?

I read a couple of articles about it but, sometimes it is good to hear from someone who has experience on it.

Thank you very much in advance.

Sener

09 Dec 2010

Let's take your example above.

0b10011000 is binary notation (unsupported in C++, alas). In hex it would be 0x98, just as you wrote. Let's split it into four-bit nibbles for clarity.

1001 1000

When we shift the byte to the left (byte << 1), we literally do just that with the bit string.

  1001 1000
1 0011 000x

The left shift in C fills the new bits at the right with 0 (which happens to match the shift_left call).

1 0010 1000

shift_left() had 1 for the 'bytes' parameter. Since we now have nine bits, we would have to drop the ninth one:

0010 1000

The dropped bit is "shifted out", and shift_left() returns its value.

Since we don't have shift_left(), we have to make an equivalent code. We know that the bit to be shifted out is the leftmost one (also called most significant bit), and we can test its value by ANDing our byte with 0x80 (0b10000000).

I hope this makes it somewhat clearer.

09 Dec 2010

Thank you very much for that precious explanation Igor, I really appreciate for your efforts. This is now very much clear it's never been before.

P.S. Is there typo in the left byte shifting illustration?

  1001 1000   
1 0010 100x === >? 1 0011 000x
09 Dec 2010

Yep, good catch!