AVR microcontroller test problem

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;

• Have you defined the F_CPU macro? – Dzarda Oct 26 '15 at 10:34
• I define"#define xtal 16000000".is it true and enough? – musefian Oct 26 '15 at 11:52
• How does delay_ms() implement the delay? Does it rely on a Timer peripheral? Have you setup the Timer peripheral? – kkrambo Oct 26 '15 at 13:05
• I didn't setup the Timer peripheral.timer settings is Predetermined. – musefian Oct 26 '15 at 13:36
• #define xtal will do absolutely nothing at all - you don't use it anywhere. Also #defines are visible only in the file they are defined in and any which include the file. <delay.h> is not going to see anything you #define in your c file. You need to define F_CPU globally. Assuming GCC, you can use the command line switch: -DF_CPU=16000000 – Tom Carpenter Oct 27 '15 at 13:00

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!

• I have done all these things.But The problem is still unresolved. – musefian Oct 26 '15 at 11:50
• Would you run a program like this and see what happen : DDRB=0x01; PORTB=0x02; In while(1) : PORTB.0=PINB.1; and attach a Push Button to B.1 and an led series with 220R to B.0 . See it work or not. – HOPE Oct 26 '15 at 12:13
• Yes,it work very well. – musefian Oct 26 '15 at 12:41
• @musefian Then problem is mainly about clocking and maybe your delay function. Try some internal parts of your MCU such as timer/counters and see if it's output PWM wave on timer output is as you configure it or not. e.g. make a 1Hz output and test it with and LED in easiest way. – HOPE Oct 26 '15 at 13:16