lilzz
Posts: 411
Joined: Sat Nov 30, 2013 5:27 pm

Pi Python and C Program generates different rates for GPIO

Sat May 17, 2014 1:06 am

Why Python and C toggle GPIO at different rates? If you want to toggle certain frequency you want , then how do you control that?

Code: Select all

#!/usr/bin/python

# Toggle GPIO pin 23.
# Output pulse frequency is about 18 kHz (varies due to other activity).


pin_path = '/sys/class/gpio/gpio23'

def write_once(path, value):
    f = open(path, 'w')
    f.write(value)
    f.close()
    return


# Set pin for output, with initial value low. 
write_once(pin_path + '/direction', 'out\n')

f = open(pin_path + '/value', 'w')

# Blink as fast as possible...

while 1:
    f.write('1')
    f.flush()

    f.write('0')
    f.flush()
    
f.close()

* Toggle GPIO pin 23 as fast as possible.
GPIO 23 must be exported before this program runs.
Once exported, file permissions for the gpio23 direction and value
files must be set appropriately, if user is not root.

Output pulse frequency is 120 kHz (typically varies a few kHz due to other activities such as network traffic, clock maintenence, etc.).
*/

Code: Select all

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
  int       fd, fd2;

  fd = open("/sys/class/gpio/gpio23/value", O_RDWR);
  if (fd < 0)
    {
      perror("Open value failed");
      exit(2);
    }
  
  fd2 = open("/sys/class/gpio/gpio23/direction", O_RDWR);
  if (fd2 < 0)
    {
      perror("Open direction failed");
      exit(2);
    }
  else
    {
      if (3 != write(fd2, "out", 3))
	{
	  perror("Write direction failed");
	  exit(2);
	}
      close(fd2);
    }

  while (1)
    {
      if (0 > write(fd, "1", 1))
	{
	  perror("Write 1 failed");
	  exit(3);
	}
      if (0 > write(fd, "0", 1))
	{
	  perror("Write 0 failed");
	  exit(3);
	}
    }
  return 0;
}

User avatar
FLYFISH TECHNOLOGIES
Posts: 1750
Joined: Thu Oct 03, 2013 7:48 am
Location: Ljubljana, Slovenia
Contact: Website

Re: Pi Python and C Program generates different rates for GP

Sat May 17, 2014 1:30 am

Hi,
lilzz wrote:Why Python and C toggle GPIO at different rates?
Python code is interpreted, where C code in compiled.


Best wishes, Ivan Zilic.
Running out of GPIO pins and/or need to read analog values?
Solution: http://www.flyfish-tech.com/FF32

lilzz
Posts: 411
Joined: Sat Nov 30, 2013 5:27 pm

Re: Pi Python and C Program generates different rates for GP

Sat May 17, 2014 3:15 pm

FLYFISH TECHNOLOGIES wrote:Hi,
lilzz wrote:Why Python and C toggle GPIO at different rates?
Python code is interpreted, where C code in compiled.


Best wishes, Ivan Zilic.
So, the frequency of toggling cannot be controlled? whatever come out comes out?

User avatar
Douglas6
Posts: 4861
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Pi Python and C Program generates different rates for GP

Sat May 17, 2014 3:41 pm

lilzz wrote:So, the frequency of toggling cannot be controlled? whatever come out comes out?
Pretty much. Python also has garbage collection activities which could throw things off. You'll get most consistent results with something like Pigpio, but the Pi is not a microcontroller.

Return to “General discussion”