| Author |
Message |
jthomsonmain Ardent Poster

Joined: 11 Jan 2008 Posts: 80 Location: Albion, NY (USA)
|
Posted: Fri Jan 11, 2008 5:29 pm Post subject: My C Interfacing Program |
|
|
| Code: |
# include <stdio.h>
# include <stdlib.h>
# include <windows.h>
# include <Rcode.h>
# include <Labpac32.h>
# include <string.h>
# define OUTPUT 0
# define INPUT 3
# define BOX1_CON1_PORTA 0
# define BOX1_CON1_PORTB 1
# define BOX1_CON1_PORTC 2
# define BOX1_CON2_PORTA 3
# define BOX1_CON2_PORTB 4
# define BOX1_CON2_PORTC 5
# define BOX1_CON3_PORTA 6
# define BOX1_CON3_PORTB 7
# define BOX1_CON3_PORTC 8
# define BOX1_CON4_PORTA 9
# define BOX1_CON4_PORTB 10
# define BOX1_CON4_PORTC 11
# define BOX2_CON1_PORTA 12
# define BOX2_CON1_PORTB 13
# define BOX2_CON1_PORTC 14
# define BOX2_CON2_PORTA 15
# define BOX2_CON2_PORTB 16
# define BOX2_CON2_PORTC 17
# define BOX2_CON3_PORTA 18
# define BOX2_CON3_PORTB 19
# define BOX2_CON3_PORTC 20
# define BOX2_CON4_PORTA 21
# define BOX2_CON4_PORTB 22
# define BOX2_CON4_PORTC 23
# define BOX3_CON1_PORTA 24
# define BOX3_CON1_PORTB 25
# define BOX3_CON1_PORTC 26
# define BOX3_CON2_PORTA 27
# define BOX3_CON2_PORTB 28
# define BOX3_CON2_PORTC 29
# define BOX3_CON3_PORTA 30
# define BOX3_CON3_PORTB 31
# define BOX3_CON3_PORTC 32
# define BOX3_CON4_PORTA 33
# define BOX3_CON4_PORTB 34
# define BOX3_CON4_PORTC 35
# define BOX4_CON1_PORTA 36
# define BOX4_CON1_PORTB 37
# define BOX4_CON1_PORTC 38
# define BOX4_CON2_PORTA 39
# define BOX4_CON2_PORTB 40
# define BOX4_CON2_PORTC 41
# define BOX4_CON3_PORTA 42
# define BOX4_CON3_PORTB 43
# define BOX4_CON3_PORTC 44
# define BOX4_CON4_PORTA 45
# define BOX4_CON4_PORTB 46
# define BOX4_CON4_PORTC 47
int main()
{
ULONG DevNum;
int box,
con,
port,
port_num;
int run = 0;
unsigned char data = 0xFF;
LabPac_AddDev("pbbd0", &DevNum);
LabPac_DioSetConfig(DevNum, BOX1_CON1_PORTA, OUTPUT);
LabPac_DioSetConfig(DevNum, BOX1_CON1_PORTB, INPUT);
LabPac_DioSetConfig(DevNum, BOX1_CON1_PORTC, OUTPUT); //Set I/O
LabPac_DoRAW(DevNum, BOX1_CON1_PORTA, 0xFF);
LabPac_DoRAW(DevNum, BOX1_CON1_PORTC, 0xFF); //Turn everything off
do
{
LabPac_DoRAW(DevNum, BOX1_CON1_PORTA, 0xFF);
LabPac_DoRAW(DevNum, BOX1_CON1_PORTC, 0xFF);
data = (unsigned char) LabPac_DiRAW(DevNum, BOX1_CON1_PORTB);
if(data == 0xD6)
{
LabPac_DoRAW(DevNum, BOX1_CON1_PORTA, 0xF8);
data = (unsigned char) LabPac_DiRAW(DevNum, BOX1_CON1_PORTB);
while(data != 0xD6)
{
sleep(100);
printf(".");
data = (unsigned char) LabPac_DiRAW(DevNum, BOX1_CON1_PORTB);
}
}
if(data == 0xD9)
{
LabPac_DoRAW(DevNum, BOX1_CON1_PORTA, 0xC7);
data = (unsigned char) LabPac_DiRAW(DevNum, BOX1_CON1_PORTB);
while(data == 0xD9)
{
sleep(100);
printf(".");
data = (unsigned char) LabPac_DiRAW(DevNum, BOX1_CON1_PORTB);
}
}
if(data == 0xE5)
{
LabPac_DoRAW(DevNum, BOX1_CON1_PORTC, 0xDF);
data = (unsigned char) LabPac_DiRAW(DevNum, BOX1_CON1_PORTB);
while(data == 0xE5)
{
sleep(100);
printf(".");
data = (unsigned char) LabPac_DiRAW(DevNum, BOX1_CON1_PORTB);
}
}
}
while(run == 0);
return(0);
}
|
I made this program from scratch. It uses the Scientific Solutions Interfacing PCI Card and Ctrl box to operate a test box for animals. Works well, and is just testing out the features. Hard part was decoding the cryptic directions that came with the SciSol product! _________________ [img:8728bad64c]http://i212.photobucket.com/albums/cc118/jthomsonmain/l2psig.jpg[/img:8728bad64c] |
|
| Back to top |
|
| |
LP-SolidRaven Dictator of the Dump

Joined: 06 Jun 2004 Posts: 7182 Location: The cheese is made out of moon
|
Posted: Fri Jan 11, 2008 6:03 pm Post subject: |
|
|
It would be interesting if you'd explain it a bit more and give a reason for posting it :S _________________
| Quote: |
<bart416> I just realized something
<bart416> we celebrate the fact that this piece of rock made one rotation around a glowing ball of plasma that is kept together due to its own gravity well
<njsg> HAPPY NEW YEAR
<Easter> ^^
|
|
|
| Back to top |
|
| |
jthomsonmain Ardent Poster

Joined: 11 Jan 2008 Posts: 80 Location: Albion, NY (USA)
|
Posted: Sat Jan 12, 2008 6:21 pm Post subject: |
|
|
| SolidRaven wrote: | | It would be interesting if you'd explain it a bit more and give a reason for posting it :S |
Eh, just sharing it with the world. You should see my old programs, I had comments EVERYWHERE!!! Then one day, I tried to edit one of those commented programs. Its a pain in the butt to try to edit around the comments, so I just did away with them. Oh well, because I am lifeless now, here is a description of each line of the program:
| Code: |
# include <stdio.h>
# include <stdlib.h>
# include <windows.h>
# include <Rcode.h>
# include <Labpac32.h>
# include <string.h>
|
These are the C "include" commands. These import premade librarys of code, and each included files has a command in it that is used by the program. stdio.h has the printf command, stdlib.h has the system command, and so on.
| Code: |
# define OUTPUT 0
# define INPUT 3
# define BOX1_CON1_PORTA 0
# define BOX1_CON1_PORTB 1
# define BOX1_CON1_PORTC 2
# define BOX1_CON2_PORTA 3
# define BOX1_CON2_PORTB 4
# define BOX1_CON2_PORTC 5
# define BOX1_CON3_PORTA 6
# define BOX1_CON3_PORTB 7
# define BOX1_CON3_PORTC 8
# define BOX1_CON4_PORTA 9
# define BOX1_CON4_PORTB 10
# define BOX1_CON4_PORTC 11
# define BOX2_CON1_PORTA 12
# define BOX2_CON1_PORTB 13
# define BOX2_CON1_PORTC 14
# define BOX2_CON2_PORTA 15
# define BOX2_CON2_PORTB 16
# define BOX2_CON2_PORTC 17
# define BOX2_CON3_PORTA 18
# define BOX2_CON3_PORTB 19
# define BOX2_CON3_PORTC 20
# define BOX2_CON4_PORTA 21
# define BOX2_CON4_PORTB 22
# define BOX2_CON4_PORTC 23
# define BOX3_CON1_PORTA 24
# define BOX3_CON1_PORTB 25
# define BOX3_CON1_PORTC 26
# define BOX3_CON2_PORTA 27
# define BOX3_CON2_PORTB 28
# define BOX3_CON2_PORTC 29
# define BOX3_CON3_PORTA 30
# define BOX3_CON3_PORTB 31
# define BOX3_CON3_PORTC 32
# define BOX3_CON4_PORTA 33
# define BOX3_CON4_PORTB 34
# define BOX3_CON4_PORTC 35
# define BOX4_CON1_PORTA 36
# define BOX4_CON1_PORTB 37
# define BOX4_CON1_PORTC 38
# define BOX4_CON2_PORTA 39
# define BOX4_CON2_PORTB 40
# define BOX4_CON2_PORTC 41
# define BOX4_CON3_PORTA 42
# define BOX4_CON3_PORTB 43
# define BOX4_CON3_PORTC 44
# define BOX4_CON4_PORTA 45
# define BOX4_CON4_PORTB 46
# define BOX4_CON4_PORTC 47
|
These are all definitions for custom codes. Within the LabPac code library, for example, the number 47 is referring to Box # 4, Connector 4, Port C. Instead of going to a chart every time I want to turn a port on, these definitions allow me to say BOX4_CON4_PORTC Example from the program:
Instead of saying:
| Code: |
LabPac_DioSetConfig(DevNum, 0);
|
I can say:
| Code: |
LabPac_DioSetConfig(DevNum, BOX1_CON1_PORTA, OUTPUT);
|
This bit of code is saying to the computer "Look here, this is where you start doing something that the user can see". Basically the start of the program.
| Code: |
ULONG DevNum;
int box,
con,
port,
port_num;
int run = 0;
unsigned char data = 0xFF;
|
When working with variables, you have to declare them, and then initialize them. Declaring is like saying "Here is a variable named ____". Initializing a variable is like saying "5 is the value of the Variable ____".
| Code: |
ULONG DevNum;
int box,
con,
port,
port_num;
|
Those are Declarations. The code is Declaring variables of the Integer and the Unsigned Long type, named DevNum, Box, Port, Con, and Port_num
| Code: |
int run = 0;
unsigned char data = 0xFF;
|
Those are both declarations and initializations. The code is declaring the variables and then giving them a value
| Code: |
LabPac_AddDev("pbbd0", &DevNum);
|
This segment of code is looking for the PCI card in the computer so that the program can assign the device a number (0) and interact with the card
| Code: |
LabPac_DioSetConfig(DevNum, BOX1_CON1_PORTA, OUTPUT);
LabPac_DioSetConfig(DevNum, BOX1_CON1_PORTB, INPUT);
LabPac_DioSetConfig(DevNum, BOX1_CON1_PORTC, OUTPUT);
|
In LabPac, there can be 4 Boxes hooked up to a PCI Card. Each box has 4 connectors consisting of 30 bits divided into A, B, and C with a ground bus. Each bit can either be an input or an output. This code is setting Box #1, Connector #1, Ports A and C as outputs and Port B as an input
| Code: |
LabPac_DoRAW(DevNum, BOX1_CON1_PORTA, 0xFF);
LabPac_DoRAW(DevNum, BOX1_CON1_PORTC, 0xFF);
|
This section of code is sending the bits data, in this case, telling them to turn off
| Code: |
do
{
LabPac_DoRAW(DevNum, BOX1_CON1_PORTA, 0xFF);
LabPac_DoRAW(DevNum, BOX1_CON1_PORTC, 0xFF);
data = (unsigned char) LabPac_DiRAW(DevNum, BOX1_CON1_PORTB);
if(data == 0xD6)
{
LabPac_DoRAW(DevNum, BOX1_CON1_PORTA, 0xF8);
data = (unsigned char) LabPac_DiRAW(DevNum, BOX1_CON1_PORTB);
while(data != 0xD6)
{
sleep(100);
printf(".");
data = (unsigned char) LabPac_DiRAW(DevNum, BOX1_CON1_PORTB);
}
}
if(data == 0xD9)
{
LabPac_DoRAW(DevNum, BOX1_CON1_PORTA, 0xC7);
data = (unsigned char) LabPac_DiRAW(DevNum, BOX1_CON1_PORTB);
while(data == 0xD9)
{
sleep(100);
printf(".");
data = (unsigned char) LabPac_DiRAW(DevNum, BOX1_CON1_PORTB);
}
}
if(data == 0xE5)
{
LabPac_DoRAW(DevNum, BOX1_CON1_PORTC, 0xDF);
data = (unsigned char) LabPac_DiRAW(DevNum, BOX1_CON1_PORTB);
while(data == 0xE5)
{
sleep(100);
printf(".");
data = (unsigned char) LabPac_DiRAW(DevNum, BOX1_CON1_PORTB);
}
}
}
while(run == 0);
|
This is where it gets confusing! Basically, the code above continually looks for an input, and when an input of the correct type is found, the program turns on an output. For this program with the current set up we have, it is looking for one of two switches to be pressed, and when that switch is pressed, it turns a light on for the duration that the switch is pressed, then turns it off when the switch is no longer being pressed.
This tells the computer that the program is done
Doesn't do anything overly impressive, but its just a small example of what the system can do.
Man, I must be lifeless  _________________ [img:8728bad64c]http://i212.photobucket.com/albums/cc118/jthomsonmain/l2psig.jpg[/img:8728bad64c] |
|
| Back to top |
|
| |
LP-SolidRaven Dictator of the Dump

Joined: 06 Jun 2004 Posts: 7182 Location: The cheese is made out of moon
|
Posted: Sun Jan 13, 2008 3:46 am Post subject: |
|
|
No need to explain C code to me, but I think you just dodged my question. What is exactly the point of the program :S
| Quote: | | It uses the Scientific Solutions Interfacing PCI Card and Ctrl box to operate a test box for animals |
Doesn't exactly explain a lot. _________________
| Quote: |
<bart416> I just realized something
<bart416> we celebrate the fact that this piece of rock made one rotation around a glowing ball of plasma that is kept together due to its own gravity well
<njsg> HAPPY NEW YEAR
<Easter> ^^
|
|
|
| Back to top |
|
| |
jthomsonmain Ardent Poster

Joined: 11 Jan 2008 Posts: 80 Location: Albion, NY (USA)
|
Posted: Sun Jan 13, 2008 3:01 pm Post subject: |
|
|
| SolidRaven wrote: | No need to explain C code to me, but I think you just dodged my question. What is exactly the point of the program :S
| Quote: | | It uses the Scientific Solutions Interfacing PCI Card and Ctrl box to operate a test box for animals |
Doesn't exactly explain a lot. |
Ok, misunderstood your question. In our lab, we have a computer with a Scientific Solutions PCI card in it. With this PCI card, we can communicate with 4 boxes. Each box has around 40 I/O ports. From these ports, we can run electronic devices and test for inputs from switches. This is basically a microprocessor, only you can interact with it directly from the computer without needing to get a programmer and upload a program to flash memory where the microprocessor can read it. What we are doing with with this is running test boxes for a Psychology class. Basically, you put a rat in this box. the box has 3 switches, a buzzer, 2 sets of cue lamps, a food dispenser, and a house light. We can then run a program that makes the box flash a cue lamp that tells the rat what switch to press, and when that switch is pressed, the box dispenses a feed pellet for the rat. Now I cant even begin to try to explain the psychological aspect, after all, I am just the programmer. _________________ [img:8728bad64c]http://i212.photobucket.com/albums/cc118/jthomsonmain/l2psig.jpg[/img:8728bad64c] |
|
| Back to top |
|
| |
|
|
|