Need online resources to help me with 8051 calculator project.

Discussion in 'Embedded Systems and Microcontrollers' started by THCynical, Oct 25, 2014.

  1. THCynical

    Thread Starter New Member

    May 5, 2014
    26
    0
    I have recently been assigned a 8051 calculator project.
    Figure it out for yourself type assignment, We were given no notes/lectures on coding.
    I'm not sure where to begin.

    I'm looking for Online resources to help me with this project.
    How to write basic code in C in general.
    And stuff specially relating 8051 as a calculator.

    The starting point for this project is from:
    http://saeedsolutions.blogspot.ie/2012/06/interfacing-keypad-with-8051at89c51-c.html

    I have ISIS 8 and Keil5 on the laptop.

    Final note - I have looked online for a while for videos and sites to help me but my search terms are to general as i don't know what I'm looking for.

    Thanks for your time.
     
  2. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,398
    497
  3. THCynical

    Thread Starter New Member

    May 5, 2014
    26
    0
    well that's exactly what I'm looking for

    do i put "#include" just before "Char key"?

    Thanks your after saving me allot of time.

    If anyone knows of good online resources that will help me so i know what I'm doing, i would love to see them.
     
  4. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,398
    497
    You can download the C code from that blog entry. The download link is in the end of the blog entry.
    http://dl.dropbox.com/u/22020714/8051 Calculator Code.rar

    Code (Text):
    1.  
    2. // ***********************************************************
    3. // Project:   Calculator
    4. // Author:    M.Saeed Yasin
    5. // Module description:  4 function single positive digit calculator
    6. // ***********************************************************
    7.  
    8. #include<reg51.h>
    9. #include<string.h>
    10.  
    11. //Define Macros
    12. #define Error  13    // Any value other than 0 to 9 is good here
    13.  
    14. //Function declarations
    15. void cct_init(void);
    16. void delay(int);
    17. void lcdinit(void);
    18. void writecmd(int);
    19. void writedata(char);
    20. void writeline(char[]);
    21. void ReturnHome(void);
    22. char READ_SWITCHES(void);
    23. char get_key(void);
    24. int get_num(char);
    25. char get_func(char);
    26. void DispError(int);
    27. void disp_num(int);
    28.  
    29. //*******************
    30. //Pin description
    31. /*
    32. P2 is data bus
    33. P3.7 is RS
    34. P3.6 is E
    35. P1.0 to P1.3 are keypad row outputs
    36. P1.4 to P1.7 are keypad column inputs
    37. */
    38. //********************
    39. // Define Pins
    40. //********************
    41. sbit RowA = P1^0;     //RowA
    42. sbit RowB = P1^1;     //RowB
    43. sbit RowC = P1^2;     //RowC
    44. sbit RowD = P1^3;     //RowD
    45.  
    46. sbit C1   = P1^4;     //Column1
    47. sbit C2   = P1^5;     //Column2
    48. sbit C3   = P1^6;     //Column3
    49. sbit C4   = P1^7;     //Column4
    50.  
    51. sbit E    = P3^6;     //E pin for LCD
    52. sbit RS   = P3^7;     //RS pin for LCD
    53.  
    54. // ***********************************************************
    55. // Main program
    56. //
    57. int main(void)
    58. {
    59.    char key;                     //key char for keeping record of pressed key
    60.    int num1 = 0;                 //First number
    61.    char func = '+';              //Function to be performed among two numbers
    62.    int num2 = 0;                 //Second number
    63.  
    64.    cct_init();                   //Make input and output pins as required
    65.    lcdinit();                    //Initilize LCD
    66.  
    67.    while(1)
    68.    {
    69.      //get numb1
    70.      key = get_key();
    71.      writecmd(0x01);            //clear display
    72.      writedata(key);            //Echo the key pressed to LCD
    73.      num1 = get_num(key);       //Get int number from char value, it checks for wrong input as well
    74.    
    75.      if(num1!=Error)            //if correct input then proceed, num1==Error means wrong input
    76.      {
    77.          //get function
    78.          key = get_key();
    79.          writedata(key);                  //Echo the key pressed to LCD
    80.          func = get_func(key);            //it checks for wrong func
    81.        
    82.          if(func!='e')                    //if correct input then proceed, func=='e' means wrong input
    83.          {
    84.              //get numb2
    85.              key = get_key();
    86.              writedata(key);              //Echo the key pressed to LCD
    87.              num2 = get_num(key);         //Get int number from char value, it checks for wrong input as well
    88.            
    89.              if(num2!=Error)              //if correct input then proceed, num2==Error means wrong input
    90.              {
    91.                  //get equal sign
    92.                  key = get_key();
    93.                  writedata(key);          //Echo the key pressed to LCD
    94.                
    95.                  if(key == '=')           //if = is pressed then proceed
    96.                  {
    97.                      switch(func)         //switch on function
    98.                      {
    99.                      case '+': disp_num(num1+num2); break;
    100.                      case '-': disp_num(num1-num2); break;
    101.                      case 'x': disp_num(num1*num2); break;
    102.                      case '/': disp_num(num1/num2); break;
    103.                      }
    104.                  }
    105.                  else                      //key other then = here means error wrong input
    106.                  {
    107.                      if(key == 'C')       //if clear screen is pressed then clear screen and reset
    108.                         writecmd(0x01);   //Clear Screen
    109.                      else
    110.                         DispError(0);       //Display wrong input error
    111.                  }                                
    112.              }
    113.          }
    114.      }
    115.    }
    116. }
    117.  
    118.  
    119. void cct_init(void)
    120. {
    121.     P0 = 0x00;   //not used
    122.     P1 = 0xf0;   //used for generating outputs and taking inputs from Keypad
    123.     P2 = 0x00;   //used as data port for LCD
    124.     P3 = 0x00;   //used for RS and E  
    125. }
    126.  
    127. void delay(int a)
    128. {
    129.    int i;
    130.    for(i=0;i<a;i++);   //null statement
    131. }
    132.  
    133. void writedata(char t)
    134. {
    135.    RS = 1;             // This is data
    136.    P2 = t;             //Data transfer
    137.    E  = 1;             // => E = 1
    138.    delay(150);
    139.    E  = 0;             // => E = 0
    140.    delay(150);
    141. }
    142.  
    143.  
    144. void writecmd(int z)
    145. {
    146.    RS = 0;             // This is command
    147.    P2 = z;             //Data transfer
    148.    E  = 1;             // => E = 1
    149.    delay(150);
    150.    E  = 0;             // => E = 0
    151.    delay(150);
    152. }
    153.  
    154. void lcdinit(void)
    155. {
    156.   ///////////// Reset process from datasheet /////////
    157.      delay(15000);
    158.    writecmd(0x30);
    159.      delay(4500);
    160.    writecmd(0x30);
    161.      delay(300);
    162.    writecmd(0x30);
    163.      delay(650);
    164.   /////////////////////////////////////////////////////
    165.    writecmd(0x38);    //function set
    166.    writecmd(0x0c);    //display on,cursor off,blink off
    167.    writecmd(0x01);    //clear display
    168.    writecmd(0x06);    //entry mode, set increment
    169. }
    170.  
    171. void ReturnHome(void)     /* Return to 0 cursor location */
    172. {
    173.    writecmd(0x02);
    174.    delay(1500);
    175. }
    176.  
    177. void writeline(char Line[])
    178. {
    179.    int i;
    180.    for(i=0;i<strlen(Line);i++)
    181.    {
    182.       writedata(Line[i]);     /* Write Character */
    183.    }
    184.  
    185.    ReturnHome();          /* Return to 0 cursor position */
    186. }
    187.  
    188. char READ_SWITCHES(void)  
    189. {  
    190.     RowA = 0; RowB = 1; RowC = 1; RowD = 1;     //Test Row A
    191.  
    192.     if (C1 == 0) { delay(10000); while (C1==0); return '7'; }
    193.     if (C2 == 0) { delay(10000); while (C2==0); return '8'; }
    194.     if (C3 == 0) { delay(10000); while (C3==0); return '9'; }
    195.     if (C4 == 0) { delay(10000); while (C4==0); return '/'; }
    196.  
    197.     RowA = 1; RowB = 0; RowC = 1; RowD = 1;     //Test Row B
    198.  
    199.     if (C1 == 0) { delay(10000); while (C1==0); return '4'; }
    200.     if (C2 == 0) { delay(10000); while (C2==0); return '5'; }
    201.     if (C3 == 0) { delay(10000); while (C3==0); return '6'; }
    202.     if (C4 == 0) { delay(10000); while (C4==0); return 'x'; }
    203.    
    204.     RowA = 1; RowB = 1; RowC = 0; RowD = 1;     //Test Row C
    205.  
    206.     if (C1 == 0) { delay(10000); while (C1==0); return '1'; }
    207.     if (C2 == 0) { delay(10000); while (C2==0); return '2'; }
    208.     if (C3 == 0) { delay(10000); while (C3==0); return '3'; }
    209.     if (C4 == 0) { delay(10000); while (C4==0); return '-'; }
    210.    
    211.     RowA = 1; RowB = 1; RowC = 1; RowD = 0;     //Test Row D
    212.  
    213.     if (C1 == 0) { delay(10000); while (C1==0); return 'C'; }
    214.     if (C2 == 0) { delay(10000); while (C2==0); return '0'; }
    215.     if (C3 == 0) { delay(10000); while (C3==0); return '='; }
    216.     if (C4 == 0) { delay(10000); while (C4==0); return '+'; }
    217.  
    218.     return 'n';               // Means no key has been pressed
    219. }
    220.  
    221. char get_key(void)           //get key from user
    222. {
    223.     char key = 'n';              //assume no key pressed
    224.  
    225.     while(key=='n')              //wait untill a key is pressed
    226.         key = READ_SWITCHES();   //scan the keys again and again
    227.  
    228.     return key;                  //when key pressed then return its value
    229. }
    230.  
    231. int get_num(char ch)         //convert char into int
    232. {
    233.     switch(ch)
    234.     {
    235.         case '0': return 0; break;
    236.         case '1': return 1; break;
    237.         case '2': return 2; break;
    238.         case '3': return 3; break;
    239.         case '4': return 4; break;
    240.         case '5': return 5; break;
    241.         case '6': return 6; break;
    242.         case '7': return 7; break;
    243.         case '8': return 8; break;
    244.         case '9': return 9; break;
    245.         case 'C': writecmd(0x01); return Error; break;  //this is used as a clear screen and then reset by setting error
    246.         default: DispError(0); return Error; break;     //it means wrong input
    247.     }
    248. }
    249.  
    250. char get_func(char chf)            //detects the errors in inputted function
    251. {
    252.     if(chf=='C')                   //if clear screen then clear the LCD and reset
    253.     {
    254.         writecmd(0x01);            //clear display
    255.         return 'e';
    256.     }
    257.    
    258.     if( chf!='+' && chf!='-' && chf!='x' && chf!='/' )  //if input is not from allowed funtions then show error
    259.     {
    260.         DispError(1);
    261.         return 'e';
    262.     }
    263.  
    264.     return chf;                        //function is correct so return the correct function
    265. }
    266.  
    267.  
    268. void DispError(int numb)           //displays differet error messages
    269. {
    270.     writecmd(0x01);                //clear display
    271.    
    272.     switch(numb)
    273.     {
    274.     case 0:     writeline("Wrong Input");      break;
    275.     case 1:     writeline("Wrong Function");   break;
    276.     default:    writeline("Wrong Input");      break;
    277.     }
    278. }
    279.  
    280. void disp_num(int numb)            //displays number on LCD
    281. {  
    282.     unsigned char UnitDigit  = 0;  //It will contain unit digit of numb
    283.     unsigned char TenthDigit = 0;  //It will contain 10th position digit of numb
    284.  
    285.     if(numb<0)
    286.     {
    287.         numb = -1*numb;  // Make number positive
    288.         writedata('-');     // Display a negative sign on LCD
    289.     }
    290.  
    291.     TenthDigit = (numb/10);              // Findout Tenth Digit
    292.  
    293.     if( TenthDigit != 0)              // If it is zero, then don't display
    294.         writedata(TenthDigit+0x30);      // Make Char of TenthDigit and then display it on LCD
    295.  
    296.     UnitDigit = numb - TenthDigit*10;
    297.  
    298.     writedata(UnitDigit+0x30);      // Make Char of UnitDigit and then display it on LCD
    299. }
    300.  
     
  5. THCynical

    Thread Starter New Member

    May 5, 2014
    26
    0
    Sorry, yeah cheers
     
Loading...