Untitled

 avatar
unknown
plain_text
a year ago
4.4 kB
3
Indexable
#include <avr/io.h>
#include <util/delay.h>

#define D7 7
#define D6 6
#define D5 5
#define D4 4
#define RS 0
#define RW 1
#define EN 2
#define BTN PD3
#define BTN2 PD2

void lcdInit();
void lcdCmd(unsigned char cmd);
void lcdDado(unsigned char dado);
void lcdPrint(char *str);
void lcdGoto(int col, int lin);
void lcdClear();
void lcdDireita(int caracteres);
void nomeGrupo();
void exibirNumeroUm();
void exibirNumeroDois();

char nome1[] = "Rhuan G. Candeia";
char matricula1[] = "201921140024";

char nome2[] = "Lucas S. Felix";
char matricula2[] = "20201114007";

int main() {
    DDRD |= (1 << D7) | (1 << D6) | (1 << D5) | (1 << D4) | (1 << BTN);
    DDRB |= (1 << RS) | (1 << RW) | (1 << EN);

    PORTD = PORTB = 0x00;

    lcdInit();

    int estado = 0;  // 0 para o estado normal, 1 para exibir "1", 2 para exibir "2"

    while (1) {     
      
    	// the estado variable should be 0 unless the BTN is pressed and released, then it should increase up until 2.

        if (estado == 0) {
            if (PIND & (1 << BTN)) {
                estado = 1;
                displayLed(estado);
                _delay_ms(100);
            }
        } else if (estado == 1) {
            if (PIND & (1 << BTN)) {
                estado = 2;
                displayLed(estado);
                _delay_ms(100);
            }
        } else if (estado == 2) {
            if (PIND & (1 << BTN)) {
                estado = 0;
                displayLed(estado);
                _delay_ms(100);
            }
        }

        // now make the BTN2 decrease the value of estado, and if it reaches 0, it should go back to 2.

        if (estado == 0) {
            if (PIND & (1 << BTN2)) {
                estado = 2;
                displayLed(estado);
                _delay_ms(100);
            }
        } else if (estado == 1) {
            if (PIND & (1 << BTN2)) {
                estado = 0;
                displayLed(estado);
                _delay_ms(100);
            }
        } else if (estado == 2) {
            if (PIND & (1 << BTN2)) {
                estado = 1;
                displayLed(estado);
                _delay_ms(100);
            }
        }     
    }
  
    return 0;

}

void displayLed(int estado){
  switch (estado) {
            case 0:
                nomeGrupo();
                break;
            case 1:
                exibirNumeroUm();
                break;
            case 2:
                exibirNumeroDois();
                break;
  }
}

void lcdInit() {
    _delay_ms(100);
    lcdCmd(0x33);
    lcdCmd(0x32);
    lcdCmd(0x28);
    lcdCmd(0x0F);
    lcdCmd(0x01);
    lcdCmd(0x06);
}

void lcdCmd(unsigned char cmd) {
    PORTD = cmd & 0xF0;
    PORTB &= ~(1 << RS);
    PORTB &= ~(1 << RW);
    PORTB |= (1 << EN);
    _delay_us(1);
    PORTB &= ~(1 << EN);
    _delay_ms(5);
    PORTD = cmd << 4;
    PORTB |= (1 << EN);
    _delay_us(1);
    PORTB &= ~(1 << EN);
    _delay_ms(5);
}

void lcdDado(unsigned char dado) {
    PORTD = dado & 0xF0;
    PORTB |= (1 << RS);
    PORTB &= ~(1 << RW);
    PORTB |= (1 << EN);
    _delay_us(1);
    PORTB &= ~(1 << EN);
    _delay_us(120);
    PORTD = dado << 4;
    PORTB |= (1 << EN);
    _delay_us(1);
    PORTB &= ~(1 << EN);
    _delay_us(120);
}

void lcdPrint(char *str) {
    for (int i = 0; str[i] != '\0'; i++) {
        lcdDado(str[i]);
        _delay_ms(200);
    }
}

void lcdGoto(int col, int lin) {
    int addr;
    if (lin >= 2) lin = 1;
    if (col >= 16) col = 15;
    if (lin == 0) addr = 0x80;
    else addr = 0xC0;
    lcdCmd(col + addr);
}

void lcdClear() {
    lcdCmd(0x01);
}

void lcdDireita(int caracteres) {
    for (int pos = 0; pos < caracteres; pos++) {
        lcdCmd(0x1C);
        _delay_ms(100);
    }
}

void nomeGrupo() {
    lcdClear();
    lcdGoto(0, 0);
    lcdPrint(nome1);
    lcdGoto(0, 1);
    lcdPrint(matricula1);
    lcdDireita(16);
    _delay_ms(2000);

    lcdClear();
    lcdGoto(0, 0);
    lcdPrint(nome2);
    lcdGoto(0, 1);
    lcdPrint(matricula2);
    _delay_ms(2000);
}

void exibirNumeroUm() {
    lcdClear();
    lcdGoto(0, 0);
    lcdPrint(" 1");
}

void exibirNumeroDois() {
    lcdClear();
    lcdGoto(0, 0);
    lcdPrint(" 2");
}
Editor is loading...
Leave a Comment