PROBLEM WITH SWITCH INSTRUCTION

Please check the frequently asked questions before posting to the forum.
Post Reply
Author
Message
fernandomarques
Posts: 26
Joined: 24 May 2010 10:15

PROBLEM WITH SWITCH INSTRUCTION

#1 Post by fernandomarques » 18 Oct 2010 17:20

HI!
I'm using the PIC18F452 20MHZ
I have a problem with the switch statement because when I add more "case" in the switch statement appears many meaningless characters on my display.
NOTE: I've tried on the compiler 4.10 and 4.15 and i have the same problem.


Thank you
fernandomarques

#define UP 26
#define LF 9
#define RH 11
#define DW 12

//TO RECEIVE CHARACTERS FROM MY KEYBOARD
unsigned char MGetc(void)
{
while(!PIR1.RCIF);
return RCREG;
}

void main()
{
unsigned short key;


UART1_Init(9600);

while(1)
{

key=MGetc(); //THIS IS MY FUNCTION TO RECEIVE CHARACTERS FROM MY KEYBOARD

switch(key)
{
case UP: UART1_Write_Text("UP");
break;
case LF: UART1_Write_Text("LF");
break;
case RH: UART1_Write_Text("RH");
break;
case DW: UART1_Write_Text("DW");
break;

case 10: UART1_Write_Text("JK"); //UNTILL THIS LINE WOKS WELL
break;

/* FROM THIS LINE UNTILL THE END DOES NOT WORK
APPEARS (TO MANY CHARACTERS ON THE DISPLAY */

case 13: UART1_Write_Text("LM");
break;
case 14: UART1_Write_Text("MN");
break;
case 15: UART1_Write_Text("OP");
break;
case 16: UART1_Write_Text("HJ");
break;
default: UART1_Write_Text("UI");
break;
}
}
}

User avatar
slavisa.zlatanovic
mikroElektronika team
Posts: 1321
Joined: 07 Apr 2009 09:39

Re: PROBLEM WITH SWITCH INSTRUCTION

#2 Post by slavisa.zlatanovic » 20 Oct 2010 10:23

Hi!

The Switch statement is working fine.
I have made some changes to your code and now it is working properly:

Code: Select all

#define UP 26
#define LF 9
#define RH 11
#define DW 12

//TO RECEIVE CHARACTERS FROM MY KEYBOARD
unsigned char MGetc(void)
{
while(!PIR1.RCIF);
return RCREG;
}


void main()
{
unsigned short key;


UART1_Init(9600);
Delay_ms(100);
UART1_Write_Text("START");

while(1){

//key=MGetc(); //THIS IS MY FUNCTION TO RECEIVE CHARACTERS FROM MY KEYBOARD
if (UART1_Data_Ready()) {      // ! use Library routines  
 key = UART1_Read();            // ! see the Help file for more details

 switch(key)
{
case UP: UART1_Write_Text("UP");
break;
case LF: UART1_Write_Text("LF");
break;
case RH: UART1_Write_Text("RH");
break;
case DW: UART1_Write_Text("DW");
break;

case 10: UART1_Write_Text("JK"); //UNTILL THIS LINE WOKS WELL
break;

/* FROM THIS LINE UNTILL THE END DOES NOT WORK
APPEARS (TO MANY CHARACTERS ON THE DISPLAY */

case 13: UART1_Write_Text("LM");
break;
case 14: UART1_Write_Text("MN");
break;
case 15: UART1_Write_Text("OP");
break;
case 16: UART1_Write_Text("HJ");
break;
default: UART1_Write_Text("UI");
//break;}  // ! 

   }
  }
 }
}
P.S.
We strongly encourage users to switch to PRO version because non-PRO version is an ancestor of PRO compiler
and is no longer being developed.
Every new feature, improvement and bug fix will only affect new versions of PRO compilers.
If you're a registered user you'll receive PRO license key free of charge.
See the migration document for more details:
Migration_document.PNG
Migration_document.PNG (15.87 KiB) Viewed 3012 times
Best regards
Slavisa

fernandomarques
Posts: 26
Joined: 24 May 2010 10:15

Re: PROBLEM WITH SWITCH INSTRUCTION

#3 Post by fernandomarques » 22 Oct 2010 09:27

Dear Slavisa

I apologize because it was my mistake. I have one problem with my bootloader, because it do not put the null terminator at the end of the string, so it cause senseless strings

Once again my apologies

thank you
fernandomarques

Post Reply

Return to “mikroPascal FAQ”