Ir ao conteúdo
  • Cadastre-se

PIC Calculadora PIC18F4550 Usando Mplab x ide


Posts recomendados

Estou tentando fazer uma calculadora usando o pic através de uma atividade pedida no meu curso e só consegui fazer ate agr com q ela mostre resultados q sejam menores q 9 e q faça contas simples de somar,multiplicar,dividir e subtrair, ah alguma forma de fazer com q apareça um resultado maior e conseguir fazer cálculos com mais de 1 sinal?

segue abaixo o que consegui ate agr:


#include <xc.h>
#include <stdio.h>
#include <string.h>
#pragma config WDT=OFF
#pragma config PBADEN=OFF
#pragma config LVP=OFF
#define _XTAL_FREQ 4000000

void LCD_Init(void);
void LCD_Dado(char _Dado);
void LCD_Comando(char _Comando);
void LCD_SETXY(char x, char y);
void main(void)
{   int a,c,d,e,f,g,h,i,z,j,cd=0,v,n;
char m[10]={'0','1','2','3','4','5','6','7','8','9'};

    TRISD=0b11110000;
    TRISCbits.RC6 =1;
    TRISCbits.RC7 =1;
        LCD_Init();
    while(1){
   PORTD=0b00000001;
        __delay_ms (55);
        if(PORTDbits.RD4==1){ LCD_Dado('7');if(cd==0){a=7;}if(cd==1){c=7;}if(cd==2){d=7;}if(cd==3){e=7;}if(cd==4){f=7;}if(cd==5){g=7;}if(cd==6){h=7;} if(cd==8){j=7;}}      
         if(PORTDbits.RD5==1){LCD_Dado('8');if(cd==0){a=8;}if(cd==1){c=8;}if(cd==2){d=8;}if(cd==3){e=8;}if(cd==4){f=8;}if(cd==5){g=8;}if(cd==6){h=8;} if(cd==8){j=8;}}       
         if(PORTDbits.RD6==1){ LCD_Dado('9');if(cd==0){a=7;}if(cd==1){c=9;}if(cd==2){d=9;}if(cd==3){e=9;}if(cd==4){f=9;}if(cd==5){g=9;}if(cd==6){h=9;} if(cd==8){j=9;}}        
         if(PORTDbits.RD7==1){
            LCD_Dado('/');
            v=42;
            cd++;
            
        }
        
    PORTD=0b00000010;
        __delay_ms (55);
        if(PORTDbits.RD4==1){LCD_Dado('4');if(cd==0){a=4;}if(cd==1){c=4;}if(cd==2){d=4;}if(cd==3){e=4;}if(cd==4){f=4;}if(cd==5){g=4;}if(cd==6){h=4;} if(cd==8){j=4;}} 
         if(PORTDbits.RD5==1){LCD_Dado('5');if(cd==0){a=5;}if(cd==1){c=5;}if(cd==2){d=5;}if(cd==3){e=5;}if(cd==4){f=5;}if(cd==5){g=5;}if(cd==6){h=5;} if(cd==8){j=5;}} 
         if(PORTDbits.RD6==1){LCD_Dado('6');if(cd==0){a=6;}if(cd==1){c=6;}if(cd==2){d=6;}if(cd==3){e=6;}if(cd==4){f=6;}if(cd==5){g=6;}if(cd==6){h=6;} if(cd==8){j=6;}}
         if(PORTDbits.RD7==1){
            LCD_Dado('x');
            v=11;
            cd++;
        }
      
    PORTD=0b00000100;
        __delay_ms (55); 
        if(PORTDbits.RD4==1){LCD_Dado('1');if(cd==0){a=1;}if(cd==1){c=1;}if(cd==2){d=1;}if(cd==3){e=1;}if(cd==4){f=1;}if(cd==5){g=1;}if(cd==6){h=1;} if(cd==8){j=1;}}
         if(PORTDbits.RD5==1){ LCD_Dado('2');if(cd==0){a=2;}if(cd==1){c=2;}if(cd==2){d=2;}if(cd==3){e=2;}if(cd==4){f=2;}if(cd==5){g=2;}if(cd==6){h=2;} if(cd==8){j=2;}}
         if(PORTDbits.RD6==1){LCD_Dado('3');if(cd==0){a=3;}if(cd==1){c=3;}if(cd==2){d=3;}if(cd==3){e=3;}if(cd==4){f=3;}if(cd==5){g=3;}if(cd==6){h=3;} if(cd==8){j=3;}}
         if(PORTDbits.RD7==1){
            LCD_Dado('-');
            v=22;
            cd++;
        }
      
      
    PORTD=0b00001000;
        __delay_ms(55);
        if(PORTDbits.RD4==1){
           LCD_Init();
           return;
        }
        if(PORTDbits.RD5==1){ LCD_Dado('0');if(cd==0){a=0;}if(cd==1){c=0;}if(cd==2){d=0;}if(cd==3){e=0;}if(cd==4){f=0;}if(cd==5){g=0;}if(cd==6){h=0;} if(cd==8){j=0;}}
         if(PORTDbits.RD6==1){
            LCD_Dado('=');
            if(v==12){ n=a+c+d+e+f+g+h+i+j+z;}
            if(v==22){ n=a-z-j-d-e-f-g-h-i-c;}
            if(v==11){n=a*c; }
            if(v==42){ n=a/c; }
            
            LCD_SETXY(0,2);

            sprintf(m,"%d",n);
           
           LCD_Dado(m[n]); }
           if(PORTDbits.RD7==1){
            LCD_Dado('+');
            v=12;
            cd++; }
       }
      return;
}
void LCD_Init(void)
{
    TRISB = 0b00000000;
    TRISCbits.RC0 = 0; //RS 
    TRISCbits.RC1 = 0; //RW 
    TRISCbits.RC2 = 0; //EN 
    
    PORTCbits.RC0 = 0; //RS
    PORTCbits.RC1 = 0; //RW
    PORTCbits.RC2 = 0; //EN
    __delay_ms(15); //delay 15ms
    LCD_Comando(0x30);
    __delay_ms(5); //delay 5ms
    LCD_Comando (0x30);
    __delay_ms(10);//delay 100us
    LCD_Comando (0x30);
    __delay_ms(5);//delay 5 ms
    
    LCD_Comando(0x3c);//N=1(2 linhas) F=1(fonte 5x7)
    __delay_ms(5);
    LCD_Comando(0x08);//desliga o display
    __delay_ms(5);
    LCD_Comando(0x01);//apaga a DRAM
    __delay_ms(5);
    LCD_Comando(0x06);//Entry mode set
    __delay_ms(5);
    
    LCD_Comando(0x0F);//liga o display e mantem o cursor piscando
    __delay_ms(5);
    }
void LCD_Dado(char _Dado)
{
    PORTCbits.RC0 = 1;//RS = 1 envio de dados
    PORTCbits.RC1 = 0;//RW = 0 escreva dados
    PORTB = _Dado;
    PORTCbits.RC2 = 1;//EN
    __delay_ms(10);
    PORTCbits.RC2 = 0;//EN
}
void LCD_Comando(char _Comando)
{
    PORTCbits.RC0 = 0;//RS = 0 envio de instruções
    PORTCbits.RC1 = 0;//RW = 0 Escrita
    PORTB= _Comando;
    PORTCbits.RC2 = 1;//EN
    __delay_ms(10);
    PORTCbits.RC2 = 0;//EN
}
void LCD_SETXY(char x,char y)
{
    if(y == 1)//primeira linha do lcd
        LCD_Comando(0x80 | x);
    else if(y ==2)//segunda linha do lcd
        LCD_Comando(0x80 | (x + 0x40));
            
}


 

Link para o comentário
Compartilhar em outros sites

  • Membro VIP

Seu código pode ser otimizado. Eu acho que primeiro você deve melhorar a leitura do teclado. Use um matricial

Capturar_thumb4.png?imgmax=800

http://pk2lab.blogspot.com/2012/04/teclado-matricial.html

 

Escreva no display, faça uma continha, defina o modo de interface/interação , e etc, ou seja, vá por partes

Depois de algum progresso, retorne, mostre, o esquema, e etc.

O código daquele blog pode ser resumido pra 3 ou 4 linhas. Se nos der vontade, mostrá-lo-emos...

 

Link para o comentário
Compartilhar em outros sites

@Isadora Ferraz não entendi muito bem como funciona o código daquele blog,eu inciei no pic ah 2 semanas, então tem muita coisa que ainda não sei como botar pra funcionar, eu acho que consegui botar o matricial, não tinha nenhum parecido com o da foto no meu isis mas tinha alguns matriciais, ontem apos algumas tentativas consegui fazer algumas mudanças no código para mostrar resultados maiores mas fica muito bagunçado cheio de if e LCD_Dado, tem alguma dica de vídeo q eu possa ver pra me situar melhor? ontem passei a tarde procurando melhorar o código tentei ver alguns vídeo na net, mas grande maioria é em espanhol usando um compilador diferente .

outra duvida pra escrever no display qual comando eu poderia usar? ate agora só consegui usando o comando LCD_Dado q só consegue escrever uma coisa de cada vez, tinha visto algo sobre o sprintf, com ele consegui escrever mais coisas mas o numero do resulta n aparecia corretamente .

#include <xc.h>
#include <stdio.h>
#include <string.h>
#pragma config WDT=OFF
#pragma config PBADEN=OFF
#pragma config LVP=OFF
#define _XTAL_FREQ 4000000

void LCD_Init(void);
void LCD_Dado(char _Dado);
void LCD_Comando(char _Comando);
void LCD_SETXY(char x, char y);
void main(void)
{   int a,c,d,e,f,g,h,i,z,j,cd=0,v,n;
char m[10]={'0','1','2','3','4','5','6','7','8','9'};

    TRISD=0b11110000;
    TRISCbits.RC6 =1;
    TRISCbits.RC7 =1;
        LCD_Init();
    while(1){
   PORTD=0b00000001;
        __delay_ms (55);
        if(PORTDbits.RD4==1){ LCD_Dado('7');if(cd==0){a=7;}if(cd==1){c=7;}if(cd==2){d=7;}if(cd==3){e=7;}if(cd==4){f=7;}if(cd==5){g=7;}if(cd==6){h=7;} if(cd==8){j=7;}}      
         if(PORTDbits.RD5==1){LCD_Dado('8');if(cd==0){a=8;}if(cd==1){c=8;}if(cd==2){d=8;}if(cd==3){e=8;}if(cd==4){f=8;}if(cd==5){g=8;}if(cd==6){h=8;} if(cd==8){j=8;}}       
         if(PORTDbits.RD6==1){ LCD_Dado('9');if(cd==0){a=9;}if(cd==1){c=9;}if(cd==2){d=9;}if(cd==3){e=9;}if(cd==4){f=9;}if(cd==5){g=9;}if(cd==6){h=9;} if(cd==8){j=9;}}        
         if(PORTDbits.RD7==1){
            LCD_Dado('/');
            v=42;
            cd++;
            
        }
        
    PORTD=0b00000010;
        __delay_ms (55);
        if(PORTDbits.RD4==1){LCD_Dado('4');if(cd==0){a=4;}if(cd==1){c=4;}if(cd==2){d=4;}if(cd==3){e=4;}if(cd==4){f=4;}if(cd==5){g=4;}if(cd==6){h=4;} if(cd==8){j=4;}} 
         if(PORTDbits.RD5==1){LCD_Dado('5');if(cd==0){a=5;}if(cd==1){c=5;}if(cd==2){d=5;}if(cd==3){e=5;}if(cd==4){f=5;}if(cd==5){g=5;}if(cd==6){h=5;} if(cd==8){j=5;}} 
         if(PORTDbits.RD6==1){LCD_Dado('6');if(cd==0){a=6;}if(cd==1){c=6;}if(cd==2){d=6;}if(cd==3){e=6;}if(cd==4){f=6;}if(cd==5){g=6;}if(cd==6){h=6;} if(cd==8){j=6;}}
         if(PORTDbits.RD7==1){
            LCD_Dado('x');
            v=11;
            cd++;
        }
      
    PORTD=0b00000100;
        __delay_ms (55); 
        if(PORTDbits.RD4==1){LCD_Dado('1');if(cd==0){a=1;}if(cd==1){c=1;}if(cd==2){d=1;}if(cd==3){e=1;}if(cd==4){f=1;}if(cd==5){g=1;}if(cd==6){h=1;} if(cd==8){j=1;}}
         if(PORTDbits.RD5==1){ LCD_Dado('2');if(cd==0){a=2;}if(cd==1){c=2;}if(cd==2){d=2;}if(cd==3){e=2;}if(cd==4){f=2;}if(cd==5){g=2;}if(cd==6){h=2;} if(cd==8){j=2;}}
         if(PORTDbits.RD6==1){LCD_Dado('3');if(cd==0){a=3;}if(cd==1){c=3;}if(cd==2){d=3;}if(cd==3){e=3;}if(cd==4){f=3;}if(cd==5){g=3;}if(cd==6){h=3;} if(cd==8){j=3;}}
         if(PORTDbits.RD7==1){
            LCD_Dado('-');
            v=22;
            cd++;
        }
      
      
    PORTD=0b00001000;
        __delay_ms(55);
        if(PORTDbits.RD4==1){
           LCD_Init();
           return;
        }
        if(PORTDbits.RD5==1){ LCD_Dado('0');if(cd==0){a=0;}if(cd==1){c=0;}if(cd==2){d=0;}if(cd==3){e=0;}if(cd==4){f=0;}if(cd==5){g=0;}if(cd==6){h=0;} if(cd==8){j=0;}}
         if(PORTDbits.RD6==1){
            LCD_Dado('=');
            if(v==12){ n=a+c+d+e+f+g+h+i+j+z;}
            if(v==22){ n=a-z-j-d-e-f-g-h-i-c;}
            if(v==11){n=a*c*d; }
            if(v==42){ n=a/c; }
            LCD_SETXY(0,2);
 if(n<9){LCD_Dado(m[n]);}if(n>9 && n<=19){ z=1;}if(n>19 && n<=29){ z=2;}if(n>29 && n<=39){ z=3;}if(n>39 && n<=49){ z=4;}if(n>49 && n<=59){ z=5;}if(n>59 && n<=69){ z=6;}
            if(n>69 && n<=79){ z=7;}if(n>79 && n<=89){ z=8;}if(n>89 && n<=99){ z=9;}if(n>99 && n<=109){ z=10;}if(n>109 && n<=119){ z=11;}if(n>119 && n<=129){ z=12;}
 if(z==1){ LCD_Dado('1');n=n-10; LCD_Dado(m[n]); }if(z==2){LCD_Dado('2');n=n-20;  LCD_Dado(m[n]);} if(z==3){LCD_Dado('3');n=n-30;  LCD_Dado(m[n]);}if(z==4){LCD_Dado('4');n=n-40;  LCD_Dado(m[n]);}if(z==5){LCD_Dado('5');n=n-50;  LCD_Dado(m[n]);}if(z==6){LCD_Dado('6');n=n-60;  LCD_Dado(m[n]);}
            if(z==7){ LCD_Dado('7');n=n-70; LCD_Dado(m[n]); }if(z==8){LCD_Dado('8');n=n-80;  LCD_Dado(m[n]);} if(z==9){LCD_Dado('9');n=n-90;  LCD_Dado(m[n]);}if(z==10){LCD_Dado('1');LCD_Dado('0');n=n-100;  LCD_Dado(m[n]);}if(z==11){LCD_Dado('1');LCD_Dado('1');n=n-110;  LCD_Dado(m[n]);}if(z==12){LCD_Dado('1');LCD_Dado('2');n=n-120;  LCD_Dado(m[n]);}
         }
           if(PORTDbits.RD7==1){
            LCD_Dado('+');
            v=12;
            cd++; }
       }
      return;
}
void LCD_Init(void)
{
    TRISB = 0b00000000;
    TRISCbits.RC0 = 0; //RS 
    TRISCbits.RC1 = 0; //RW 
    TRISCbits.RC2 = 0; //EN 
    
    PORTCbits.RC0 = 0; //RS
    PORTCbits.RC1 = 0; //RW
    PORTCbits.RC2 = 0; //EN
    __delay_ms(15); //delay 15ms
    LCD_Comando(0x30);
    __delay_ms(5); //delay 5ms
    LCD_Comando (0x30);
    __delay_ms(10);//delay 100us
    LCD_Comando (0x30);
    __delay_ms(5);//delay 5 ms
    
    LCD_Comando(0x3c);//N=1(2 linhas) F=1(fonte 5x7)
    __delay_ms(5);
    LCD_Comando(0x08);//desliga o display
    __delay_ms(5);
    LCD_Comando(0x01);//apaga a DRAM
    __delay_ms(5);
    LCD_Comando(0x06);//Entry mode set
    __delay_ms(5);
    
    LCD_Comando(0x0F);//liga o display e mantem o cursor piscando
    __delay_ms(5);
    }
void LCD_Dado(char _Dado)
{
    PORTCbits.RC0 = 1;//RS = 1 envio de dados
    PORTCbits.RC1 = 0;//RW = 0 escreva dados
    PORTB = _Dado;
    PORTCbits.RC2 = 1;//EN
    __delay_ms(10);
    PORTCbits.RC2 = 0;//EN
}
void LCD_Comando(char _Comando)
{
    PORTCbits.RC0 = 0;//RS = 0 envio de instruções
    PORTCbits.RC1 = 0;//RW = 0 Escrita
    PORTB= _Comando;
    PORTCbits.RC2 = 1;//EN
    __delay_ms(10);
    PORTCbits.RC2 = 0;//EN
}
void LCD_SETXY(char x,char y)
{
    if(y == 1)//primeira linha do lcd
        LCD_Comando(0x80 | x);
    else if(y ==2)//segunda linha do lcd
        LCD_Comando(0x80 | (x + 0x40));
            
}

 

Link para o comentário
Compartilhar em outros sites

  • Membro VIP

Procure no seu compilador a função pra escrever no display. Veja nos exemplos que vêm com ele. Mas sim a lcd_dado() já lhe seria suficiente mas teremos dificuldades em te mostrar como. Mas algo como:

void lcd_decimal(unsigned int dd)
{
while(dd)
	{
	lcd_dado(dd%10);
	dd/=10;
	}
}

Tenta agora

lcd_decimal(12345);

Se você combinar com a função lcd_setxy(), você controla direitinho a posição dos números.

Pode te dar alguma luz mesmo que futura...

Sério que ao googlar calculadora com pic não te retornou nada relevante? Aff o google não é + aquele...

não vale dizer que o video é pra 16f pois como a linguagem c é universal, serve pro 18 com mínimas adaptações...

afff vi agora no fim do video que é assembly... mas vou deixar aí, ok?

 

Olha isso que bacaninha

https://www.electronicwings.com/users/lokeshc/codes/digital_calculator_using_pic18f4550

Algo me diz que é o que você procura...

Link para o comentário
Compartilhar em outros sites

Crie uma conta ou entre para comentar

Você precisa ser um usuário para fazer um comentário

Criar uma conta

Crie uma nova conta em nossa comunidade. É fácil!

Crie uma nova conta

Entrar

Já tem uma conta? Faça o login.

Entrar agora

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...