Home About Technology Travel Pictures

AVR code collection

Microcontollers are part of most of my projects. They are cheap, robust and flexible in use due to their various interfaces. Robots often contain multiple microcontrollers that communicate over busses.

I started to work with microcontrollers by building a simple test board that was extended bit by bit. This way I was able to gain experience in programming of microcontrollers. I use Atmel controllers because Atmel has good Documentation and free developing software. Further on there are many sources of information and a helpful community on the Internet. This page serves as collection of the programs, which I developed so far on these controllers.

Test board schematic

The circuit is programmed over a 6 pin ISP connection. I use the ISP Programmer from Atmel, which is available from Reichelt Electronics. Three R/C servos can be attached. The board communicates with the PC over a MAX232. In addition the circuit contains two push-buttons, an low current LED and an operation amplifier, which serves for amplifying of the PPM signal of a R/C receiver.

Structure of the test board

The following programs were developed with "AVR Studio 4" on a ATMega8. They partially trace back to the AVR Tutorial by Andreas Schwarz. The circuit was built on a test board, that was extended bit by bit. For correct representation in the browser a tab width of 8 was adjusted in AVR studio.

AtMega as switch

This program uses the full capacity of the ATMega :-). It queries whether the push-button S1 at pin 7 is pressed and switches the LED accordingly.


Text output with UART

The text "test" is sent repeatedly over the serial interface.


Switch LED by UART

A "1" sent to the controller, switches the LED on. A "0" switches it off.


Perfect timing

This program served as preparation to the servo control. It flashes the LED in a 1-second pulse controlled by the push-buttons. The duration of the shining phase is changeable by pressing the buttons. Button S1 extends the shining phase and S2 shortens it. Those happens however with overflow, i.e. if the maximum illuminated period is exceeded it continues with a dark LED.


Servo controlled by buttons

Here now the first real servo control. A servo at port c1 is moved to the left or right by the push-buttons. After reset the program starts with the servo in central position. This is specified with a pulse width of 1.5 ms. The minimum position is with 0.7 ms, maximum with 2.3 ms. These values are to my knowledge not reached by usual remote controls. If driven to one of the extreme positions the servo may start to hum. In this case the limits should be set more closely in the source code.


Servo controlled by UART

The same servo at port c1 can now be controlled from the PC. Therefor a five character string must be sent to the contoller. The string contains 4 digits, that indicate the pulse width of the servo signal, followed from a '*' as termination character. If one would thus like drive the servo with 0.9 ms the string "0900*" must be sent.


Controlling multiple servos by UART

Now the example for multiple servos. Again the pulse width of the servo signal in microseconds is transmitted here over the serial interface. Additionally the command string contains the servo number to identfiy the servo. The format is "x:nnnn*", whereby x is the servo number of 1-5 and nnnn the pulse width.


Processing the PPM signal

Some R/C receivers for model remote controls supply the originally transmitted, so-called PPM signal beside the actual servo signals. This contains the pulse widths of the individual servos in the order, coded by the temporal distance of the pulses. The example was developed for the C19 receiver from Graupner JR. This supplies the PPM signal with a voltage of 0.9V. The op-amp LM 324 amplifies it in such a way that the AtMega can process it using the timer interrupt. In place of the LM 324 probably also another op-amp can be used. The receiver itself uses a LM 339 compatible op-amp.

The program currently only measures the timing of the PPM signal and sends the pulse widths of the individual servos over the serial connection to the PC. The servos are not driven yet. I will continue to develop this soon. If several short pulses are received, it can occur that a measured value will not be transfered. This is however because of the timing of the program and the relatively slow serial connection. The actual measurement takes place correctly.

19.03.2005, Achim Walther, Mail