|
|
View previous topic :: View next topic |
Author |
Message |
Pete Smith
Joined: 17 Sep 2003 Posts: 55 Location: Chester, UK
|
Using input() with a variable |
Posted: Tue Nov 11, 2003 6:33 am |
|
|
Hi all.
Got a question. I'm sure it's been done before, but the new "Search" facility isn't as good as the old one...
In order to save space, I'm wanting to loop a sequence of instructions...
Code: |
my_write_eeprom(LOC_SENSOR0_ANALOGUE,read_sensor_raw(0));
my_write_eeprom(LOC_BOOLEANSTATUS_0,read_sensor_digital(0));
my_write_eeprom(LOC_SENSOR1_ANALOGUE,read_sensor_raw(1));
my_write_eeprom(LOC_BOOLEANSTATUS_1,read_sensor_digital(1));
my_write_eeprom(LOC_SENSOR2_ANALOGUE,read_sensor_raw(2));
my_write_eeprom(LOC_BOOLEANSTATUS_2,read_sensor_digital(2));
my_write_eeprom(LOC_SENSOR3_ANALOGUE,read_sensor_raw(3));
my_write_eeprom(LOC_BOOLEANSTATUS_3,read_sensor_digital(3)); |
I've set up arrays, containing the addresses for the section above. This all works fine, and has saved quite a bit of space.
Code: |
my_write_eeprom(analogue_storage[temp],read_sensor_raw(temp));
my_write_eeprom(boolean_storage[temp],read_sensor_digital(temp)); |
I now want to do the same for this...
Code: | my_write_eeprom(LOC_CURRENT_DIG_0_RESULT,input(PIN_DIGITAL_SENSOR_0));
my_write_eeprom(LOC_CURRENT_DIG_1_RESULT,input(PIN_DIGITAL_SENSOR_1));
my_write_eeprom(LOC_CURRENT_DIG_2_RESULT,input(PIN_DIGITAL_SENSOR_2));
my_write_eeprom(LOC_CURRENT_DIG_3_RESULT,input(PIN_DIGITAL_SENSOR_3));
|
But I can't store the PIN_DIGITAL... in an array. These are defined as various inputs, eg PIN_B4, PIN_C2. They're not fixed, and not sequential - this is very generic code.
Code: |
my_write_eeprom(digital_storage[temp],input(digital_source[temp])); |
Does anyone know a way of passing a variable to the input() function, or possibly by-passing it, but still be able to refer to it as PIN_XX.
Many thanks,
Pete. |
|
|
DragonPIC
Joined: 11 Nov 2003 Posts: 118
|
Constant |
Posted: Tue Nov 11, 2003 4:00 pm |
|
|
I think the pin needs to be constant in input(pin).
you could make your own function:
Code: | int pin_var; //0=0x01 1=0x02 2=0x04......
int port_var;
short bit;
#byte port_b = 6
#byte port_c = 7
main()
{
port_var = 6; //port b
pin_var = 0x04; //pin 2
bit = input_pin(port_var, pin_var);
}
short input_pin(int port,int pin)
{
if(port == 6)
{
if((port_b & pin) > 1)
return(1);
else
return(0);
}
if(port == 7)
{
if((port_c & pin) > 1)
return(1);
else
return(0);
}
} |
Did this help?
You could also think about using a variable pointer to point to the ports.
Matt |
|
|
Pete Smith
Joined: 17 Sep 2003 Posts: 55 Location: Chester, UK
|
Re: Constant |
Posted: Tue Nov 11, 2003 4:31 pm |
|
|
DragonPIC wrote: | I think the pin needs to be constant in input(pin).
you could make your own function:
Did this help?
You could also think about using a variable pointer to point to the ports.
Matt |
Thanks for the suggestion.
Unfortunately, to keep my device setup as simple as possible, I want to keep code space down to a minimum, and setup down to a minimum.
FWIW, the code snippet you supplied is already provided by CCS. It's the bit_test(port,bit); function.
I could do this, but I'd then need to set up each pin twice, e.g.
#define digital_input PIN_B4
#define digital_input_port port_b
#define digital_input_bit 4
If I then changed one input, I'd have to remember to change all the references. This doesn't seem a very neat way of doing it.
Another option would be to reverse-engineer the pin ID from the number, by keeping a record of what ID belongs to which pin. I could then work out the port register and bit from this, but this code would probably end up being bigger than the original code it was trying to replace!
In the end, I just left it as it was! I managed to save code space in other areas.
All I now need to do is get my X10 functions down (currently running at ~500 bytes).
I could always just go up to a bigger PIC ;-)
Thanks anyway,
Pete. |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
|
Posted: Wed Nov 12, 2003 10:25 am |
|
|
#define Port_B 0xF81 // PORT B DIRECT MEMORY ADDRESS LABLE
#bit PIN_CONFIG_S1 = Port_B.0
#bit PIN_CONFIG_S2 = Port_B.1
#bit PIN_CONFIG_S3 = Port_B.2
#bit PIN_CONFIG_S4 = Port_B.3
#bit PIN_CONFIG_S5 = Port_B.4
#bit PIN_EXE_PB = Port_B.5
then in code operate on bits like this
Code: |
if(PIN_EXE_PB)
{ PIN_CONFIG_S5 =0;
}
else
{ PIN_CONFIG_S5 =1;
}
|
The assembly code generated will be minimal. |
|
|
|
|
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
|