Software > Software
PIC PORT value with Switch statements
Hal9000:
Hi there,
I am using RB4, RB5, RB6 and RB7 on a PIC16F628a microcontroller
Each pin goes low when it recieves sound, in order that timer readings (from Timer1) can be taken and then output these to the screen.
Say if RB1 recieves the sound (goes low) before the others, it should be assigned Timerreading = 0
RB4 is next, say
Then RB3
And finally RB2
Then output from RS232 RB1=0,RB2=70,RB3=60,RB4=20, for example
The problem I am having is where to start with conditional loops. Ovbiously a switch statement would be ideal, but I don't know how it could be implemented.
Would it be something like:
switch (PORT B)
case 0b00001110:
//Stuff that happens when RB7 is activated (i.e set the timer reading for RB7 to 0, start timer and poll for the other pins)
break;
case 0b00001101:
//Stuff that happens when RB6 is activated (i.e set the timer reading for RB6 to 0, start timer and poll for the other pins)
break;
case 0b00001011:
//Stuff that happens when RB5 is activated (i.e set the timer reading for RB5 to 0, start timer and poll for the other pins)
break;
case 0b00000111:
//Stuff that happens when RB4 is activated (i.e set the timer reading for RB4 to 0, start timer and poll for the other pins)
break;
default:
break;
Thing is RB0,RB1,RB2 and RB3 are doing different things, and will always be changing state, therefore meaning that my switch statement wouldnt work, right?
I was looking at http://www.learn-c.com/experiment1.htm, but it's all very confusing.
Thanks for taking a look!
JesseWelling:
Are you making the sound?
E.G. are you doing some kind of sonar here or just trying to detect any sound?
Admin:
--- Quote ---Thing is RB0,RB1,RB2 and RB3 are doing different things, and will always be changing state
--- End quote ---
Have you considered running interrupts?
Basically call an interrupt when a pin changes, then go to your case based stuff to 1) figure out which pin changed and 2) run your action code.
Hal9000:
Thanks for your replies.
I am not making sound, but simply detecting the time difference between sound arriving at different locations from one source.
I have considered using interrupt on pin changes, but my application will also have to know where the interrupt came from, so I would then have to poll for the sensors.
Therefore, as I was saying, if the sound was recieved at RB4 first, then this would start the timer, and assign the timervalue belonging to RB4 to 0.
If RB5 was to get the sound next, then it would assign a timer value to this
Then If RB7 was to get the sound next, then it would assign a timer value to this
Then If RB6 was to get the sound next, then it would assign a timer value to this and then output ALL the values over RS232
If you can imagine, after I did one interrupt, not only would I have to poll for the sensor, but I would also have to carry on with some other form of loop anyway, for the other sensors after the first. This is how i arrived at switch statements.
e.g (To get a logical idea, I have used the word 'if'. This is not indicative that I will be using If statements)
If RB4
then poll for RB5, RB6, RB7<---------------see below
If RB5
then poll for RB4, RB6, RB7
If RB6
then poll for RB4, RB5, RB7
If RB7
then poll for RB4, RB5, RB6
To poll for RB5,RB6 and RB7 you would have another switch statement
If RB5
then poll for RB6, RB7<----------------------see below
If RB6
then poll for RB5, RB7
If RB7
then poll for RB5, RB6
To poll for RB6 and RB7 you would have another switch statement
If RB6
then poll for RB7<---------------see below
If RB7
then poll for RB6
At this point, you would then wait for RB7, which is now the last sensor to detect the sound. After you have all the timing readings, you would then output them all over RS232, which I already have set up.
But, I guess my whole question was, how would you implement a switch statement with only the last 4 bits?
(0b00001111)
Cheers
Ian
Admin:
this is probably how id do it. timingarray[] would store both the port number and the time recorded.
--- Code: ---interrupt:
{
if RB4 = high && timingarray does not have number=4 yet
number=4;
if RB5 = high && timingarray does not have number=5 yet
number=5;
if RB6 = high && timingarray does not have number=6 yet
number=6;
if RB7 = high && timingarray does not have number=7 yet
number=7;
timingarray[i]=number;
i++;
timingarray[i]=time_from_timer();
i++;
}
main loop:
wait for sound
if i=7 //array is full and all sensors recorded
printf(the array);
--- End code ---
edit: after posting that code, i realized that the 'bracket i bracket' also means use italics . . . i also had my i++ in the wrong spot.
Navigation
[0] Message Index
[#] Next page
Go to full version