0
\$\begingroup\$

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;

\$\endgroup\$
  • 2
    \$\begingroup\$ Have you defined the F_CPU macro? \$\endgroup\$ – Dzarda Oct 26 '15 at 10:34
  • \$\begingroup\$ I define"#define xtal 16000000".is it true and enough? \$\endgroup\$ – musefian Oct 26 '15 at 11:52
  • 1
    \$\begingroup\$ How does delay_ms() implement the delay? Does it rely on a Timer peripheral? Have you setup the Timer peripheral? \$\endgroup\$ – kkrambo Oct 26 '15 at 13:05
  • \$\begingroup\$ I didn't setup the Timer peripheral.timer settings is Predetermined. \$\endgroup\$ – musefian Oct 26 '15 at 13:36
  • 1
    \$\begingroup\$ #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\$ – Tom Carpenter Oct 27 '15 at 13:00
1
\$\begingroup\$

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!

\$\endgroup\$
  • \$\begingroup\$ I have done all these things.But The problem is still unresolved. \$\endgroup\$ – musefian Oct 26 '15 at 11:50
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – HOPE Oct 26 '15 at 12:13
  • \$\begingroup\$ Yes,it work very well. \$\endgroup\$ – musefian Oct 26 '15 at 12:41
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ – HOPE Oct 26 '15 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.