i have to do this thing
Simulate a matrix keyboard that, in addition to the numbers 0 to 9, contains Add, Subtraction, Clear and Result keys.
The output consists of 2 7-segment displays, shown in a multiplexed manner at the output.
When typing the 1st digit (ex. 5), it must be shown on the display (05). When typing the 2nd digit (ex. 3), the 1st must be shifted to the left to form a 2-digit number (53).
By clicking on addition or subtraction, the number entered is saved and a 2nd number is read, it must be read and then the result is displayed.
This is the circuit that I built
and this is the code
The problem is when I test the code with the circuit nothin happens,I don’t know if is the circuit or code
> #include <avr/io.h>
> #include <util/delay.h>
> Definições dos pinos dos displays de 7 segmentos
> #define DISPLAY_PORT PORTB
> #define DISPLAY_DDR DDRB
> #define DIGIT1_PIN PB0
> #define DIGIT2_PIN PB1
>
> Tabela de mapeamento dos segmentos dos números
> const uint8_t numToSegment[10] = {
> 0b00111111, // 0
> 0b00000110, // 1
> 0b01011011, // 2
> 0b01001111, // 3
> 0b01100110, // 4
> 0b01101101, // 5
> 0b01111101, // 6
> 0b00000111, // 7
> 0b01111111, // 8
> 0b01101111 // 9
> };
>
> #define KEYPAD_PORT PIND
> #define KEYPAD_DDR DDRD
> #define KEYPAD_PIN PIND
>
> Definições dos pinos do teclado matricial
> const uint8_t rowPins[4] = {PD4, PD5, PD6, PD7};
> const uint8_t colPins[4] = {PD0, PD1, PD2, PD3};
>
> Mapeamento das teclas do teclado matricial
> const char keys[4][4] = {
> {'1', '2', '3', '+'},
> {'4', '5', '6', '-'},
> {'7', '8', '9', 'C'},
> {'0', '=', ' ', ' '}
> };
>
> int digit1 = 0;
> int digit2 = 0;
> char operation = '';
> uint8_t isSecondDigit = 0;
>
> void init();
> void displayNumber(int num1, int num2);
> char readKeypad();
> int calculateResult();
>
> int main(void) {
> init();
> while (1) {
> char key = readKeypad();
> if (key) {
> if (key >= '0' && key <= '9') {
> if (isSecondDigit) {
> digit2 = key - '0';
> displayNumber(digit1, digit2);
> isSecondDigit = 0;
> else {
> digit1 = key - '0';
> isSecondDigit = 1;
> displayNumber(digit1, digit2);
> }
> else if (key == '+') {
> operation = '+';
> isSecondDigit = 0;
> else if (key == '-') {
> operation = '-';
> isSecondDigit = 0;
> else if (key == '=') {
> int result = calculateResult();
> displayNumber(result / 10, result % 10);
> digit1 = 0;
> digit2 = 0;
> operation = '';
> else if (key == 'C') {
> digit1 = 0;
> digit2 = 0;
> operation = '';
> isSecondDigit = 0;
> displayNumber(0, 0);
> }
> }
> }
> }
>
> void init() {
> Configura os pinos dos displays
> DISPLAY_DDR = 0xFF; // Configura PORTB como saída para os displays
> KEYPAD_DDR = 0xF0; // Configura as colunas como saída e as linhas como entrada
> KEYPAD_PORT = 0xFF; // Ativa pull-ups nas linhas do teclado
>
> Inicializa os displays com 00
> displayNumber(0, 0);
> }
>
> void displayNumber(int num1, int num2) {
> Exibe o primeiro dígito
> DISPLAY_PORT = numToSegment[num1];
> PORTB |= (1 << DIGIT1_PIN);
> _delay_ms(5);
> PORTB &= ~(1 << DIGIT1_PIN);
>
> Exibe o segundo dígito
> DISPLAY_PORT = numToSegment[num2];
> PORTB |= (1 << DIGIT2_PIN);
> _delay_ms(5);
> PORTB &= ~(1 << DIGIT2_PIN);
> }
>
> char readKeypad() {
> for (uint8_t col = 0; col < 4; col++) {
> Configura a coluna atual como saída
> KEYPAD_DDR |= (1 << colPins[col]);
> KEYPAD_PORT &= ~(1 << colPins[col]); // Define o nível baixo na coluna atual
>
> Verifica as linhas
> for (uint8_t row = 0; row < 4; row++) {
> if (!(KEYPAD_PIN & (1 << rowPins[row]))) {
> Tecla pressionada, retorna o caractere correspondente
> return keys[row][col];
> }
> }
>
> KEYPAD_DDR &= ~(1 << colPins[col]); // Configura a coluna como entrada
> KEYPAD_PORT |= (1 << colPins[col]); // Define o nível alto na coluna
> }
> return ''; // Nenhuma tecla pressionada
> }
>
> int calculateResult() {
> switch (operation) {
> case '+': return digit1 + digit2;
> case '-': return digit1 - digit2;
> default: return 0;
> }
> }
>
>
Mamute Assasino is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.