Version 7.0a

Cmega is c compiler IDE for the AVR mega family.

you can write c code with it and compile it to generate assembler code.

to begin working with Cmega you need to:

1) install Cmega.

2) install avrstudio4.0 will be used to generate machine code (also as debugger).

3) you can connect LCD 16X2 . (optional)

4) you can add FPU chip for math operations. (optional).

FPU is optional ,if you don't add it you still can add math operation but not floating point math.

with micro-ide you will be able to, read Flash & EEProm memory's ,program avr fuses ,and load you code to atmega flash.

in our examples we will use boards like (AVR-MT-128 from olimex) and avr stamp .

we will show examples from compiling simple code to compiling boot loader code.(AVR109)

our first example is to print "Hello" string.

a LCD 16X2 HD44780 type is used ,connected to port E.(see below)


to print string we will add include file.

#include <LCD4_128.h> // contains LCD functions
void main()
{  
   LCD_STRING("HELLO"); // print "HELLO"
}

to compile it use F7 and to build use F8.

Now you can load it to the atmgea connected threw LPT by pressing F9.

Its connected threw ISP ISP diagram.

When ISP is connected Cmega automatically will search all ports LPT1..3

to fined the used port.


Adding delay

Sleep(10); // 10 micro seconds

Sleep(10 mic); // 10 micro seconds

Sleep(10 mil); // 10 Milli seconds

Sleep(10 sec); // 10 seconds


Accessing Ports and registers

using upper case

DDRA = 0xFF; // set port A to output

PORTA = 0x52; // out port A 0x52

PORTA = 0b00111010 ; // out port A 0x3A in binary form

PORTA = 55 ; // out port A 0x37


PORTA.3 = 1; // set pin 3 at port A to 1 .(other pins on port A will not change)

PORTA = (1<<3); // set pin 3 at port A to 1 and others to 0.

---------------------------------------------------------------------------------------

void main()
{

  char x;
  DDRB = 0x00; // set port B to input.
  x = PORTB; // read port B to x
}

----------------------------------------------------------------------------------------

ADCSRA.ADEN = 1; // set ADEN in register ADCSRA (enable ADC)

PORTF.3 = 1; // sets pin 3 on port F

comments

/*

add comments

multi line

*/

// comment one line


data types

char -127 -> 127

int -32767 -> 32767

long -2147483647 -> 2147483647


in line assembler

#asm

   ldi r16,0x52
   mov r0,r16

#endasm


loops

char x;

for(x=0;x<60 ;x++) // for loop
{
   // do somthing
}


while(x)
{
  // do somthing
}


do
{
  // do somthing
}while(x<6)



Conditional statement

char z;

if(z == 5)
{
  // do somthing
}

else
{
  // do somthing
}


transfer control

void main()
{

  char x;
  x=4;
line3:
  x++;
 if(!(x>7))
 {
   // do something

   goto line3;

 }

}



switch, case, default



void main()
{
   char x;
   x=1;

   switch(++x)
   {
      case 1:
      {
       // do somthing

      }
      break;

      case 4:
     {
       // do somthing

     }

     break;
    case 15:
    {

       // do somthing
    }

     break;
    default:
    x=5;

     }
}

continue

void main()
{
  char t,x;

  for(x=0;x<100;x++)
  {

    if(x>5) continue;
    t = x; // will be bypassed if x>5

   }

}


functions

char func(int x)
{
   return ++x;
}

void main()
{
   int w;
   w = func(7);
}



to call function in assembler:

#asm

   ldi r16,byte1(5)
   push r16 //push parameters
   ldi r16,byte2(5)
   push r16
   call _ func // call function add _ to function name
   pop r16 pop r16 //free pushed parameters

#endasm

function return will be in R20..

printing

1)

#include<LCD4_128.h>

void main()
{
  long out;
  char x;
  x=5;
  Lcd_init();
  LCD_PRINT(out = 66);
}

//------------------------------

2)

#include<LCD4_128.h>
#include<FPU_128.h>
void main()
{
  float x;
  int k;
  k =6;
  x=32.32;  

  LCD.PRINT(x); // float number print
  LCD.PRINT(k); // integer number print
  LCD.PRINT(4); // integer constant print
  LCD.PRINT("string print"); // string print

}


working with FPU


#include<LCD4_128.h>
#include<FPU_128.h>

void main()
{  

  FPU.VERSION(); // send print version to FPU
  PRINT_BUFFER(); // print buffer

}



#include<LCD4_128.h>
#include<FPU_128.h>

void main()
{
  float f; 

  FPU.FWRITEA(1,3.3); // load 3.3 to register 1
  FPU.FWRITEB(2,6.3); // load 6.3 to register 2
  FPU.FADD(2); // add A + B (b is register no - 2)
  f = FPU.FREAD(1); // read A to f

  LCD.PRINT(f);

}



#include<LCD4_128.h>
#include<FPU_128.h>

void main()
{
  float f;  

  FPU.FWRITEA(1,4.0); // write 4.0 to register 1 and set A to 1
  FPU.EXP10(); // A = 10 ** A

  f = FPU.FREAD(1);

  LCD.PRINT(f); // will print 10000.0

}


#include<LCD4_128.h>
#include<FPU_128.h>

void main()
{
  float f; 

  FPU.FWRITEA(1,9.05);
  FPU.FWRITEB(2,8.3);

  if(FPU.FCOMPARE() == 0x80) // compare A and B if A > B status returns 0x80

  {

    LCD.PRINT("A > B ");

  }

}