|
|
View previous topic :: View next topic |
Author |
Message |
rxpu99
Joined: 23 Jun 2014 Posts: 10
|
output(x & 0xF0) destroys the whole direction pin config |
Posted: Sun Aug 07, 2016 7:53 am |
|
|
As display routine, I have the following code:
Code: |
byte const LEDPortB[12] = {
0b11110000,
0b10000000,
0b11010000,
0b11000000,
0b10100000,
0b01100000,
0b01110000,
0b11100000,
0b11110000,
0b11100000,
0b00000000
};
byte const LEDPortC[12] = {
0b01010000,
0b01000000,
0b10010000,
0b11010000,
0b11000000,
0b11010000,
0b11010000,
0b01000000,
0b11010000,
0b11010000,
0b00000000
};
void Display(int16 Column3value,int16 Column2Value,int16 Column1Value)
{
output_high(LED1);
output_b(LEDPortB[11] & 0xF0);
output_c(LEDPortC[11] & 0xF0);
output_b(LEDPortB[Column1value] & 0xF0);
output_c(LEDPortC[Column1value] & 0xF0);
output_low(LED1);
output_high(LED2);
output_b(LEDPortB[11] & 0xF0);
output_c(LEDPortC[11] & 0xF0);
output_b(LEDPortB[Column3value] & 0xF0);
output_c(LEDPortC[Column3value] & 0xF0);
output_low(LED2);
output_high(LED3);
output_b(LEDPortB[11] & 0xF0);
output_c(LEDPortC[11] & 0xF0);
output_b(LEDPortB[Column2value] & 0xF0);
output_c(LEDPortC[Column2value] & 0xF0);
output_low(LED3); ;
}
setup_ccp1(CCP_PWM_HALF_BRIDGE | CCP_PWM_H_H);
|
As seen from the code I plan to use the C5,C6,C7,C8 as the output to 7segment. I have defined a PWM on C2 as output. I want to use the C0 as input port, but the above code automatically assigns all ports of C as output and as expected I can not use the C0 as input.
I thought if I write
Code: | output_c(LEDPortC[Column2value] & 0xF0); |
The compiler should address only the high 4 bits and do not touch the lower ones. It is true that above command filters the above bits and send it to port C, BUT it assigns the whole C as output?, How can I assign the C0 as input in this manner?
I must find a similar command such as output_c() that partially modifies the port so that i can use my lookup table.
thanks in advance. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Sun Aug 07, 2016 10:45 am |
|
|
Basic PIC101...
you need to read the datasheet and the ccs manual, esp. the sections on PORTS. Especially how to use TRIS, fast_IO, and standard I/O.
Since you don't present you complete program we have no idea if you are using std or fast I/O. There's probably 100+ examples and threads about this on the site....
more reading is required.
Jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19495
|
|
Posted: Sun Aug 07, 2016 11:59 am |
|
|
As a comment, think about it. A '0' is just as much an output value, as a '1'. Just because you & the byte to be sent, with 0xF0, doesn't change it from being a byte. You are sending a whole byte out portD, just with the bottom 4 bits all set to zero....
Fast_io, is the normal way to do what you want, or do direct I/O, to the port latch. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
rxpu99
Joined: 23 Jun 2014 Posts: 10
|
|
Posted: Tue Aug 09, 2016 5:24 am |
|
|
Thank all of you for the tips. I found a solution with the command output_bit(port,bool value).
I changed my btye orşented output_port() to bit oriented output_bit(pin,value)
I select the bit using bitC7=((LEDPortC[Column1value] >> 7 ) & 1);
and send the bit to the output.
output_bit(PIN_C7,bitC7);
Is there a much simpler , shorter elegant way to do it?
Code: |
void Display(int16 Column3value,int16 Column2Value,int16 Column1Value)
{
output_high(LED1);
output_b(LEDPortB[11] & 0xF0);
// output_c(LEDPortC[11] & 0xF0);
output_b(LEDPortB[Column1value] & 0xF0);
bitC7=((LEDPortC[Column1value] >> 7 ) & 1);
bitC6=((LEDPortC[Column1value] >> 6 ) & 1);
bitC5=((LEDPortC[Column1value] >> 5 ) & 1);
bitC4=((LEDPortC[Column1value] >> 4) & 1);
output_bit(PIN_C7,bitC7);
output_bit(PIN_C6,bitC6);
output_bit(PIN_C5,bitC5);
output_bit(PIN_C4,bitC4);
output_low(LED1);
output_high(LED2);
output_b(LEDPortB[11] & 0xF0);
output_b(LEDPortB[Column3value] & 0xF0);
bitC7=((LEDPortC[Column3value] >> 7 ) & 1);
bitC6=((LEDPortC[Column3value] >> 6 ) & 1);
bitC5=((LEDPortC[Column3value] >> 5 ) & 1);
bitC4=((LEDPortC[Column3value] >> 4) & 1);
output_bit(PIN_C7,bitC7);
output_bit(PIN_C6,bitC6);
output_bit(PIN_C5,bitC5);
output_bit(PIN_C4,bitC4);
output_low(LED2);
output_high(LED3);
output_b(LEDPortB[11] & 0xF0);
output_b(LEDPortB[Column2value] & 0xF0);
bitC7=((LEDPortC[Column2value] >> 7 ) & 1);
bitC6=((LEDPortC[Column2value] >> 6 ) & 1);
bitC5=((LEDPortC[Column2value] >> 5 ) & 1);
bitC4=((LEDPortC[Column2value] >> 4) & 1);
output_bit(PIN_C7,bitC7);
output_bit(PIN_C6,bitC6);
output_bit(PIN_C5,bitC5);
output_bit(PIN_C4,bitC4);
output_low(LED3); ;
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19495
|
|
Posted: Tue Aug 09, 2016 7:28 am |
|
|
Depends whether any other bits are set as output in the port.
If so, then you have to use 'bit by bit'. or these pins will be changed. Understand the PIC itself only has bitwise or bytewise output instructions.
However the code can be a lot simpler/faster. Using rotations is silly.
Code: |
// bitC7=((LEDPortC[Column1value] >> 7 ) & 1);
// bitC6=((LEDPortC[Column1value] >> 6 ) & 1);
// bitC5=((LEDPortC[Column1value] >> 5 ) & 1);
// bitC4=((LEDPortC[Column1value] >> 4) & 1);
output_bit(PIN_C7, Column1Value&0x80 !=0);
output_bit(PIN_C6, Column1Value&0x40 !=0);
output_bit(PIN_C5, Column1Value&0x20 !=0);
output_bit(PIN_C4, Column1Value&0x10 !=0);
|
Compare the code size, and the time needed for this.....
Then don't repeat all this, make it a function. So:
Code: |
void nibble_to_port(int8 value)
{
output_bit(PIN_C7, value&0x80 !=0);
output_bit(PIN_C6, value&0x40 !=0);
output_bit(PIN_C5, value&0x20 !=0);
output_bit(PIN_C4, value&0x10 !=0);
}
void Display(int16 Column3value,int16 Column2Value,int16 Column1Value)
{
output_high(LED1);
output_b(LEDPortB[11] & 0xF0);
//Puzzled by this, your very next instruction changes what is output
//Are you sure this is wanted....
output_b(LEDPortB[Column1value] & 0xF0);
//This overrides the instruction before.....
nibble_to_port(Column1Value);
output_low(LED1);
etc...
|
There are lots of strange things though. You output Column3value, with LED2 selected, and Column2value with LED3. You put two successive values out port B in each part, without latching it or any delay. The second will override the first only a few uSec after it is output. |
|
|
guy
Joined: 21 Oct 2005 Posts: 297
|
|
Posted: Sun Aug 14, 2016 11:21 pm |
|
|
Instead of Code: | void nibble_to_port(int8 value)
{
output_bit(PIN_C7, value&0x80 !=0);
output_bit(PIN_C6, value&0x40 !=0);
output_bit(PIN_C5, value&0x20 !=0);
output_bit(PIN_C4, value&0x10 !=0);
} |
I would use Code: | void nibble_to_port(int8 value)
{
output_bit(PIN_C7, bit_test(value,7));
output_bit(PIN_C6, bit_test(value,6));
output_bit(PIN_C5, bit_test(value,5));
output_bit(PIN_C4, bit_test(value,4));
} |
I believe it is better optimized and definitely more readable and 'the CCS way' of doing things. |
|
|
|
|
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
|