Hello,

We are trying to control 8 7-segment displays by using multiplexing.
The problem we have is that we have to program buttons, how can we make it?

Best regards,
Floris

/* Ansteuerung von drei 7-Segmentanzeige im Multiplexbetrieb
 * Gemeinsame Anode, PB0..PB6
 * Spalten: PC0..PC2
 *
 *    a
 *  f   b
 *    g
 *  e   c
 *    d
 *
 */

#include <avr/io.h>
#include <util/delay.h>
#define F_CPU
void delay_ms(uint16_t);
void digit (uint8_t, uint8_t);
int main (void);
int knoppen(int pin, int getal);
const int8_t numbers [10] =
{
//    1gfedcba
    0b11000000,     // 0
    0b11111001,     // 1
    0b10100100,     // 2
    0b10110000,     // 3
    0b10011001,     // 4
    0b10010010,     // 5
    0b10000010,     // 6
    0b11111000,     // 7
    0b10000000,     // 8
    0b10010000,     // 9
};


void delay_ms (uint16_t ms)
{
  for(uint16_t t=0; t<=ms; t++)
    _delay_ms(1); 
}


}
void digit (uint8_t wert, uint8_t pin)
{
int i;
i=1;
    PORTA |= (1 << PA0) | (1 << PA1); if (i==8) i=1;        
    PORTB = numbers[wert];                              
    PORTA &= ~(1 << pin);                             
    delay_ms (15);
}

int main()
{
    int i;
    i=1;
    DDRB = 0xFF;                                          
    DDRA |= (1 << DDA0) | (1 << DDA1) | (1 << DDA2) | (1 << DDA3) | (1 << DDA4) ; 
PORTA |= (1 << PA0) | (1 << PA1); if (i==8 ) i=1;

    while (1)
    {
        digit (1, PA0);
        digit (2, PA1);
        digit (3, PA2);
        digit (4, PA3);
        digit (5, PA4);
    }
    return 0;
}

first you should set the internal pull up resistors for the pins you're attaching the buttons to:

PORTB = 0b11100000;

then you can check if button1 is pressed:

#define button1_pressed	!(PINB & 0b10000000)

if (button1_pressed)
{
    //do something
}

for button2 and 3:

#define button2_pressed	!(PINB & 0b01000000)
#define button3_pressed	!(PINB & 0b00100000)

else if (button2_pressed)
{
    //do something
}
else if (button3_pressed)
{
    //do something
}

hmm okay,
But With 1 button we have to +1 the counter and with 1 button we have to -1 the counter.
How can we do this?
best regards,
Floris

you mean a variable?
if so then it will be something like this:

#define button1_pressed	!(PINB & 0b10000000)
#define button2_pressed	!(PINB & 0b01000000)

int counter = 0;
int main()
{
	PORTB = 0b11100000;
	while (1)
	{
		if (button1_pressed)
		{
			counter++;
		}
		else if (button2_pressed)
		{
			counter--;
		}
		// do something with counter..
	}
}

hello,
yes that's what I mean, but how can I read an array in this code?
And how to use then multiplexing to send numbers to different 7 segment displays?
Can you make it in the code?
best regards,
Floris

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.