1

Is it possible to output integer binary values to GPIO pins at once like a parallel port?

I need that because I am afraid if I toggled pins one by one there will be invalid intermediate states that will cause wrong behavior in the circuit connected to GPIO.

I prefer software solution but any other idea to overcome this is welcomed.

1

You can't change the values of multiple GPIO pins simultaneously. Parallel ports don't do this either, they handle it by having an additional control pin (Strobe) to tell the device that the data is ready.

If you use an extra pin (I'll call it the Ready pin) you could use a process like this:

  • Set Ready pin to low
  • Set data pins to the correct values.
  • Set Ready pin to high

Now this has a problem, how do you know if the remote end has read the data and can accept more? 2 possible solutions:

  • Assume the remote end is guaranteed to read the data in some maximum time and keep the Ready pin high for a minimum of that amount of time.
  • Add an additional Acknowledge pin that the remote end will set to high when it has read the data. The Raspberry pi will keep the Ready pin high until the ACK pin goes high.
| improve this answer | |
  • The other end does not read the data. I am using GPIO with a simple digital circuit I have made to control some switches. It's very simple and asynchronous like turning LEDs on/off but instead of connecting every led to a GPIO pin. I am outputing binary to GPIO and then decoding it to control more pins. So with 4 pins I can control 15 switches instead of 4. The idea of the Ready/Strobe pin is great and it's just what I need in this simple scenario. The ACK signal will be very useful too for other scenarios. – M.Sameer Jun 13 '13 at 23:56
1

@OP The answers provided are not correct and its apparent that these posters do not understand the GPIO architecture of the device. You can and I've done it. There's C code here - http://elinux.org/RPi_Low-level_peripherals#C_2. You just need to modify a couple of lines. Then you can set and clear GPIO in parallel.

| improve this answer | |
  • So in this example if I did *gpio = 3; for example will it set the first 2 pins and clear the remaining all at once? If I do not understand correctly please consider providing code example in your answer as it would be very helpful to everyone. – M.Sameer Feb 28 '14 at 13:23
1

M.Sameer: It sounds like you are on the right track. To test this, you can modify the following lines in the file

// Old                                  
  for (rep=0; rep<10; rep++)            
  {                     
    for (g=7; g<=11; g++)     // Delete    
     {                        // Delete    
       GPIO_SET = 1<<g;       // Delete    
       sleep(1);              // Delete    
     }                        // Delete    
     for (g=7; g<=11; g++)    // Delete    
     {                        // Delete    
       GPIO_CLR = 1<<g;       // Delete    
       sleep(1);              // Delete    
     }                        // Delete    
  }                     

  // New                    
  for (rep=0; rep<10; rep++)            
  {                     
    // Toggle GPIO 7 & 8 concurrently       
    GPIO_SET = 3<<7;       // Added         
    sleep(1);              // Added         
    GPIO_CLR = 3<<7;       // Added         
    sleep(1);              // Added         
  }     

Have a look at the Broadcom 2835 datasheet. There are separate SET and CLEAR registers to control a vector of GPIO. What value you write to these registers will determine which and how many GPIO get set and cleared at the same time. Hope this helps.

| improve this answer | |
  • How does it do it concurrently if there is a sleep(1) between the two lines ? – M.Sameer Feb 28 '14 at 21:07
  • Here's the functionality: It sets GPIO 7 & 8, then sleeps(1), then clears GPIO 7 & 8, then sleeps(1), this process repeats 'rep' amount of times (10). Note: GPIO_SET writes a value of 3 ('b11) shifted by 7 to the SET register. GPIO_CLR writes a value of 3 ('b11) shifted by 7 to the CLEAR register. The shifting process determines which GPIO are set. If no shift is performed, 'b11 would be written to the SET and CLEAR registers of GPIO 0 & 1. You could use the modified code to blink two LEDs on and off for instance. Hope this helps. – Right Feb 28 '14 at 21:42
  • It may help to note that these two registers (SET/CLR) control the data path of multiple GPIO. There is a block diagram of an I/O data path in the datasheet. Stare at it and see if things make more sense, you will notice that I/O data path functionality is controlled via multiple registers (not just SET/CLR). Hope this helps. – Right Feb 28 '14 at 21:57
0

parallel communication is long gone, most HDD drives today are SATA and you can hardly find any parallel port on the back of your average PC.

considering the lack of GPIO pins, the best idea might be to use serial communication, either the serial port or, if you are adventurous type, the i2c port.

| improve this answer | |
  • The binary from the GPIO is input to a decoder IC in my circuit to control a group of switches .. It's like I enter the number of switch in RPi and it is turned on or off .. with 4 pins I can control 15 switches. If I used I2C as you suggest, will I be able to have the binary value easily by connecting an IC to the I2C pins in RPi or will it cause me to have a large circuit with more cost to do that? – M.Sameer Jun 13 '13 at 22:33
  • parallel port == synchronization issues, avoid it. if you need to flip 1 bit -- use GPIO pin, if you need to flip 2 bits, maybe GPIO pins, but if you need to flip 3+ pins -- use serial communication. it takes only 1 pin and you don't have to worry about synchronization and acknowledgement. regarding the receiving end, most contemporary microprocessors have serial/i2c implemented in the hardware, and you should learn it and use every day. and yeah, there are plenty of i2c-enabled decoders on the market. – lenik Jun 13 '13 at 23:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.