Samplecode

From Sprut Usb4All Framework
Jump to: navigation, search

Contents

Connect to the the Usb4All device

RS232 device

IUsb4AllDevice device = Sprut.Usb4All.DeviceManager.GetRS232Device("COM1");

First USB device

IUsb4AllDevice device = Sprut.Usb4All.DeviceManager.GetUsbDevice(0);

All connected USB devices

  int n = DeviceManager.FindUsbDevices();
  for (int i = 0; i < n; i++)
  {
      DeviceManager.GetUsbDevice(i);
  }
  var allUsbDevices = DeviceManager.ActiveDevices(DeviceFilter.USB);


A simulator device

IUsb4AllDevice device = Sprut.Usb4All.DeviceManager.GetSimulator();


Subsystems

ADC

void Adc_Sample(IUsb4AllDevice device)
{
    var adc = (IAdc)device[Subsystem.Adc];
    adc.Setup(1, 1);    //internal ref.voltage and 1 channel
    adc.SwitchOn();     
    var rawValue = adc.Measure(AnalogPort.AN0);
}


Counter

void Counter_Sample(IUsb4AllDevice device)
{
    var counter = (ICounter)device[Subsystem.Counter0];
    counter.SwitchOn();
    counter.Value = 0;  //reset or preset the counter value
    int n = counter.Value;
}


Counter

void Counter_Sample(IUsb4AllDevice device)
{
    var counter = (ICounter)device[Subsystem.Counter0];
    counter.SwitchOn();
    counter.Value = 0;  //reset or preset the counter value
    int n = counter.Value;
}


FrqCounter

void FrqCounter_Sample(IUsb4AllDevice device)
{
    var frq = (IFrqCounter)device[Subsystem.FrqCounter];
    frq.Setup();
    frq.SwitchOn();
    var hz = frq.Measure();
}


I2C

void I2C_Sample(IUsb4AllDevice device)
{
    var pcf8574 = 0x40; //Baseaddress of the Portexpander
    pcf8574 >>= 1;      //remove the r/w bit
    pcf8574 += 1;       //configured address (A0=1 A1=0 A2=0) 
 
    var i2c = (II2C)device[Subsystem.I2C];
    i2c.Setup(400);   //Busspeed is 400 kHz
    i2c.SwitchOn();
    var x = i2c.ReadFrom7BitAddress(pcf8574);
    if (i2c.TransmitStatus != I2CTransmitStatus.OK)
    {
        //error handling ...
    }
}


L297 StepperMotor

void StepperMotor_Sample(IUsb4AllDevice device)
{
    var stepperMotor = (IL297StepperMotor)device[Subsystem.L297StepperMotor1];
    stepperMotor.Setup(false);       //async-mode
    stepperMotor.SwitchOn();
    stepperMotor.TrunLeft(500, 20);  //turn 500 steps left for 20ms
}

LCD

void LCD_Sample(IUsb4AllDevice device)
{
    var lcd = (ILcd)device[Subsystem.Lcd1];
    lcd.Setup(4, 20);  //LCD size = 4x20
    lcd.SwitchOn();
    lcd.Clear();
    lcd.WriteData("Hello World");
}


MWire

void MWire_Sample(IUsb4AllDevice device)
{
    var mwire = (IMicrowire)device[Subsystem.Microwire];
    mwire.Setup(750, false);  //Busspeed is 750 kHz  and sync-mode
    mwire.SwitchOn();
    mwire.Send(0x22);
    var x = mwire.Read(3);
}


PWM

void Pwm_Sample(IUsb4AllDevice device)
{
    var pwm = (IPwm)device[Subsystem.Pwm1];
    pwm.Setup(PWM_Mode.Steps_256_Freq_187kHz);
    pwm.SwitchOn();
    pwm.Value = 6;
}

RS232

void Rs232_Sample(IUsb4AllDevice device)
{
    var rs232 = (IRs232)device[Subsystem.Rs232];
    rs232.Setup(9600, true);    //9600 Baud and an connected external driver
    rs232.SwitchOn();
    rs232.Send("Hello World");
}


ShiftRegister

void ShiftRegister_Sample(IUsb4AllDevice device)
{
    var shiftreg = (IShiftRegister)device[Subsystem.ShiftRegister];
    shiftreg.Setup(50, true, true, true); //50Hz, transmit on rising clock, idlelevel is low, sample at end of transmission
    shiftreg.SwitchOn();
    byte[] dataOut = { 0x3, 0x4, 0x7 };
    var dataIn = shiftreg.TransmitAndReceive(dataOut);
}


TTL

void TTL_Sample(IUsb4AllDevice device)
{
    var ttl = (ITtl)device[Subsystem.TTL_IO];
    ttl.OutputPort['A'] = 0xff;     //all Pins of PortA as output
    ttl.Setup(false);               //disable pull-ups
    ttl.SwitchOn();                 //set Portdirections and pullup 
 
    ttl.Port['A'] = 0x0f;           //write to Portbuffer
    ttl.Write();                    //write all Portbuffers to the device
 
    ttl.Read();                     //read all pinlevels into the Portbuffers
    var portB = ttl.Port['B'];      //read pinlevel from PortB buffer
}


ServoChannel

void ServoChannel_Sample(IUsb4AllDevice device)
{
    var servoChannel = (IServoChannel)device[Subsystem.ServoChannelB];
    servoChannel.Setup(1, 50);           //Servo on Port RB1 to position 50
    servoChannel.Setup(2, 90);           //Servo on Port RB2 to position 90
    servoChannel.SetMiddlePosition(60);  //adjust middleposition for all servos
    servoChannel.SwitchOn();
 
    servoChannel[1] = 100;               //Servo on Port RB1 to position 100
}


SPI

public void Setup_Master_and_read_from_Slave()
{
    var spi = (ISpi)base.Device[Subsystem.Spi];
    spi.Setup(1, Spi_Mode.Master_at_750kHz, true, true, true);
    spi.SwitchOn();
    spi.Send(1, 0xFF, false);
    var x = spi.Read(1, false);
    spi.SwitchOff();
    Console.WriteLine("\naddr: {0} read value: {1} \n", 1, x);
}


StepperMotor

void StepperMotor _Sample(IUsb4AllDevice device)
{
    var stepper1 = (IStepperMotor)device[Subsystem.StepperMotor1];
    stepper1.Setup();
    stepper1.SwitchOn();
    stepper1.TrunLeft(20, 15);
}