Write A PIC 18 program to read RB0 and RB1 bits issue ASCII character to PD according to the following table:
RB1 RB0
0 0 send '0' to PORTD ( notice ASCII '0' is 0x30) "I got mixed up here."
0 1 send '1' t0 PORTD
1 0 send '2' to PORTD
1 1 send '3' to PORTD
#include <p18f458.h>
void main(void)
{
unsigned char z;
TRISB = 0xFF; //make Port B an input
TRISD = 0; //make Port D an output
{
z = PORTB; //read Port B
z = z & 0x30; //mask the unused bits
switch (z)
{
case (0): // issue ASCII 0
{
PORTD = '0';
break;
}
case (1): //issue ASCII 1
{
PORTD = '1';
break;
}
case (2): //issue ASCII 2
{
PORTD = '2';
break;
}
case (3): //issue ASCII 3
{
PORTD = '3';
break;
}
}
}
}
When you do z = z & 0x30 the result is one of 0, 16, 32 or 48, not 0, 1, 2 or 3. Either change the case statements or change the line to z = (z & 0x30)>>4.
HTH
Mike.
Edit, wrong numbers.
17: break;
00D6 D009 BRA 0xea
18: }
19: case (1):
20: {
21: PORTD = '1';
00D8 0E31 MOVLW 0x31
00DA 6E83 MOVWF 0xf83, ACCESS
22: break;
00DC D006 BRA 0xea
23: }
24: case (2):
25: {
26: PORTD = '2';
00DE 0E32 MOVLW 0x32
00E0 6E83 MOVWF 0xf83, ACCESS
27: break;
00E2 D003 BRA 0xea
28: }
29: case (3):
30: {
31: PORTD = '3';
00E4 0E33 MOVLW 0x33
00E6 6E83 MOVWF 0xf83, ACCESS
32: break;
00E8 D000 BRA 0xea
33: }
34: }
35: }
36: }
Here is the new code:
#include <p18f458.h>
void main(void)
{
unsigned char z;
TRISB = 0xFF;
TRISD = 0;
{
z = PORTB;
z = (z & 0x30>>4);
switch (z)
{
case (0):
{
PORTD = '0';
break;
}
case (1):
{
PORTD = '1';
break;
}
case (2):
{
PORTD = '2';
break;
}
case (3):
{
PORTD = '3';
break;
}
}
}
}
Hi KV,
For one thing I don't think you're supposed to have brackets around the case statement.
I'm not quite sure what you're saying is going wrong. A switch/case statement will only run one of the case statements, depending on the value of the switch variable. It's basically the same as if/else.
Matt
I've had a very pleasant night with friends in the pub so my response may be suspect. However, z=(z & 0x30 >>4) is not the same as z = (z & 0x30)>>4. It's all about priority.
Mike.
You're reading PORTB to create the switch statement, yet PORTB doesn't appear to ever change. Since only the first case statement (z = 0) is the only one being hit, I expect that means PORTB is 0 (0x00) all the time. Try changing the values of PORTB and see if it changes which case statement is hit.
You're reading PORTB to create the switch statement, yet PORTB doesn't appear to ever change. Since only the first case statement (z = 0) is the only one being hit, I expect that means PORTB is 0 (0x00) all the time. Try changing the values of PORTB and see if it changes which case statement is hit.
[
I didn't get this question you asked or at least the statement. I'm at work and doing this between jobs. Sorry about that....
Post #14 changed the value of z = (z & 0x02)>>4 PORTB is equal to "0x00" and "0" so apparently it's not placing the correct value like you said in post # 8.
z = PORTB;
z = (z & 0x30)>>4;
#include <p18f458.h>
void main(void)
{
unsigned char z;
TRISB = 0xFF;
TRISD = 0;
z = PORTB;
z = (z & 0x02)>>4;
switch (z)
{
case 0:
{
PORTD = '0';
}
case 1:
{
PORTD = '1';
}
case 2:
{
PORTD = '2';
}
case 3:
{
PORTD = '3';
break;
}
}
}
#include <p18f458.h>
void main(void)
{
unsigned char z;
TRISB = 0xFF;
TRISD = 0;
z = PORTB;
z = z & 0x30;
switch (z)
{
case (0):
{
PORTD = '0';
}
case (1):
{
PORTD = '1';
}
case (2):
{
PORTD = '2';
}
case (3):
{
PORTD = '3';
break;
}
}
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?