Code check please!

Discussion in 'Programmer's Corner' started by Ranayh, May 16, 2013.

  1. Ranayh

    Thread Starter New Member

    May 6, 2013
    8
    0
    hey everyone,

    i was wondering if someone would help me make sure that these 2 codes are the same. one was meant to be compiled on mpLAB but i redjusted it a little to fit on mikroC. can some1 just make sure its right and not missing anything important? thank uu!!!

    Original code:

    Code ( (Unknown Language)):
    1. #include <p18f452.h>
    2. #include <delays.h>
    3. #include <adc.h>
    4. #include <math.h>
    5.  
    6. #define gravity_ss 512
    7. #define display_divider 20
    8.  
    9. //PORTD - 0bBGCDHAEF
    10. #define one     0b10100000
    11. #define two     0b11010110
    12. #define three   0b11110100
    13. #define four    0b11100001
    14. #define five    0b01110101
    15. #define six     0b01110111
    16. #define seven   0b10100100
    17. #define eight   0b11110111
    18. #define nine    0b11110101
    19. #define zero    0b10110111
    20. #define blank   0b00000000
    21.  
    22. void update_char_display(int *char_disp)
    23. {
    24.     int i=0;
    25.     int all_digits[11] = {zero, one, two, three, four, five, six, seven, eight, nine};
    26.     for(i=0;i<3;i++)
    27.     {
    28.  
    29.         switch(i)
    30.         {
    31.         case 0 :    PORTB = all_digits[char_disp[i]] + 0b1000;
    32.             break;
    33.         case 1 :    PORTD = all_digits[char_disp[i]];
    34.             break;
    35.         case 2 :    PORTC = all_digits[char_disp[i]];
    36.             break;
    37.         default :
    38.             break;
    39.         }
    40.     }
    41.  
    42. }
    43.  
    44. void main(void)
    45. {
    46.  
    47. int char_disp[3];
    48.  
    49. int current_result, past_result = 0;
    50. int g_val, temp = 0;
    51.  
    52. int i=0;
    53.  
    54. TRISA = 0xFF;
    55. TRISB = 0x00;
    56. TRISC = 0x00;
    57. TRISD = 0x00;
    58.                 //Turn All Displays Off Initially
    59. PORTB = blank;      //PORTB -> Display 1
    60. PORTC = blank;      //PORTC -> Display 2
    61. PORTD = blank;      //PORTD -> Display 3
    62.  
    63.                     //Initialize A/D Converter
    64. OpenADC( ADC_FOSC_32
    65.         & ADC_RIGHT_JUST
    66.         & ADC_8ANA_0REF,
    67.         ADC_CH0 & ADC_INT_OFF );
    68.  
    69. while(1){
    70.  
    71. update_char_display(char_disp);
    72. Delay1KTCYx(250);
    73.  
    74.  
    75. Delay10TCYx(5);
    76. ConvertADC();
    77. while( BusyADC() );
    78.     current_result = ReadADC();
    79.  
    80. temp = current_result - past_result;
    81.  
    82.     if( temp > 2 || temp < -2 )
    83.     {
    84.     past_result = current_result;
    85.     g_val = current_result - gravity_ss;
    86.     g_val = g_val << 5;
    87.     g_val = g_val / display_divider;
    88.  
    89.     if(g_val < 0)
    90.         g_val = g_val * -1;
    91.  
    92.     i = 2;
    93.     }
    94.     while(i!= 255)
    95.     {
    96.         char_disp[i]=g_val%10;
    97.         g_val = g_val/10;
    98.         i--;
    99.     }
    100.  
    101. }
    102.  
    103.  
    104.  
    105. }
    106. [/i][/i][/i][/i]


    MikroC code:

    Code ( (Unknown Language)):
    1. #device PIC18F452
    2.  
    3. #define gravity_ss 512
    4. #define display_divider 20
    5.  
    6. //PORTD - 0bBGCDHAEF
    7. #define one    0b10100000
    8. #define two    0b11010110
    9. #define three    0b11110100
    10. #define four    0b11100001
    11. #define five    0b01110101
    12. #define six    0b01110111
    13. #define seven    0b10100100
    14. #define eight    0b11110111
    15. #define nine    0b11110101
    16. #define zero    0b10110111
    17. #define blank   0b00000000
    18.  
    19. void update_char_display(int *char_disp)
    20. {
    21.    int i=0;
    22.    int all_digits[11] = {zero, one, two, three, four, five, six, seven, eight, nine};
    23.    for(i=0;i<3;i++)
    24.    {
    25.  
    26.       switch(i)
    27.       {
    28.       case 0 :   PORTB = all_digits[char_disp[i]] + 0b1000;
    29.          break;
    30.       case 1 :   PORTD = all_digits[char_disp[i]];
    31.          break;
    32.       case 2 :   PORTC = all_digits[char_disp[i]];
    33.          break;
    34.       default :
    35.          break;
    36.       }
    37.    }
    38.  
    39. }
    40.  
    41. void main(void)
    42. {
    43.  
    44. int char_disp[3];
    45.  
    46. int current_result, past_result = 0;
    47. int g_val, temp = 0;
    48.  
    49. int i=0;
    50.  
    51. TRISA = 0xFF;
    52. TRISB = 0x00;
    53. TRISC = 0x00;
    54. TRISD = 0x00;
    55.             //Turn All Displays Off Initially
    56. PORTB = blank;      //PORTB -> Display 1
    57. PORTC = blank;      //PORTC -> Display 2
    58. PORTD = blank;      //PORTD -> Display 3
    59.  
    60.                //Initialize A/D Converter
    61. OpenADC( ADC_FOSC_32
    62.       & ADC_RIGHT_JUST
    63.       & ADC_8ANA_0REF,
    64.       ADC_CH0 & ADC_INT_OFF );
    65.  
    66. while(1){
    67.  
    68. update_char_display(char_disp);
    69. Delay1KTCYx(250);
    70.  
    71.  
    72. Delay10TCYx(5);
    73. ConvertADC();
    74. while( BusyADC() );
    75.    current_result = ReadADC();
    76.  
    77. temp = current_result - past_result;
    78.  
    79.    if( temp > 2 || temp < -2 )
    80.    {
    81.    past_result = current_result;
    82.    g_val = current_result - gravity_ss;
    83.    g_val = g_val << 5;
    84.    g_val = g_val / display_divider;
    85.  
    86.    if(g_val < 0)
    87.       g_val = g_val * -1;
    88.  
    89.    i = 2;
    90.    }
    91.    while(i!= 255)
    92.    {
    93.       char_disp[i]=g_val%10;
    94.       g_val = g_val/10;
    95.       i--;
    96.    }
    97.  
    98. }
    99.  
    100.  
    101.  
    102. }
    103.  
    104.  
    105. [/i][/i][/i][/i]
     
  2. WBahn

    Moderator

    Mar 31, 2012
    17,757
    4,800
    What have YOU done to verify them up to this point?
     
  3. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    Actually, what exactly have you done to adapt the code? As far as diff tells me, you only deleted these three lines:
    #include <delays.h>
    #include <adc.h>
    #include <math.h>
    Have you at the very least tried to compile the code?
    And to answer your question, you are missing three important libraries that are essential for the code to work. Ok make that two as I don´t see anything that needs math.h, but still.
     
  4. Ranayh

    Thread Starter New Member

    May 6, 2013
    8
    0
    sorry i copied the wrong mikroC code. this is the correct one.
    i only changed the parts related to the ADC initialization and read. i wana make sure i theres nothing missing that i couldve deleted to remove errors to build it.

    Code ( (Unknown Language)):
    1.  
    2. #define gravity_ss 512
    3. #define display_divider 20
    4.  
    5. //PORTD - 0bBGCDHAEF
    6. #define one         0b10100000
    7. #define two         0b11010110
    8. #define three         0b11110100
    9. #define four         0b11100001
    10. #define five         0b01110101
    11. #define six         0b01110111
    12. #define seven         0b10100100
    13. #define eight         0b11110111
    14. #define nine         0b11110101
    15. #define zero         0b10110111
    16. #define blank   0b00000000
    17.  
    18. void update_char_display(int *char_disp)
    19. {
    20.         int i=0;
    21.         int all_digits[11] = {zero, one, two, three, four, five, six, seven, eight, nine};
    22.         for(i=0;i<3;i++)
    23.         {
    24.  
    25.                 switch(i)
    26.                 {
    27.                 case 0 :        PORTB = all_digits[char_disp[i]] + 0b1000;
    28.                         break;
    29.                 case 1 :        PORTD = all_digits[char_disp[i]];
    30.                         break;
    31.                 case 2 :        PORTC = all_digits[char_disp[i]];
    32.                         break;
    33.                 default :
    34.                         break;
    35.                 }
    36.         }
    37.  
    38. }
    39.  
    40. void main(void)
    41. {
    42.  
    43. int char_disp[3];
    44.  
    45. int current_result=0;
    46. int past_result = 0;
    47. int g_val, temp = 0;
    48.  
    49. int i=0;
    50.  
    51. TRISA = 0xFF;
    52. TRISB = 0x00;
    53. TRISC = 0x00;
    54. TRISD = 0x00;
    55.                                 //Turn All Displays Off Initially
    56. PORTB = blank;                //PORTB -> Display 1
    57. PORTC = blank;                //PORTC -> Display 2
    58. PORTD = blank;                //PORTD -> Display 3
    59.  
    60.                                         //Initialize A/D Converter
    61. ADC_Init();
    62. while(1){
    63.  
    64. update_char_display(char_disp);
    65. Delay_ms(250);
    66.  
    67.  
    68. Delay_ms(5);
    69.  
    70. while( 1 );
    71.         current_result = ADC_read(1);
    72. temp = current_result - past_result;
    73.  
    74.         if( temp > 2 || temp < -2 )
    75.         {
    76.         past_result = current_result;
    77.         g_val = current_result - gravity_ss;
    78.         g_val = g_val << 5;
    79.         g_val = g_val / display_divider;
    80.  
    81.         if(g_val < 0)
    82.                 g_val = g_val * -1;
    83.  
    84.         i = 2;
    85.         }
    86.         while(i!= 255)
    87.         {
    88.                 char_disp[i]=g_val%10;
    89.                 g_val = g_val/10;
    90.                 i--;
    91.         }
    92.  
    93. }
    94.  
    95.  
    96.  
    97. }[/i][/i][/i][/i]
     
  5. Ranayh

    Thread Starter New Member

    May 6, 2013
    8
    0
    i only changed the parts related to the ADC initialization and read. i wana make sure i theres nothing missing that i couldve deleted to remove errors to build it.
     
  6. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    i never really used mikroc to be honest, doesn´t it need some header files too?
    The only importatnt thing that has changed is that in the old code the main loop waited for the adc to be done with the last conversion, and now it doesn´t. Or is ADC_read(1) a blocking function?
     
    Ranayh likes this.
  7. Ranayh

    Thread Starter New Member

    May 6, 2013
    8
    0
    the ADC_read initializes the PICs internal ADC module to work with rc clock. Then takes input reading
     
  8. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,606
    Mikro C does indeed secretly includes the most vital of include files: the individual device header. Actually, they include it into the project as a dot C file.
     
  9. Brownout

    Well-Known Member

    Jan 10, 2012
    2,375
    998
    You can use comparison programs, such as "Beyond Compare" to see what differences there are between two source files. I think it might be a free program, not sure.

    If you use Linux, you get "diff" as part of the package for comparison.
     
  10. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    In windows, I use torotiseGIT for versioning, which comes with tortoiseDiff, a nice tool to compare versions graphically.
     
  11. Ranayh

    Thread Starter New Member

    May 6, 2013
    8
    0
    Thank u guys i just downloaded beyond compare. Great software!
     
Loading...