|
|
View previous topic :: View next topic |
Author |
Message |
logical
Joined: 21 Dec 2009 Posts: 57 Location: SouthPort, UK
|
Quick Bits Question |
Posted: Fri Feb 19, 2010 11:55 am |
|
|
Hi Guys/Gals
Just a quick question, in the following code, I am wanting to output individual bits to the port pins for Parallel Data Transfer, so far I can't get the Screen to display anything BUT, I have put a scope on the port pins and some form of data is being sent out.
Does the following seem like a reasonable idea, I am using the ">>" to select the individual bits but unsure of this is correct, the compiler however does not complain.
Thanks
Chris
Code: | void write_c(unsigned char out_command)
{
delay_us(1);
OUTPUT_BIT(RS,0);
OUTPUT_BIT(CSB,0);
OUTPUT_BIT(WRB,0);
//P1=out_command; //ammended for the following commands
OUTPUT_BIT(D17,(out_command>>8));
OUTPUT_BIT(D16,(out_command>>7));
OUTPUT_BIT(D15,(out_command>>6));
OUTPUT_BIT(D14,(out_command>>5));
OUTPUT_BIT(D13,(out_command>>4));
OUTPUT_BIT(D12,(out_command>>3));
OUTPUT_BIT(D11,(out_command>>2));
OUTPUT_BIT(D10,(out_command>>1));
delay_us(1); //used for debug purposes, no difference if enabled/commented out
OUTPUT_BIT(WRB,1);
OUTPUT_BIT(CSB,1);
OUTPUT_BIT(RS,1);
} |
Basically output the LSB of out_command to D10, 2nd bit to D11 and so on, these are defined as port pins. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Feb 19, 2010 1:08 pm |
|
|
Quote: | OUTPUT_BIT(D10,(out_command>>1)); |
It won't work, because the byte parameter is evaluated as a boolean
by the compiler. It assesses the entire byte value when it does this.
If the byte (after your shift operation) is equal to 0xC0, that value is
non-zero, so it's converted to a '1' by the compiler, and a '1' is
output to the Port pin.
You need to do something like the following, and use bitmasks to mask
out everything except for the desired bit. Then when the compiler
evaluates the byte, on your desired bit will control whether the byte
is evaluated as a '1' or '0'.
Code: |
OUTPUT_BIT(D12, (out_command & 4)); // Test bit 2
OUTPUT_BIT(D11, (out_command & 2)); // Test bit 1
OUTPUT_BIT(D10, (out_command & 1)); // Test bit 0
|
The CCS manual tells you this, but you have to understand how to
read the manual. When they say "0 or 1", it means they're expecting
a boolean argument.
Quote: | Syntax: output_bit (pin, value)
Parameters: Value is a 1 or a 0.
|
Here's the Wikipedia article on it:
http://en.wikipedia.org/wiki/Boolean_data_type
Here is the key point, about how C treats booleans:
Quote: |
Languages without an explicit Boolean data type, like C and Lisp, may
still represent truth values it by some other data type.
C uses an integer type, with false represented as the zero value, and
true as any non-zero value |
|
|
|
logical
Joined: 21 Dec 2009 Posts: 57 Location: SouthPort, UK
|
|
Posted: Fri Feb 19, 2010 1:56 pm |
|
|
Thanks PCM,
I was expecting it to check the binary value hence 1 or 0 and output directly to the pin as I would do in assembly.
I have redone that part of code based on your suggestion as below, one post I did read stated that putting !! in front would convert to boolean, have included for reference but would be easy to remove
Code: | void write_d(unsigned char out_data)
{
//delay_us(1);
OUTPUT_BIT(RS,1);
OUTPUT_BIT(CSB,0);
OUTPUT_BIT(WRB,0);
OUTPUT_BIT(D17, !!(out_data & 128));
OUTPUT_BIT(D16, !!(out_data & 64));
OUTPUT_BIT(D15, !!(out_data & 32));
OUTPUT_BIT(D14, !!(out_data & 16));
OUTPUT_BIT(D13, !!(out_data & 8));
OUTPUT_BIT(D12, !!(out_data & 4));
OUTPUT_BIT(D11, !!(out_data & 2));
OUTPUT_BIT(D10, !!(out_data & 1));
//delay_us(1); // same as above comment for write_c routine
OUTPUT_BIT(WRB,1);
OUTPUT_BIT(CSB,1);
} |
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|