Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] need help with AVR button

Status
Not open for further replies.

anschau

Newbie level 4
Joined
Jun 14, 2011
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,333
I am using Atmega8 for an led project. i wold like to use a button to tell the program to change between different light patterns with the leds. i think what i need to do is something like this

void main (){

DDRD = 0x00;
PORTD = 0xff;

while (1){

/* code for light pattern

if (PIND == 1){
return (1);
}else return (0);
}
}

can some one tell me what im doing wrong in this code or suggest a simpler easy to understand code.
 

I suppose that you know how the three port registers work, you can take a look at

Also check my post here https://www.edaboard.com/threads/166246/#post894717

In your code you enable the pullups so all the inputs will have the value of 1 unless they are grounded by a button.
Using the CHECKBIT define ( #define CHECKBIT(ADDRESS,BIT) (ADDRESS & (1<<BIT)) ) you can read the input using if (!CHECKBIT(PIND,x)) where x in the pin number (0 to 7) to detect when the input is grounder.
So for example if (!CHECKBIT(PIND,0)) will be true if PORTD bit 0 is grounded by the button.

Alex
 

i tried this but it did not work. can you tell me if there is a problem with the program? and can you also use your code in an example script?

#ifndef F_CPU
#define F_CPU 1000000UL
#endif
#include <util/delay.h>
#include <avr/io.h>
#define CHECKBIT(PIND,1) (PIND & (1<<1))
int main(void)
{
DDRB = 0xff;
DDRC = 0xff;
DDRD = 0b00000000;
while (1)
{

while (1) {
PORTC = 0b11101111;
PORTB = 0b00000001;
_delay_ms(200);
if (!CHECKBIT(PIND,1))
{
return (1);
}else return (1);
}
PORTC = 0b11111111;
PORTB = 0b00000000;
_delay_ms(10000);

}


return (0);
}
 

The define you have used is not correct, the define I have provided is #define CHECKBIT(ADDRESS,BIT) (ADDRESS & (1<<BIT)), you only use specific port and bit in your code later.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#ifndef F_CPU
#define F_CPU 1000000UL
#endif
#include <util/delay.h>
#include <avr/io.h>
#define CHECKBIT(ADDRESS,BIT) (ADDRESS & (1<<BIT))
 
int main(void)
{
    DDRB = 0xff;
    DDRC = 0xff;
    DDRD = 0b00000000;
    PORTD =0b00000010;  // use the pullup in the pin with the button (bit1)
    while (1)
    {
 
        while (1) {              // you will never get out of this loop
            PORTC = 0b11101111;
            PORTB = 0b00000001;
            _delay_ms(200);
            if (!CHECKBIT(PIND,1))  // this will be true when pin1 is 0, the same as if (!(PIND & 0x02))
            {
              //  return (1); // don't use return, set a variable or execute some code 
            } else //return (1);
        }
        PORTC = 0b11111111;  // you will never execute this
        PORTB = 0b00000000;
        _delay_ms(10000);
 
    }
 
 
    return (0);
}



You also have to use the internal pullup (or external but there is no reason for that) so that the default state of PORTD pin1 is 1 and you can detect when it becomes 0 (when button is pressed).
Why do you use return inside main?
You shouldn't exit the main function in an embedded system , that is why you use the while loop, if you want a function that returns a result then call another custom function.
The main() function is the lowest level task, getting out of it will have unpredicted random results

Alex
 
Last edited:

okay thank you very much for your help. The example code helped a ton!
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top