Lesson 1
Lesson 1
To run and compile programs you should use IDE.
You can download SimulIDE (to simulate circuits)
(Link1) https://launchpad.net/simulide
(Link2) https://www.simulide.com/p/downloads.html
Recommended version is SimulIDE_1.1.0-RC1_Win64 (or any other version >=1.1.0).
Atmel Studio (to use avrasm) (recommended version Atmel Studio 6.0)
(Link1) https://atmel-studio.software.informer.com/6.0
(Link2) https://www.google.com/search?q=Atmel+Studio+6.0
WinAVR (to use C)
https://easy-english-study.com/WinAVR-2 ... nstall.exe
Arduino IDE (to use arduino)
https://support.arduino.cc/hc/en-us/art ... rduino-IDE
Re: Lesson 1
Unzip and copy a downloaded SimulIDE to your working place ("Local Disk C" for example). Let's name it as "SimulIDE".
Install Atmel Studio 6.0 to your working place ("Local Disk C\Program Files" for example).
Install WinAVR to your working place ("Local Disk C" for example).
Install Arduino IDE to your working place ("Local Disk C" for example).
Find the folder "avrassembler" in "C:\Program Files\Atmel\Atmel Studio 6.0\extensions\Atmel\AVRAssembler\2.1.51.64" and copy this folder.
Create the new folder "my-avr" in "C:\SimulIDE\examples\Micro\" and paste the folder "avrassembler" to there.
Create the new folder "Arduino" in "C:\Users\YourUsername\Documents\" (YourUsername is your name) and copy all files from "C:\arduino-1.8.18\" to there.
======================================================
If you want to make an AVR project with avrasm or C then you should go to the folder "C:\SimulIDE\examples\Micro\my-avr"
From now on whenever you want to make an own project with an AVR microcontroller you should do the following:
A.Create a new project folder in the directory "my-avr".
B.Create the subfolder "gcb_code" in the project folder.
C.Make a circuit and save it to your project folder.
D.Add a simulation program to your project.
E.Set correct Compiler Settings.
Avrasm2 Avrgcc ======================================================
If you want to make Arduino project then you should go to the folder "C:\SimulIDE\examples\Micro\my-avr"
From now on whenever you want to make an own project with an Arduino microcontroller you should do the following:
A.Create a new project folder in the directory "my-avr".
B.Create the subfolder "yourprojectname_ino" in the project folder. .
C.Make a circuit and save it to your project folder.
D.Add a simulation program to your project.
======================================================
Install Atmel Studio 6.0 to your working place ("Local Disk C\Program Files" for example).
Install WinAVR to your working place ("Local Disk C" for example).
Install Arduino IDE to your working place ("Local Disk C" for example).
Find the folder "avrassembler" in "C:\Program Files\Atmel\Atmel Studio 6.0\extensions\Atmel\AVRAssembler\2.1.51.64" and copy this folder.
Create the new folder "my-avr" in "C:\SimulIDE\examples\Micro\" and paste the folder "avrassembler" to there.
Create the new folder "Arduino" in "C:\Users\YourUsername\Documents\" (YourUsername is your name) and copy all files from "C:\arduino-1.8.18\" to there.
======================================================
If you want to make an AVR project with avrasm or C then you should go to the folder "C:\SimulIDE\examples\Micro\my-avr"
From now on whenever you want to make an own project with an AVR microcontroller you should do the following:
A.Create a new project folder in the directory "my-avr".
B.Create the subfolder "gcb_code" in the project folder.
C.Make a circuit and save it to your project folder.
D.Add a simulation program to your project.
E.Set correct Compiler Settings.
Avrasm2 Avrgcc ======================================================
If you want to make Arduino project then you should go to the folder "C:\SimulIDE\examples\Micro\my-avr"
From now on whenever you want to make an own project with an Arduino microcontroller you should do the following:
A.Create a new project folder in the directory "my-avr".
B.Create the subfolder "yourprojectname_ino" in the project folder. .
C.Make a circuit and save it to your project folder.
D.Add a simulation program to your project.
======================================================
Re: Lesson 1
We will make all our avr projects with ATmega168. ATmega168 is often used as a main chip in Arduino boards.
Let's make our first project.
Blinking LED in AVR Assembler.
Create the folder "blinking-led-avr-asm" in the directory "my-avr".
Create the subfolder "gcb_code" in the folder "blinking-led-avr-asm".
Find the file "m168def.inc" in "C:\SimulIDE\examples\Micro\Avr\my-avr\avrassembler\include\". Copy this file.
Paste "m168def.inc" to the folder "C:\blinking-led-avr-asm\gcb_code\".
Open the program SimulIDE and click "Save Circuit As", then save your project to the folder "blinking-led-avr-asm\gcb_code" with the file name "blinking-led-avr-asm".
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:
mega168 (Micro->AVR->atmega->mega168)
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. The resistor must be connect to D0. Pay attention to LED, since it emits light only if it's connected correctly. The current must go from an anode to a cathode (LED will not emit light if you connect it in the opposite direction).
Click "Save Circuit". It's time to add the code to our program.
Let's make our first project.
Blinking LED in AVR Assembler.
Create the folder "blinking-led-avr-asm" in the directory "my-avr".
Create the subfolder "gcb_code" in the folder "blinking-led-avr-asm".
Find the file "m168def.inc" in "C:\SimulIDE\examples\Micro\Avr\my-avr\avrassembler\include\". Copy this file.
Paste "m168def.inc" to the folder "C:\blinking-led-avr-asm\gcb_code\".
Open the program SimulIDE and click "Save Circuit As", then save your project to the folder "blinking-led-avr-asm\gcb_code" with the file name "blinking-led-avr-asm".
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:
mega168 (Micro->AVR->atmega->mega168)
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. The resistor must be connect to D0. Pay attention to LED, since it emits light only if it's connected correctly. The current must go from an anode to a cathode (LED will not emit light if you connect it in the opposite direction).
Click "Save Circuit". It's time to add the code to our program.
Re: Lesson 1
- ;*******************************************************
- ;Chip Model: MEGA168
- ;Assembler header file
- .INCLUDE "m168def.inc" ;tells the AVR assembler to add the contents of a file to our program
- ;*******************************************************
- .ORG 0 ;indicates the beginning of the address; puts our code at the beginning of flash memory
- ;The next block initializes the SP to point to the last location of RAM (RAMEND),
- ;(set up stack)
- .MACRO INITSTACK ;macro AVR assembler
- LDI R20, HIGH(RAMEND) ;loads R20 with the high byte of RAMEND
- OUT SPH, R20 ;initializes SPH
- LDI R20, LOW(RAMEND) ;loads R20 with the low byte of RAMEND
- OUT SPL, R20 ;initializes SPL
- .ENDMACRO ;macro AVR assembler
- INITSTACK ;macro AVR assembler
- SBI DDRD, 0 ;makes PD0 an output port
- BACK:
- SBI PORTD,0 ;turns on PD0
- RCALL DELAY ;time delay, calls the DELAY subroutine
- CBI PORTD,0 ;turns off PD0
- RCALL DELAY ;time delay, calls the DELAY subroutine
- RJMP BACK ;jumps to BACK, keeps doing it forever (an infinite loop)
- DELAY:
- LDI R20, 100 ;loads R20 with the value 100 in dec
- L0: LDI R21, 250 ;loads R21 with the value 250 in dec
- L1: LDI R22, 250 ;loads R22 with the value 250 in dec
- L2:
- NOP ;does nothing (1 Instruction cycle)
- NOP ;does nothing (1 Instruction cycle)
- DEC R22 ;decrements R22 by one, assign Z:=1 if R22==0 (1 Instruction cycle)
- BRNE L2 ;tests the zero flag (Z), branch to L2 if Z==0 (2 Instruction cycles)
- DEC R21 ;decrements R21 by one, assign Z:=1 if R21==0
- BRNE L1 ;tests the zero flag (Z), branch to L1 if Z==0
- DEC R20 ;decrements R20 by one, assign Z:=1 if R20==0
- BRNE L0 ;tests the zero flag (Z), branch to L0 if Z==0
- RET ;returns to caller (the end of the DELAY subroutine)
- ;*******************************************************
- ;COMMENTS
- ;Let's calculate the time delay of the DELAY subroutine. You can find the crystal frequency of atmega168 by right-clicking the component atmega168 on Form and clicking Properties. It's 16 Mhz by default. So instruction cycle = 1/16 MHz = 0.0625 μs = 62.5 ns (nanosecond).
- ;We have 100 loops, 250 loops, 250 loops.
- ;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, since we have not included the overhead associated with the two outer loops.
Initialization (1-17)
------|---------------
Make PORTD0 an output port (19)
------|---------------
BACK:
Main program (21-24)
go to BACK
===============================================
You can copy-paste the Initialization block to every AVR assembler program without thinking, since it's the same for every program.
===============================================
SBI DDRD, 0 ;makes PD0 an output port
DDRD has 8 bits (bbbbbbbb).
We set bit 0 of DDRD to 1 (bbbbbbb1).
This setting makes PORTD0 an output (it can be input also).
===============================================
SBI PORTD,0 ;turns on PD0
RCALL DELAY ;time delay, calls the DELAY subroutine
CBI PORTD,0 ;turns off PD0
RCALL DELAY ;time delay, calls the DELAY subroutine
SBI (Set Bit in I/O register)
CBI (Clear Bit in I/O register)
It's the main part of our program. This block of the code turns on and turns off our LED with delay 2 seconds.
===============================================
DELAY subroutines makes 2 seconds delay. We make delays by looping 100*250*250 times (it takes time to run those loops for AVR and keeps the microcontroller busy for 2 seconds).
Re: Lesson 1
Code: Select all
;*******************************************************
;Chip Model: MEGA168
;Assembler header file
.INCLUDE "m168def.inc" ;tells the AVR assembler to add the contents of a file to our program
;*******************************************************
.ORG 0 ;indicates the beginning of the address; puts our code at the beginning of flash memory
;The next block initializes the SP to point to the last location of RAM (RAMEND),
;(set up stack)
.MACRO INITSTACK ;macro AVR assembler
LDI R20, HIGH(RAMEND) ;loads R20 with the high byte of RAMEND
OUT SPH, R20 ;initializes SPH
LDI R20, LOW(RAMEND) ;loads R20 with the low byte of RAMEND
OUT SPL, R20 ;initializes SPL
.ENDMACRO ;macro AVR assembler
INITSTACK ;macro AVR assembler
SBI DDRD, 0 ;makes PD0 an output port
BACK:
SBI PORTD,0 ;turns on PD0
RCALL DELAY ;time delay, calls the DELAY subroutine
CBI PORTD,0 ;turns off PD0
RCALL DELAY ;time delay, calls the DELAY subroutine
RJMP BACK ;jumps to BACK, keeps doing it forever (an infinite loop)
DELAY:
LDI R20, 100 ;loads R20 with the value 100 in dec
L0: LDI R21, 250 ;loads R21 with the value 250 in dec
L1: LDI R22, 250 ;loads R22 with the value 250 in dec
L2:
NOP ;does nothing (1 Instruction cycle)
NOP ;does nothing (1 Instruction cycle)
DEC R22 ;decrements R22 by one, assign Z:=1 if R22==0 (1 Instruction cycle)
BRNE L2 ;tests the zero flag (Z), branch to L2 if Z==0 (2 Instruction cycles)
DEC R21 ;decrements R21 by one, assign Z:=1 if R21==0
BRNE L1 ;tests the zero flag (Z), branch to L1 if Z==0
DEC R20 ;decrements R20 by one, assign Z:=1 if R20==0
BRNE L0 ;tests the zero flag (Z), branch to L0 if Z==0
RET ;returns to caller (the end of the DELAY subroutine)
;*******************************************************
;COMMENTS
;Let's calculate the time delay of the DELAY subroutine. You can find the crystal frequency of atmega168 by right-clicking the component atmega168 on Form and clicking Properties. It's 16 Mhz by default. So instruction cycle = 1/16 MHz = 0.0625 μs = 62.5 ns (nanosecond).
;We have 100 loops, 250 loops, 250 loops.
;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, since we have not included the overhead associated with the two outer loops.
Re: Lesson 1
Create a new code file in SimulIDE. 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 "Save". Then enter "File name: blinking-led-avr-asm", select "Save as type: Asm(*.asm)" and click "Save" in the pop up window "Save Document As".
Click the gear wheel button "Settings" and click "File Settings".
Now select "Compiler: Avrasm2",
Click the gear wheel button "Settings" and click "Compiler Settings".
Click "Select tool path" and browse to "C:/SimulIDE/examples/Micro/my-avr/avrassembler/" (or your path), click "Select Folder",
"Include path: ./".
Then close "Compiler Settings" window.
Click "Compile", then click "UpLoad".
You should see "Assembly complete, 0 errors. 0 warnings" when everything is OK.
Whenever you want to edit a code you have to click the next buttons after changes: "Compile" and "UpLoad".
Then your program will work correctly.
Click the red button "Power Circuit".
You should see blinking LED.
Click "Save". Then enter "File name: blinking-led-avr-asm", select "Save as type: Asm(*.asm)" and click "Save" in the pop up window "Save Document As".
Click the gear wheel button "Settings" and click "File Settings".
Now select "Compiler: Avrasm2",
Click the gear wheel button "Settings" and click "Compiler Settings".
Click "Select tool path" and browse to "C:/SimulIDE/examples/Micro/my-avr/avrassembler/" (or your path), click "Select Folder",
"Include path: ./".
Then close "Compiler Settings" window.
Click "Compile", then click "UpLoad".
You should see "Assembly complete, 0 errors. 0 warnings" when everything is OK.
Whenever you want to edit a code you have to click the next buttons after changes: "Compile" and "UpLoad".
Then your program will work correctly.
Click the red button "Power Circuit".
You should see blinking LED.
Re: Lesson 1
Blinking LED in AVR GCC.
Create the folder "blinking-led-avr-c" in the directory "my-avr".
Create the subfolder "gcb_code" in the folder "blinking-led-avr-c".
Open the program SimulIDE and click "Save Circuit As", then save your project to the folder "blinking-led-avr-c\gcb_code" with the file name "blinking-led-avr-c".
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:
mega168 (Micro->AVR->atmega->mega168)
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. The resistor must be connect to D0. Pay attention to LED, since it emits light only if it's connected correctly. The current must go from an anode to a cathode (LED will not emit light if you connect it in the opposite direction).
Click "Save Circuit". It's time to add the code to our program.
Create the folder "blinking-led-avr-c" in the directory "my-avr".
Create the subfolder "gcb_code" in the folder "blinking-led-avr-c".
Open the program SimulIDE and click "Save Circuit As", then save your project to the folder "blinking-led-avr-c\gcb_code" with the file name "blinking-led-avr-c".
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:
mega168 (Micro->AVR->atmega->mega168)
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. The resistor must be connect to D0. Pay attention to LED, since it emits light only if it's connected correctly. The current must go from an anode to a cathode (LED will not emit light if you connect it in the opposite direction).
Click "Save Circuit". It's time to add the code to our program.
Re: Lesson 1
- #include <avr/io.h> //includes library, standard AVR header
- #define LED 0 //defines the constant LED. Now LED:=0
- void delayms(volatile unsigned long j)
- {
- volatile unsigned long i;
- for(i=0; i < 157*j; i++); //loops i from 0 to 157*j
- }
- int main(void)
- {
- DDRD |= (1<<LED); //makes PORTD0 an output
- while(1)
- {
- PORTD=(1<<LED); //PD0:=1
- delayms(2000); //calls delayms
- PORTD=(0<<LED); //PD0:=0
- delayms(2000); //calls delayms
- }
- return 0;
- }
Initialization (1-2)
-----|----------
Make PORTD0 an output port (12)
-----|----------
loop:
Main program (15-18)
================================
1 << LED means 00000001 << 0. The result is 00000001.
DDRD |= (1<<LED) means (DDRD) OR (00000001), this command sets DDRD:=bbbbbbb1 (we just set bit 0 to 1, we don't care of other bits b).
2000 ms is 2 seconds
"volatile" prevents the compiler to optimize memory access. Without "volatile" delays wouldn't be 2 seconds all the time.
The microcontroller runs delayms(2000) as the loop for(i=0; i < 157*2000; i++);
The number 157 is different for different compilers.
Re: Lesson 1
Code: Select all
#include <avr/io.h> //includes library, standard AVR header
#define LED 0 //defines the constant LED. Now LED:=0
void delayms(volatile unsigned long j)
{
volatile unsigned long i;
for(i=0; i < 157*j; i++); //loops i from 0 to 157*j
}
int main(void)
{
DDRD |= (1<<LED); //makes PORTD0 an output
while(1)
{
PORTD=(1<<LED); //PD0:=1
delayms(2000); //calls delayms
PORTD=(0<<LED); //PD0:=0
delayms(2000); //calls delayms
}
return 0;
}
Re: Lesson 1
Create a new code file in SimulIDE. 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 "Save". Then enter "File name: blinking-led-avr-c", select "Save as type: C(*.c)" and click "Save" in the pop up window "Save Document As".
Click the gear wheel button "Settings" and click "File Settings".
Now select "Compiler: Avrgcc".
Click the gear wheel button "Settings" and click "Compiler Settings".
Click "Select tool path" and browse to "C:/WinAVR/bin/" (or your path), click "Select Folder",
"Device: atmega168".
Then close "Compiler Settings" window.
Click "Compile", then click "UpLoad".
You should see
"Executing:
"C:/WinAVR/bin/avr-objcopy" -j .text -j .data -O ihex "C:/SimulIDE/examples/Micro/my-avr/blinking-led-avr-c/gcb_code/build_blinking-led-avr-c\"blinking-led-avr-c.elf "C:/SimulIDE/examples/Micro/my-avr/blinking-led-avr-c/gcb_code/build_blinking-led-avr-c\"blinking-led-avr-c.hex"
Whenever you want to edit a code you have to click the next buttons after changes: "Compile" and "UpLoad".
Then your program will work correctly.
Click the red button "Power Circuit".
You should see blinking LED.
Click "Save". Then enter "File name: blinking-led-avr-c", select "Save as type: C(*.c)" and click "Save" in the pop up window "Save Document As".
Click the gear wheel button "Settings" and click "File Settings".
Now select "Compiler: Avrgcc".
Click the gear wheel button "Settings" and click "Compiler Settings".
Click "Select tool path" and browse to "C:/WinAVR/bin/" (or your path), click "Select Folder",
"Device: atmega168".
Then close "Compiler Settings" window.
Click "Compile", then click "UpLoad".
You should see
"Executing:
"C:/WinAVR/bin/avr-objcopy" -j .text -j .data -O ihex "C:/SimulIDE/examples/Micro/my-avr/blinking-led-avr-c/gcb_code/build_blinking-led-avr-c\"blinking-led-avr-c.elf "C:/SimulIDE/examples/Micro/my-avr/blinking-led-avr-c/gcb_code/build_blinking-led-avr-c\"blinking-led-avr-c.hex"
Whenever you want to edit a code you have to click the next buttons after changes: "Compile" and "UpLoad".
Then your program will work correctly.
Click the red button "Power Circuit".
You should see blinking LED.
Re: Lesson 1
Arduino has an AVR microcontroller inside. It can be ATmega328, ATmega168 or ATmega8.
We will prove it by creating our first Arduino program in Avrasm.
Blinking LED in Arduino Avrasm.
Create the folder "blinking-led-arduino-asm" in the directory "my-avr".
Create the subfolder "blinking-led-arduino-asm_ino" in the folder "blinking-led-arduino-asm".
Open the program SimulIDE and click "Save Circuit As", then save your project to the folder "blinking-led-arduino-asm\blinking-led-arduino-asm_ino" with the file name "blinking-led-arduino-asm".
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:
Arduino Uno (Micro->Arduino->Uno)
resistor 100 Ω (Passive->Resistors->Resistor)
Led (Outputs->Leds->Led).
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. The resistor must be connect to 13. Rotate LED and connect it to GND. Pay attention to LED, since it emits light only if it's connected correctly. The current must go from an anode to a cathode (LED will not emit light if you connect it in the opposite direction).
Click "Save Circuit".
We will prove it by creating our first Arduino program in Avrasm.
Blinking LED in Arduino Avrasm.
Create the folder "blinking-led-arduino-asm" in the directory "my-avr".
Create the subfolder "blinking-led-arduino-asm_ino" in the folder "blinking-led-arduino-asm".
Open the program SimulIDE and click "Save Circuit As", then save your project to the folder "blinking-led-arduino-asm\blinking-led-arduino-asm_ino" with the file name "blinking-led-arduino-asm".
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:
Arduino Uno (Micro->Arduino->Uno)
resistor 100 Ω (Passive->Resistors->Resistor)
Led (Outputs->Leds->Led).
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. The resistor must be connect to 13. Rotate LED and connect it to GND. Pay attention to LED, since it emits light only if it's connected correctly. The current must go from an anode to a cathode (LED will not emit light if you connect it in the opposite direction).
Click "Save Circuit".
Re: Lesson 1
Create inside the subfolder "blinking-led-arduino-asm_ino" the next files blinking-led-arduino-asm.ino, blinking-led-arduino-asm.S using Notepad.
======================
1.Open Notepad
2.File->Save As... and browse to the subfolder "blinking-led-arduino-asm_ino"
3.File name: blinking-led-arduino-asm.ino (blinking-led-arduino-asm.S)
4.Save as Type: All Files
5.Click Save
======================
Copy-paste the next code to files
blinking-led-arduino-asm.ino
blinking-led-arduino-asm.S
Open blinking-led-arduino-asm.ino in SimulIDE.
Click "Compile", then click "UpLoad".
Click the red button "Power Circuit".
You should see blinking LED.
The pin 13 in Arduino is the same as PB5 in AVR microcontrollers.
We will not comment much this project, since it was made only for a demonstration.
======================
1.Open Notepad
2.File->Save As... and browse to the subfolder "blinking-led-arduino-asm_ino"
3.File name: blinking-led-arduino-asm.ino (blinking-led-arduino-asm.S)
4.Save as Type: All Files
5.Click Save
======================
Copy-paste the next code to files
blinking-led-arduino-asm.ino
Code: Select all
/*
blinking-led-arduino-asm.ino
Blink an LED on pin 13 every 250ms
using assembly routines.
│Take a look at blinking-led-arduino-asm.S │
*/
Code: Select all
;blinking-led-arduino-asm.S
; Blink LED on PB5(Arduino Uno pin 13)
#define __SFR_OFFSET 0
#include "avr/io.h"
.global main
main:
sbi DDRB, 5 ; Set PB5 as output
blink:
sbi PINB, 5 ; Toggle PINB
ldi r25, hi8(1000)
ldi r24, lo8(1000)
call delay_ms
jmp blink
delay_ms:
; Delay about (r25:r24)*ms. Clobbers r30, and r31.
; One millisecond is about 16000 cycles at 16MHz.
; The inner loop takes 4 cycles, so we repeat it 3000 times
ldi r31, hi8(4000)
ldi r30, lo8(4000)
1:
sbiw r30, 1
brne 1b
sbiw r24, 1
brne delay_ms
ret
Click "Compile", then click "UpLoad".
Click the red button "Power Circuit".
You should see blinking LED.
The pin 13 in Arduino is the same as PB5 in AVR microcontrollers.
We will not comment much this project, since it was made only for a demonstration.
Re: Lesson 1
Blinking LED in Arduino.
Now we will create an Arduino project using a sketch. A sketch is the name that Arduino uses for a program. It's the unit of code that is uploaded to and run on an Arduino board.
Create the folder "blinking-led-arduino" in the directory "my-avr".
Create the subfolder "blinking-led-arduino_ino" in the folder "blinking-led-arduino".
Open the program SimulIDE and click "Save Circuit As", then save your project to the folder "blinking-led-arduino\blinking-led-arduino_ino" with the file name "blinking-led-arduino".
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:
Arduino Uno (Micro->Arduino->Uno)
resistor 100 Ω (Passive->Resistors->Resistor)
Led (Outputs->Leds->Led).
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. The resistor must be connect to D0. Rotate LED and connect it to GND. Pay attention to LED, since it emits light only if it's connected correctly. The current must go from an anode to a cathode (LED will not emit light if you connect it in the opposite direction).
Click "Save Circuit".
Now we will create an Arduino project using a sketch. A sketch is the name that Arduino uses for a program. It's the unit of code that is uploaded to and run on an Arduino board.
Create the folder "blinking-led-arduino" in the directory "my-avr".
Create the subfolder "blinking-led-arduino_ino" in the folder "blinking-led-arduino".
Open the program SimulIDE and click "Save Circuit As", then save your project to the folder "blinking-led-arduino\blinking-led-arduino_ino" with the file name "blinking-led-arduino".
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:
Arduino Uno (Micro->Arduino->Uno)
resistor 100 Ω (Passive->Resistors->Resistor)
Led (Outputs->Leds->Led).
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. The resistor must be connect to D0. Rotate LED and connect it to GND. Pay attention to LED, since it emits light only if it's connected correctly. The current must go from an anode to a cathode (LED will not emit light if you connect it in the opposite direction).
Click "Save Circuit".
Re: Lesson 1
- int led=13; //creates variable led:=13
- void setup(){
- pinMode(led, OUTPUT); //makes the pin 13 an output
- }
- void loop(){
- digitalWrite(led, 1); //writes the HIGH value to the digital pin 13
- delay(2000); //delay 2 seconds
- digitalWrite(led, 0); //writes the LOW value to the digital pin 13
- delay(2000);
- }
The function loop() is an endless loop.
The setup function is a great place to initialize input and output pins so they are ready to be used. Then the program moves to the loop function code.
Re: Lesson 1
Code: Select all
int led=13; //creates variable led:=13
void setup(){
pinMode(led, OUTPUT); //makes the pin 13 an output
}
void loop(){
digitalWrite(led, 1); //writes the HIGH value to the digital pin 13
delay(2000); //delay 2 seconds
digitalWrite(led, 0); //writes the LOW value to the digital pin 13
delay(2000);
}
Re: Lesson 1
Create a new code file in SimulIDE. 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 "Save". Then enter "File name: blinking-led-arduino", select "Save as type: ino(*.ino)" and click "Save" in the pop up window "Save Document As".
Click "Compile", then click "UpLoad".
Click the red button "Power Circuit".
You should see blinking LED.
Click "Save". Then enter "File name: blinking-led-arduino", select "Save as type: ino(*.ino)" and click "Save" in the pop up window "Save Document As".
Click "Compile", then click "UpLoad".
Click the red button "Power Circuit".
You should see blinking LED.
Re: Lesson 1
You can make a real circuit and use blinking LED for a Christmas tree or put it in a toy police car and give it as a gift to children.