PiHeich
Posts: 25
Joined: Sun Jul 14, 2013 8:48 pm

square wave C

Mon Jul 15, 2013 11:39 am

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 :)

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 26659
Joined: Sat Jul 30, 2011 7:41 pm

Re: square wave C

Mon Jul 15, 2013 11:48 am

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.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed.
I've been saying "Mucho" to my Spanish friend a lot more lately. It means a lot to him.

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: square wave C

Mon Jul 15, 2013 11:49 am

Without using a library?

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

PiHeich
Posts: 25
Joined: Sun Jul 14, 2013 8:48 pm

Re: square wave C

Mon Jul 15, 2013 1:38 pm

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?

PiHeich
Posts: 25
Joined: Sun Jul 14, 2013 8:48 pm

Re: square wave C

Wed Jul 17, 2013 2:58 pm

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).

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: square wave C

Wed Jul 17, 2013 3:14 pm

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.

PiGraham
Posts: 3932
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: square wave C

Wed Jul 17, 2013 3:42 pm

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

PiHeich
Posts: 25
Joined: Sun Jul 14, 2013 8:48 pm

Re: square wave C

Thu Jul 18, 2013 12:17 am

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 ;)

Return to “C/C++”