Changes between Initial Version and Version 1 of atmega8a-fastpwm-simulation


Ignore:
Timestamp:
Oct 10, 2018, 11:09:51 AM (22 months ago)
Author:
dsl
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • atmega8a-fastpwm-simulation

    v1 v1  
     1= II.3. Simulation example - ATmega8A and Fast PWM (Timer/Counter2) =
     2
     3== Preparing files ==
     4We're going to prepare all the necessary files in order to run firmware to generate PWM signal using Timer/Counter2 of the ATmega8A microcontroller in this example. We'll need these files and programs to be available or pre-installed:
     5
     6* mcusim
     7* firmware file (Intel HEX format, for ATmega8A)
     8* [http://gtkwave.sourceforge.net/ GTKWave] (traces viewer, something similar to a logic analyser)
     9
     10First of all, we'll need an additional directory to keep all our files in:
     11{{{
     12# mkdir ~/pwm-simulation
     13}}}
     14There is a firmware file `firmware.hex` compiled from `main.c` (available in archive attached to the page).
     15It's a time to generate a run script for mcusim using `mcusim-config`:
     16{{{
     17# cd ~/pwm-simulation
     18# mcusim-config
     19}}}
     20
     21== Generate a run script ==
     22It is an interactive script which will ask you to provide some details (including MCU model, firmware file, etc). We're going to simulate ATmega8A (model is `m8a`, type `mcusim -p ?` to get all available models) running at 16 MHz with firmware file `firmware.hex` and I/O registers `TCNT2,OCR2,TIFR6,PORTB3` dumped into VCD file:
     23{{{
     24        Directory to save configuration script in [ /home/dsl ]: .
     25        Name of the script [ start-avr ]: run-pwm
     26        Microcontroller [ m8 m8a m328 m328p m2560 ]: m8a
     27        Path to firmware (Leave empty to skip): firmware.hex
     28        ...
     29        Do you want to perform additional configuration? [y/N]: y
     30        Frequency (in Hz, leave empty for default): 16000000
     31        ...
     32        High fuse byte (leave empty for default): 0xC9
     33        Low fuse byte (leave empty for default): 0xEF
     34        Do you want I/O registers to be dumped? [y/N]: y
     35        Registers (TCNT2,PORTD,PINB1,...): TCNT2,OCR2,TIFR6,PORTB3
     36        Do you want to set up a firmware test? [y/N]: y
     37        ...
     38}}}
     39We'll receive a `run-pwm` script generated. However, we'll need our simulation to be terminated after, say, `400,000` clock cycles. Let's write a simple `stop-test.lua` to do this and modify `run-pwm` to load our script before simulation:
     40{{{
     41#!lua
     42ticks_passed = 0
     43
     44function module_tick(mcu)
     45        if ticks_passed > 400000 then
     46                -- Test finished successfully
     47                MSIM_SetState(mcu, AVR_MSIM_STOP)
     48                print("[Test stopper] ticks passed: " .. ticks_passed)
     49        end
     50
     51        ticks_passed = ticks_passed + 1
     52end
     53}}}
     54`MODELS` variable of `run-pwm` should be modified to contain name of the script:
     55{{{
     56#!shell
     57# Append models to the list in order to load them during simulation.
     58MODELS="./stop-test.lua"
     59}}}
     60
     61== Dump traces ==
     62It's a time to run `run-pwm` and dump traces into `ATmega8A-trace.vcd`:
     63{{{
     64# ./run-pwm
     65mcusim 0.1.75 - microcontroller-based circuit simulator <https://trac.mcusim.org>
     66[I]: Model ./stop-test.lua doesn't provide configuration function: attempt to call a nil value
     67                  Model: ATmega8A
     68              Signature: 1E937
     69        Clock frequency: 16000.0 kHz
     70         Program memory: 0x0-0xFFF words
     71     Bootloader section: 0xC00-0xFFF words
     72            Data memory: 0x60-0x45F bytes
     73                 EEPROM: 0x0-0x1FF bytes
     74                     PC: 0x0 word
     75          Reset address: 0x0 word
     76Interrupt vectors table: 0x1 word
     77[Test stopper] ticks passed: 400001
     78# ls -lah
     79drwxr-xr-x   2 dsl  wheel     8B Jun 15 10:15 .
     80drwxr-xr-x  26 dsl  wheel    59B Jun 15 10:13 ..
     81-rw-r--r--   1 dsl  wheel    14M Jun 15 10:15 ATmega8A-trace.vcd
     82-rw-r--r--   1 dsl  wheel   664B May 20 22:46 firmware.hex
     83-rw-r--r--   1 dsl  wheel   4.0K May 20 22:46 firmware.hex.txt
     84-rw-r--r--   1 dsl  wheel   1.1K Jun 14 19:18 main.c
     85-rwxr-xr-x   1 dsl  wheel   2.5K Jun 15 10:14 run-pwm
     86-rw-r--r--   1 dsl  wheel   244B Jun 15 09:39 stop-test.lua
     87}}}
     88We'll have a PWM signal generated on a third pin of Port B (`PORTB3` in VCD). Resulted file will look in [http://gtkwave.sourceforge.net/ GTKWave] like:   
     89
     90[[Image(atmega8a-fastpwm-simulation.png​)]]
     91
     92We may compare simulation result with a real oscillogram obtained from ATmega8A with exactly the same firmware uploaded:   
     93
     94[[Image(atmega8a-fastpwm-oscillogram0.png​)]]
     95[[Image(atmega8a-fastpwm-oscillogram1.png​)]]