Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Switch-Case

Status
Not open for further replies.

EN0

Member
Hey Everyone,

I'm having a little trouble with my switch-case statement code.

First I had this:

Code:
104.        while(1)
105.        {
106.                if(!PORTAbits.RA1)      // Switch 1
107.                {
108.                        LATB = 0x01;    //LED 1
109.                        playSong(0);
110.                }
111.                else if(!PORTAbits.RA2) // Switch 2
112.                {
113.                        LATB = 0x02;    // LED 2
114.                        playSong(1);
115.                }
116.                else if(!PORTAbits.RA3) // Switch 3
117.                {
118.                        LATB = 0x04;    // LED 3
119.                        playSong(2);
120.                }
121.                else
122.                {
123.                        LATB = 0x08;    // Otherwise, LED 4
124.                }
125.        }

Now I have this:

Code:
1.while(1)
2.{
3.      switch(PORTA)
4.      {
5.            case 0x0D:
6.            {
7.               LATB = 0x01;
8.               playSong(0);
9.               break;
10.            }
11.            case 0x0B:
12.            {
13.               LATB 0x02;
14.               playSong(1);
15.               break;
16.            }
17.            case 0x07:
18.            {
19.               LATB = 0x04;
20.               playSong(2);
21.               break;
22.            }
23.            default:
24.            {
25.               0x08;
26.            }
27.      }
28.}

For some reason the latter code doesn't work! Any noticeable errors? I'm using C18 with PIC18F1320.

Thanks,

Austin
 
Code:
while(1)
{
	switch(PORTA)
	{
		case 0x02:
			LATB = 0x01;
			playSong(0);
			break;
		case 0x04:
			LATB = 0x02;
			playSong(1);
			break;
		case 0x08:
			LATB = 0x04;
			playSong(2);
			break;
		default:
			LATB = 0x08;
	}
}

Should look more like that. You weren't using the right HEX values to represent the correct PORTA pins, you missed an equals sign and for the default case you missed the LATB assignment. Also, you don't need to enclose the various cases in curly brackets.

You should be warned though, this code will not work if more than one pin on PORTA is high. Your first example did not have this problem.


____________________________________________________________________________
Edit: I just noticed the negative logic in your first example. Are the other PORTA pins (0,4-7) to be presumed always high or low? In the latter case your code should look like this:

Code:
while(1)
{
	switch(PORTA)
	{
		case 0x0C:
			LATB = 0x01;
			playSong(0);
			break;
		case 0x0A:
			LATB = 0x02;
			playSong(1);
			break;
		case 0x06:
			LATB = 0x04;
			playSong(2);
			break;
		default:
			LATB = 0x08;
	}
}

This code would also be adversely affected by other pins (0,4-7) changing from the expected value.
 
Last edited:
Hi,

I forgot to note how my switches are set up:

PORTA = 0b00001111, where the first bit is on due to my speaker.

  • 2nd Bit (RA1) → SW1
  • 3rd Bit (RA2) → SW2
  • 4th Bit (RA3) → SW3

So if SW1 is pressed, PORTA = 0b00001101, etc.
 
This should do it:

Code:
while(1)
{
	switch(PORTA)
	{
		case 0x0D:
			LATB = 0x01;
			playSong(0);
			break;
		case 0x0B:
			LATB = 0x02;
			playSong(1);
			break;
		case 0x07:
			LATB = 0x04;
			playSong(2);
			break;
		default:
			LATB = 0x08;
	}
}
 
Aha, I tried your edited code and it worked! I assumed I would have the first bit always on since my speaker was on, but I guess that isn't the case.

Edit: Oh dear, I just realized that the first bit in PORTA will be a '0' after all, it is only a '1' after I initiate one of my commands and a song begins to play. Sorry!
 
Last edited:
The code blocks are not necessary for the cases but I prefer that people learning C use them in that it makes the code flow a bit more clear. That is why I ask EN0 to use them.

It would be good to mask PORTA by anding it with 0x0E. This would eliminate any problem regarding the 0 bit which is not a switch. If the switches are active low the three case values would be:
0b1100 or 0x0C
0b1010 or 0x0A
0b0110 or 0x06
 
Thanks 3v0, I implemented that ANDing.

I appreciate everyone's help!

Thanks,

Austin
 
Last edited:
I thought of one more thing to address: What is the best debouncing technique to use in my circumstance?
 
You do not need more code to debounce the switches. The first time the code see the switch it goes off and plays a song. Then it ignores the switches until the song finishes.

If we set the switches up on interrupts or checked the switches frequently while the songs were playing we would need to make sure we did not have switch bounce problems.
 
Status
Not open for further replies.

Latest threads

Back
Top