Update: When I went to "file Registers" window of the debugger (not) "Special Function Registers" I get 0,1,2,3 ASCII character. Seems to be working in the "File Registers"
I guess I don't know how to change the value of PORTB inside "Special Function Registers" but with this new code it seems to work now. When viewing the "File Registers"
Edit: It doesn't change when I have () or ' ' around case: or PORTD ' ' the "File Registers" show the same result as above.
case (0):
{
PORTD = '0';
}
case (0):
PORTD = '0';
break;
z = PORTB;
z = z & 0x30;
you're just trying to increase the value of PORTD each iteration of the loop? Then what are you using PORTB for, if you don't have any inputs? I'm not following your logic on this at all, to be honest
#include <p18f458.h>
void main(void)
{
unsigned char z;
TRISB = 0xFF;
TRISD = 0;
z = PORTB;
z = z & 0x32;
switch (z)
{
case (0):
PORTD = '0x30';
break;
case (1):
PORTD = '0x31';
break;
case (2):
PORTD = '0x32';
break;
case (3):
PORTD = '0x33';
break;
}
}
Maybe an explanation of a few things might help. In C values can be represented in many ways, 48, 0x30, 0b00110000, '0' "01234" (and it seems '01234') will all result in the value 48 decimal. The quotes may confuse people but all they do is single quotes return the ascii value of the (first) character within and double quotes return a zero terminated string. Your compiler is converting (casting) a string to a single character and using it's ascii value. So PORTD = '0x78' (or any other number) will be equivalent to PORTD = 48 (ascii for 0, the first character).
When you AND a value you 'keep' certain bits. z = z & 0x30 will keep bits 4 and 5 as 0x30 = binary 00110000. ANDing with 0x32 (00110010) will also keep bit 1. Is this what you intended?
The >>4 moves the value right 4 binary places and so converts 0x30 (00110000) to 0x03 (00000011).
HTH
Mike.
// Write a code PIC C18 program to read the RB0 and RB1 bits and issue a
// ASCII character to PD according to the following table.
// RB1 RB0
// 0 0 Send '0' to PORTD (Notice ASCII '0'is 0x30)
// 0 1 Send '1' to 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 & 0x3; //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;
}
}
}
}
I'm assuming
Code:// RB0 RB0
is supposed to be
Code:// RB0 RB1
I wish you had posted this at the start
Maybe an explanation of a few things might help. In C values can be represented in many ways, 48, 0x30, 0b00110000, '0' "01234" (and it seems '01234') will all result in the value 48 decimal. The quotes may confuse people but all they do is single quotes return the ascii value of the (first) character within and double quotes return a zero terminated string. Your compiler is converting (casting) a string to a single character and using it's ascii value. So PORTD = '0x78' (or any other number) will be equivalent to PORTD = 48 (ascii for 0, the first character).
When you AND a value you 'keep' certain bits. z = z & 0x30 will keep bits 4 and 5 as 0x30 = binary 00110000. ANDing with 0x32 (00110010) will also keep bit 1. Is this what you intended?
The >>4 moves the value right 4 binary places and so converts 0x30 (00110000) to 0x03 (00000011).
HTH
Mike.
//Write a C18 program to convert packet BCD 0x29 to ASCII and display the bytes on PORTB and PORTC
//Solution:
#include <p18f458.h>
void main(void)
{
unsigned char x, y, z;
unsigned char mybyte = 0x29;
TRISB = 0;
TRISC = 0; //make Ports B and C output
x = mybyte & 0x0F; //mask upper 4 bits
PORTB = x | 0x30; //make it an ASCII
y = mybyte & 0xF0; //mask lower 4 bits
y = y >>4; //shift it to lower 4 bits
PORTC = y | 0x30; //make it ASCII
}
#include <p18f4580.h>
void main(void)
{
unsigned char z;
TRISB = 0xFF; //make Port B an input
TRISC = 0; //make Port C an output
{
z = LATB; //read LATB
z = z & 0x03;
LATB = z | 0x03; //added
switch (z)
{
case (0): // issue ASCII 0
{
LATC = '0';
break;
}
case (1): //issue ASCII 1
{
LATC = '1';
break;
}
case (2): //issue ASCII 2
{
LATC = '2';
break;
}
case (3): //issue ASCII 3
{
LATC = '3';
break;
}
}
}
}
#include <p18f4580.h>
void main(void)
{
unsigned char z;
TRISB = 0xFF; //make Port B an input
TRISC = 0; //make Port C an output
while(1){
z = PORTB & 0x03; //read LATB
switch (z)
{
case (0): // issue ASCII 0
{
LATC = '0';
break;
}
case (1): //issue ASCII 1
{
LATC = '1';
break;
}
case (2): //issue ASCII 2
{
LATC = '2';
break;
}
case (3): //issue ASCII 3
{
LATC = '3';
break;
}
}
}
}
/
// Write a code PIC C18 program to read the RC0 and RC1 bits and issue a
// ASCII character to PORTC according to the following table.
// RC1 RC0
// 0 0 Send '0' to PORTC (Notice ASCII '0'is 0x30)
// 0 1
// 1 0
// 1 1
#include <p18f458.h>
void main(void)
{
unsigned char z;
TRISB = 1; //make Port B an input, I did change this from 0xFF
//Following the Datasheet not the book.
TRISC = 0; //make Port C an output; this was changed from PORTD
//I could never make PORTD work?
ADCON1 = 0x06; //I added this and the book I followed did not say I needed PORTS as Digital.
//I'm not sure I did this correct Datasheet shows 0x07 or 0x06
{
z = PORTB ; //read LATB
z = z & 0x03;
PORTB = z | 0x03; //I added an OR that was not in the original from the book.
switch (z)
{
case (0): //issue ASCII 0
{
PORTC = '0';
break;
}
case (1): //issue ASCII 1
{
PORTC = '1';
break;
}
case (2): //issue ASCII 2
{
PORTC = '2';
break;
}
case (3):
{
PORTC = '3'; //issue ASCII 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?