Compilation problems (mega328p button)

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

Hello everybody! I'm new to programming on avrs. This code, despite being simple, has errors in the compilation. Need help!
Thank you very much.

 

 

 

/* ============================================================

   teste botão...
   
   MCU: Atmega328p
   Autor: ELK ALEXANDRE
   Data:  JULHO DE 2022

============================================================ */

#include <avr/io.h>
#include <stdbool.h>
#define F_CPU16000000UL

// ============================================================
// --- Mapeamento de Hardware ---
#define   bt1   (1<<4)
#define   led   (1<<5)

// ============================================================
// --- Variáveis Globais ---
int  bt1_f = 0x00;

// ============================================================
// --- Programa Principal ---
void setup()
{
    DDRB  &=  ~bt1;     //configura entrada para o botão
    PORTB |=   bt1;     //habilita o pull-up interno
    DDRB  |=   led;     //configura saída para o led

    while(1)
    {
        if(!(PINB&bt1)) bt1_f = 0x01;

        if((PINB&bt1) && bt1_f)
        {
            bt1_f = 0x00;

            PORTB ^= led;
            
        } //end if bt1
        
        
    } //end while
    
} //end setup

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

void setup()

 

use ONLY = in your initialization setup, not &=  or  |=

DDRB =  flfdlfdfkdlfd

PORTB = dkgjhkdgjdjdk

 

Do not use bt1 and bt1_f...is is very easy to get confused and make a mistake  (Harrold, Harold) that you will not spot.

You have 200 billion names to choose from!!!  Use them

 

#define   bt1   (1<<4)
#define   led   (1<<5)

shouldn't these be on the SAME pin? or are your tryting to do something else.

 

You forget delays---your led will blink so fast, you can't see it! (are you not using an actual chip?)

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

Studio expects a main() function, what you have seems to be a bit of Arduino style code.

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

And do NOT use "Error List" to find out what is wrong anyway! It tells you almost nothing and when it does tell you anything it's in completely the wrong order.

 

What matters when a build fails is the "output" and within that the only thing you need to solve (initially) is the reason for the very first error you see listed there (often the fix for the first error also fixes the next 50 errors too!).

 

As John says the real error is almost certainly the lack of main(). Arduino sketches have two entry points called setup() and loop() but real C/C++ programs have only one called main(). In fact in Arduino it has a main() too only you don't see it. It basically says:

int main(void) {
    setup_arduino_system();
    
    setup(); // this calls the setup() you write in your sketch
    while (1) {
        loop(); // this repeatedly calls your loop() code
    }
}

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

Hi! First, apologies for the delay in getting back to you.
I understand your concern about flag names etc but this is a very simple program.
Yes, I'm using the chip.

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

You solved my problem! It was really in main void(). Thanks a lot for the help!

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

Your observation of the "errors" is of paramount importance.
Thank you very much!

 

Ps. Sorry for the delay in getting back...

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

Hello,   You haven't told us if you are using standard C++ or Arduino.  With Arduino, you need to split the main() into two parts: setup() and loop().

 

Here is the program for Arduino:

#define   bt1   (1<<4)
#define   led   (1<<5)

int  bt1_f = 0x00;

void setup() {
    DDRB  &=  ~bt1;     //configura entrada para o botπo
    PORTB |=   bt1;     //habilita o pull-up interno
    DDRB  |=   led;     //configura saφda para o led
}

void loop() {
   if (!(PINB & bt1)) bt1_f = 0x01;
   if ( (PINB & bt1) && bt1_f) {
       bt1_f = 0x00;
       PORTB ^= led;
   } //end if bt1
   
} 

The LED will still not switch on and off smoothly because of switch bouncing. With Arduino, I suggest this loop:

    if (button press detected)  record millis() value,  add 15 to millis() value to get toggle_time,  retest millis() until it reaches toggle-time.  Then test if button is still pressed.  If yes, then toogle LED. 

   Next delay until button is released, and test using millis() for 15 milliseconds in order to confirm that button is OFF.

  

 

 

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

Hi! Works fine with 20ms. Thank you for the tip.
Hug!