getting problem in code showing lot of error

Discussion in 'Embedded Systems and Microcontrollers' started by ect_09, Oct 18, 2014.

  1. ect_09

    Thread Starter Member

    May 6, 2012
    180
    1
    am using MPLAB 8.90 and Hi-Tech compiler for PIC18F452
    i write this code but getting lot of error.
    its seems OK from my side. kindly check it.

    Code (Text):
    1. #include<htc.h>
    2.  
    3. __CONFIG(1, OSCSDIS & HSPLL);
    4. __CONFIG(2, BORDIS & PWRTDIS & WDTDIS);
    5. __CONFIG(3, CCP2RC1);
    6. __CONFIG(4, LVPEN & STVREN);
    7. __CONFIG(5, UNPROTECT);
    8. __CONFIG(6, WRTEN);
    9. __CONFIG(7, TRU);
    10.  
    11. void sevenseg(char ch);
    12.  
    13. void main()
    14. {
    15. TRISB=0;
    16. PORTB=0x00;
    17. sevenseg(ch);
    18. __delay_ms(1000);
    19.  
    20. switch(ch)
    21. {
    22.     case'0': ch'0'; break;
    23.     case'1': ch'1'; break;
    24.     case'2': ch'2'; break;
    25.     case'3': ch'3'; break;
    26.     case'4': ch'4'; break;
    27.     case'5': ch'5'; break;
    28.     case'6': ch'6'; break;
    29.     case'7': ch'7'; break;
    30.     case'8': ch'8'; break;
    31.     case'9': ch'9'; break;
    32.     case'A': ch'A'; break;
    33.     case'B': ch'B'; break;
    34.     default ch'0': break;
    35.  
    36.  
    37. }
    38.  
    39. void sevenseg(char ch)
    40. {
    41.  
    42. switch(ch)
    43. {
    44. case'0': PORTB=0x3F; break;
    45. case'1': PORTB=0x06; break;
    46. case'2': PORTB=0x5B; break;
    47. case'3': PORTB=0x4F; break;
    48. case'4': PORTB=0x66; break;
    49. case'5': PORTB=0x6D; break;
    50. case'6': PORTB=0x7D; break;
    51. case'7': PORTB=0x07; break;
    52. case'8': PORTB=0x7F; break;
    53. case'9': PORTB=0x6F; break;
    54. case'A': PORTB=0x77; break;
    55.     default: PORTB=0x3F;
    56. }
    57. }  
    58.  
     
  2. MrChips

    Moderator

    Oct 2, 2009
    12,442
    3,361
    Your code makes no sense and is incomplete.

    Here are some errors or omissions:

    1. You are missing a closing } in main().

    2. What is the meaning of
    case'0': ch'0'; break;

    3. Why the switch structure in main()?

    4. Where is ch defined? Where does ch get its input?

    5. Is there supposed to be an endless loop in main()?

    Code (Text):
    1.  
    2. #include<htc.h>
    3.  
    4. __CONFIG(1, OSCSDIS & HSPLL);
    5. __CONFIG(2, BORDIS & PWRTDIS & WDTDIS);
    6. __CONFIG(3, CCP2RC1);
    7. __CONFIG(4, LVPEN & STVREN);
    8. __CONFIG(5, UNPROTECT);
    9. __CONFIG(6, WRTEN);
    10. __CONFIG(7, TRU);
    11.  
    12. void sevenseg(char ch);
    13.  
    14. void main()
    15. {
    16.    char ch;
    17.    TRISB=0;
    18.    PORTB=0x00;
    19.  
    20.   while (1)
    21.   {
    22.      ch = (++ch) % 10;
    23.      sevenseg(ch);
    24.      __delay_ms(1000);
    25.   }
    26. }
    27.  
    28. void sevenseg(char ch)
    29. {
    30.   switch(ch)
    31.  {
    32.    case 0: PORTB=0x3F; break;
    33.    case 1: PORTB=0x06; break;
    34.    case 2: PORTB=0x5B; break;
    35.    case 3: PORTB=0x4F; break;
    36.    case 4: PORTB=0x66; break;
    37.    case 5: PORTB=0x6D; break;
    38.    case 6: PORTB=0x7D; break;
    39.    case 7: PORTB=0x07; break;
    40.    case 8: PORTB=0x7F; break;
    41.    case 9 : PORTB=0x6F; break;
    42.    default: PORTB=0x3F;
    43.  }
    44. }
    45.  
    46.  
    Instead of a switch statement I would prefer to use an array.
     
    ect_09 likes this.
  3. ect_09

    Thread Starter Member

    May 6, 2012
    180
    1
    Thank you for Reply ,
    can you explain this line
    Code (Text):
    1. ch = (++ch) % 10;
    what does its mean??
     
  4. MrChips

    Moderator

    Oct 2, 2009
    12,442
    3,361
    The purpose of
    Code (Text):
    1.  
    2. ch = (++ch) % 10;
    3.  
    is to have ch cycle through from 0 to 9.

    How does it work?

    ++ch increments ch.
    % is the modulus operator, i.e. it returns the integer remainder after performing an integer divide.
    Hence % 10 returns the remainder after dividing by 10.

    I could also have used
    Code (Text):
    1.  
    2. if (++ch > 9) ch = 0;
    3.  
     
    ect_09 likes this.
  5. b1u3sf4n09

    Member

    May 23, 2014
    115
    14
    Last edited: Oct 18, 2014
    ect_09 likes this.
  6. MrChips

    Moderator

    Oct 2, 2009
    12,442
    3,361
    Instead of using a switch statement I would have used an array:

    Code (Text):
    1.  
    2. char seven_seg []  = { 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F }
    3.  
    Then all you need is:
    Code (Text):
    1.  
    2. PORTB = seven_seg[ch];
    3.  
     
    ect_09 likes this.
  7. ect_09

    Thread Starter Member

    May 6, 2012
    180
    1
    this kind of sample code can i get from internet that guide at beginner level.???
    otherwise trying effort to approach the program well with right and wrong way (Y)
     
  8. MrChips

    Moderator

    Oct 2, 2009
    12,442
    3,361
    There are so many ways to skin a cat. b1u3sf4n09 points out another way:

    Code (Text):
    1.  
    2. ch = (++ch < 10) ? ch: 0;
    3.  
     
  9. MrChips

    Moderator

    Oct 2, 2009
    12,442
    3,361
    All of these techniques are at the beginner's level. You just have to learn what are the common operators and how to use them.

    They are listed here and many other places:

    http://en.wikipedia.org/wiki/Operators_in_C_and_C++
     
    ect_09 likes this.
  10. ect_09

    Thread Starter Member

    May 6, 2012
    180
    1
    Exactly,
    Before 7 segment i did blinking LED on HARDWARE, i made it own .
    well i was excited because i did it own after config bit etc.

    but today i disappointed because simulation didnt work as the code getting error. but u made it possible for me.
    making efforts to improve on daily basis.

    Thanks for helping ,.....!

    Respect.
     
  11. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,387
    497
    1. increment ch by 1, new ch
    2. find modulus 10 of new ch
    3. store result in ch
     
Loading...