Skip to content
  • Main
  • FAQ
  • How to Start
  • Forum
  • Student Lobby
  • Games
  • Grammar
  • Listening
  • Speaking
  • Vocabulary
  • Chats
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Admin
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse

Forum Easy English Study

  1. Home
  2. Categories
  3. Information technology
  4. AVR Microcontroller and Arduino Programming
  5. Lesson 1

Lesson 1

Scheduled Pinned Locked Moved AVR Microcontroller and Arduino Programming
17 Posts 1 Posters 898 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • adminA Offline
    adminA Offline
    admin
    wrote on last edited by
    #5
    ;*******************************************************
    ;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.
    
    1 Reply Last reply
    0
    • adminA Offline
      adminA Offline
      admin
      wrote on last edited by
      #6

      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 a blinking LED.

      1 Reply Last reply
      0
      • adminA Offline
        adminA Offline
        admin
        wrote on last edited by admin
        #7

        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".
        blinking-led-avr-c-circuit.png
        It's time to add the code to our program.

        1 Reply Last reply
        0
        • adminA Offline
          adminA Offline
          admin
          wrote on last edited by admin
          #8

          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.

          1 Reply Last reply
          0
          • adminA Offline
            adminA Offline
            admin
            wrote on last edited by
            #9
            #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;
            }
            
            1 Reply Last reply
            0
            • adminA Offline
              adminA Offline
              admin
              wrote on last edited by
              #10

              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.

              1 Reply Last reply
              0
              • adminA Offline
                adminA Offline
                admin
                wrote on last edited by admin
                #11

                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".
                blinking-led-arduino-asm-circuit.png

                1 Reply Last reply
                0
                • adminA Offline
                  adminA Offline
                  admin
                  wrote on last edited by admin
                  #12

                  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.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.

                  1 Reply Last reply
                  0
                  • adminA Offline
                    adminA Offline
                    admin
                    wrote on last edited by admin
                    #13

                    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".
                    blinking-led-arduino-circuit.png

                    1 Reply Last reply
                    0
                    • adminA Offline
                      adminA Offline
                      admin
                      wrote on last edited by
                      #14

                      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.

                      1 Reply Last reply
                      0
                      • adminA Offline
                        adminA Offline
                        admin
                        wrote on last edited by admin
                        #15
                        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);
                        }
                        
                        1 Reply Last reply
                        0
                        • adminA Offline
                          adminA Offline
                          admin
                          wrote on last edited by
                          #16

                          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 Reply Last reply
                          0
                          • adminA Offline
                            adminA Offline
                            admin
                            wrote on last edited by
                            #17

                            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.

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            Shoutbox
                            • Sound
                            • Notification
                            • Hide
                            Learning English Broadcasts (use a limited vocabulary)

                            1 - 2025-02-12

                            Your browser does not support the audio element.

                            2 - 2025-02-11

                            Your browser does not support the audio element.

                            3 - 2025-02-10

                            Your browser does not support the audio element.
                            Radio Voice of America
                            Your browser does not support the audio element.
                            News in the USA and world
                            The Brooklyn Bridge: A Symbol of New York City

                            The U.S. Bullion Depository at Fort Knox Holds America’s Gold

                            Researchers: South Korea’s Birth Rate Increase Last Year Unclear

                            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.

                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Admin