Rudi's Homepage - AVR Projekte

EnglishDeutsch

myAVR Board

Description:

Project goal

This section should demonstrate how one can use the relatively cheap (12 Euro) myAVR-testboard with free or at least gratis programs.

The myAVR evaluation board.
The myAVR evaluation board.
The evaluation system is based on a ATmega8 which will be programmed by an SP12 compatible ISP-design connected to the parallel printer port. On the bord one can find 3 LED's, 2 switches and 2 variable resistors and a buzzer. Moreover the serial port of the ATmega is connected via a MAX232 level shifter to a standard SUBD-9 serial port.

As nice as the hardware is, the programs delivered with the board are limited test versions. The IDE isn't that bad, but you can use it only for half an hour and that for the first 30 days only.

Progamming in Assembler language

First it should be demonstrated how one can edit, compile and simulate assembler programs with VM-Lab and finally how to load the machine code with avrdude into the MCU. After starting the VMLab program one should see a window like that:

Start screen of VMLab
Start screen of VMLab
First one needs to create a new project via the Project->New menu link. Then one has to fill in the name and working directory of the project plus the name of the assembler source file. Lets name it blinken.asm. Afterwards one has to choose the MCU, obviously the ATmega8.
Creation of a new project.
Creation of a new project.
Now we work on the project file. There we have to change the clock frequency to 3,9894 MHz and describe the simulated hardware (the big advantage of VMLab). For the first step we want only a LED to flash. Therefore we add with the following lines an LED which is connected via a resistor to PortC pin 0. Notice that in difference to the evaluation board the LED will be connected to Vcc instead to GND!
			R1  PC0 vb1 1.2k
			D1  VDD vb1  ; x: Panel LEDs 1 - 8
			
The templates for the external hardware are available via the components-menu. Written in plain English the upper code lines would command: "connect resistor R1 (1.2 kOhm) from PortC pin 0 to node vb1. Then connect node vb1 with an LED and the other pin of the LED to Vss." Next follows the order to plot the voltage on PortC pin 0 over the time:
			.plot v(PC0)
			
The project file should looks like this one now:
The project file.
The project file.
Now we can write the program's source code itself. As usual the assembler code starts with the definition of register variables and a macro for the used port pin. Then follows the interrupt jump table, which is completed here for safety reasons for every interrupt with an immediate "return from interrupt" instruction. Afterwards the stack pointer will be initialized to point to the end of the RAM since the stack grows backwards. Different to the smaller ATtiny MCU's with their 3-level-hardware stack the bigger ATmega MCU's use a real RAM stack. The stack will be used to store the return address for every subroutine / interrupt service routine call. Finally we come to the "working part" of the program, an endless loop which switches the LED on and off and calls a delay subroutine between.

If the compiling of the source code (started with F9 or Project->Build) succeded one can start a simulation of the scenario. Before this one can make the virtual oscilloscope and the control panel with the simulated hardware visible by using "View->scope" and "View->control panel". The screen should looks like this one:

VM-Lab
VM-Lab

Programming with AVR-Dude

Since we want to see not only the simulated LED flash but the real LED too we have to load the executable machine code into the MCU. For this purpose I used the AVRdude command line utility. For automating the upload I wrote a Makefile, which will upload the .hex file by typing "make install".

Uploading with avr-dude and make.
Uploading with avr-dude and make.
Before uploading the first time you have to write the configuration word via "make write_fuse". After configurating and flashing the MCU the LED should flash if you connect it via wire with PortC pin 0.

Sending over the serial port and A/D conversion.

This project digitalizes analog values and sends the samples over the serial port. Since the a/d conversion would run at a minimal sampling frequency of 50 kHz (a bit too fast for serial communication over long lines) the serial transmission defines the sampling period. After each completed transmission a new single-shot A/D conversion will be started. For visualising and analysing the sampled data one can use the free MatLab clone Octave. To read the samples from the serial port and plot the result one can use the following script under Unix derivates:

			fid=fopen("/dev/ttyS0","rb"); x=fread(fid,10240,"uchar"); fclose(fid); plot(x)
			
An example result (voltage with respect to time) is depictured below.
Voltage with respect to time, plotted with Gnu Octave.
Voltage with respect to time, plotted with Gnu Octave.

Receiving over the serial port and digital-to-analog conversion via PWM.

This project receives text commands (brightness given absolute by 0-9 and relative by "+" and "-", on/off via "Space" key) over the serial port (settings: 8N1, 9600 baud) and regulates the brightness of an LED attached to PortB pin 3 via pulse width modulation (PWM). PWM means that you vary the pulse-pause ratio of the output signal and therefore its mean value. After a low-pass filtering you get an analog output signal. Digital-to-analog conversion done this way is most common for MCU's since implementing a "common" D/A converter (like R2R-ladders) would require a different way of semiconductor making and therefore increase the production costs. In most cases you can neglect the low-pass filtering of the output signal due to the inertiality of the output system (engines, lamps) or low-pass character of the output system (loudspeaker etc.).


IR-SoftUART

Description:

Project goal

The goal of this project is to realize the transmission of sampled data via a modulated IR transmission in such a way that a PC can receive the samples via a TSOP IR receiver connected to the serial port using a Max232 TTL->V24 level shifter. For the MCU I choosed a ATtiny15 (depreaced now) since it has an A/D converter and the necessary quantity of timers. The RC oscillator proved itself as accurable enough for a 9600 baud connection. Thus one needs only the 8-pin MCU, a resistor and the IR transmitter diode for the sender part, making the circuit very compact.

The circuit plan

The circuit plan is depicted below (analog input: PortB pin 2).

Circuit plan.
Circuit plan.
I realized the circuit on an experimental board.
Experimental setup.
Experimental setup.
For real-world use one should connect the other pin of the IR sending diode to +Us since the MCU can sink more current than it could drain (adapt the program!).

The program.

The complete source code is here, together with a Makefile for assembling,uploading and configuring the MCU when using an SP12 compatible programmer. The program uses Timer 1 for generating the 38 kHz carrier and Timer 0 for generating the bit clock. The sampling frequency is realized by a down-scaled A/D cycle. In the interrupt service routine for the "A/D sampling completed" interrupt triggers a transmission only every 256th call. Therefore the sampling frequency is done to 1.6 Hertz. The sending subroutine itself starts with setting the bit counter to 10 bits (8 Databits + Start bit + Stop bit), activates the modulator for the start bit and starts timer 0. The further transmission will be handled by the timer overrun interrupt of timer 0. In this service routine the preload value for timer 0 will be renewed at first for getting equal bit lengths. Then it checks if a data-bit will be send or if it's time for the beginning or ending of the stop bit. In the later case timer0 will be deactivated since the transmission is finished now, in the first case timer 1 to deengage the modulator. For a data bit the 38 kHz clock will be turned on/off depending on the LSB of the data word which will be shifted right afterwards. When adapting the program for a baud rate other than 1200 baud make sure that cpu-clocl / (baud rate * prescaler timer 0) < 255 holds!


Remote Control/Fernsteuerung