Code:
Following code will make the Led blink every second (appx.) at 16MHz crystal. If you have bought new controller then It will be set at internal 1MHz osc. (by default). So,if you don’t change the fuse values to enable external crystal, the LED will blink 16 times slower!!
For programming microcontroller, check my post: “DIY AVR Programmer” at www.dharmanitech.com
//***************************************************
//************ BLINKING LED ***********************
//***************************************************
// Target : ATmega32
// Crystal: 16.000Mhz
// Compiler: ICCAVR
// Author: CC Dharmani, Chennai, India
//***************************************************
#include <iom32v.h>
#include <macros.h>
//-----------------------------------------------
void port_init(void)
{
PORTD = 0x00;
DDRD = 0x10;
}
//------------------------------------------------
void delay_ms(int miliSec) //1 ms delay (appx)for 16MHz crystal
{
int i,j;
for(i=0;i<miliSec;i++)
for(j=0;j<1550;j++)
{
asm("nop");
asm("nop");
}
}
//------------------------------------------------
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
//SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//--------------- MAIN FUNCTION -------------
void main()
{
init_devices();
while(1)
{
PORTD |= 0x10;
delay_ms(500);
PORTD &= ~0x10;
delay_ms(500);
}
}
//********************* END ***********************
www.dharmanitech.com
Here is the same code written for AVR-GCC (winAVR) compiler:
//******* LED BLINKING PROGRAM *********
//Controller: ATmega32 (1Mhz or 16Mhz)
//Compiler: AVR-GCC (winAVR)
//Author: CC Dharmani, Chennai (INDIA)
//*************************************
//#define F_CPU 1000000UL //use this if internal oscillator is used
#define F_CPU 16000000UL //use this when ext 16MHz crystal is used
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
void port_init(void)
{
PORTD = 0x00;
DDRD = 0x10;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
cli(); //disable all interrupts
port_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
// sei(); //re-enable interrupts
//all peripherals are now initialized
}
int main()
{
init_devices();
while(1)
{
PORTD = 0x10;
_delay_ms(500);
PORTD = 0;
_delay_ms(500);
}
return(0);
}
//---------- END -------------
Will this code work do get a IR receiver to accept. We are in a Robotics class and we need to try and get the IR from the microcontroller to interact with a IR emitter on a platform. We need to send a signal to the platform to get it to release a switch that will drop items that we need. We are practicing for a Robotics competition for high school.