Exemplos de Código para LCD 4 bits, Sensor LDR e Comunicação Serial EUSART

Eletrônica5,24 KB

Exemplo 1: LCD 4 bits com deslocamento de texto

Este código demonstra como utilizar a biblioteca LCD em modo de 4 bits. O display é inicializado, em seguida é escrito um texto e, por fim, o texto é deslocado.

// Conexões do módulo LCD
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// Fim das conexões do módulo LCD

char txt1[] = "mikroElektronika";
char txt2[] = "EasyPIC6";
char txt3[] = "Lcd4bit";
char txt4[] = "example";
char i; // variável do loop

void Move_Delay() { // função de delay
  Delay_ms(500);
}

void main() {
  ANSEL = 0;  // configura pinos AN como I/O digital
  ANSELH = 0;
  C1ON_bit = 0; // desabilita comparadores
  C2ON_bit = 0;

  Lcd_Init();              // inicializa LCD
  Lcd_Cmd(_LCD_CLEAR);     // limpa display
  Lcd_Cmd(_LCD_CURSOR_OFF); // desliga cursor
  Lcd_Out(1, 6, txt3);     // escreve texto na primeira linha
  Lcd_Out(2, 6, txt4);     // escreve texto na segunda linha
  Delay_ms(2000);

  Lcd_Cmd(_LCD_CLEAR);     // limpa display
  Lcd_Out(1, 1, txt1);     // escreve texto na primeira linha
  Lcd_Out(2, 5, txt2);     // escreve texto na segunda linha
  Delay_ms(2000);

  // Movendo texto
  for (i = 0; i < 4; i++) { // move o texto para a direita 4 vezes
    Lcd_Cmd(_LCD_SHIFT_RIGHT);
    Move_Delay();
  }

  while (1) { // loop infinito
    for (i = 0; i < 8; i++) { // move texto para a esquerda 8 vezes
      Lcd_Cmd(_LCD_SHIFT_LEFT);
      Move_Delay();
    }
    for (i = 0; i < 8; i++) { // move texto para a direita 8 vezes
      Lcd_Cmd(_LCD_SHIFT_RIGHT);
      Move_Delay();
    }
  }
}

Exemplo 2: Leitura de sensor LDR e potenciômetro no LCD

Este programa lê continuamente o valor de resistência de um sensor de luminosidade LDR e a tensão de um potenciômetro, exibindo ambos em um display LCD.

// Conexões do display
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// Fim das conexões

char txt1[] = "Resist. LDR:"; // 12 caracteres
char txt2[] = "Tensao pot:"; // 11 caracteres
unsigned char ch;
unsigned int adc_rd;

// Declaração das variáveis
long tensao; // tensão no potenciômetro
long rldr;   // resistência LDR

void Move_Delay() {
  Delay_ms(200);
}

void main() {
  INTCON = 0;   // interrupções desabilitadas
  ANSEL = 0x06; // pino RA2 como entrada analógica
  TRISA = 0x06;
  ANSELH = 0;   // todos os outros pinos como digital
  C1ON_bit = 0;
  C2ON_bit = 0;
  ADCON1 = 0x82;

  Lcd_Init();               // inicializa LCD
  Lcd_Cmd(_LCD_CLEAR);      // limpa display
  Lcd_Cmd(_LCD_CURSOR_OFF); // cursor desligado
  Lcd_Out(1, 1, txt1);
  Lcd_Out(2, 1, txt2);

  while (1) {
    // TENSÃO DO POTENCIÔMETRO
    adc_rd = ADC_Read(2);          // conversão A/D pino RA2
    tensao = (long)adc_rd * 5000;  // converte para mV
    tensao = tensao / 1023;
    ch = tensao / 1000;            // converte para V
    Lcd_Chr(2, 13, 48 + ch);       // escreve o resultado em formato ASCII
    Lcd_Chr_CP('.');
    // unidades, centenas, dezenas
    ch = (tensao / 100) % 10;
    Lcd_Chr_CP(48 + ch);
    ch = (tensao / 10) % 10;
    Lcd_Chr_CP(48 + ch);
    ch = tensao % 10;
    Lcd_Chr_CP(48 + ch);
    Lcd_Chr_CP('V');
    Delay_ms(1);

    // RESISTÊNCIA LDR
    adc_rd = ADC_Read(1);          // conversão A/D pino RA1
    rldr = (long)adc_rd * 5000;    // converte o resultado
    rldr = rldr / 1023;
    ch = (rldr * 10) / (5 - rldr);
    Lcd_Chr(1, 12, 48 + ch);       // escreve o resultado em formato ASCII
    Lcd_Chr_CP('.');
    ch = (rldr / 100) % 10;
    Lcd_Chr_CP(48 + ch);
    ch = (rldr / 10) % 10;
    Lcd_Chr_CP(48 + ch);
    ch = rldr % 10;
    Lcd_Chr_CP(48 + ch);
    Lcd_Chr_CP('V');
    Delay_ms(1);

    if (rldr > tensao)
      PORTA.RA3 = 1;
    else
      PORTA.RA3 = 0;
  }
}

Exemplo 3: Comunicação serial EUSART com LEDs e indicação de erro

Cada byte recebido via comunicação serial é exibido usando LEDs conectados na porta B e é retornado automaticamente ao transmissor. Se ocorrer um erro na recepção, ele é indicado pela ativação de um LED.

void INICIALIZA_EUSART(void) {
  TXSTA.BRGH = 1;
  BAUDCTL.BRG16 = 1;
  SPBRGH = 0;
  SPBRG = 207;
  TXSTA.SYNC = 0;
  RCSTA.SPEN = 1;
  TXSTA.TXEN = 1;
  RCSTA.CREN = 1;
  PIE1.RCIE = 1;
  INTCON.PEIE = 1;
  INTCON.GIE = 1;
}

void INTERRUPT(void) {
  if (PIR1.RCIF) {
    if ((RCSTA.FERR == 0) && (RCSTA.OERR == 0)) {
      Delay_ms(1000);
      PORTB = RCREG;
      TXREG = PORTB;
      PORTA.F0 = 0;
    }
    else {
      PORTA.F0 = 1;
    }
  }
}

void main(void) {
  TRISB = 0x00;
  TRISA = 0x00;
  ANSEL = 0;
  ANSELH = 0;
  INICIALIZA_EUSART();
  while (1);
}

Baixar

PDF Word HTML