Newb: My ATmega324's aren't working.

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

Read my update below "Posted: Apr 21, 2008 - 11:49 PM"...I dont think this is a code issue.
======================================================
This is what I came up with after pouring over the ATmega324 datasheet...particularily chapter 16.

//Generate a PWM Pulse at approximately 33Hz with 50% duty cycle
//F. Andre Bertomeu 4/19/08
#include 
int main()
{
//Set up the constant PWM signal
OCR2A = 0x7F; //set to 50% PWM?
TCCR2A = 0x81; //set TCCR2A to the values (0x1000--01b)
TCCR2B = 0x04; //set TCCR2A to the values (0x00--0100b)
//Set N=64, f=33.3Hz
DDRD = 0x80; //as of right now, only enable the PWM pin as output
while(1);
return 0;
}

I think I got it to burn on the chip with my AVR Dragon. To verify that the PWM is operational, I plugged in the mega324 into a breadboard, connected power and ground +10K resistor&switch for the reset.

Then I checked pin D7 on the scope hoping to see a nice waveform of some kind. Nothing.

Can anyone verify that my code is correct? Maybe some tips on how to trouble shoot. Right now, I'm not messing with JTAG just FYI.

I did run it under the AVR simulator in AVR Studio, and everything checked out perfect except for Port D showed nothing. I read somewhere that PWM and AVR Simulator don't mix (something about OC registers not buffering) and that the only way to test PWM is to actually program it and hook it up. I might even be hooking up the circuit wrong...

Last Edited: Tue. Apr 22, 2008 - 06:54 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It'd be much easier to understand your code if you'd replaced:

TCCR2A = 0x81; //set TCCR2A to the values (0x1000--01b) 
TCCR2B = 0x04; //set TCCR2A to the values (0x00--0100b)

with

TCCR2A = (1<<COM2A1) | (1<<WGM20);
TCCR2B = (1<<WGM22);

The obvious omission would then become instantly apparent. You never set any of the CS2x bits in TCCR2B so the timer never actually starts counting!

Now there may be other problems but that one has to be pretty fundamental.

And just to check you did mean to use WGM mode 5 (Phas Correct PWM) ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
TCCR2B = (1<<WGM22);

No, setting TCCR2B to 0x04 is:

TCCR2B = (1<<CS22);

Not that I am disagreeing that the bit names should be used. In fact, your example exemplifies the problem!

Regards,
Steve A.

The Board helps those that help themselves.

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

Yes, well atleast I think I want to use mode 5 phase corrected PWM...

Thanks for the correction. I thought I was setting the CS bits... Is this standard C syntax? I'm not familiar with it. And I didnt know that you could access those bits directly with keywords like that!

I'll give it a try this afternoon (crosses fingers).

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

Quote:
Is this standard C syntax? I'm not familiar with it.

Yup - handy tutorial:

https://www.avrfreaks.net/index.p...

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

Quote:
And I didnt know that you could access those bits directly with keywords like that!

It is not accessing bits directly, it is using symbols to generate the 0x04 that you want. In this case it will result in something like:

LDI rX, 0x04
OUT TCCR2B, rX

If the register had been in one of the lower I/O registers (and since it is setting only one bit), it would have resulted in a single SBI instruction.

Regards,
Steve A.

The Board helps those that help themselves.

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

So in other words. My original code should work right?
When I run it on AVR simulator, it starts the timer, and everything runs except PORTD's OC2A, it doesnt respond in the simulator. The compare match flag works, everything works except OC2A. Thats expected from what I have read, but atleast it shows that my code does make the timer work, and the compare match flags...

I'm here in the lab now, and I tried clawson's code...modified to take into account Koschi's correction

TCCR2A = (1<<COM2A1) | (1<<WGM20);
TCCR2B = (1<<WGM22)|(1<<CS22);

It doesnt work either. I might try another spare uC...but I'm thinking I'm doing something wrong. It might be in my breadboard circuit? I dunno. I'll post that circuit up to see if theres something wrong. You guys are my only lifeline 8) but it's an invaluable one.

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

Can anyone send me a very basic ATmega324 program so I can test it? It needs to be verified to work. Maybe just make a pin go high for a second and then low and repeat. At this point, I need to rule out any hardware issues on my part (the Dragon not working right, or my setting up the AVR on the bread board etc..) If it works then it means I'm doing something wrong in code. If it doesnt work, then it means something is wrong on the hardware side of things (or maybe I'm not setting the right fuses??) Talking about fuses...I haven't set any of them. I just went with what was already there. Maybe thats my problem?
I can search examples, but they are usually for other ATmegas. I dont want to be altering any code at this point to suit my 324 because it opens up the possibility that I screwed up the code.

Anyways...anyone wanna help? I've been beating my head over this too much, and I found (possibly) some help but he wont be here until tomorrow.

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

Well, something definately isnt right and its not the code. I downloaded this into it.

#include 
int main()
{
PORTD=0xFF;
DDRD=0xFF;
return 0;
}

Nothing. In every case the pins just give out noise ranging in millivolts, which I think is normal if its not doing anything. So its not doing anything.

Heres how I am setting it up on my breadboard for power

VCC=5.0V...I tried 5.5V too.
Then I just probe the pins in question. For the PWM prog, I look at Pin21 and then check the rest just in case. They all do the same thing, nothing.

For the code above, I check all the pins. Same thing. The code above SHOULD flip all the portD pins high correct?

I dont think there is anything wrong with the Dragon board itself. I could see it programming it on the scope (hooking up the scope to Mosi and Miso and the clock etc...)

So the issue is either my breadboard setup, and or, flipping on the right fuses on AVRStudio? I've tried programming it with and without the JTAG fuse enabled.
Oh and I've tried two different micros.

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

Quote:

return 0;

So, what do you think the next instruction is after this one? And the one after that? In other words, where are you returning to?

First step: make a real microcontroller program, that runs "forever". This is usually an infinite loop of some sort, the "main loop".

When you program your chip, do you do a full sequence, starting with signature read, erase, program, and verify? And your tool reports success to each step?

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

Does the pwoer supply EXPLODE every time you hit the reset button? :roll:
No cap on the reset pin? If the BOD is not enabled then you most likely don't get a reset and the poor chip wonders aimlessly " where do I start, where do I start"

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

No external clock source shown in your schematic. So you have to use internal RC oscillator. Have you set the fuses accordingly? I would also set the CKOUT fuse so you can actually see the clock on PortB1.

And, please follow the advice from all the other guys.

Jörg.

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

theusch wrote:
Quote:

return 0;

So, what do you think the next instruction is after this one? And the one after that? In other words, where are you returning to?

First step: make a real microcontroller program, that runs "forever". This is usually an infinite loop of some sort, the "main loop".

When you program your chip, do you do a full sequence, starting with signature read, erase, program, and verify? And your tool reports success to each step?

Lee

I realize I made this post at midnight and there are a few mistakes...I forgot the while(1) in the code I gave (doesnt matter I guess since the other codes had it and they didnt work), and my mspaint diagram has the resistor misplaced sorry about that.

I think there is something to the fuses.
Here are the fuses that are set (by default).
In the course of all this I deselected the JTAG fuse, but it made no difference.

When I change any of them (setting any BOD, or changing PB1 for CKOUT) I get an error.

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

So are you using ISP?

It looks like you are still using an older version of Studio. May want to upgrade. The programming screen will be different.

What is the ISP frequency set at? (Advanced tab?? keep at 125KHz for now).

The fuse as they are are ok for a 1 MHz. When it starts to work you may want to Boot size down to 256 bytes to give you maximum room if you don't intend to use a bootloader.

Keep JTAG off if you don't want to use the Dragon for debug, otherwise reenable it. Keep in mind that you will lose pc2-pc5 with JTAG on.

BOD can be set at 4.3V with a 5V supply.

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

For some reason, it let me set the BOD this time...wierd? I set it to 4.3V and still nothing....

Oh and the message told me to keep it well below 1/4 the frequency so I put it below 125KHz. However, I have tried it with 125KHz, and just now I tried it again. It made no difference.

Here are all my tabs except for the fuses tab.




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

well I was also able to set the CKOUT fuse. If I try to verify it, it gives an error, but if I ignore it and just program it, then it lets me!

I can see PB1 is giving out the clock. Its on my scope now. The voltage amplitude is 4.8V, and the frequency is dead on 1MHz. PB0 and PB2 are also giving out some kind of signal too. smaller in amplitude and they range from -mv's to +mv's but have the same 1MHz freq. I wonder what that is?

Is it normal for me to activate the reset and have the clock on PB1 be unaffected? I thought all the pins go into tristate when reset is active.... oh well I guess thats off topic.

I want my code to work! Why is it not working?
I'm thinking maybe my include files are in the wrong place, or I'm saving my project where I'm not supposed to? Maybe I'm not including the right file? I'm thinking my AVRstudio4 is just sending in garbage into my micro.

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

You gotta be sh***** me... it works now. All of it. PWM and all. But only with flash. I tried the flash thing before but that was pointless because I would remove the chip from the dragon and pop it on my breadboard (which means the flash would erase correct?)...so I stuck to eeprom ever since. Only reason I tried flash now is because I configured the AVR for offboard programming this time.

Its great that it works, but I really do need to program the eeprom.

EDIT:
Ok well I guess it still works even if you turn the power off. Am I wrong about the EEPROM? I guess it doesnt matter now, but it bothers me that the EEPROM wont work...

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

Quote:
which means the flash would erase correct?...
WRONG!!!

Do you mean ALL this time you have been trying to run your code from EEPROM???? Code can ONLY run from program space which is flash, it cannot run from anywhere else. :roll:

I'm taking an aspirin and lay down for a while...

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Well, I don't see why it can't load from the eeprom. This is what I was taught in school with the old microcontrollers that make you design your own memory maps, and force you to use a willem programmer to flash your eeprom. it would then load into ram from your eeprom on startup. I guess it doesnt apply here.

Hey thanks for hanging in there. If it makes you feel any better, I was trying the flash here and there before...I think it worked now because of your advice to set the BOD and the ISP frequency. thanks for your help.

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

Flash and EEPROM are non volatile. The AVR program is stored in the flash and variables etc are loaded into RAM or registers when the program runs. You can store information in the EEPROM and save to the EEPROM during runtime if you want. When you disconnect power to the AVR, your ram contents are lost.

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

Quote:

it would then load into ram from your eeprom on startup.

Just to make it absolutely clear, if you didn't infer this from chief11's answer: The code is not copied to RAM and executed from there. The code is in FLASH, and in FLASH only. Instructions are fetched from there directly to the CPU for execution.

It seems that you have experience with the "classic" computer architecture where, at execution time, both code and data is in RAM - like on all our desktop/laptop/mainframe/super-computers. This is known as Von Neumann architecture. One generic memory for both instructions and data.

AVRs use what is called the Harvard architecture. The essence of this is that there are two memories - one for code/instructions (the FLASH), and one for data (RAM).

http://en.wikipedia.org/wiki/Von...
http://en.wikipedia.org/wiki/Har...

Hope that this clears up the possible misconception.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

eeprom is useful for storing user changable parameters on a byte by byte basis. Flash gets erased and burned in sectors... good for program code

Imagecraft compiler user

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

Quote:

It seems that you have experience with the "classic" computer architecture where, at execution time, both code and data is in RAM - like on all our desktop/laptop/mainframe/super-computers. This is known as Von Neumann architecture. One generic memory for both instructions and data.

AVRs use what is called the Harvard architecture. The essence of this is that there are two memories - one for code/instructions (the FLASH), and one for data (RAM).


And us Freaks know which one is way better, right, Johan?

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

Lee!

Upon first reflection, I personally might be thrilled by an AVR-like architecture but with RAM that is slightly to moderately larger than FLASH, and code in FLASH is loaded/shadowed to RAM upon power-up and execution then commencing in a standard Von Neumann fashion.

OTOH: I realize the extra robustness given by the Harvard architecture with a code memory that is somewhat read-only. I'm a hobbyist when it comes to AVRs, and do PC (Windows/Linux, but I was around in the glory days of MS-DOS 3.30) programming for a living. I've had my share of run-away code that thrashes itself when a pointer is pointing straight into code-space and then accidentally used as a destionation for a memcpy or some such. Boooom!

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

Quote:

Upon first reflection, I personally might be thrilled by an AVR-like architecture but with RAM that is slightly to moderately larger than FLASH, and code in FLASH is loaded/shadowed to RAM upon power-up and execution then commencing in a standard Von Neumann fashion.

It (AT94) is already here, for years. But we rarely even mention it, 'cause most of us are thrilled with our near-single-chip-solution microcontroller apps using AVRs.
http://www.atmel.com/dyn/product...

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

JohanEkdahl, thank you. Your comments complement what I learned 1 year ago in my Computer Architecture class...its actually a pleasant refresher. Everything you said I understand, feels good to not be confused :)

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

Quote:
Everything you said I understand, feels good to not be confused
...now if I could just reach out and grab your neck for a few minutes... :lol:

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly