Lesson 1
Lesson 1
To run and compile programs you should use IDE.
You can download SimulIDe 0.4.15.
https://launchpad.net/simulide/0.4.15/0.4.15-stable
https://www.simulide.com/p/downloads.html
and AVR Studio (recommended version AVR Studio 4.19 (build 730) (new versions of AVR Studio are very large and slow))
http://easy-english-study.com/AvrStudio4Setup.zip
Re: Lesson 1
Unzip and copy a downloaded SimulIDE to your working place ("Local Disk C" for example).
Then go to the next folder \SimulIDE\share\simulide\examples\Avr
Create the folder "my-avr" in the folder "Avr". It's where your all projects will be located.
From now on whenever you want to make an own project with AVR microcontroller you can do the following:
A.Create a new project folder in the directory "my-avr".
B.Copy a file with the file extension .asm from an existing, ready-made AVR project to your project folder, empty this file and rename it.
C.Make a circuit and save it to your project folder.
D.Add a simulation program to your project.
Then go to the next folder \SimulIDE\share\simulide\examples\Avr
Create the folder "my-avr" in the folder "Avr". It's where your all projects will be located.
From now on whenever you want to make an own project with AVR microcontroller you can do the following:
A.Create a new project folder in the directory "my-avr".
B.Copy a file with the file extension .asm from an existing, ready-made AVR project to your project folder, empty this file and rename it.
C.Make a circuit and save it to your project folder.
D.Add a simulation program to your project.
Re: Lesson 1
Let's make our first project.
Blinking LED
Create the folder "blinking-led" in the directory "my-avr".
Open "\SimulIDE\share\simulide\examples\Avr\7Seg_Counter_mega328p", copy the file "7Seg_Counter_mega328p.asm" to the folder "blinking-led", empty this file and rename it as "blinking-led" with the file extension .asm.
Open the program SimulIDE and click "Save Circuit As", then save your project to the folder "blinking-led".
Find (you can type a name of a component in the field "Search Components" or search a component by browsing Categories of Components in the left menu), drag and drop on Form the next components:
atmega328 (Micro->AVR->atmega->atmega328)
resistor 100 Ω (Passive->Resistors->Resistor)
Led (Outputs->Leds->Led)
Ground (0 V) (Sources->Ground (0 V)).
You can zoom in/zoom out your working area by using a scroll wheel.
Connect all components with wires in the same way, as in the picture. You can rotate a component by right-clicking a component and clicking "Rotate CW/Rotate CCW" a few times. Resistor must be connect to D0.
Click "Save Circuit".
Click the button "Open" in the upper-right menu of SimulIde, go to "\SimulIDE\share\simulide\examples\Avr\my-avr\blinking-led", open the file "blinking-led.asm". It's time to add the code to our program.
Blinking LED
Create the folder "blinking-led" in the directory "my-avr".
Open "\SimulIDE\share\simulide\examples\Avr\7Seg_Counter_mega328p", copy the file "7Seg_Counter_mega328p.asm" to the folder "blinking-led", empty this file and rename it as "blinking-led" with the file extension .asm.
Open the program SimulIDE and click "Save Circuit As", then save your project to the folder "blinking-led".
Find (you can type a name of a component in the field "Search Components" or search a component by browsing Categories of Components in the left menu), drag and drop on Form the next components:
atmega328 (Micro->AVR->atmega->atmega328)
resistor 100 Ω (Passive->Resistors->Resistor)
Led (Outputs->Leds->Led)
Ground (0 V) (Sources->Ground (0 V)).
You can zoom in/zoom out your working area by using a scroll wheel.
Connect all components with wires in the same way, as in the picture. You can rotate a component by right-clicking a component and clicking "Rotate CW/Rotate CCW" a few times. Resistor must be connect to D0.
Click "Save Circuit".
Click the button "Open" in the upper-right menu of SimulIde, go to "\SimulIDE\share\simulide\examples\Avr\my-avr\blinking-led", open the file "blinking-led.asm". It's time to add the code to our program.
Re: Lesson 1
- ;********************************************************
- ;Chip Model: MEGA328P
- ;Assembler header file
- .INCLUDE "m328pdef.inc" ;tell the AVR assembler to add the contents of a file to our program
- ;********************************************************
- .ORG 0 ;indicate the beginning of the address
- ;initialize the SP to point to the last location of RAM (RAMEND)
- LDI R20, HIGH(RAMEND) ;load R20 with the high byte of RAMEND
- OUT SPH, R20 ;initialize SPH
- LDI R20, LOW(RAMEND) ;load R20 with the low byte of RAMEND
- OUT SPL, R20 ;initialize SPL
- LDI R20, 0xFF ;load R20 with the value 0xFF in hex
- OUT DDRD, R20 ;make PORTD an output port
- BACK:
- SBI PORTD,0 ;turn on PD0
- CALL DELAY ;time delay, call the DELAY subroutine
- CBI PORTD,0 ;turn off PD0
- CALL DELAY ;time delay, call the DELAY subroutine
- JMP BACK ;jump to BACK, keep doing it forever (an infinite loop)
- DELAY:
- LDI R20, 100 ;load R20 with the value 100 in dec
- L0: LDI R21, 250 ;load R21 with the value 250 in dec
- L1: LDI R22, 250 ;load R22 with the value 250 in dec
- L2:
- NOP ;do nothing (1 Instruction cycle)
- NOP ;do nothing (1 Instruction cycle)
- DEC R22 ;decrement R22 by one, assign Z:=1 if R22==0 (1 Instruction cycle)
- BRNE L2 ;test the zero flag (Z), branch to L2 if Z==0 (2 Instruction cycles)
- DEC R21 ;decrement R21 by one, assign Z:=1 if R21==0
- BRNE L1 ;test the zero flag (Z), branch to L1 if Z==0
- DEC R20 ;decrement R20 by one, assign Z:=1 if R20==0
- BRNE L0 ;test the zero flag (Z), branch to L0 if Z==0
- RET ;return to caller (the end of the DELAY subroutine)
- ;Let's calculate the time delay of the DELAY subroutine. You can find the crystal frequency of atmega328 by right-clicking the component atmega328 on Form and clicking Properties. Usually it's 16 Mhz. So instruction cycle = 1/16 MHz = 0.0625 μs = 62.5 ns (nanosecond)
- ;Delay=100*250*250*(1+1+1+2)*62.5 ns=1,953,125,000 ns=1,953,125 µs≈2 s.
- ;The time delay is not precise. In this calculation, we have not included the overhead associated with the two outer loops.
Re: Lesson 1
Code: Select all
;********************************************************
;Chip Model: MEGA328P
;Assembler header file
.INCLUDE "m328pdef.inc" ;tells the AVR assembler to add the contents of a file to our program
;********************************************************
.ORG 0 ;indicate the beginning of the address
;initialize the SP to point to the last location of RAM (RAMEND)
LDI R20, HIGH(RAMEND) ;load R20 with the high byte of RAMEND
OUT SPH, R20 ;initialize SPH
LDI R20, LOW(RAMEND) ;load R20 with the low byte of RAMEND
OUT SPL, R20 ;initialize SPL
LDI R20, 0xFF ;load R20 with the value 0xFF in hex
OUT DDRD, R20 ;make PORTD an output port
BACK:
SBI PORTD,0 ;turn on PD0
CALL DELAY ;time delay, call the DELAY subroutine
CBI PORTD,0 ;turn off PD0
CALL DELAY ;time delay, call the DELAY subroutine
JMP BACK ;jump to BACK, keep doing it forever (an infinite loop)
DELAY:
LDI R20, 100 ;load R20 with the value 100 in dec
L0: LDI R21, 250 ;load R21 with the value 250 in dec
L1: LDI R22, 250 ;load R22 with the value 250 in dec
L2:
NOP ;do nothing (1 Instruction cycle)
NOP ;do nothing (1 Instruction cycle)
DEC R22 ;decrement R22 by one, assign Z:=1 if R22==0 (1 Instruction cycle)
BRNE L2 ;test the zero flag (Z), branch to L2 if Z==0 (2 Instruction cycles)
DEC R21 ;decrement R21 by one, assign Z:=1 if R21==0
BRNE L1 ;test the zero flag (Z), branch to L1 if Z==0
DEC R20 ;decrement R20 by one, assign Z:=1 if R20==0
BRNE L0 ;test the zero flag (Z), branch to L0 if Z==0
RET ;return to caller (the end of the DELAY subroutine)
;Let's calculate the time delay of the DELAY subroutine. You can find the crystal frequency of atmega328 by right-clicking the component atmega328 on Form and clicking Properties. Usually it's 16 Mhz. So instruction cycle = 1/16 MHz = 0.0625 μs = 62.5 ns (nanosecond)
;Delay=100*250*250*(1+1+1+2)*62.5 ns=1,953,125,000 ns=1,953,125 µs≈2 s.
;The time delay is not precise. In this calculation, we have not included the overhead associated with the two outer loops.
Re: Lesson 1
Copy-paste the program to the code window of SimulIde (you may click the button Select All (from the previous post) and press Ctrl+С (to copy the code from the forum) and Ctrl+V (to paste the code)).
Click "Compile".
You should see "SUCCESS!!! Compilation Ok" when everything is OK.
Click "Debug".
Click "Step" a few times.
Whenever you edit a code you have to click the next buttons after changes: 1."Compile" 2."Debug" 3.Click "Step" a few times.
Then your program will work correctly (it's because of such specific of SimulIde).
Stop Debugger.
Click the red button "Power Circuit".
You should see blinking LED.
Click "Compile".
You should see "SUCCESS!!! Compilation Ok" when everything is OK.
Click "Debug".
Click "Step" a few times.
Whenever you edit a code you have to click the next buttons after changes: 1."Compile" 2."Debug" 3.Click "Step" a few times.
Then your program will work correctly (it's because of such specific of SimulIde).
Stop Debugger.
Click the red button "Power Circuit".
You should see blinking LED.
Re: Lesson 1
Install AVR Studio 4.19.
Project->New Project->Atmel AVR Assembler
Project name: "blinking-led"
Click Next>>
Select Device: Atmega328
Click Finish
Copy-paste the code in the code window
Click Build->Build and Run(Ctrl-F7)
You should see the message in the output window "AVR Simulator: ATmega328 Configured OK"
Click Debug->Step Into(F11) a few times
Speed up step by step running by assigning R20:=3, R21:=2, R22:=2 in the code and click "Build and Run"
Find and click Registers tab in the left menu, find R20, R21, R22 registers
Find and click PORTD tab in the right menu
Click "Step Into" a few times and simultaneously monitor registers. Numbers loaded in registers must change when you run the program
You can change values loaded in registers when you run the program
Happy debugging!:)
Project->New Project->Atmel AVR Assembler
Project name: "blinking-led"
Click Next>>
Select Device: Atmega328
Click Finish
Copy-paste the code in the code window
Click Build->Build and Run(Ctrl-F7)
You should see the message in the output window "AVR Simulator: ATmega328 Configured OK"
Click Debug->Step Into(F11) a few times
Speed up step by step running by assigning R20:=3, R21:=2, R22:=2 in the code and click "Build and Run"
Find and click Registers tab in the left menu, find R20, R21, R22 registers
Find and click PORTD tab in the right menu
Click "Step Into" a few times and simultaneously monitor registers. Numbers loaded in registers must change when you run the program
You can change values loaded in registers when you run the program
Happy debugging!:)