Send a byte on a pin

Thread Starter

pjreijiri

Joined Aug 19, 2015
101
Hello everyone,
I am working on a project and I need your help with a project I am working on.
I have MPLab on Mac using XC8. Also I am using PIC16F887
I want to control a DMX512 device. (for those who don't know what a DMX512 protocol is, check this link http://www.dmx512-online.com/packt.html)
I am trying to send information to my DMX512 controller, but I need to be send a byte (0-255). Is there a way to set up a pin to send it without using the TX pin (because it is already connected to a bluetooth module)
So I need the pin to transform the byte to bits and send 8 bits to the DMX controller.

I hope I was clear and thanks for your help in advance.
Regards
 

Thread Starter

pjreijiri

Joined Aug 19, 2015
101
yes but i have any number between 0 and 255 which means 00000000 and 11111111. I have 256 different combinations. definitely there is something that can translate the number I have to the corresponding bit-banging instead of writing a code for every number or combination.
 

shteii01

Joined Feb 19, 2010
4,644
yes but i have any number between 0 and 255 which means 00000000 and 11111111. I have 256 different combinations. definitely there is something that can translate the number I have to the corresponding bit-banging instead of writing a code for every number or combination.
It is called function library. If nobody before you have written one, then you be the first and blaze the trail for the generations to come.
 

shteii01

Joined Feb 19, 2010
4,644
Also, doing 256 cases is valid way to do it, but, like you said, a lot of work.
You should be able to do it smarter than that, you should be able to send 8 bit binary to the function, the function runs through the individual bits. If bit is 1, turn pin On. If bit 0, turn pin Off. I am thinking such a function would have maybe 15-20 lines of code.
 

Thread Starter

pjreijiri

Joined Aug 19, 2015
101
There is this function that I found on wikipedia but to be honest I am not an expert in coding, I am still learning, so if anyone can explain to me what it means maybe I can build up from here:

Code:
// transmit byte serially, MSB first
void send_8bit_serial_data(unsigned char data)
{
   int i;

   // select device
   output_high(SD_CS);

   // send bits 7..0
   for (i = 0; i < 8; i++)
   {
       // consider leftmost bit
       // set line high if bit is 1, low if bit is 0
       if (data & 0x80)
           output_high(SD_DI);
       else
           output_low(SD_DI);

       // pulse clock to indicate that bit value should be read
       output_low(SD_CLK);
       output_high(SD_CLK);

       // shift byte left so next bit will be leftmost
       data <<= 1;
   }

   // deselect device
   output_low(SD_CS);
}
Can anyone explain this "if (data & 0x80)" This might solve my problem.
 

JWHassler

Joined Sep 25, 2013
306
yes but i have any number between 0 and 255 which means 00000000 and 11111111. I have 256 different combinations. definitely there is something that can translate the number I have to the corresponding bit-banging instead of writing a code for every number or combination.
Please: do look up what 'bit-banging' means
 

AlbertHall

Joined Jun 4, 2014
12,347
Can anyone explain this "if (data & 0x80)" This might solve my problem.
data & 0x80 does a bitwise 'and' function between the data value and 0x80 ( = binary '10000000'). So if bit 7 of data is set the result is 0x80, otherwise the result is zero. So it checks the most significant bit of data. Then later we have data <<= 1; which shifts the bits of data one place to the left, ready to check the next bit of data.
 

Thread Starter

pjreijiri

Joined Aug 19, 2015
101
So just to see if I got it right. It checks bit 7, compares it with 0x80, if both are 1 (meaning the bit 7 in data is 1), then data<<=1, it moves wherever the value of bit 6 into bit 7 and then compares it. Is that right?
 

AlbertHall

Joined Jun 4, 2014
12,347
Yup :)
Edited to add:
I'll expand data & 0x80. Let's say data is 10101010
data 10101010
0x80 10000000 then 'and these tow together bit by bit
= 10000000 as this is not zero, 'C' counts it as true
data <<= 1 left shift data
data 01010100 '0' is shifted into bit 0
0x80 10000000 repeat the bit by bit 'and'
= 00000000 as this is zero, 'C' treats it as false
... and so on ...
 
Last edited:

Thread Starter

pjreijiri

Joined Aug 19, 2015
101
Kjeldgaard, this is really good. But I am working with C and I know only the very basics of assembly.
Albert, thank you for your help, this solved my question
 

Thread Starter

pjreijiri

Joined Aug 19, 2015
101
would they work also on MPLAB?
I put it and it gave me an error after trying to build it.
if I just put this, won't it be enough?
Code:
            for (int i = 0; i < 8; i++){
                if (data & 0x80)
                PORTCbits.RC2=1;
                else
                PORTCbits.RC2=0;
                data <<= 1;
                }
The data<<=1; will let the clock forward while the port is set to HIGH or LOW
 

Sensacell

Joined Jun 19, 2012
3,453
DMX runs at 250,000 baud.

Bit banging at that speed is not going to be so easy.

4 Microseconds per bit...

Swap the UART to the DMX and bit-bang the Bluetooth.
 

Thread Starter

pjreijiri

Joined Aug 19, 2015
101
well that opens up a lot more questions for me.
Can you help me figure out the bit-bang sequence for the receiving?
I can store the bits in an array then combine to receive the byte (which I don't know how to make). You tell me if there is another way.
also how will I be able to get the timing right for this process?
 

atferrari

Joined Jan 6, 2004
4,771
Can you help me figure out the bit-bang sequence for the receiving?
If you are sending bytes as per a established protocol, your receiver should be suitable to interpret it.

Or are you trying to implement both ends?

/EDIT to add

You said in the OP that you have a controller.

I suggest you draw the sequence of bits yourself for a certain value as it should be produced by your pin and then imagine if you have to code it, no matter what ridiculous it could look.

The document that you recommended us to check, you should read again, I think.
EDIT/
 
Last edited:
Top