View previous topic :: View next topic |
Author |
Message |
xyzsor
Joined: 19 Dec 2010 Posts: 12
|
PIC16f877a Hex to Binary / Port Pin |
Posted: Tue Dec 28, 2010 10:44 am |
|
|
Good day guys.. I need a little help here
Here's my setup
PC ---> UART ---> PIC16f877a ---> ARRAY of LED's
The PC will transmit a series of in HEX data using UART to the PIC uC.. The PIC uC must then convert this hex data into binary and output them to the LED's.. The LED's are at located at PORT A (pin 1-4) and port B (pin5-8)
For example if the data transmitted thru UART was "D3", in binary thats
1101 0011
the first four bits, (1101) will be outputted at PORTA pin 1 to 4
the last four bits, (0011) will be outputted at PORTB pin 5 to 8
Here's my code.. The UART transmission part is working OK.. The problem is when i get the DATA, i dont know how to convert it to binary and output them to their respective pins..
Code: |
#include <16F877A.h>
#fuses HS, NOWDT NOLVP, NOPROTECT
#use delay (clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, parity=N, bits=8, ERRORS)
void main()
{
int8 data;
while(TRUE)
{
data = getc();
//????????????
//????????????
//????????????
//????????????
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
jecottrell
Joined: 16 Jan 2005 Posts: 559 Location: Tucson, AZ
|
|
![](templates/subSilver/images/spacer.gif) |
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Tue Dec 28, 2010 11:32 am |
|
|
Something to be very careful with is how the PC is handling your data you are sending. If you are using the standard windows drivers, I have (at least in the past) run into a number of instances where the driver was "helping" me by doing such things as appending a linefeed when it saw a CR go by (or maybe it was the reverse?). Anyway, you need to be careful if you are sending raw data as to how it is handled by windows when you get into the range of data that are control characters.
mikey _________________ mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3 |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Dec 28, 2010 12:58 pm |
|
|
Quote: | For example if the data transmitted thru UART was "D3", in binary thats
1101 0011
the first four bits, (1101) will be outputted at PORTA pin 1 to 4
the last four bits, (0011) will be outputted at PORTB pin 5 to 8
|
I'm not sure what you mean by "pin 1 to 4". Do you means bits ?
Port bits are numbered 0 to 7 in the PIC.
Mark has an example of a structure of Port bits in this post:
http://www.ccsinfo.com/forum/viewtopic.php?t=20182
He has several contiguous i/o Ports in one structure. He sets the base
address of the structure to the register address of the lowest Port in
the structure. He then defines bit fields in the structure for his lcd signals.
He also has another similar structure for the TRIS registers. |
|
![](templates/subSilver/images/spacer.gif) |
xyzsor
Joined: 19 Dec 2010 Posts: 12
|
|
Posted: Tue Dec 28, 2010 6:01 pm |
|
|
gpsmikey wrote: | Something to be very careful with is how the PC is handling your data you are sending. If you are using the standard windows drivers, I have (at least in the past) run into a number of instances where the driver was "helping" me by doing such things as appending a linefeed when it saw a CR go by (or maybe it was the reverse?). Anyway, you need to be careful if you are sending raw data as to how it is handled by windows when you get into the range of data that are control characters.
mikey
|
As far as I know the PC sends the data in a sequence of bytes in HEX. It does not include the linefeed. Just the raw data. From my understanding also of the code, the PIC will receive the data byte by byte as well. Please correct me if I'm wrong. |
|
![](templates/subSilver/images/spacer.gif) |
xyzsor
Joined: 19 Dec 2010 Posts: 12
|
|
Posted: Tue Dec 28, 2010 6:07 pm |
|
|
PCM programmer wrote: |
I'm not sure what you mean by "pin 1 to 4". Do you means bits ?
Port bits are numbered 0 to 7 in the PIC.
|
YES. Thats exactly what I meant. Sorry. Thanks for correcting me.
PCM programmer wrote: |
Mark has an example of a structure of Port bits in this post:
http://www.ccsinfo.com/forum/viewtopic.php?t=20182
He has several contiguous i/o Ports in one structure. He sets the base
address of the structure to the register address of the lowest Port in
the structure. He then defines bit fields in the structure for his lcd signals.
He also has another similar structure for the TRIS registers.
|
Thank you. This is exactly what I'm looking for. However I don't understand it much to make my own structure of ports. Could anyone please guide me ? The first nibble of the byte data that is received must go to to portA bit 0-3, then the other nibble must go to portB 4-7. |
|
![](templates/subSilver/images/spacer.gif) |
bkamen
Joined: 07 Jan 2004 Posts: 1611 Location: Central Illinois, USA
|
|
Posted: Tue Dec 28, 2010 9:01 pm |
|
|
xyzsor wrote: |
Thank you. This is exactly what I'm looking for. However I don't understand it much to make my own structure of ports. Could anyone please guide me ? The first nibble of the byte data that is received must go to to portA bit 0-3, then the other nibble must go to portB 4-7. |
Code: | union {
unsigned int value;
struct {
unsigned int LO:4;
unsigned int HI:4;
} nibble;
struct {
unsigned int bit0:1;
unsigned int bit1:1;
unsigned int bit2:1;
unsigned int bit3:1;
unsigned int bit4:1;
unsigned int bit5:1;
unsigned int bit6:1;
unsigned int bit7:1;
} bits;
} portb;
#locate portb = getenv("sfr:LATB")
|
Remember, a lot of the times you want to use LAT not PORT to avoid the read-modify-write issues that sometimes occur.
With that, in this example, you can modify portB (via LATB) values through
portb.value = 0;
portb.nibble.lo = 0xf
portb.bits.bit0 = 1
Are you following?
-Ben
Edit: To my knowledge, you would need 2 setups. One for each port. (PortA and PortB) _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
![](templates/subSilver/images/spacer.gif) |
xyzsor
Joined: 19 Dec 2010 Posts: 12
|
|
Posted: Tue Dec 28, 2010 10:02 pm |
|
|
bkamen wrote: |
Are you following?
|
I was a bit confused a while ago.. But now i'm plain lost...
I thought you could like make a structure that points to pin a0-3 and pin b4-7 to replace PORTB..
Code: | #include <16F877A.h>
#fuses HS, NOWDT NOLVP, NOPROTECT
#use delay (clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, parity=N, bits=8, ERRORS)
/* I KNOW THIS STRUCTURE IS WRONG BUT, I JUST WANTED TO SHOW WHAT THE STRUCTURE I AM TRYING TO BUILD LOOKS LIKE
struct
{
unsigned int porta0;
unsigned int porta1;
unsigned int porta2;
unsigned int porta3;
unsigned int portb4;
unsigned int portb5;
unsigned int portb6;
unsigned int portb7;
}mystuct
*/
void main()
{
int8 data;
while(TRUE)
{
data = getc();
mystuct = data;
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Dec 28, 2010 11:33 pm |
|
|
Here is a simple method that will be easier to understand. In the
example below, we wait for an incoming byte from the serial port.
Then we load it into a variable. Then we test each bit of the variable
and set the specified Port pin to the same value as the variable.
I have done one line of it below. I test bit 0 of 'c', and set Pin A0
to whatever value it is (either a 0 or 1). I've put in seven dots
for you to fill in the remainder of the lines. In the next line, you
need to test bit 1 of 'c', and enter in the next pin in the sequence
(presumably PIN_A1).
Code: |
#include <18F452.h>
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//======================================
void main(void)
{
char c;
while(1)
{
c = getc(); // Wait for an incoming character
output_bit(PIN_A0, bit_test(c, 0));
.
.
.
.
.
.
.
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
xyzsor
Joined: 19 Dec 2010 Posts: 12
|
|
Posted: Wed Dec 29, 2010 1:43 am |
|
|
Sweet!.. Your good PCM programmer!!...
Thanks alot..
And its even simpler than i thought!..
Again, Thanks PCM programmer.. |
|
![](templates/subSilver/images/spacer.gif) |
|