Newbie Questions

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

Hello everyone,
I'm new to using Atmel microcontrollers and GNU GCC Compilers. I have a fair amount of experience using Microchip PIC microcontrollers and the CCS PCM PIC C Compiler. I thought it's about time to see what the Atmel chips are all about.

I'm looking for some guidance about getting my Atmel setup working properly. I have a mkII and STK500.

A few questions...

1) The CCS PIC compiler that I am used to comes with many microcontroller specific build in functions like the following. Are there similar functions in GNU CCC? If not, are there add on libraries that have similar functions? Could someone recommned one?
get_timer1()
input(PIN_1)
setup_adc(adc_clock_internal)
set_adc_channel( 1 )
read_eeprom(0x44)

2) I noticed that after installing AVR Studio I have the choice to make a project with "AVR GCC". Is this just a place holder/configuration for when/if you install the GNU CCC Compiler? Or does AVR Studio come with the GNU CCC compiler built in? Does WinAVR install a different C compiler then?

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

AVR GCC is the name of the thing you installed when you installed WinAVR. It's the AVR variant of GCC. Studio itself has no C compiler (only the "GCC plugin"). When it starts up the GCC plugin looks for Winavr (avr-gcc) being present and, if so, when you create a new project as well as "assembler" (using the Atmel assembler that DOES come as part of Studio) it now offers "avr GCC" too.

As for libs - avr-libc (click through my sig for manual) that is part of WinAVR does have support for generic things but not timers/ADC - people who don't want to read the datasheet often use Pascal Stang's Procyon lib for things like that - but to be honest you'll write better code if you understand how it ALL works and the best way to do that is to write it yourself rather than using a "library" of unknown provenance. (though the Stang lib is pretty good!). Another popular set of "library" support is that from Peter Fleury.

Cliff

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

Thanks Cliff. This was very helpful.

Another newbie question...

Where do you setup things like oscilator type, oscilator frequency, watchdog timer on/off, etc. Do you set these up inside the code or in an AVR Studio menu?

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

Well the AVR chips have a series of configuration fuses that cannout be accessed (for write) at runtime by the AVR themselves but they must be programmed using ISP. They are used, amongst other things, to say what form and speed the clock source will take (it can be an internal RC oscillator and that, in fact, at ~1MHz, is the default in AVRs).

While there is a fuse that can be used to say "watchdog permanently on" that's one thing where runtime code, programming registers inside the AVR, can achieve the same.

Cliff

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

Ah, this is much different than with the Microchip parts.

For the Oscillator I see in AVR Studio where you set the fuses that will be flahed into the device. I see there is a fuse called "SUT_CKSEL" which sets the "Type" of oscillator.

Where do you set the actual speed of the oscillator? Also, do you need to define the oscillator speed inside the code alsa, so that the other functions like _delayms() know the speed? How do these funcitons know the value for F_CPU, is that read from the fuse value at statup?

Thanks so much for your help!

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

Well it depends on which CKSEL you choose. If you choose "int RC" then either you have a modern AVr where it's simply 8MHz but there's also a CKDIV8 fuse that can divide that down to 1MHz (and the CLKPR register allows a whole raft of values at runtime). Or it's an older AVR and just has fixed 1/2/4/8MHz int RC oscillators and you pick one.

As soon as you change the fuses to say "external" (either clock or crystal) then the speed the AVR runs at is set by the oscillating frequency of that external device (except that CKDIV8 may still come into play and divide it by 8)

Cliff

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

immo wrote:
How do these funcitons know the value for F_CPU, is that read from the fuse value at statup?
The value of F_CPU is specified in the makefile and it is supplied to the code via a -D command line option for each file compiled. You could, of course, specify the value of F_CPU in a convenient place in your code (instead of doing it via the makefile).

#define F_CPU 14745600

It is up to you to make certain that the value specified for F_CPU matches the actual clock frequency, whatever the source.

Don Kinzer
ZBasic Microcontrollers
http://www.zbasic.net

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

Just to be clear that F_CPU does not "set" a frequency as such - it's merely letting the code know what the internal/oscillator is doing so that the code can calculate things like soft timing delays and UART baud divisors accurately.

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

clawson wrote:
Just to be clear that F_CPU does not "set" a frequency as such - it's merely letting the code know what the internal/oscillator is doing so that the code can calculate things like soft timing delays and UART baud divisors accurately.

Yes, thanks it is clear.

In the delay.h file it says:

#ifndef F_CPU
/* prevent compiler error by supplying a default */
# warning "F_CPU not defined for "
# define F_CPU 1000000UL
#endif

So in my program (running on of a 20MHZ external crystal) I have this in my code:

#define F_CPU 2000000UL

I then set fuse SUT_CKSEL to "Ext Crystal Isc. 8.0 MHZ Start-up time: 1K CK +65ms"

Is this all correct?

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

It is easier to set F_CPU from the Studio Project Configuration. Then you can re-build without editing your source code.

CCS C for PICs is completely non-standard. It may have several optimised functions but it may have taught you some illegal C operations. e.g. a string in ROM.

The AVR is well suited to C compilers, so you will find that you can write regular C code AND it will be compiled very efficiently. CCSC makes fairly efficient code with its built-in functions but regular C is hampered by the PIC architecture.

You will soon learn how to use the AVR data sheets and special function registers. And most code is written ONCE and will just re-compile for a different AVR model.

Otherwise the only other difference is that you program your code and fuses as two separate operations. The PIC will do everything in one go. Just think carefully before you alter AVR fuses.

David.