Problems with Clock

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

Hello! First of all, I want to say sorry for any english mystakes that can be found here.If you found something wrong, PLEASE CORRECT ME.

I have followed the forum for some time and I'm a beginner in uC. Today I tried to do my first test, which basically comes down to blink some LED's, but I'm alredy encountering problems. I am trying to use the function _delay_ms() from but I guess something is not right.
For instance, if I use _delay_ms(200), the delay I get is far more than 200ms, could say that is aprox. 2~3 secs.
I'm using a 12Mhz crystal as clock for my Atmega16.
Here is the code of my program:

#include
#define F_CPU 12000000UL
#include

int main(void){

DDRA=0xFF;

while(1){

PORTA=0b00000001;
_delay_ms(200);

PORTA=0b00000010;
_delay_ms(200);

PORTA=0b00000100;
_delay_ms(200);

PORTA=0b00000010;
_delay_ms(200);

PORTA=0b00000001;
_delay_ms(200);

PORTA=0x00;
_delay_ms(10);

}
return 0;
}

What I am doing wrong ?

Thanks.

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

Do you have your fuse bits set correctly? Make sure they are set up to use an external crystal.

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

Well, looking at the data sheet I get this:

Quote:
The device has the following clock source options, selectable by Flash Fuse bits as
shown below. The clock from the selected source is input to the AVR clock generator,
and routed to the appropriate modules.
CKSEL3..0
External Crystal 1111

I'm not quite sure on what I have to write in AVR Studio. I tried:

CKSEL = 1111;

But when I try to "Build Active Configuration", I get this error message:

Quote:
error: 'CKSEL' undlecared (first use in this funciton)

The code that I'm tring to compile differs only in this part from the other I posted above:

#include
#define F_CPU 12000000UL
#include

int main(void){

DDRA=0xFF;
CKSEL = 1111;

while(1){

PORTA=0b00000001; 
.
.
.

Thanks for the help. By the way, what's the meaning of the sufix UL after the #define F_CPU "value"UL ?

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

Quote:

By the way, what's the meaning of the sufix UL

Unsigned Long

But doesn't your C book tell you this? (if not you may want to consider getting a better book)