AVR microcontroller test problem

avrcmicrocontroller

Recently my microcontroller does not run any program, I am testing the code below:

#include <mega32.h>
#include <delay.h>
#define xtal 16000000
void main(void)
{
    DDRA=0xFF;
    DDRB=0xFF;
    DDRC=0xFF;
    DDRD=0xFF;

    while (1)       
    {
        PORTA=0x00;
        PORTB=0x00;
        PORTC=0x00;
        PORTD=0x00;
        delay_ms(500);
        PORTA=0xFF;
        PORTB=0xFF;
        PORTC=0xFF;
        PORTD=0xFF;
        delay_ms(500);
    }
}

I have tested four different controller, both mega32a and mega16a. The code is compiled by CodeVisionAVR Evaluation and programmed by Atmel Studio 6.1(I have tried PROGISP too). The programmer is an original MK2 and ISP programming.

Fusebits:

  • low: 0xEF
  • High: 0xC9

Internal and external clock crystal tested. LED with 220 \$ \Omega \$ resistor connected to a pin.

Another test code:

When I program the code below:

#include <mega32.h>
#include <delay.h>
void main(void)
{
    DDRA=0xFF;
    DDRB=0xFF;
    while (1)       
    {
        PORTA=0x00;
        PORTB=0x00;
        delay_ms(500);
        PORTA=0xFF;
        PORTB=0xFF;
        delay_ms(500);
    }
}  

All LEDs are off, and when I program the code below:

#include <mega32.h>
#include <delay.h>
void main(void)
{
    DDRA=0xFF;
    DDRB=0xFF;
    while (1)       
    {
        PORTA=0xFF;
        PORTB=0xFF;
        delay_ms(500);
        PORTA=0x00;
        PORTB=0x00;
        delay_ms(500);
    }
}

All LEDs are on. What's the problem?
Another test:

#include <mega32.h>
#include <delay.h>
#define xtal 16000000
void main(void)
{
    DDRA=0xFF;
    DDRB=0x01;
    while (1)       
    {
       PORTA=0x00;
       PORTB=0x02;
       delay_ms(500);
       PORTA=0xFF;
       PORTB=0x00;
       delay_ms(500);
    }
}

Second pin of port B is on low light and PORTB=0x00; after delay_ms(500); No matter if it is:0x00; or 0xFF;

Best Answer

From What you declared in last statement about two codes one with all LED on and other all off, first assume these conditions are true :

  • Programming is done without any error and verification of data with programmer is valid.
  • You have tried different MCU with this problem.
  • Your MCUs are not overheated and consume little current. (I suggest measure current consumption of MCU. It should not be zero or a hundred of mili-ampere.)

Then Problem is about your clocking.Because difference of your two last codes are mainly about before "delay_ms(500)" statement. Then I think problem relate to clocking of your MCU. First of all check frequency of your program by :

Project->Configure->C-Compiler->Clock frequency.

and make sure it's 8 in mega-hertz unit.

Now change your MCU fuse-bits to :

LOW : 0xE4

High: 0xD9

Now we expect your code running well!