getting problem in code showing lot of error

Thread Starter

ect_09

Joined May 6, 2012
180
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:
#include<htc.h>

__CONFIG(1, OSCSDIS & HSPLL);
__CONFIG(2, BORDIS & PWRTDIS & WDTDIS);
__CONFIG(3, CCP2RC1);
__CONFIG(4, LVPEN & STVREN);
__CONFIG(5, UNPROTECT);
__CONFIG(6, WRTEN);
__CONFIG(7, TRU);

void sevenseg(char ch);

void main()
{
TRISB=0;
PORTB=0x00;
sevenseg(ch);
__delay_ms(1000);

switch(ch)
{
    case'0': ch'0'; break;
    case'1': ch'1'; break;
    case'2': ch'2'; break;
    case'3': ch'3'; break;
    case'4': ch'4'; break;
    case'5': ch'5'; break;
    case'6': ch'6'; break;
    case'7': ch'7'; break;
    case'8': ch'8'; break;
    case'9': ch'9'; break;
    case'A': ch'A'; break;
    case'B': ch'B'; break;
    default ch'0': break;


}

void sevenseg(char ch)
{

switch(ch)
{
case'0': PORTB=0x3F; break;
case'1': PORTB=0x06; break;
case'2': PORTB=0x5B; break;
case'3': PORTB=0x4F; break;
case'4': PORTB=0x66; break;
case'5': PORTB=0x6D; break;
case'6': PORTB=0x7D; break;
case'7': PORTB=0x07; break;
case'8': PORTB=0x7F; break;
case'9': PORTB=0x6F; break;
case'A': PORTB=0x77; break;
    default: PORTB=0x3F;
}
}
 

MrChips

Joined Oct 2, 2009
25,928
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:
#include<htc.h>

__CONFIG(1, OSCSDIS & HSPLL);
__CONFIG(2, BORDIS & PWRTDIS & WDTDIS);
__CONFIG(3, CCP2RC1);
__CONFIG(4, LVPEN & STVREN);
__CONFIG(5, UNPROTECT);
__CONFIG(6, WRTEN);
__CONFIG(7, TRU);

void sevenseg(char ch);

void main()
{
   char ch;
   TRISB=0;
   PORTB=0x00;

  while (1)
  {
     ch = (++ch) % 10;
     sevenseg(ch);
     __delay_ms(1000);
  }
}

void sevenseg(char ch)
{
  switch(ch)
 {
   case 0: PORTB=0x3F; break;
   case 1: PORTB=0x06; break;
   case 2: PORTB=0x5B; break;
   case 3: PORTB=0x4F; break;
   case 4: PORTB=0x66; break;
   case 5: PORTB=0x6D; break;
   case 6: PORTB=0x7D; break;
   case 7: PORTB=0x07; break;
   case 8: PORTB=0x7F; break;
   case 9 : PORTB=0x6F; break;
   default: PORTB=0x3F;
 }
}
Instead of a switch statement I would prefer to use an array.
 

MrChips

Joined Oct 2, 2009
25,928
The purpose of
Code:
ch = (++ch) % 10;
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:
if (++ch > 9) ch = 0;
 

MrChips

Joined Oct 2, 2009
25,928
Instead of using a switch statement I would have used an array:

Code:
char seven_seg []  = { 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F }
Then all you need is:
Code:
PORTB = seven_seg[ch];
 

Thread Starter

ect_09

Joined May 6, 2012
180
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)
 

MrChips

Joined Oct 2, 2009
25,928
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)
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++
 

Thread Starter

ect_09

Joined May 6, 2012
180
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.
 

Attachments

Top