1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

ATmega16A LED blinking code is not working in Dev. Board.

Discussion in 'AVR' started by ikelectro, Jul 3, 2015.

  1. ikelectro

    ikelectro Member

    Joined:
    Aug 25, 2012
    Messages:
    388
    Likes:
    4
    Location:
    Kolkata, Proud to be an Indian.
    Hello everyone, I'm new to AVR programming. I'm using a ATmega16A to blinking LED. AVR Studio showing that program is ok but the LED is not blinking in the Development board. I have connected pin no. 10 and 30 to VCC +5V and pin no. 11 & 31 to GND. I'm using a 16Mhz Crystal at pin no. 12 & 13. and also the clock is not happening. what to do???

    here is the code:
    Code (text):
    .include "m16adef.inc"

                .org    $0000
                .DEF    MR  = R16            ;R16 NAMED AS ACC
                .DEF    MR0 = R20
                .DEF    MR1 = R21

    INTL:        LDI        MR,255                ;LOAD R16/ACC 255
                      OUT        DDRA,MR                ;MAKE PORTD AS OUTPUT
                      LDI        MR0,00
                      LDI        MR1,255

    START:        OUT        PORTA,MR1                ;SET ALL THE BIT OF PORTA
                         RCALL    DLY                        ;CALL DLY ROUTINE
                         OUT        PORTA,MR0                ;CLEAR ALL THE BIT OF PORTA
                         RCALL    DLY                         ;CALL DLY ROUTINE
                         RJMP    START                      ; JUMP AGAIN TO START

    DLY:          LDI        R17, 04                        ;LOAD R17 WITH 08
    DLY1:        LDI        R18, 200                    ;LOAD R18 WITH 200
    DLY2:        LDI        R19, 255                    ;LOAD R19 WITH 255

    DLY3:        DEC        R19                            ;DECRIMENT R19 WITH 1
                       BRNE    DLY3

                       DEC        R18
                       BRNE    DLY2
         
                      DEC        R17
                      BRNE    DLY1
                      RET
     
    Last edited: Jul 4, 2015
  2. Les Jones

    Les Jones Well-Known Member

    Joined:
    May 15, 2015
    Messages:
    1,474
    Likes:
    191
    Location:
    Lancashire UK
    Have you set the clock selection fuse bit to select the external crystal ? (See page 25 of the data sheet) These fuse bits need to be set separately to programming in the code.
    This caught me out when I moved from using PICs to AVR chips.
    Have a look at these notes on my website
    http://lesjhobbies.weebly.com/uploads/2/8/3/1/28311931/programming_the_attiny4313.pdf

    Also you are setting PORTA but your comments say that you are setting PORTD

    Les.
     
    Last edited: Jul 4, 2015
  3. ikelectro

    ikelectro Member

    Joined:
    Aug 25, 2012
    Messages:
    388
    Likes:
    4
    Location:
    Kolkata, Proud to be an Indian.
    Hi, Jones... thanks for your reply. You are absolutely right. I'm missing that point from last two days (In my case I'm moving from 8051 to AVR). After posting in ETO, i aslo figured it out that I have to configure the Virgin Chip before programming it. bye the way thanks a lot for your reply.

    And as far as the comment is concern : I had changed the ports too many times in program as I panicked so mush that little chip is not working! and forgot to change the comments!!!
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. ikelectro

    ikelectro Member

    Joined:
    Aug 25, 2012
    Messages:
    388
    Likes:
    4
    Location:
    Kolkata, Proud to be an Indian.

    And what about my delay (DLY) routine I think I write it wrong?! because blinking is not happening and also cant find any frequency in the port. But later I changed the program as "NOP" instead of writing "RCALL DLY" and I find the frequency 2.02MHz at the ports.

    could you plz check my DLY routine also!
     
  6. Les Jones

    Les Jones Well-Known Member

    Joined:
    May 15, 2015
    Messages:
    1,474
    Likes:
    191
    Location:
    Lancashire UK
    I cannot see why your delay routine does not work. I would first try changing the routine to just a few NOPs and a RET instruction. If it works OK like that then just put one of the three delay loops in the routine to see if it still works.

    Les.
     
  7. ikelectro

    ikelectro Member

    Joined:
    Aug 25, 2012
    Messages:
    388
    Likes:
    4
    Location:
    Kolkata, Proud to be an Indian.
    I have tried that already but no luck..
    here is fuse bit configeration. Am i doing it right???
    cong1.jpg cong2.jpg cong3.jpg
     
  8. Les Jones

    Les Jones Well-Known Member

    Joined:
    May 15, 2015
    Messages:
    1,474
    Likes:
    191
    Location:
    Lancashire UK
    I have not used the ATmega16a (I have mainly used the ATtiny4313 and it's over a year since I worked with AVR code.) so I am not familiar with all the fuse bits. I would not think that any of the fuse bits would affect the RCALL or RET instructions. There is a limitation on the RCALL instruction on devices with more than 4K of program memory but I do not think it would affect such a small program. You could try the CALL instruction to see if that works.

    Les.
     
  9. Les Jones

    Les Jones Well-Known Member

    Joined:
    May 15, 2015
    Messages:
    1,474
    Likes:
    191
    Location:
    Lancashire UK
    I have tried running your code on an ATtiny2313 with a 20 MHZ xtal and using PORTB. It seems to run OK giving about 30 mS on time and 30 mS off time. This is about what I calculate it should be. I do not have an ATmega16A to test it on. I think your problem must be related to the RCALL of the delay routine.

    Les.
     
  10. ikelectro

    ikelectro Member

    Joined:
    Aug 25, 2012
    Messages:
    388
    Likes:
    4
    Location:
    Kolkata, Proud to be an Indian.
    Thanks for your reply... I have gone through some of the AVR tutorial on the internet and found that while using RCALL or CALL type of intruction, something have to be done with satck ; SPL SPH. but I cant figure it out...

    And have running the same code on ATtiny2313??? Its great that works on your chip. it's made me happy... But what the wrong in mine!!!!!!
     
  11. atferrari

    atferrari Well-Known Member

    Joined:
    Oct 8, 2003
    Messages:
    2,816
    Likes:
    121
    Location:
    Buenos Aires - Argentina
    Is any simulator for that chip that you could use?
     
  12. ikelectro

    ikelectro Member

    Joined:
    Aug 25, 2012
    Messages:
    388
    Likes:
    4
    Location:
    Kolkata, Proud to be an Indian.
    Yes, I'm using Atmel Studio 6.2
     
  13. ikelectro

    ikelectro Member

    Joined:
    Aug 25, 2012
    Messages:
    388
    Likes:
    4
    Location:
    Kolkata, Proud to be an Indian.
    The code is running now. I just have to define the stack. Thanks every one for their support.....

    LDI temp,low(RAMEND) ;temp can be any register from R16 to R31
    OUT SPL,temp
    LDI temp,high(RAMEND)
    OUT SPH,temp
     
    • Like Like x 1

Share This Page