assembly, atmel 8515

Discussion in 'Programmer's Corner' started by nice_song, Mar 11, 2008.

  1. nice_song

    Thread Starter New Member

    Mar 11, 2008
    6
    0
    dear all,
    i'm a new at programming AT8515 assembly language, right now, im stuying it by myself. I use AVR studio simulator tool for my programs.
    I need help programming the UART. I think i need some sample programs for UART programming so i can move on in my programming..

    who can help me in this?
    hope you will reply.
    thanks!
     
  2. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    Take a look at this webpage www.avrbeginners.net. There is a section on setting up the UART in an AVR device. It is written in assembly language but the register initialization will be the same in either case.

    There is actually a pretty good example of initializing the USART in the ATMEGA8515's datasheet on page 141.

    hgmjr
     
  3. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    What programming language are you using for your ATMEGA8515?

    hgmjr
     
  4. nice_song

    Thread Starter New Member

    Mar 11, 2008
    6
    0
    hi!

    i'm using assembly language..

    ..one particular problem i have is that i have to send the positions of the switch and display the received positions of the switch on the hyperterm programs in windows and on the other STK500 LED when connected together through their serial ports. I use port A for my switches and port C for my leds..

    thanks for the reply.
     
  5. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    What assistance do you need at this point? Have you sorted out the UART initialization difficulty you mentioned you were having?

    hgmjr
     
  6. nice_song

    Thread Starter New Member

    Mar 11, 2008
    6
    0
    yup.. i have sorted out the UART initialization already.
    thanks for the link!

    i'm currently integrating things...
    i might need some help later.. please keep posted.:)

    thanks so much..
     
  7. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    Let us know if you have any further problemS coding your avr.

    hgmjr
     
  8. nice_song

    Thread Starter New Member

    Mar 11, 2008
    6
    0
    hi!

    i got this program code from a friend,
    the problem with this program is whenever i try in with the board,
    the output on the screen (windows hyper term) appears to be more than one number (eg. when i press the push button for the 1, what appears in the screen is 1111111111111111111111111111, something like that).. it could have a problem with the delay, but i don't know where specifically i could place the delay.. or rather, how to do it..:(

    by the way i now use the AT90S8515, STK500.. if you have the board, u can try it...
     
  9. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    Code ( (Unknown Language)):
    1. ;-------------STK500 UART Program ---------------;
    2. ;  Program Description:                          ;
    3. ;     This program reads the push button and     ;
    4. ;     sends the position of the pressed          ;
    5. ;     pushbutton to the PC through the serial    ;
    6. ;     port. The values are displayed using       ;
    7. ;     Hyperterm program in Windows               ;
    8. ;-------------STK500 UART Program ---------------;
    9. .include "8515def.inc"
    10. .def temp =r16
    11. .def temp2=r17
    12. .def ch   =r18
    13. ;---- Start of Program----;
    14. .CSEG     ; code segment
    15. Vector:
    16.      rjmp Reset             ;Program Reset:1
    17.      rjmp VECInt0           ;External IRQ 0:2
    18.      rjmp VECInt1           ;External IRQ 1:3
    19.      rjmp VECTmr1_Capt      ;Timer/Ctr 1 Capture Event:4
    20.      rjmp VECTmr1_CompA     ;Timer/Ctr 1 Compare Match A:5
    21.      rjmp VECTmr1_CompB     ;Timer/Ctr 1 Compare Match B:6
    22.      rjmp VECTmr1_Ovf       ;Timer/Ctr 1 Overflow:7
    23.      rjmp VECTmr0_Ovf       ;Timer/Ctr 0 Overflow:8
    24.      rjmp VECSPI_STC        ;Serial Transfer Complete:9
    25.      rjmp VECUART_RX        ;UART, RX Complete:10
    26.      rjmp VECUART_UDRE      ;UART, Data Register Empty:11
    27.      rjmp VECUART_TX        ;UART, TX Complete:12
    28.      rjmp VECAna_Comp       ;Analog Comparator:13
    29.  
    30. Reset:
    31.      rjmp Init
    32. VECInt0:
    33.      reti
    34. VECInt1:
    35.      reti
    36. VECTmr1_Capt:
    37.      reti
    38. VECTmr1_CompA:
    39.      reti
    40. VECTmr1_CompB:
    41.      reti
    42. VECTmr1_Ovf:
    43.      reti
    44. VECTmr0_Ovf:
    45.      reti
    46. VECSPI_STC:
    47.      reti
    48. VECUART_RX:
    49.      reti
    50. VECUART_UDRE:
    51.      reti
    52. VECUART_TX:
    53.      reti
    54. VECAna_Comp:
    55.      reti
    56.  
    57. Init:
    58.      ldi temp,high(RAMEND)     ;set stack ptr
    59.      out SPH,temp
    60.      ldi temp,low(RAMEND)
    61.      out SPL,temp
    62.      ldi temp,0x80             ;enable global interrupt
    63.      out SREG,temp
    64.      ldi temp,0x00             ;disable external interrupts
    65.      out GIMSK,temp
    66.      out TIMSK,temp            ;disable timer interrupts
    67.      out TCCR0,temp            ;Disable Timer0
    68.      out TCCR1B,temp           ;disable Timer1
    69.      out DDRA,temp             ;set portA as input
    70.      ldi temp,0xff
    71.      out DDRB,temp             ;set PortB as output
    72.      out PORTB,temp            ;clear LEDs
    73.  
    74. UART_init:
    75.      ldi temp,0x16             ;set 9600 baud rate
    76.      out UBRR,temp
    77.      ldi temp,0x18             ;disable RX/TX interrupt, 8-bit
    78.      out UCR,temp
    79.  
    80. Main:
    81.       rcall getkey
    82.       out PORTB,temp
    83.       out UDR,ch                ;send to serial port
    84. loop:
    85.       sbis USR,[B][COLOR=red]0x05[/COLOR][/B]             ;check if sent (UDRE is bit 5 instead of bit 6...)
    86.       rjmp loop
    87.       rjmp Main
    88. getkey:                         ;check if button pressed
    89.       sbis PINA,0x00
    90.       rjmp k0
    91.       sbis PINA,0x01
    92.       rjmp k1
    93.       sbis PINA,0x02
    94.       rjmp k2
    95.       sbis PINA,0x03
    96.       rjmp k3
    97.       sbis PINA,0x04
    98.       rjmp k4
    99.       sbis PINA,0x05
    100.       rjmp k5
    101.       sbis PINA,0x06
    102.       rjmp k6
    103.       sbis PINA,0x07
    104.       rjmp k7
    105.       rjmp getkey
    106.  
    107. k0: ldi ch,'0'
    108.       ret
    109. k1: ldi ch,'1'
    110.       ret
    111. k2: ldi ch,'2'
    112.       ret
    113. k3: ldi ch,'3'
    114.       ret
    115. k4: ldi ch,'4'
    116.       ret
    117. k5: ldi ch,'5'
    118.       ret
    119. k6: ldi ch,'6'
    120.       ret
    121. k7: ldi ch,'7'
    122.       ret
    I imported your code into AVRSTUDIO4 and simulated it. The main thing I see is that in the loop used to test the "UART Data Register Empty" flag, the bit tested should be bit-5 rather than bit-6. (See my code insert above for the actual line involved.)

    See if that change fixes your problem. If not let me know and I will see if there is something else amiss.

    Good Luck,
    hgmjr
     
  10. nice_song

    Thread Starter New Member

    Mar 11, 2008
    6
    0
    thanks for that..

    i'm surprised with my simple mistake, but honestly i don't really understand why the UDRE should be just a 5-bit..??..
     
  11. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    I did not mean to infer that it is a 5 bit value. The UDRE flag occupies the bit-5 position in the control register for the UART in the AVR device you are programming.

    hgmjr

    keywords: atmega8515, AVR, AVRSTUDIO4, assembly language
     
  12. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    Code ( (Unknown Language)):
    1.                                                                    
    2.  
    3. ;-------------STK500 UART Program ---------------;
    4. ;  Program Description:                          ;
    5. ;     This program reads the push button and     ;
    6. ;     sends the position of the pressed          ;
    7. ;     pushbutton to the PC through the serial    ;
    8. ;     port. The values are displayed using       ;
    9. ;     Hyperterm program in Windows               ;
    10. ;-------------STK500 UART Program ---------------;
    11.  
    12. .nolist
    13. .include "8515def.inc"
    14. .list
    15.  
    16. .def temp             = r16
    17. .def temp2            = r17
    18. .def ch               = r18
    19. .def switch_state = r19
    20. .def any_switch     = r20
    21. .def sw_temp        = r21
    22.  
    23. ;---- Start of Program----;
    24.  
    25. .CSEG                    ; code segment
    26.  
    27. .org    0
    28.  
    29. Reset:
    30.     rjmp    Init
    31. VECInt0:
    32.     reti
    33. VECInt1:
    34.     reti
    35. VECTmr1_Capt:
    36.     reti
    37. VECTmr1_CompA:
    38.     reti
    39. VECTmr1_CompB:
    40.     reti
    41. VECTmr1_Ovf:
    42.     reti
    43. VECTmr0_Ovf:
    44.     reti
    45. VECSPI_STC:
    46.     reti
    47. VECUART_RX:
    48.     reti
    49. VECUART_UDRE:
    50.     reti
    51. VECUART_TX:
    52.     reti
    53. VECAna_Comp:
    54.     reti
    55.  
    56. Init:
    57.     ldi    temp,high(RAMEND)    ;set stack ptr
    58.     out    SPH,temp
    59.     ldi    temp,low(RAMEND)
    60.     out    SPL,temp
    61.  
    62.     ldi    temp,0x80        ;enable global interrupt
    63.     out    SREG,temp
    64.  
    65.     ldi    temp,0x00        ;disable external interrupts
    66.     out    GIMSK,temp
    67.     out    TIMSK,temp        ;disable timer interrupts
    68.  
    69.     out    TCCR0,temp        ;Disable Timer0
    70.     out    TCCR1B,temp        ;disable Timer1
    71.  
    72.     out    DDRA,temp        ;set portA as input
    73.     ldi    temp,0xff
    74.     out    DDRB,temp        ;set PortB as output
    75.     out    PORTB,temp        ;clear LEDs
    76.  
    77.  
    78. UART_init:
    79.     ldi    temp,0x16        ;set 9600 baud rate
    80.     out    UBRR,temp
    81.     ldi    temp,0x18        ;disable RX/TX interrupt, 8-bit
    82.     out    UCR,temp
    83.  
    84.     ldi    switch_state,0
    85.  
    86. Main:
    87.  
    88.     ; Set up a state-machine...
    89.  
    90.  
    91.     cpi    switch_state,0x00
    92.     breq    state0    
    93.     cpi    switch_state,0x01
    94.     breq    state1
    95.     cpi    switch_state,0x02
    96.     breq    state2
    97.     cpi    switch_state,0x03
    98.     breq    state3
    99.  
    100. continue:
    101.  
    102.     ; Do useful stuff here between key presses and key processing....
    103.  
    104.  
    105.  
    106.     rjmp main
    107.  
    108.  
    109. state0:    ; Idle state here for any key pressed...
    110.     in        sw_temp,PINA
    111.     cpi    sw_temp,0xff
    112.     breq    idle_s0
    113.     ldi    switch_state,0x01    ; Bump state...    
    114.     rjmp    continue
    115. idle_s0:
    116.     rjmp    continue
    117.  
    118. state1:    ; Process the key and transmit it...
    119.     rcall    getkey
    120.     out    PORTB,temp
    121.     out    UDR,ch            ;send to serial port
    122.     ldi    switch_state,0x02    ; Bump state....
    123.     rjmp    continue
    124.  
    125. state2:    ; Wait here for send complete...
    126.     sbis    USR,0x05        ;check if sent
    127.     rjmp  continue
    128.     ldi    switch_state,0x03    ; Bump state...
    129.     rjmp     continue
    130.  
    131. state3:    ; Wait here for all switches to release...
    132.     in        sw_temp,PINA
    133.     cpi    sw_temp,0xff
    134.     brne    idle_s3
    135.     cbi    $0b,0x05
    136.     ldi    switch_state,0x00 ; Return to looking for switch...
    137.     rjmp    continue
    138. idle_s3:        
    139.     rjmp    continue
    140.  
    141.  
    142.  
    143. getkey:                    ;check if button pressed
    144.     sbis    PINA,0x00
    145.     rjmp    k0
    146.     sbis    PINA,0x01
    147.     rjmp    k1
    148.     sbis    PINA,0x02
    149.     rjmp    k2
    150.     sbis    PINA,0x03
    151.     rjmp    k3
    152.     sbis    PINA,0x04
    153.     rjmp    k4
    154.     sbis    PINA,0x05
    155.     rjmp    k5
    156.     sbis    PINA,0x06
    157.     rjmp    k6
    158.     sbis    PINA,0x07
    159.     rjmp    k7
    160.     rjmp    getkey
    161.  
    162. k0:    ldi    ch,'0'
    163.     ret    
    164. k1:    ldi    ch,'1'
    165.     ret
    166. k2:    ldi    ch,'2'
    167.     ret
    168. k3:    ldi    ch,'3'
    169.     ret
    170. k4:    ldi    ch,'4'
    171.     ret
    172. k5:    ldi    ch,'5'
    173.     ret
    174. k6:    ldi    ch,'6'
    175.     ret
    176. k7:    ldi    ch,'7'
    177.     ret
    178.  
    Here is a modification to your original program in which I have added a state-machine with four states. By taking the state-machine approach, it is possible to handle those cases in which you "fat-finger" the switch. The state machine forces you to release the current pressed switch before you can press the next switch. This should result in one and only one character per switch depression.

    It can no doubt be improved upon with features like switch debouncing and some additional bells and whistles. It can however serve as an example of one possible implementation of a state machine written in AVR assembler.

    hgmjr
     
  13. nice_song

    Thread Starter New Member

    Mar 11, 2008
    6
    0
    that's so cool!! it will help me a lot! thanks!

    what if i'll use two stk500 boards and i connect them together through serial ports.. one will be a receiver and the other will be a transmitter..

    when the switch/push button is pressed on the transmitter, the corresponding LED should light on the receiver..

    what will be the modified program for this? (from the one you've sent) for the receiver and transmitter?

    thanks again!
     
  14. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    How about you take a stab at implementing the two programs and then post your efforts here. I will be more than happy to assist in debugging your code.

    The example of a state machine I have provided should give you a boiler-plate for you to use in such a program.

    hgmjr
     
Loading...