Same program code works differently for ATmega32 and ATmega16/8

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

hello everyone!

here is the same code in 2 different ways. code 1(284 bytes) is working both(atmega 32 and atmega 8) fine.But code 2(294 bytes) is only working on atmega32 only. is there any problem in code? or something else?

 

I have simple led using button click.

AtmelStudio7 defined F_CPU=16MHZ.

 

Atmega32A PU 1336(ALL CODE WORKS FINE)

Atmega16A U-35459R 1804J27

Atmega8A U 1910S93 35469L

all MCUs fuse bits-->L=FF  H=C9. 16mHZ external crystal.

 

code 1

#include <avr/io.h>
#include <util/delay.h>
//#define numberOfButtons 1
//#include"ButtonPress.h"

//char ButtonPressed();

int Pressed=0;
int Pressed_Confidence_Level=0; //Measure button press confidence
int Released_Confidence_Level=0; //Measure button release confidence

 

int main(void)
{
    //Set up the data direction for the LEDs as output and Button as Input.
    DDRC |= (1<<0);//led
    DDRB &= ~(1<<1);//switch
    PORTB |= (1<<1);//internal pullup
    
    //if (ButtonPressed()) PORTC ^= 1<<0;
    
    
    while (1)
    {
        
        
        //ButtonPressed();
        //char ButtonPressed()
        //{
            
            
            if (bit_is_clear(PINB, 1))
            {
                Pressed_Confidence_Level ++; //Increase Pressed confidence
                Released_Confidence_Level = 0; //Reset released button confidence since there is a button press
                if (Pressed_Confidence_Level > 500) //Indicator of good button press
                {
                    if (Pressed == 0)
                    {
                        //do something
                        PORTC ^= 1<<0;
                        Pressed = 1;
                        //return 1;//no return
                    }
                    //Zero it so a new pressed condition can be evaluated
                    Pressed_Confidence_Level = 0;
                }
            }
            else
            {
                Released_Confidence_Level ++; //This works just like the pressed
                Pressed_Confidence_Level = 0; //Reset pressed button confidence since the button is released
                if (Released_Confidence_Level > 500)
                {
                    Pressed = 0;
                    Released_Confidence_Level = 0;
                }
            }
            //return 0;
        //}

        
        
        
    }
    
}

 

code 2

#include <avr/io.h>
#include <util/delay.h>
//#define numberOfButtons 1
//#include"ButtonPress.h"

char ButtonPressed();

int Pressed=0;
int Pressed_Confidence_Level=0; //Measure button press confidence
int Released_Confidence_Level=0; //Measure button release confidence
 
 
 

int main(void)
{
    //Set up the data direction for the LEDs as output and Button as Input.
    DDRC |= (1<<0);//led
    DDRB &= ~(1<<1);//switch
    PORTB |= (1<<1);//internal pullup
    
    //if (ButtonPressed()) PORTC ^= 1<<0;
    
    
    while (1)
    {
        
        
        ButtonPressed();
        
        
    }
    
}
 
char ButtonPressed()
{
    
    
    if (bit_is_clear(PINB, 1))
    {
        Pressed_Confidence_Level ++; //Increase Pressed confidence
        Released_Confidence_Level = 0; //Reset released button confidence since there is a button press
        if (Pressed_Confidence_Level > 500) //Indicator of good button press
        {
            if (Pressed == 0)
            {
                //do something
                PORTC ^= 1<<0;
                Pressed = 1;
                return 1;//no return
            }
            //Zero it so a new pressed condition can be evaluated
            Pressed_Confidence_Level = 0;
        }
    }
    else
    {
        Released_Confidence_Level ++; //This works just like the pressed
        Pressed_Confidence_Level = 0; //Reset pressed button confidence since the button is released
        if (Released_Confidence_Level > 500)
        {
            Pressed = 0;
            Released_Confidence_Level = 0;
        }
    }
    return 0;
}

 

please suggest me.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What have you done to narrow down the problem?

BTW your debounce technique is valid, but the implementation is rather flawed in that the debounce time will most likely be too short. Sample the input every 10ms and count 5 or so.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Define "working"

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

saumyaranjanrout wrote:
DDRC |= (1<<0);//led
    DDRB &= ~(1<<1);//switch
    PORTB |= (1<<1);//internal pullup

 

Use only = here and set all bits as you need.

 

jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

saumyaranjanrout wrote:

Atmega32A PU 1336(ALL CODE WORKS FINE)

Atmega16A U-35459R 1804J27

Atmega8A U 1910S93 35469L

all MCUs fuse bits-->L=FF  H=C9. 16mHZ external crystal.

 

What about the other fuse selections? You haven't configured each chip the same way.

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

"spot the difference", eh?  (Indeed, at some point we'll need to know what "not working" means.)

 

So I installed Windiff at home (not my first choice when I was working, but eh) and ran through Thing1 and Thing2.  It appears that in one case the function was embedded in the mainline code.  Just commenting out the "return" statement seems to change the logic flow to me.  I'd run it through the simulator to verify.  So can you.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.