Send a byte on a pin

Discussion in 'Programmer's Corner' started by pjreijiri, Jul 12, 2016.

  1. pjreijiri

    Thread Starter Member

    Aug 19, 2015
    60
    1
    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
     
  2. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,908
    378
    If you search for bit-bang serial you will find plenty of code around.
     
  3. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,386
    496
    You just turn the pin On and Off to simulate 0 and 1 bits of the byte. Like Albert said, bit-bang.
     
  4. pjreijiri

    Thread Starter Member

    Aug 19, 2015
    60
    1
    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.
     
  5. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,386
    496
    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.
     
    atferrari likes this.
  6. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,386
    496
    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.
     
  7. pjreijiri

    Thread Starter Member

    Aug 19, 2015
    60
    1
    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 (Text):
    1. // transmit byte serially, MSB first
    2. void send_8bit_serial_data(unsigned char data)
    3. {
    4.    int i;
    5.  
    6.    // select device
    7.    output_high(SD_CS);
    8.  
    9.    // send bits 7..0
    10.    for (i = 0; i < 8; i++)
    11.    {
    12.        // consider leftmost bit
    13.        // set line high if bit is 1, low if bit is 0
    14.        if (data & 0x80)
    15.            output_high(SD_DI);
    16.        else
    17.            output_low(SD_DI);
    18.  
    19.        // pulse clock to indicate that bit value should be read
    20.        output_low(SD_CLK);
    21.        output_high(SD_CLK);
    22.  
    23.        // shift byte left so next bit will be leftmost
    24.        data <<= 1;
    25.    }
    26.  
    27.    // deselect device
    28.    output_low(SD_CS);
    29. }
    Can anyone explain this "if (data & 0x80)" This might solve my problem.
     
  8. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,386
    496
    Masking I think.
     
  9. JWHassler

    Member

    Sep 25, 2013
    201
    33
    Please: do look up what 'bit-banging' means
     
  10. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,908
    378
    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.
     
    pjreijiri likes this.
  11. pjreijiri

    Thread Starter Member

    Aug 19, 2015
    60
    1
    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?
     
  12. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,908
    378
    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: Jul 13, 2016
    pjreijiri likes this.
  13. Kjeldgaard

    Member

    Apr 7, 2016
    71
    16
    How about this some inspiration from this app. note: AN1076 "Using a PIC Microcontroller for DMX512 Communication"
     
    pjreijiri likes this.
  14. pjreijiri

    Thread Starter Member

    Aug 19, 2015
    60
    1
    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
     
  15. pjreijiri

    Thread Starter Member

    Aug 19, 2015
    60
    1
    one more question, for MPLAB XC8, do I need to let the clock advance one step?
     
  16. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,908
    378
    These lines handle the clock transition:
    // pulse clock to indicate that bit value should be read
    • output_low(SD_CLK);
    • output_high(SD_CLK);
     
  17. pjreijiri

    Thread Starter Member

    Aug 19, 2015
    60
    1
    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 (Text):
    1.             for (int i = 0; i < 8; i++){
    2.                 if (data & 0x80)
    3.                 PORTCbits.RC2=1;
    4.                 else
    5.                 PORTCbits.RC2=0;
    6.                 data <<= 1;
    7.                 }
    The data<<=1; will let the clock forward while the port is set to HIGH or LOW
     
  18. Sensacell

    Well-Known Member

    Jun 19, 2012
    1,128
    266
    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.
     
    JWHassler likes this.
  19. pjreijiri

    Thread Starter Member

    Aug 19, 2015
    60
    1
    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?
     
  20. atferrari

    AAC Fanatic!

    Jan 6, 2004
    2,648
    760
    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: Jul 13, 2016
Loading...