sp3ctrm5tr

PIC16F877A 4x4 keypad interfacing (Simple Calculator)

Apr 12th, 2012
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.03 KB | None | 0 0
  1. /*-----------------------------------------------------/
  2.     Name    : Mohd Syahri Fikri Bin Samusi
  3.  
  4.     [LAB02 Q05] KEYPAD 4x4 interfacing with
  5.     PIC16F877A. A simple calculator, with
  6.     divide by zero check.
  7.    
  8.     Date modified : 12 April 2012
  9. /-----------------------------------------------------*/
  10.  
  11. #include <htc.h>
  12. #include <pic16f877a.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <usart.c>
  16. #define _XTAL_FREQ 20000000
  17. #include <KEYPAD_RB.c>
  18.  
  19. void init_rs232( )
  20. {  
  21.     TRISC = TRISC | 0b10000000;
  22.     BRGH = 1;    
  23.     SPBRG = 129;    // set baud rate: Fosc=20Mhz, BR=9600
  24.     TX9  = 0;       // ninebits?1:0,,,8- or 9-bit transmission
  25.     SYNC = 0;       // asynchronous
  26.     SPEN = 1;       // enable serial port pins
  27.     TXEN = 1;       // enable the transmitter & (automatically)TXIF=1
  28.     CREN = 1;       // enable reception
  29.     SREN = 0;       // no effect
  30.     TXIE = 0;       // disable tx interrupts
  31.     RCIE = 0;       // disable rx interrupts
  32. }
  33.  
  34. int isamong (char a, char *s)
  35. {  
  36.     while (*s)
  37.     {
  38.         if (a==*s++)
  39.         {
  40.             return 1;
  41.         }
  42.     }
  43.     return 0;
  44. }
  45.  
  46. void calc(char *op, long int *a, long int *b)
  47. {  
  48.     double ans;
  49.     int f;
  50.     if (*op=='+')       //Performing addition
  51.     {
  52.         f=0;
  53.         ans = *a + *b;
  54.     }
  55.     else if (*op=='-')  //Performing subtraction
  56.     {
  57.         f=0;
  58.         if (*a > *b)
  59.         {
  60.             ans = *a - *b;
  61.         }
  62.         else
  63.         {
  64.             ans = -1*(*b - *a);
  65.         }
  66.     }
  67.     else if (*op=='*')  //Performing multiplication
  68.     {
  69.         f=0;
  70.         ans = *a * *b;
  71.     }
  72.     else if (*op=='/')  //Performing division
  73.     {
  74.  
  75.         if(*b==0)
  76.         {
  77.             f=1;
  78.         }
  79.         else
  80.         {
  81.             f=0;
  82.             ans = *a  *1.0 / *b;
  83.         }
  84.     }
  85.    
  86.     if(f==1)
  87.     {
  88.         printf ("\r\n ERROR!! Cannot divide by 0 \n");
  89.     }
  90.     else
  91.     {
  92.         printf ("\r\n%ld %c %ld \n= %f", *a, *op, *b, ans);
  93.     }
  94. }
  95.  
  96. void main( )
  97. {      
  98.     char k=0;
  99.     long int a=0, b=0;
  100.     int c=0,c1=0, d=0, e=0, op;
  101.     kbd_init( );                //Initialize keypad
  102.     init_rs232( );              //Initialize serial I/O
  103.     printf ("\fReady...\r\n");  //Show the ready message
  104.     while (1)                   //Keep running
  105.     {  
  106.         if (kbd_getc( )==0)
  107.         {
  108.             do
  109.             {
  110.                 k=kbd_getc( );
  111.                 __delay_ms(30);
  112.                     if (k>='0'&&k<='9')
  113.                         c1=1;
  114.                     else if (isamong(k,"+-/*C="))
  115.                         c1=1;
  116.                     else
  117.                         c1=0;
  118.             }
  119.             while(c1==0);
  120.         }
  121.        
  122.         if(k!=0)
  123.         {  
  124.             if(k=='C')
  125.             {  
  126.                 printf ("\r\nAgain...\n");
  127.                 c=0;
  128.                 d=0;
  129.             }
  130.             else
  131.             {  
  132.                 if(c==2)
  133.                 {  
  134.                     printf ("\fReady...\n");
  135.                     c=0;
  136.                 }
  137.                 if(k!='=')
  138.                 {  
  139.                     putch(k);              
  140.                     if((k>='0')&&(k<='9'))
  141.                     {  
  142.                         if(c==0)
  143.                         {  
  144.                             if(d==0)
  145.                             {  
  146.                                 a = k - '0';
  147.                                 d++;
  148.                             }
  149.                             else
  150.                             {      
  151.                                 a = (a*10)+(k-'0');
  152.                                 d++;
  153.                             }
  154.                         }
  155.                         else
  156.                         {  
  157.                             if(e==0)
  158.                             {  
  159.                                 b = k - '0';
  160.                                 e++;
  161.                             }
  162.                             else
  163.                             {      
  164.                                 b = (b*10)+(k-'0');
  165.                                 e++;
  166.                             }
  167.                         }
  168.                     }
  169.                     else
  170.                     {  
  171.                         op = k;
  172.                         c = 1;                  
  173.                     }
  174.                 }
  175.                 else
  176.                 {  
  177.                     calc(&op, &a, &b);
  178.                     c = 2;
  179.                     d = 0;
  180.                     e = 0;
  181.                 }
  182.             }
  183.             k=0;
  184.         }
  185.     }
  186. }//End of Program
Advertisement
Add Comment
Please, Sign In to add comment