CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Pass a variable to Output_High ?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
laser47
Guest







Pass a variable to Output_High ?
PostPosted: Sat May 17, 2003 2:33 am     Reply with quote

I'm trying to set up a sequencer where I have an array of events, and wat to pass a variable to Output_High/Output_Low. I've got an array of a structure that has time, pin, and high/low. I'm trying something like:

if (EventArray[CurrentEvent].Value = 1)
Output_High (EventArray[CurrentEvent].Pin);
else
Output_Low (EventArray[CurrentEvent].Pin);

the .pin element is equated to PIN_B5, but the compiler complains with "Must Equate to a Constant"

I could accomplish this with:

if (EventArray[CurrentEvent].Pin = PIN_B5)
if (EventArray[CurrentEvent].Value = 1)
Output_High (PIN_B5);
else
Output_Low (PIN_B5);
else if (EventArray[CurrentEvent].Pin = PIN_B6)
if (EventArray[CurrentEvent].Value = 1)
Output_High (PIN_B6);
else
Output_Low (PIN_B6);
.... Etc...

This seems counter-intuative, and very poor for expandability. Any suggestions?

Thanx!

Laser
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514521
R.J.Hamlett
Guest







Re: Pass a variable to Output_High ?
PostPosted: Mon May 19, 2003 5:20 am     Reply with quote

:=I'm trying to set up a sequencer where I have an array of events, and wat to pass a variable to Output_High/Output_Low. I've got an array of a structure that has time, pin, and high/low. I'm trying something like:
:=
:=if (EventArray[CurrentEvent].Value = 1)
:= Output_High (EventArray[CurrentEvent].Pin);
:=else
:= Output_Low (EventArray[CurrentEvent].Pin);
:=
:=the .pin element is equated to PIN_B5, but the compiler complains with "Must Equate to a Constant"
:=
:=I could accomplish this with:
:=
:=if (EventArray[CurrentEvent].Pin = PIN_B5)
:= if (EventArray[CurrentEvent].Value = 1)
:= Output_High (PIN_B5);
:= else
:= Output_Low (PIN_B5);
:=else if (EventArray[CurrentEvent].Pin = PIN_B6)
:= if (EventArray[CurrentEvent].Value = 1)
:= Output_High (PIN_B6);
:= else
:= Output_Low (PIN_B6);
:=.... Etc...
:=
:=This seems counter-intuative, and very poor for expandability. Any suggestions?
:=
:=Thanx!
:=
:=Laser
These functions require constants that are present at compile time. They do not support dynamic modification of the bit to be changed, using a variable. However, there are a lot of other ways you can approach this.
For instance, deal with the port as a byte (using the output_b function), and maintain your own byte mask, and output this. to the byte. You can then use the standard bit set/reset commands on your byte.
You can also use the bit commands directly on the port itself (but with the normal caveats about read-modify-write operations, and assuming you are using 'fast_io').
So something like:

#byte PORTB=0x106 //This will depend on the chip involved.

if (EventArray[CurrentEvent].Value)
//Warning, your code here would not work, '=' means to set
//a value. '==' is the test function...
bit_set(PORTB,EventArray[CurrentEvent].Pin);
else
bit_clear(PORTB,EventArray[CurrentEvent].Pin);

With the .Pin values being from 0 to 7.

Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514545
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: Pass a variable to Output_High ?
PostPosted: Mon May 19, 2003 12:31 pm     Reply with quote

Here is a general purpose bit setting function.
The problem is that if the compiler knows the bit
value at compile-time, it can generate really efficient
code. But if it can only learn the bit value at run-time,
then it's not very efficient. If you look at the
.LST file, there's a lot of code, and it's going
to be slow. I don't know if that's an issue for you.

This issue is heavily discussed on a Piclist page:
<a href="http://www.piclist.com/techref/microchip/math/bit/setbit.htm" TARGET="_blank">http://www.piclist.com/techref/microchip/math/bit/setbit.htm</a>
CCS appears to use a method of generating the bitmask
that's somewhat similar to Kevin Blain's code.

<PRE>
#include "c:\Program Files\Picc\Devices\16F877.H"
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 8000000)
#use rs232(baud = 9600, xmit=PIN_C6, rcv = PIN_C7, ERRORS)
<BR>
#define PORT_B 6 // Note this is a define, not a #byte statement
<BR>
void set_port_bit(char *port, char bit, char value);
<BR>
//=====================================
void main()
{
char port;
char bit;
char value;
<BR>
// Define the pin as an output and initialize
// it to a low level. (Or, do this with a
// set_tris_b() statement, etc.).
output_low(PIN_B7);
<BR>
// Initialize the port and bit values for this test.
port = PORT_B;
bit = 7;
<BR>
// Toggle the specified pin. Watch it on the oscilloscope.
while(1)
{
value = 1;
set_port_bit(port, bit, value);
delay_us(500);
<BR>
value = 0;
set_port_bit(port, bit, value);
delay_us(500);
}
<BR>
}
<BR>
//========================================================
// This function will set a specified bit (0-7) of an i/o
// port to the specified value (0 or 1). The port number
// must be 5 for Port A, 6 for Port B, etc. (Per the data
// sheet for a 16F877).
void set_port_bit(char *port, char bit, char value)
{
if(value)
bit_set(*port, bit);
else
bit_clear(*port, bit);
}
</PRE>
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514555
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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