Page 1 of 1

square wave C

Posted: Mon Jul 15, 2013 11:39 am
by PiHeich
I need to make a square wave (26.146 KHz) in a pin when i send data from the serial port (for make a analog modulation). How i can make this?

Please, only C without using python and other library..

Thanks :)

Re: square wave C

Posted: Mon Jul 15, 2013 11:48 am
by jamesh
I think that the sort of accuracy you are requesting may not be possible - do you need it that accurate? This is down to jitter caused by the Linux operating system interrupting your code at random points to handle OS tasks. If you want that level of accuracy, you may need to go to baremetal programming (no OS).

As for the C, making a line go up and down would be easy. Getting the right frequency less so.

Re: square wave C

Posted: Mon Jul 15, 2013 11:49 am
by joan
Without using a library?

Read up on the gpio and PWM peripherals http://www.raspberrypi.org/wp-content/u ... herals.pdf

Re: square wave C

Posted: Mon Jul 15, 2013 1:38 pm
by PiHeich
probably if i make a 26KHz is the same..

i need to make this signal every time i write on serial port, how i can generate the 26KHz whit timers or someone else?

Re: square wave C

Posted: Wed Jul 17, 2013 2:58 pm
by PiHeich
joan wrote:Without using a library?

Read up on the gpio and PWM peripherals http://www.raspberrypi.org/wp-content/u ... herals.pdf
Only use standard libraries,
When i send data from the serial port i need to make also a 26KHz for an analog modulation.
if is possible i need to create a 19us timer, so every 19us i change the value of the gpio (1->0 , 0->1).

Re: square wave C

Posted: Wed Jul 17, 2013 3:14 pm
by joan
PiHeich wrote:
joan wrote:Without using a library?

Read up on the gpio and PWM peripherals http://www.raspberrypi.org/wp-content/u ... herals.pdf
Only use standard libraries,
When i send data from the serial port i need to make also a 26KHz for an analog modulation.
if is possible i need to create a 19us timer, so every 19us i change the value of the gpio (1->0 , 0->1).
Yes, using the PWM or PCM peripherals to pace DMA transfers to the gpios.

Look at the servoblaster daemon code for the type of code you'll need to write. You'll need to be fairly competent in C and understand the Broadcom documentation.

Re: square wave C

Posted: Wed Jul 17, 2013 3:42 pm
by PiGraham
This project generating 100MHz FM PCM /PWM hardware might give you some clues for much lower frequency.
webcache.googleusercontent.com/search?q=cache:2sSuOAJQhN4J:www.icrobotics.co.uk/wiki/index.php/Tur ... clnk&gl=us

Re: square wave C

Posted: Thu Jul 18, 2013 12:17 am
by PiHeich
i have programmed a h8s micro and make some C programm, i try to read the servoblaster.c and understan how to generate this 26KHz

Thanks for the help ;)