1 2 Previous Next 17 Replies Latest reply on Jun 24, 2019 9:22 PM by MoTa_728816

    How to send 1 byte as 8 bits

    BUTA_1301626

      I have a TX pin. I want to send hexadecimal AA information to this pin. When I look at the logic analyzer, I want to see the logic of 10101010. How can I do that?

        • 1. Re: How to send 1 byte as 8 bits
          JoMe_264151

          You can use a UART component which will do all you want. Just UART_Start() and send your byte. Have a look at UART datasheet to see all the facilities offered.

           

          Bob

          • 2. Re: How to send 1 byte as 8 bits
            BUTA_1301626

            When I use uart, the start and end bits are added.  I need to use uart. but I need to find a different solution because of the start and end bit.

            • 3. Re: How to send 1 byte as 8 bits
              JoMe_264151

              Use a shift rergister component.

               

              Bob

              • 4. Re: How to send 1 byte as 8 bits
                BUTA_1301626

                I wrote it here because I didn't know how to do it with Shift Register.

                I'd appreciate it if you help.

                • 5. Re: How to send 1 byte as 8 bits
                  JoMe_264151

                  I just see that there is no shift register in PSoC6. Makes things right complicated. Someone else has to jump in to solve that problem.

                   

                  Bob

                  • 6. Re: How to send 1 byte as 8 bits
                    MoTa_728816

                    Hi,

                     

                    Assuming that we can Write to TX pin by TX_Write(value),

                    If I may write it in a program, it will look like

                    ===============

                    void serialize(uint8_t theByte)

                    {

                        uint8_t mask = 0x80 ; /* 1000_0000 */

                        while(mask) {

                          if (theByte & mask) {

                              TX_Write(1) ;

                           } else {

                              TX_Write(0) ;

                           }

                          mask >>= 1 ;

                          CyDelayUs(1) ; // adjust delay for your purpose

                        }

                    }

                    ===============

                     

                    moto

                    1 of 1 people found this helpful
                    • 7. Re: How to send 1 byte as 8 bits
                      BUTA_1301626

                      void serialize(uint8_t theByte)

                      {

                          uint8_t mask = 0x80 ; /* 1000_0000 */

                          while(mask) {

                            if (theByte & mask) {

                                TX_Write(1) ;

                             } else {

                                TX_Write(0) ;

                             }

                            mask >>= 1 ;

                            CyDelayUs(1) ; // adjust delay for your purpose

                          }

                      }

                       

                      it works only if I use the tx pin. but does not work when connected to uart_1.I want to send both data only to the pin tx (uart data = start bit data stop bit). So I want it to work when connected to uart_1.

                      Ekran Alıntısı2.JPG

                      • 8. Re: How to send 1 byte as 8 bits
                        MoTa_728816

                        Hi,

                         

                        In your question you wrote 0xAA -> 10101010

                        this is called "MSB First"

                         

                        But usually UART sends "LSB First", wihch means

                        if you use UART_1_PutChar(0xAA) ;

                        tx pin will generate (start bit) 01010101 (stop bit)

                         

                        So you must convert your data from 0xAA to 0x55 before calling UART_1_PutChar(data) ;

                        Except this, you can send byte data by

                        (1) call UART_1_Start() at the beginning of your program

                        (2) put data with reversed bit order

                        (3) then call UART_1_PutChar(data) ;

                         

                        If your application can put up with LSB First you don' t have to do (2)

                         

                        moto

                        • 9. Re: How to send 1 byte as 8 bits
                          AH_96

                          Hello

                           

                          Can you please elaborate your requirement? When a UART block is connected to a pin, the start and stop bits are added since the protocol requires that.

                          If you wish to toggle a pin, it should not be connected to the UART block and must be a software driven pin (Hardware connection should be disabled).

                           

                          Please let us know the exact requirement so that we can suggest the suitable solution.

                           

                          Regards

                          Harigovind

                          • 10. Re: How to send 1 byte as 8 bits
                            JoMe_264151

                            Most problem you will tun into is to indicate to the receiver that a transmission is about to start (synchronization). This will requre an additional pin or a construct like start/stop bits.

                            For the latter case an UART will bw best, for the former one an SPI interface does exactly what you need.

                             

                            Bob

                            • 11. Re: How to send 1 byte as 8 bits
                              BUTA_1301626

                              is it possible to do the opposite of this process?

                              that is, converting bits (8 bits ==> bytes) to bytes.

                              • 12. Re: How to send 1 byte as 8 bits
                                JoMe_264151

                                Can be done with a SPI component as well. Think about how it must work: There will be the data signal, there must be a clock to indicate the bit positions within the data stream and there should be a signal to indicate the byte boundaries to synchronize the data stream. All this is maintained within the SPI protocol.

                                 

                                Bob

                                • 13. Re: How to send 1 byte as 8 bits
                                  BUTA_1301626

                                  I tried it with SPI. He's working. I'm wondering how I can do that by writing code.

                                  Is there a problem in the code below?

                                   

                                  uint8 DATA()

                                  {

                                  uint8 DATABIT = 0x08;

                                  uint8 RF0=0;

                                  RF0 = RF0 << 1;

                                  if (DIO_Read())

                                  RF0 = RF0 | 0x01;

                                  else

                                  RF0 = RF0 & 0xFE;

                                  while(--DATABIT);

                                  return(RF0);

                                  }

                                  • 14. Re: How to send 1 byte as 8 bits
                                    MoTa_728816

                                    Hi,

                                     

                                    As Bob-san stated, you need a way to detect when data started.

                                    Otherwise constant low pin is read as 0x00 and constant high pin will be read as 0xFF.

                                     

                                    So in the serial communication, there have been many methods invented.

                                    IMHO, there are (at least) two groups of methods,

                                    (1) include start information in the data

                                    (2) provide other signal(s) to indicate the start and/or the speed.

                                     

                                    UART takes  the strategy (1) , it keeps the signal high while idle

                                    then it uses start bit (which is a transition from high to low)

                                    and added a stop bit (let me make it simple here)

                                     

                                    On the other hand, SPI takes the strategy (2),

                                    it provides SCLK (clock).

                                     

                                    So if I add (1) to my previous code it will look like

                                    Note:

                                    To take care of the receiver, the delay is 2 us for this sample.

                                    And I'm assuming both are running same or very close clock.

                                    I have not tested these so you may (must) need to debug...

                                    ============

                                    void serialize(uint8_t theByte)

                                    {

                                        uint8_t mask = 0x80 ; /* 1000_0000 */

                                        TX_Write(1) ; /* TX should be 1 before sending anyway */

                                        CyDelayUs(2) ;

                                        while(mask) {

                                          if (theByte & mask) {

                                              TX_Write(1) ;

                                           } else {

                                              TX_Write(0) ;

                                           }

                                          mask >>= 1 ;

                                          CyDelayUs(2) ; // adjust delay for your purpose

                                        }

                                        TX_Write(2) ; /* Resume TX to idle state */

                                    }

                                    ============

                                     

                                    Or if I add (2)

                                    ============

                                    // assuming we have TX_VALID gpio pin

                                    void serialize(uint8_t theByte)

                                    {

                                        uint8_t mask = 0x80 ; /* 1000_0000 */

                                         TX_VALID_Wirte(0) ; /* must have been 0 though */

                                         CyDelayUs(2) ;

                                         TX_VALID_Write(1) ;

                                        CyDelayUs(2) ;

                                        while(mask) {

                                          if (theByte & mask) {

                                              TX_Write(1) ;

                                           } else {

                                              TX_Write(0) ;

                                           }

                                          mask >>= 1 ;

                                          CyDelayUs(2) ; // adjust delay for your purpose

                                        }

                                        TX_VALID_Write(0) ; /* Resume TX to idle state */

                                    }

                                    ============

                                     

                                    Then de_serialize() will be

                                    strategy (1)

                                    ===============

                                    uint8_t de_serialize(void)

                                    {

                                       uint8 DATABIT = 0x08;

                                       uint8 RF0=0;

                                     

                                        while(RX_Read() == 1) { /* wait for start bit */

                                             CyDelay(1)  ; /* note we are sampling 2x first */

                                        }

                                       // here we got the start bit

                                     

                                       do {

                                          CyDelay(2) ; /* wait 1 bit */

                                          RF0 = RF0 << 1;

                                          if (DIO_Read()) {

                                              RF0 = RF0 | 0x01;

                                           } else {

                                              RF0 = RF0 & 0xFE;

                                          }

                                       } while(--DATABIT);

                                       return(RF0);

                                    }

                                    ===============

                                     

                                    strategy (2)

                                    Note: Assming the TX_VALID signal is connected to an input pin RX_VALID.

                                    ===============

                                    uint8_t de_serialize(void)

                                    {

                                       uint8 DATABIT = 0x08;

                                       uint8 RF0=0;

                                     

                                        while(RX_VALID_Read() == 0) { /* wait for start bit */

                                             CyDelay(1)  ; /* note we are sampling 2x first */

                                        }

                                       // here we received the data valid

                                     

                                       do {

                                          CyDelay(2) ; /* wait 1 bit */

                                          RF0 = RF0 << 1;

                                          if (DIO_Read()) {

                                              RF0 = RF0 | 0x01;

                                           } else {

                                              RF0 = RF0 & 0xFE;

                                          }

                                       } while(--DATABIT);

                                       return(RF0);

                                    }

                                    ===============

                                     

                                    But, after all using SPI or UART is much more practical ;-)

                                     

                                    moto

                                    1 of 1 people found this helpful
                                    1 2 Previous Next