viernes, 7 de junio de 2013

EJERCICIO 5

Ejercicio 5.- Un programa que permita a través de un teclado 4x4 conectado al microcontrolador, realizar las 4 operaciones aritméticas básicas mostrando el resultado por medio de un LCD, tomando como referencia las siguientes leyendas:




SOLUCIÓN.


  • CÓDIGO DESARROLLADO EN MIKROC.

char  keypadPort at PORTD;
sbit LCD_RS at RB0_bit; sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN at RB1_bit; sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4 at RB2_bit; sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5 at RB3_bit; sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6 at RB4_bit; sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7 at RB5_bit; sbit LCD_D7_Direction at TRISB5_bit;

char teclas[] = {'7','8','9','/','4','5','6','*','1','2','3','-','.','0','=','+'};
char numero[9];
float lvalue, rvalue;
char op, flags, resultado[16];

#define CALCULO_OK flags.F0

#define NEGATIVO flags.F1
#define lcd_clear() lcd_cmd(_LCD_CLEAR);
#define LimparNumero for(i=0;i<8;i++) *(numero + i) = 0;


static char keyRead(char keys[])
{
 char key;

 key = keypad_key_press();
 if(key != 0) return keys[key-1];
 return 0;
}

void main()
{
char k, pos, op, i;

   keypad_init();
   Lcd_Init();
   Lcd_Cmd(_LCD_CLEAR);
   Lcd_Cmd(_LCD_CURSOR_OFF);

   while(1)
   {
       k = keyRead(teclas);

       if(CALCULO_OK)
       {
        pos = 0;
        CALCULO_OK = 0;
        lcd_clear();
        lcd_cmd(_LCD_RETURN_HOME);
        for(i=0;i<8;i++){
         *(numero + i) = 0;
          *(resultado + i) = 0;
          *(resultado +i + 8) = 0;
        }
       }

       if((k>='0' && k<='9') || k=='.')
       {
          *(numero + pos++) = k;
          if(pos >= 8) pos = 0;
          lcd_Out_CP((numero+(pos-1)));
       }
       else if(k == '+' || k=='-' || k=='/' || k=='*')
       {
           op = k;
           pos = 0;
           lcd_chr_CP(op);
           lvalue = atof(numero);
           LimparNumero;
       }
       else if(k == '=')
       {
           rvalue = atof(numero);
           NEGATIVO = 0;
           lcd_chr_CP('=');
           switch(op)
           {
            case '+':
             lvalue += rvalue;
             break;
            case '-':
            if(lvalue >= rvalue)
            {
               lvalue -= rvalue;
            }
            else
            {
               lvalue = rvalue - lvalue;
               NEGATIVO = 1;
            }
            break;
            case '/':
            if(rvalue!=0)lvalue /= rvalue;
            else lvalue = 0;
            break;
            case '*':
            lvalue *= rvalue;
            break;
           }


           floattostr(lvalue, (resultado  + NEGATIVO));
           if(NEGATIVO) *(resultado) = '-';
           lcd_out(1,1,"El resultado");
           Lcd_Cmd(_LCD_SECOND_ROW);
           delay_ms(1000);
           Lcd_Cmd(_LCD_CLEAR);
           lcd_out(1,1,"de la:");


               switch(op)
               {
                case '+':
                  lcd_out(1,7,"suma");
                  break;
                case '-':
                  lcd_out(1,7,"resta");
                  break;
                case '*':
                  delay_ms(1000);
                  lcd_Cmd(_LCD_CLEAR);
                  lcd_out(1,2,"multiplicacion");
                  break;
                case '/':
                  lcd_out(1,7,"division");
                  break;
                }

           delay_ms(1000);
           Lcd_Cmd(_LCD_CLEAR);
           lcd_out(2,1,"es:");
           Lcd_Cmd(_LCD_SECOND_ROW);
           lcd_out(2, 5,resultado);
           delay_ms(2000);
           CALCULO_OK = 1;
           }
   delay_ms(200);
}

  • DIAGRAMA DE FLUJO DE LA SOLUCIÓN.



  • IMAGEN DEL CIRCUITO DESARROLLADO EN PROTEUS.

FIG.1- Diagrama del circuito desarrollado en PROTEUS.


  • VÍDEO EXPLICATIVO.

No hay comentarios.:

Publicar un comentario