pic_calculator.c

来自「jklzmnm, ljjlj jksdjljl sbnkjkldl」· C语言 代码 · 共 394 行

C
394
字号
//********************************************************
//define pins
#define one PORTA.F0
#define two PORTA.F1
#define three PORTA.F2
#define four PORTA.F3
#define five PORTA.F5
#define six PORTE.F0
#define seven PORTE.F1
#define eight PORTE.F2
#define nine PORTC.F0
#define zero PORTC.F1
;****************************************************************
//define variables

char txt[12];
char in1[12];
char in2[12];
char i;
char x;
long result;
double result1;
unsigned long input1,input2;
char mode;
unsigned short kp;

//**************************************************************
//define prototype

char get_num(void);
char read_key(void);

//main routine
//setup I/O port
//setup LCD pins

void main()
{
  ADCON1 = 0x06;              // Configure analog inputs and Vref
  TRISA  = 0b11111111;              // PORTA is input
  TRISB  = 0b11110000;              //
  TRISC  = 0b11111111;
  TRISD  = 0b00000000;
  TRISE  = 0b00000111;
  OPTION_REG = 0b00000001;
  LCD_Config(&PORTD,4,5,6,3,2,1,0);
  Lcd_Init(&PORTD);                        //
  LCD_Cmd(LCD_CURSOR_OFF);                 // send command to LCD (cursor off)
  LCD_Cmd(LCD_CLEAR);                      // send command  to LCD (clear LCD)
  Lcd_Out(1,1, " Smart Calulator");
  Lcd_Out(2,1, "     System    ");
  Delay_ms(2000);
  LCD_Cmd(LCD_CLEAR);


//*******************************************************************888
//get key & save in in1 array

  do
  {
  for(i=0;i<=12;i++)			
  in1[i]=0;
  i=0;
  while(1)
  {
    x=read_key();			//get key press
    if(i<=8)
    {
      in1[i]=x;
      if(i==0)
      Lcd_chr(1,1,in1[i]);		//display key at lcd
      else
      lcd_chr_cp(in1[i]);
      i++;
    }
    if(x=='*' || x=='/' || x=='+' || x=='-')	//if key found then break
    {
    mode=x;
    LCD_Cmd(LCD_CLEAR);
    Lcd_chr(1,12,x);
    break;
    }
  }

    input1 = atol(in1);			//convert string to long & save in input1
    Delay_ms(10);

  for(i=0;i<=12;i++)
  in2[i]=0;
  i=0;

  while(1)
  {
    x=read_key();			//get key press
    if(i<=8)
    {
      in1[i]=x;
      if(i==0)
      Lcd_chr(1,1,in1[i]);		//display key at lcd
      else
      lcd_chr_cp(in1[i]);
      i++;
    }
    if(x=='=')			//if key found then break
    {
    LCD_Cmd(LCD_CLEAR);
    break;
    }
  }

    input2 = atol(in1);			//convert string to long & save in input2

  LCD_Cmd(LCD_CLEAR);		//clear all arrays
  lcd_out(1,1,"Result:");
  for(i=0;i<=12;i++)
  txt[i]=0;
  result=0;

  if(mode=='+')			//if + key
  {				//do calulation
   result= input1+input2;
   longtostr(result,txt);			//convert result to string
   lcd_chr(2,1,'=');
   for(i=0;i<=11;i++)
   {
    if(txt[i]==' ')
    continue;
    if (isdigit(txt[i]) || txt[i]=='-')		//display result in LCD
    lcd_chr_cp(txt[i]);
   }
   Delay_ms(5000);
   LCD_Cmd(LCD_CLEAR);
  }

  else if(mode=='-')			//if - key
  {
   result= input1-input2;		//do calulation
   longtostr(result,txt);			//convert result to string
   lcd_chr(2,1,'=');
   for(i=0;i<=11;i++)
   {
    if(txt[i]==' ')
    continue;
    if (isdigit(txt[i]) || txt[i]=='-')		//display result in LCD
    lcd_chr_cp(txt[i]);
   }
   Delay_ms(5000);
   LCD_Cmd(LCD_CLEAR);
  }

  else if(mode=='*')			//if * key
  {
   result= input1*input2;		//do calulation
   longtostr(result,txt);			//convert result to string
   lcd_chr(2,1,'=');
   for(i=0;i<=11;i++)
   {
    if(txt[i]==' ' ||  txt[i]=='-')		//display result in LCD
    continue;
    if (isdigit(txt[i]))
    lcd_chr_cp(txt[i]);
   }
   Delay_ms(5000);
   LCD_Cmd(LCD_CLEAR);
  }

  else if(mode=='/')			//if / key
  {
   result1= input1/input2;		//do calulation
   floattostr(result,txt);			//convert result to string
   lcd_chr(2,1,'=');
   for(i=0;i<=11;i++)
   {
    if(txt[i]==' ')
    continue;
    if (isdigit(txt[i])|| txt[i]=='-' ||txt[i]=='.')		//display result in LCD
    lcd_chr_cp(txt[i]);
   }
   Delay_ms(5000);
   LCD_Cmd(LCD_CLEAR);
  }
  }while(1);
}

//********************************************************************
//check PORTC pins
//if math return value save in num

char get_num(void)
{
char num;
 while(1)
 {
  if (PORTC==0b00000001)
  {
   num='1';
   break;
  }
  else if (PORTC==0b00000010)
  {
   num='2';
   break;
  }
  else if (PORTC==0b00000100)
  {
   num='3';
   break;
  }
  else if (PORTC==0b00001000)
  {
   num='4';
   break;
  }
  else if (PORTC==0b00010000)
  {
   num='5';
   break;
  }
  else if (PORTC==0b00100000)
  {
   num='6';
   break;
  }
  else if (PORTC==0b01000000)
  {
   num='7';
   break;
  }
  else if (PORTC==0b10000000)
  {
   num='8';
   break;
  }
  else if (PORTA==0b00000001)
  {
   num='0';
   break;
  }
  else if (PORTA==0b00000010)
  {
   num='+';
   break;
  }
  else if (PORTA==0b00000100)
  {
   num='-';
   break;
  }
  else if (PORTA==0b00001000)
  {
   num='*';
   break;
  }
  else if (PORTA==0b00010000)
  {
   num='/';
   break;
  }
 }
 return num;
}


//*****************************************************************************
//scan keypad
//scan row 1,2,3,4
//detect column
//loop until key found

char read_key(void){

unsigned short row,col;
char key;

  row = 1;
 do{
  key = 0;

    if (row==1){
    PORTB = 0b00001110;
    col = PORTB;
        switch(col){
           case  0xE0: key = '1'; break;
           case  0xD0: key = '2'; break;
           case  0xB0: key = '3'; break;
           case  0x70: key = '+'; break;
         }
        row =2  ;
    }
    else if (row ==2){
    PORTB = 0b00001101;
    col = PORTB;
         switch(col){
           case  0xE0: key = '4'; break;
           case  0xD0: key = '5'; break;
           case  0xB0: key = '6'; break;
           case  0x70: key = '-'; break;
         }
       row = 3;
    }
    else if (row==3){
    PORTB = 0b00001011;
    col = PORTB;
         switch(col){
           case  0xE0: key = '7'; break;
           case  0xD0: key = '8'; break;
           case  0xB0: key = '9'; break;
           case  0x70: key = '*'; break;
           }
       row = 4;
    }
    else if (row==4){
    PORTB = 0b00000111;
    col = PORTB;
         switch(col){
           case  0xE0: key = '*'; break;
           case  0xD0: key = '0'; break;
           case  0xB0: key = '='; break;
           case  0x70: key = '/'; break;
           }
       row = 1;
    }

  if (one)
  {
   key='1';
   while(one);
   Delay_ms(100);
  }
  else if (two)
  {
   key='2';
   while(two);
   Delay_ms(100);
  }
  else if (three)
  {
   key='3';
   while(three);
   Delay_ms(100);
  }
  else if (four)
  {
   key='4';
   while(four);
   Delay_ms(100);
  }
  else if (five)
  {
   key='5';
   while(five);
   Delay_ms(100);
  }
  else if (six)
  {
   key='6';
   while(six);
   Delay_ms(100);
  }
  else if (seven)
  {
   key='7';
   while(seven);
   Delay_ms(100);
  }
  else if (eight)
  {
   key='8';
   while(eight);
   Delay_ms(100);
  }
  else if (nine)
  {
   key='9';
   while(nine);
   Delay_ms(100);
  }
  else if (zero)
  {
   key='0';
   while(zero);
   Delay_ms(100);
  }

 }while(key);

 do{
  col = PORTB;
  col &= 0b11110000;
  delay_ms(10);
  } while(col != 0b11110000) ;
  return key;

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?