Atmega328p problem with timer interrupt

Go To Last Post
12 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello everyone. This is my first post, first of all I want to thank you for keeping up this huge knowldedge library. I hope my problem can contribute to this.

First of all, I'm working on the Atmega328P in assembly, using the Arduino Uno platform.

The issue is the following: I need to, in a bigger project, detect RFID cards with the MFRC-522 module. This module has an interrupt pin, but it's not useful for detecting cards. What i had to do, was configure timer 2 as a counter, and each time it overfows i get an interrupt and pool for cards in a certain time period. I'm attaching a test program i made, that with the interrupt searchs for cards, and if it founds one, it displays its UID (some kind of card identifier). This program runs great on my board, an Arduino Uno, it displays the UID perfectly.

The problem is, when i pass it to a partner and he runs it, it doesen't work, it never enters the routine for pooling cards, and i really can't figure why. The board he is using (it's not arduino) has the only difference that instead of an external 16 MHz clock it is using a 7,37 MHz one, but that should only change the amount of time it takes for the interrupt to occur (I beleive).

I'm pretty sure there is a rookie mistake in all of this, this is the first time we are coding in assembly and using microcontrollers, but i couldn't find any similar issues neither in the forum nor any website. 

 

I'm uploading the test code. It is a little bit extense, but i post it all just in case. The important lines should be the first 200, as the rest are mostly functions por SPI read/write and USART printing strings. The code that detects the RFID cards shouldn't be important either, as it works, and the problem is the program is not entering that segment. Thanks to everyone in advance

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Welcome to the Forum.

 

You can include the program in your post by using the "<>" Icon in the editor tool bar.

 

I don't do AVR Asm Language, but I did notice a comment about 9600 baud.

 

If your project communicates using the USART, then you will need to re-compile it for the 7.37 MHz clock frequency.

 

Make sure you have a command, (or compiler instruction, or whatever), to tell the compiler your micro's clock frequency, (16oooooo Hz or 7370000 Hz).

 

JC

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello DOcJc,

 

Thanks for the reply. I did not include the program in the post as it was too extense, and I feared it will make the reading uncomfortable.

 

About the baud rate, we did notice that, and changed the value of the register. The serial communication was working, as for debugging we send arbitrary characters in certain points of the program to see if it executed them. That is how we realized the program was never entering the part of detecting cards.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The next thing to check, if the hardware works on one PCB and not on another one, is the FUSES.

They configure the clock source, and sometimes a divide clock by 8 option, as well as other parameters.

 

Does the second PCB have the EXACT same model micro?

 

If not, yo will need to re-compile the program for the second micro.

 

If they are too different, then things like Timer/Counters, etc., may also not match, and the code could need tweaking.

 

A Nano is < $3 USD from a number of sources, (e.g. Banggood Electronics).

Life would be simpler if you both used the same hardware!

 

Others will have to comment on the code, but if it works on one board, that would not be expected to be the problem, (except as noted above).

 

JC

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

We do have the same exact micro, an Atmega328p. I can't really answer about the fuses, as I don't know very well what is their function. Where could I read more about that?

 

It is a very strange problem. Thanks for your replies. If someone with assembly knowledge could check the code, we would be very grateful

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

fourier54 wrote:
. I can't really answer about the fuses, as I don't know very well what is their function. Where could I read more about that?
In the 328p datasheet. They are programmable "links" that set some chip options. Amongst those are SUT (Start Up Time) and CKSEL (Clock Select) fuses. It's a combination of those that says "use the external crystal to run the chip". There's another one (CKDIV8 = Clock Divide by 8) that is set by default when you first get the chip and if it remains active then a chip with a 7.37Mhz crystal attached will actually run at 0.92MHz because of the divide by 8. Also when a chip is first supplied the CKSEL ones say "use an internal 8MHz RC oscillator" (which then becomes 1Mhz because of /8). So the 7.37Mhz crystal might be ignore completely if the chip fuses have not been set to activate its use.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello Clawson, thanks for answering.

 

For what I've been reading, the fuses are configured when you burn a bootloader into the microcontroller. I've never done this, as the 328 in the arduino already had a bootloader. Anyway, we are both (me and my partner) using external clocks, and they both work fine, and we've never encountered a problem like the clock being so slow. Does that mean that the CKSEL and CKDIV8 fuses are fine? We've tried multiple programs in both boards and they work fine, we even have other programs that use timers to count time and they work perfectly. Could this be an issue of fuses, anyway?

Maybe I'm wrong with my interpretation of the fuses being only configurable when you burn the bootloader. I'm doing this for a university project, so I don't have much experience with assembly and microcontrollers but I have some knowledge about the theory.

 

Another problem we thought could be happening, is about AVR studio. We are using the version 4.19, and my partner is using it under wine in ubuntu. Could this represent any problem when assembling the code? Sometimes the program presents and odd behavior when being used in wine (like most programs), could that be giving errors in the .hex file?

 

Thanks for your answers and your time

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

fourier54 wrote:
Does that mean that the CKSEL and CKDIV8 fuses are fine?
If it's an Arduino they will have been set for no divide and to enable the use of an external crystal. So that bit should be fine.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

And what about the custom board? It has an external clock of 7,37 MHz and some delay routines programmed for that frequency seemed to work fine (they had to wait around 9 seconds and they did).  Does that mean that the fuses are fine? Not to mention that lots of other programs work perfectly on it

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

DocJC wrote:

 

If your project communicates using the USART, then you will need to re-compile it for the 7.37 MHz clock frequency.

 

 

 

Also you can try 14.7456 Mhz clock.... It gives 0 % error rate for most of baud rates... like (9600, 115200) which are mostly used

Show's ON....

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello everyone. Thanks for all your answers. We've solved the issue, it turned out to be a problem with the MFRC-522. We were not performing a hard reset, so a loop that asked if it was ready never finished (a rookie mistake).

 

As we are on it, i have a question. While developing this project, I noticed that there are no (or i couldn't find any) examples of reading/writing RFID tags with the MFRC-522 in assembly language. All the examples and tutorials I found were basically "download the arduino library and use this function...". Now, I have an operating code in assembly that can read and write cards, and I want to make it public so if someone else tries to tackles this problem doesn't have the same problem I had and has some examples to work on. Where do you think I should post this code so anyone working on RFID with assembly can find it easily? Here, in the forum? In a github repository?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

fourier54 wrote:
Where do you think I should post this code so anyone working on RFID with assembly can find it easily? Here, in the forum? In a github repository?
Well Atmel have their own "repo" for AVR specific code:

 

https://spaces.microchip.com/gf/

 

You can share AVR (or it seems anything "Microchip" there now) but be warned that the conditions say that you are effectively giving Atmochip the right to use your code in anyway they see fit too.

 

But if you plan for this to be truly "open" I guess that should not matter. I happily host 3 or 4 projects there as I'm happy for anyone to make what use of them they care to do. As long as you havethe same approach that may be a good place. One effect is that you get your own free choice of SVN or Git hosting provided for you.

 

Having said all that if someone really wanted RFID support in Asm then I'm not sure why they wouldn't just take a well established C/C++ library such as that from Arduino or whoever and get the C/C++ compilers to convert it into Asm source for them then use that? The reason C/C++ use is so widespread is that large parts of the code will be more portable and maintainable that way.