AVR c compiler user manual

hgmjr

Joined Jan 28, 2005
9,027
Mik3,

It really doesn't matter what AVR you begin with since you are in the learning mode. The neat thing about AVR is that once you learn to program one of the device and that experience will make it easier to program the next one. They all share the same CPU core. The main difference between the members of the AVR 8-bit family are the built-in features and the size of FLASH, SRAM, and EEPROM.

As handy as JTAG and DEBUGWIRE are in debugging, troubleshooting AVR hardware and software are generally very easy.

hgmjr
 

John Luciani

Joined Apr 3, 2007
475
I would go with C not BASIC. Every uC has a C compiler. As newer uCs come
out you will already know the programming tool.

A C reference book that I like a lot more than K & R is "A C Reference Manual"
by Harbison and Steele ISBN 0-13-326224-3. I do like K & R for its succinct style
but have found Harbison and Steele more useful when I needed more detail.

Below is a version of "Hello World" for my ATmega644 board. "Hello World" for uCs is
a flashing LED. This compiles using avr-gcc (and is saying "Hello World" as we
speak).

(* jcl *)

Rich (BB code):
// hello world 

// uC ... ATmega644
// LED is connected to PORTD, pin PD7

#include <avr/interrupt.h>
#include <avr/iom644.h>
#include <util/delay.h>

// macros to set and clear individual bits

#define BV(bit)         (1<<(bit))
#define cbi(reg,bit)	reg &= ~(BV(bit))
#define sbi(reg,bit)	reg |= (BV(bit))

// macros to waste time

void delayms(uint16_t millis) {
  while ( millis ) {
    _delay_ms(1);
    millis--;
  }
}

void delayus(uint16_t micros) {
  while ( micros ) {
    _delay_us(1);
    micros--;
  }
}

// LED is connected to PORTD, pin PD7

#define LED_PORT PORTD
#define LED_BIT  PD7

int main(void) {
  // initialize the pin connected to the
  // LED as an output and set it low.
  DDRD = BV(LED_BIT); 
  PORTD = 0x00;
  // turn the led on,  wait 1S 
  // turn the led off, wait 1S
  // forever
  while(1) {
    sbi(LED_PORT, LED_BIT);
    delayms(1000);
    cbi(LED_PORT, LED_BIT);
    delayms(1000);
  }
  return(0);
}
 

Thread Starter

mik3

Joined Feb 4, 2008
4,843
Can anyone send me a simple C program for ATMEGA8 which lights a LED on pin pc2?

I want to see the general idea.
 

Thread Starter

mik3

Joined Feb 4, 2008
4,843
I wrote this simple code. It was supposed to keep the led on pin pc2 always on. However, the LED blinks at a frequency of 50 Hz.

Why is that?
 

hgmjr

Joined Jan 28, 2005
9,027
Can anyone send me a simple C program for ATMEGA8 which lights a LED on pin pc2?

I want to see the general idea.
Rich (BB code):
/******************************************
 This program flashes an LED that is 
 connected to BIT-2 or PORT C.  No timer
 is used to control the flash rate...
*******************************************/
#include <avr/io.h>
 
unsigned long counter;
 
int main(void)
{
     DDRC = 0b00000100; 
     /* OR */
     DDRC = (1<<PC2);
     /* OR */
     DDRC = 0x04;
 
     for(;;)
     {
       if (counter++ >= 70000)
       {
         counter = 0;
         PORTC ^= (1<<PC2);  // Flip the state of the LED drive pin with 
                             //   each execution of this statement...
       }
     }
     return(0);
}
This is a very simple and crude approach to ≈1Hz flashing LED program.
The value of counter being tested is based on a CPU clock frequency of 8MHz. Your mileage may vary.

hgmjr
 

nanovate

Joined May 7, 2007
666
Here is an example of using the CTC Mode (Clear Timer on Compare) on Timer 1 on the mega8. This is more accurate than using a busy wait loop. In this program the hardware checks the Timer count against the OCR1A Register and fires off an interrupt when it matches. It also automagically reloads/clears the counter. This can run in the "background" while you do other stuff. This uses the WinAVR compiler.

Rich (BB code):
/* Using an ATmega8's Timer 1 in CTC Mode */

#include <avr/io.h>
#include <avr/interrupt.h> 

int main (void) 
{ 
   DDRC |= (1 << PC2); // Set LED as output on Pin PC2

   /* Set-up Timer 1 for CTC Mode */
   TCCR1B |= (1 << WGM12); // Configure Timer 1 for CTC mode 
   TIMSK |= (1 << OCIE1A); // Enable CTC interrupt 
   OCR1A   = 0x3D08; // Set CTC compare value to match
   TCCR1B |= (1 << CS12) ; // Start timer at Fcpu/256 

   sei(); //  Enable global interrupts 

   while(1) 
   { 
	/*
           Do other stuff and let the interrupt service routine worry
           about toggling the LED.
	*/
   } 

} 

ISR(TIMER1_COMPA_vect) 
{ 
  
   PORTC ^= (1 << PC2); // Toggle the LED 
    
}
 

Thread Starter

mik3

Joined Feb 4, 2008
4,843
Thanks guys for the replies.

I forgot to post my code!

Here it is:

#include <avr/io.h>

int main(void)
{

DDRC=0b11111111;

while(1)
{
PORTC=0b11111111;
}

}
 

nanovate

Joined May 7, 2007
666
Your code looks OK... maybe the WDTON fuse is programmed ( = 0). The default timeout period would give you about 50 to 60Hz. Plus the default start-up time is 65ms on power-up. Both of these can be adjusted via programming the fuse bits (in the PIC world this is the CONFIG word).
 

hashoot

Joined Jun 9, 2009
11
hi , maybe u haven't heard about Codevision it's another powerful compiler for AVR in C language. but i still prefer Bascom Avr,works for me , basic is an easier language as i tested in Picmicros.
 

Thread Starter

mik3

Joined Feb 4, 2008
4,843
hi , maybe u haven't heard about Codevision it's another powerful compiler for AVR in C language. but i still prefer Bascom Avr,works for me , basic is an easier language as i tested in Picmicros.
Thanks,

I will try it.
 

nanovate

Joined May 7, 2007
666
u r right.in your area
but i'm not buying anything. just using crack versions
Why would you steal software? Pavel (the guy who wrote Codevision) is really great and very responsive. $200 is really a bargain considering the level of support -- you are not dealing with a mega-corporation. If you want to use free software then use gcc/winavr if not then don't rob an honest person of his livelihood. Codevision has an evaluation version that is free.
 

hgmjr

Joined Jan 28, 2005
9,027
If you prefer to pay for a C-compiler then Mikro-C looks like a pretty good package from what I can tell by visiting the website.

Maybe someone with first-hand experience with the software can give a better accounting.

hgmjr
 

Thread Starter

mik3

Joined Feb 4, 2008
4,843
I am playing with the demo version and it looks very good. Also, its reference manual is good like the CCS one.
 

Thread Starter

mik3

Joined Feb 4, 2008
4,843
Is it possible to set the PWM frequency of AVRs at any (possible value) you want?

I am asking because with MicroC you can choose between seven PWM frequencies at a particular clock frequency.
 

hgmjr

Joined Jan 28, 2005
9,027
The PWM interrupt is derived from the system clock. The system clock passes through a prescaler that can be used to further divide down the system clock by several internally preset(fixed) binary values. The maximum frequency of the PWM signal is therefore established by the system clock. The minimum frequency is set by the largest divide-by factor provided by the prescalar which I believe is 1024.

hgmjr
 
Top