View previous topic :: View next topic |
Author |
Message |
YeyoPIC
Joined: 13 Dec 2012 Posts: 2
|
Modbus address selected with dip switches |
Posted: Thu Dec 13, 2012 4:48 am |
|
|
Hi all, this is my first post here and I want to know how can I make preprocessors directives after of dip switches read.
For example, I am using modbus.c library for some test and I want to read a dip switch connected to portb to assign the modbus address and baudrate. I will take ex_modbus_slave.c, something like this...
Code: |
...
#define MODBUS_BUFFER_SIZE 32
#define MODBUS_ADDRESS 1 //here I want to asign the dip switch value
#define MODBUS_SERIAL_BAUD //and here too
...
|
If I do this could change the address without rewrite the Pic with each address.
How I can do that?
Thanks in advance. |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1635 Location: Perth, Australia
|
Re: Modbus address selected with dip switches |
Posted: Thu Dec 13, 2012 6:03 am |
|
|
YeyoPIC wrote: | Hi all, this is my first post here and I want to know how can I make preprocessors directives after of dip switches read.
For example, I am using modbus.c library for some test and I want to read a dip switch connected to portb to assign the modbus address and baudrate. I will take ex_modbus_slave.c, something like this...
Code: |
...
#define MODBUS_BUFFER_SIZE 32
#define MODBUS_ADDRESS 1 //here I want to asign the dip switch value
#define MODBUS_SERIAL_BAUD //and here too
...
|
If I do this could change the address without rewrite the Pic with each address.
How I can do that?
Thanks in advance. |
If you think a bit more about what you are asking you would realise it is a silly question.
You are asking how a compile time directive can be changed based on the reading of a DIP switch setting at run time. Unless you have implemented in your code, some form of communication system for passing data through worm holes traversing parallel universes, it is not possible.
Instead should be asking yourself, how can I populate a variable at run time from the setting of DIP switches than can then be used to configure the modbus address. _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
YeyoPIC
Joined: 13 Dec 2012 Posts: 2
|
|
Posted: Thu Dec 13, 2012 6:23 am |
|
|
Sorry, I'm wrong.
My question is exactly that "how can I populate a variable at run time from the setting of DIP switches than can then be used to configure the modbus address."
Do you know how I can do that?.
Thanks in advance. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9241 Location: Greensville,Ontario
|
|
Posted: Thu Dec 13, 2012 6:50 am |
|
|
comment..
a quick look in the 'examples' folder that CCS supplies shows several programs that do what you want.
One, I think named ' patg.c' should have the code snippet you need.
If it's not that program, check some of the others....or read the FAQ section of the onscreen Help files(press F11 when project is open).
All you're doing is reading an I/O port or part of one, then writing the data into a variable(RAM).
Obviously it's up to you as to which I/O port, pins,data manipulation,etc. as it is your program not mine.
hth
jay |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Dec 13, 2012 1:49 pm |
|
|
Quote: | how can I populate a variable at run time from the setting of DIP switches
|
Connect the 8-position DIP switch to an i/o port on the PIC.
Each individual DIP switch circuit should look like this:
Code: |
+5v
|
<
> 4.7K
< ___ Switch
To | _|_|_
PIC -----------------o o------
pin |
--- GND
-
|
You can use a larger resistor value. Instead of an external pull-up
resistor, you can enable the built-in pullup resistors on PortB (if the DIP
switch is connected to PortB).
Assuming you're using PortB with the internal pull-ups, the following
code will read the dip switches when the PIC first starts running:
Code: |
void main()
{
int8 dipsw_value;
port_b_pullups(0xFF);
delay_ms(1);
dipsw_value = input_b(); // Read dip switches
// Do something here.
while(1);
} |
|
|
|
|