-
1)#include <avr/io.h> //includes library, standard AVR header
2)#define LED 0 //defines the constant LED. Now LED:=0
3)
4)void delayms(volatile unsigned long j)
5){
6)volatile unsigned long i;
7)for(i=0; i < 157*j; i++); //loops i from 0 to 157 * j
8)}
9)
10)int main(void)
11){
12)DDRD |= (1<<LED); //makes PORTD0 an output
13)while(1)
14){
15)PORTD=(1<<LED); //PD0:=1
16)delayms(2000); //calls delayms
17)PORTD=(0<<LED); //PD0:=0
18)delayms(2000); //calls delayms
19)}
20)return 0;
21)}The program has the next flowchart
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. -
#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; }
-
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 a blinking LED. -
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".
-
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 SaveCopy-paste the next code to files
blinking-led-arduino-asm.ino/* 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 │ */
blinking-led-arduino-asm.S
;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
Open blinking-led-arduino-asm.ino in SimulIDE.
Click "Compile", then click "UpLoad".
Click the red button "Power Circuit".
You should see a 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. -
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".
-
1)int led=13; //creates variable led:=13
2)
3)void setup(){
4)pinMode(led, OUTPUT); //makes the pin 13 an output
5)}
6)
7)void loop(){
8)digitalWrite(led, 1); //writes the HIGH value to the digital pin 13
9)delay(2000); //delay 2 seconds
10)digitalWrite(led, 0); //writes the LOW value to the digital pin 13
11)delay(2000);
12)}
The function setup() is run only once and used for the initial setup.
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. -
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); }
-
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 a blinking LED.
1 - 2025-02-26
2 - 2025-02-25
3 - 2025-02-24
In 2024, the number of babies born in South Korea increased for the first time in nine years. The change is welcome news for a country that is dealing with serious population problems.