Friday, 2 November 2012

AVR-ATMega32


This is  project about  the ATMega32 microcontroller  .
+Write assembly language with basic , memory ...
+Write C  language with FreeRTOS ,  multistasking...
These documents need reference before we write assembly  language .
   + ATMega 32 datasheet : datasheet
   +ATMega 32 instruction  : instruction
   +ASM  directives            :directives
Development tools :Atmel Studio 6.0




Section 1: Memory storage
Memory of ATmega32   include flash memory(ROM) , SRAM , EEFROM ( disc).
    Data bus is 8 bits wide
    Address bus is  ? bits wide .
   +Program memory is flash memory : 32K bytes -> 16Kx16 bytes

    As program counter(PC) is 14 bits wide ( 2^10*2^4x16) :0->2^14 -1 . 0->16383 <=>$0000 ->$3FFF
    +Data memory is SRAM. : 2K bytes -> 1024*2 bytes
 
   EEFROM : 1024K bytes
    =====================================================================   
Section 2: Writing -Reading  SRAM memory.
; Direct addressing
STS 0x0060, R1 ; write 

LDS R1, 0x0060 ; read
 STS      LDS
;Pointer addressing
.EQU MyPreferredStorageCell = SRAM_START ;SRAM_START = 0x0060
LDI XH, HIGH(MyPreferredStorageCell)
LDI XL, LOW(MyPreferredStorageCell)
LD R1, X
 code :
.INCLUDE "M32DEF.INC"
.EQU  SRAM_Address= 0x0060;
.DEF a = R1
.DEF b = R2
.DEF c = R3
.DEVICE ATmega32 ; use the ATmega32
.DSEG  ; data segment SRAM
.ORG  0x0060 ;set SRAM address to hex , this is right after I/O
data: .BYTE 1     ;reserves memory resources in the SRAM 

.CSEG  ; code segment
.ORG  0x0000 ; set the reset vector to
       RJMP START
.ORG 0x2A
START:
    LDI R20, 0xFF;
 OUT DDRB , R20;// set port to ouput
 LDI R20 , 0x0F;
 OUT PORTB ,R20;
 STS data , R20; //Write into SRAM 
 LDS R19 ,data ; //Read from SRAM
 LDI XH, HIGH(SRAM_Address);// Pointer-registers
        LDI XL, LOW(SRAM_Address)
        LD a, X+ ; // Read from address 0x0060
        LD b, X+ ;// read from  address 0x0061
        LD c, X  ; Read from  address 0x0062
        ST X , R20; ;Write to  address 0x0062
MAIN:
      RJMP MAIN 
 We have basic instruction  for reading and writing SRAM ;
  + LDS -STS
  +LD-ST
Section 2: Reading  flash memory.
.INCLUDE "M32DEF.INC"
.DEVICE ATmega32 ; use the ATmega32
                                   
.ORG  0x0000 ; set the reset vector to
       RJMP START
.ORG 0x2A
START:
 LDI ZH,HIGH(Array*2) ; address of table to pointer Z
    LDI ZL,LOW(Array*2) ; 
MAIN:
     LPM
     ADIW ZL ,1
     RJMP MAIN

Array:
.DB 12,34,56,78; byte array

 .INCLUDE "M32DEF.INC"
.DEVICE ATmega32 ; use the ATmega32
                    
.ORG  0x0000 ; set the reset vector to
       RJMP START
.ORG 0x2A
START:
 LDI ZH,HIGH(Array*2) ; address of table to pointer Z
    LDI ZL,LOW(Array*2) ; 
MAIN:
  LPM ; Read least significant byte from program memory
  MOV R24,R0 ; Copy LSB to 16-bit register
  ADIW ZL,1 ; Point to MSB in program memory
  LPM ; Read MSB of table value
  MOV R25,R0 ; Copy MSB to 16-bit register
  ADIW ZL,1 ; 
    RJMP MAIN

Array: 
.DW 0x1001,0x2233,0x4455; Code segment   in memory  01 10 33 22 55 44           

 //======================================Assembler =======================
Section 3:If - For - while
if
 .INCLUDE "M32DEF.INC"
.DEVICE ATmega32 ; use the ATmega32                                     
.ORG  0x0000 ; set the reset vector to
       RJMP START
.ORG 0x002A
START:
   cp r1,r2 ; Compare registers r1 and r0
   breq IF ; Branch if registers equal
   ELSE:
     LDI R16 , 0x11;
  RJMP ENDIF
   IF :
      LDI R16 , 0xFF;
   ENDIF :
MAIN:
    RJMP MAIN
For-While
 .INCLUDE "M32DEF.INC"
.DEVICE ATmega32 ; use the ATmega32   
.ORG  0x0000 ; set the reset vector to
       RJMP START
.ORG 0x002A
START:
    CLR R6; // for(int i =1 ; i<= 10; i++)
 FOR :
        INC R16;
        CPI R16 ,10;
        BRNE FOR;
 NOP ;
MAIN:
    RJMP MAIM
PORT I/O
.INCLUDE "M32DEF.INC"
.DEVICE ATmega32 ; use the ATmega32
//--------------------------------
.MACRO   PortB_Onput  
      LDI R16 , 0xFF; 
       OUT  DDRB, R16;
.ENDMACRO
//--------------------------------
.CSEG                                                                                       
.ORG  0x0000 ; set the reset vector to
       RJMP START
.ORG 0x002A
START:
  PortB_Onput ;
  LDI R16 , 0xFF; 
  PUSH R16; // pointer is set to zero at program start (Bugs with the stack eperation)
MAIN:
     LDI R16 , 0x01; 
     OUT PORTB ,R16;
     RCALL DELAY ;
  LDI R16 , 0x02;
     OUT PORTB ,R16;
  RCALL DELAY ;
    RJMP MAIN
//----------- ---------------------------------------
DELAY :
       CLR R17;
     Wait:
         INC R17 ;
     CLR R18;
     Wait1: 
           INC R18 ;
        CPI R18 ,0XFF;
         BRNE Wait1 ;
         CPI R17 ,0XFF;
     BRNE Wait ;
      RET
//----------------------------------------------------

 LED 7 SEGMENT
.INCLUDE "M32DEF.INC"
.DEVICE ATmega32 ; use the ATmega32
//--------------------------------
.MACRO   PortB_Onput  
      LDI R16 , 0xFF; 
       OUT  DDRB, R16;
.ENDMACRO
//--------------------------------
.CSEG                                                                                    
.ORG  0x0000 ; set the reset vector to
       RJMP START
.ORG 0x002A
START:
  PortB_Onput ;
  LDI R16 , 0xFF; 
  PUSH R16; // pointer is set to zero at program start (Bugs with the stack eperation)
  LDI R16 , 0x00;
MAIN:
     Count :
  RCALL LED7Seg
  INC R16;
  CPI R16 ,0X0A;
  BRNE Count; // 0->9
  LDI R16 , 0x00;
    RJMP MAIN
//----------- ---------------------------------------
DELAY :
      CLR R19
   Wait2 :
     INC R19;
       CLR R17;
     Wait:
         INC R17 ;
     CLR R18;
     Wait1: 
           INC R18 ;
        CPI R18 ,0XFF;
         BRNE Wait1 ;
         CPI R17 ,0XFF;
     BRNE Wait ;
   CPI R19 ,0X0F;
     BRNE Wait2 ;
      RET
//-------------------------R16 -> value of Led_7_seg--------------------
LED7Seg:
        RCALL DELAY ;
        OUT PORTB ,R16;
       RET
//--------------------------------------------------------------------- 
LED SCAN

 .INCLUDE "M32DEF.INC"
.DEVICE ATmega32 ; use the ATmega32
.DEF LED0 = R20;//hour
.DEF LED1 = R21;
.DEF LED2 = R22;// minute
.DEF LED3 = R23;
.DEF LED4 = R24;//second
.DEF LED5 = R25;
//--------------------------------
.MACRO   PORT_ONPUT  
      LDI R16 , 0xFF; 
      OUT DDRB, R16
      OUT DDRD , R16;//portB ouput 
      OUT PORTD , R16;

.ENDMACRO
//--------------------------------
.CSEG                                                                                       
.ORG  0x0000 ; set the reset vector to
       RJMP START
.ORG 0x002A
START:
   PORT_ONPUT;//Call macro
  PUSH R20; // pointer is set to zero at program start (Bugs with the stack eperation)
  CLR LED0;
  CLR LED1;
  CLR LED2;
  CLR LED3;
  CLR LED4;
  CLR LED5;

MAIN:
     SECOND :
           RCALL LED7Seg;
     INC LED5;
           CPI LED5 ,0x0A;
           BRNE  SECOND;
              INC LED4;
     CLR LED5;
     CPI LED4 ,0x06;
           BRNE  SECOND;

     CLR LED4;
           INC LED3;
           CPI LED3 ,0x0A;
     BRNE   SECOND;
     CLR LED3;
           INC LED2;
     CPI LED2 ,0x06;
           BRNE  SECOND;
              CLR LED2;
              INC LED1;
           CPI LED1 ,0x04;
     BRNE   SECOND;
     CLR LED1;
           INC LED0;
     CPI LED0 ,0x02;
           BRNE  SECOND;
     CLR LED0;
    RJMP MAIN
//-------------------------R16 -> value of Led_7_seg  -count to 1-> 99--------------------
LED7Seg:
        CLR R19
   Wait2 :
     INC R19;
       CLR R17;
     Wait:
         INC R17 ;
     CLR R18;
     Wait1: 
           INC R18 ;
       LDI R16, 0XFF;
     //-------------hour-----------------
     CBI PORTD ,0 ; led0 turn  on
     OUT PORTB ,LED0;
     NOP ;
     SBI PORTD ,0 ; led0 turn off
     OUT PORTB ,R16;

     CBI PORTD ,1 ; led1 turn  on
     OUT PORTB ,LED1;
     NOP ;
     SBI PORTD ,1 ; led1 turn off
     OUT PORTB ,R16;
     //-----------minute---------------
     CBI PORTD ,2 ; led2 turn  on
     OUT PORTB ,LED2;
     NOP ;
     SBI PORTD ,2 ; led2 turn off
     OUT PORTB ,R16;

     CBI PORTD ,3 ; led3 turn  on
     OUT PORTB ,LED3;
     NOP ;
     SBI PORTD ,3 ; led3 turn off
     OUT PORTB ,R16;
     //------------second--------
     CBI PORTD ,4 ; Led4 turn  on
     OUT PORTB ,LED4;
     NOP ;
     SBI PORTD ,4 ; Led4 turn off
     OUT PORTB ,R16;

     CBI PORTD ,5 ; Led5 turn  on
     OUT PORTB ,LED5;
     NOP ;
     SBI PORTD ,5 ; Led5 turn off
     OUT PORTB ,R16;
     //------------------------------------
        CPI R18 ,0XFF;
        BRNE Wait1 ;
         CPI R17 ,0XFF;
     BRNE Wait ;
   CPI R19 ,0X01;
     BRNE Wait2 ;

 
       RET
//---------------------------------------------------------------------
Demo Led  cube 6x6x6
Software :  
Windows OS : windows 7 Professional - 64 bit .
Compiler  : Amtel studio 6.0  .
Hardware :
  + ATmega32
  + 216 green leds 
  + 4  IC 74138
  + Cables
  + Power : 5v
 


Code :skydrive.live.com

Demo : LED cube with AVR microcontroller
me>me>
Thong Le Trung.

No comments:

Post a Comment