EMBEDDED PROJECTS JOURNAL
100% frei und kostenlos,
als PDF und in gedruckter Form,
per Open Source Lizenz
AVR-RISC (Infos)
Embedded Software selbst entwickeln
(inkl. Absatz über USBprog)
AVR-Starterkit für 61,90 EUR
Quicklinks: Home | Online Firmware Pool | Shop
     

SimplePort - 11 Input and output ports to operate and control by computer (in C, Java, Python, ...)

Benedikt Sauter, 20.08.2007 

With this Firmware the 11 outgoing ports can easily be used as input and output ports. For this purpose, there is a small library in C, Python and Java that is applicable in Windows, Linux and if necessary in Mac.

For first tests you can use port 11 as output, the LED is connected to it! An example in C may look like this:

  struct simpleport * sp_handle;
  /* open connection to simpleport */
  sp_handle = simpleport_open();

 simpleport_set_pin_dir(sp_handle,11,1);

  for(i=0;i<4;i++){
    simpleport_set_pin(sp_handle,11,1); // LED on
    sleep(1);
    simpleport_set_pin(sp_handle,11,0); // LED off
    sleep(1);
  }
  simpleport_close(sp_handle);

In the download area there are examples that show you how the libraries can be used in the different program languages.

The serveral controls from the program languages such as Java and Python have been put into practice with SWIG. Further connections can be opened at any time without effort. For futher information click here

At this time, SWIG can open connections for the following program languages: Allegro CL, C#, Chicken, Guile, Java, Modula-3, Mzscheme, OCAML, Perl, PHP, Python, Ruby, Tcl.

State

  • tested under Linux with C, Java and Pyton
  • Windows test not done yet, but it should work

Downloads 

Pin configuration  

 signal port on usbprog
10  RX  TX  LED 
 SimplePort signals
 IO 1
VCC  IO 2  IO 3  IO 4  IO 5  IO 6  IO 7  IO 8  GND  IO 9  IO 10   IO 11

Library in C

Open connection to SimplePort:

struct simpleport* simpleport_open();

Close connection : 

void simpleport_close(struct simpleport *simpleport);

Define data direction of the signals (IO 1 - IO 8) 1 = output, 0 = input:

void simpleport_set_direction(struct simpleport *simpleport, unsigned char direction);

Bsp: IO 1 - IO 4 = Taster, und IO 5 - IO 8 = LED:
 IO 1
IO 2  IO 3 IO 4  IO 5
IO 6
IO 7
IO 8 
 0  0  0  0  0  1  1  1

(Hex: 0x0F)


With this function only the data directions for IO 1 - IO 8 can be declared! Those for IO 9 - IO 11 have to be declared individually with the function void simpleport_set_pin_dir(struct simpleport *simpleport,int pin, int dir).

Data direction of a single port (also IO 9 - IO 11) 1=output, 0=input:

void simpleport_set_pin_dir(struct simpleport *simpleport,int pin, int dir);

Outputting port (IO 1 - IO 8):

 void simpleport_set_port(struct simpleport *simpleport,unsigned char value);

Reading port (IO 1 - IO 8): 

unsigned char simpleport_get_port(struct simpleport *simpleport);

Setting a single port (IO 1 - IO 11):

void simpleport_set_pin(struct simpleport *simpleport,int pin, int value);

Reading a single port (IO 1 - IO 11):

int simpleport_get_pin(struct simpleport *simpleport, int pin);


Example in C

#include <stdio.h>
#include "simpleport.h"

int main()
{
struct simpleport * sp_handle;
/* open connection to simpleport */
sp_handle = simpleport_open();

if(sp_handle==0)
  fprintf(stderr,"unable to open device\n");


simpleport_set_direction(sp_handle,0xFF);

while(1){
  simpleport_set_port(sp_handle,0xFF);
  simpleport_set_port(sp_handle,0x00);
}
simpleport_close(sp_handle);
return 0;
}

Example in Java 

class demo
{
  public static void main(String[] args){
    try {
      // tell the system to load the shared library into memory
      System.load("/lib/_simpleport.so");
      // the functions of '_simpleport.so' are accessed over the java-class
      // 'simpleport', that was created by SWIG.
    // 'simpleport_open()' returns a instance of 'SWIGTYPE_p_simpleport' if
    // a suitable hardware was found.

      SWIGTYPE_p_simpleport sp_handle = simpleport.simpleport_open();
    // set the port-direction to 'write'
    simpleport.simpleport_set_direction(sp_handle, (short) 0xFF);
      System.out.println("... blink!");

      // periodically set entire port to '00000000' and '11111111'

      while(true){
        simpleport.simpleport_set_port(sp_handle,(short) 0xFF,(short) 0xFF);
        Thread.sleep(1000);
        simpleport.simpleport_set_port(sp_handle, (short) 0x00, (short) 0xFF);
        Thread.sleep(1000);
      }
    } catch (Exception e) {
      e.toString();
    }
  }
}

Example in Python 

import simpleport
import time

if __name__ == "__main__":
    # call simpleport_open() to retrive a handle
    sp_handle = simpleport.simpleport_open()

    # periodacally set entire port to '11111111' and '00000000'
    while 1:
        simpleport.simpleport_set_port(sp_handle, 0xFF, 0xFF)
        time.sleep(1)
        simpleport.simpleport_set_port(sp_handle, 0x00, 0xFF)
        time.sleep(1)

    # close handle (never reached in this case)
    simpleport.simpleport_close(sphand)

 © 2007 by Embedded Projects, Benedikt Sauter