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;
#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
\$\endgroup\$