Wednesday, July 13, 2011

Board Description

You can power the board by USB bus.
The USB can supply 500mA, so there is enough power to drive relays and displays. The microcontroller has a USB port on board . You can drive up to 8 relays and read the status of 8 optocoupler. You can use this inputs for switches, push buttons, or to have a feed back to see if the relays have been effectively switched. The communication protocol between PC and board is very simple, for example, sending the character "6" on the virtual RS232 the first relay will turn on and the board will replay with an "I" character, this response is an acknowledge and makes you sure that the command arrived and was correctly executed.


scheda rele opto

HW description

The board is made of:

  • PCB;
  • microcontroller Microchip (18f2455 -see the Datasheet) with 32 Kb off flash memory, 256 byte of Eeprom memory and controller USB;
  • Optocoupler for input (TPL4N25-4).
  • USB Connector
  • Relays. We can mount two type of relay, one with maximum 30Vcc (or
    125Vac) or another with 230Vac, 2A contact.

Schematics

Schematic USB Board
Fig. 1

Schematic USB Board (2)
Fig. 2

Firmware description

The board firmware receives a command from the PC (using the get function) elaborate the command (by ProcessIO function) and executes the command(toggle the relay or read the input).
This is and example of the source code of the firmware:


void main(void) { /* A7  A6  A5  A4  A3  A2  A1  A0 */ /*    1     1     1     1    1     1     0     0 */ LATA &= 0x0; TRISA &= 0xFC; /* configure port A of uC */ /* B7 B6 B5 B4 B3 B2 B1 B0  */ /*    1   1    1    1   1    1   1    1  */ LATB &= 0x0; TRISB &= 0xFF; /* configure port B of uC */ /* C7 C6 C5 C4 C3 C2 C1 C0 */ /*   0    0    1    1   1    1   0    0 */ LATC &= 0x0; TRISC &= 0x3C; /* configure porta C of uC */ InitializeSystem(); mLED_1_Off();  MSB_num_restart = ReadEEPROM(0x20); LSB_num_restart = RedEEPROM(0x21); if ((MSB_num_restart == 0xFF)&(LSB_num_restart == 0xFF)){   /* blank eeprom */ MSB_num_restart = 0 ; LSB_num_restart = 0 ;  WriteEEPROM ( 0x20, 0); WriteEEPROM ( 0x21, 0); } else {  if (LSB_num_restart == 0xFF){  MSB_num_restart = MSB_num_restart +1; WriteEEPROM ( 0x20, MSB_num_restart); WriteEEPROM ( 0x21, 0); LSB_num_restart = 0; } else {  LSB_num_restart = LSB_num_restart +1; WriteEEPROM ( 0x21, LSB_num_restart); } } EnablePullups();     /* enable pull-up on PORTB */ while(1) { USBTasks();        /*  USB Tasks  */ ProcessIO();        /* See user\user.c & .h */ } } void ProcessIO(void) {    if((usb_device_state < CONFIGURED_STATE)||(UCONbits.SUSPND==1)) return; if(getsUSBUSART(input_buffer,1)){ ... if(input_buffer[0] == '6') /* 0x36 */ { if(mUSBUSARTIsTxTrfReady()){ mRele_1_On();                /* turn on relay number 1*/ putrsUSBUSART("I");    /* acknoledge */ } } ... } }
void main(void) { /* A7 A6 A5 A4 A3 A2 A1 A0 */ /* 1 1 1 1 1 1 0 0 */ LATA &= 0x0; TRISA &= 0xFC; /* configure port A of uC */ /* B7 B6 B5 B4 B3 B2 B1 B0 */ /* 1 1 1 1 1 1 1 1 */ LATB &= 0x0; TRISB &= 0xFF; /* configure port B of uC */ /* C7 C6 C5 C4 C3 C2 C1 C0 */ /* 0 0 1 1 1 1 0 0 */ LATC &= 0x0; TRISC &= 0x3C; /* configure porta C of uC */ InitializeSystem(); mLED_1_Off(); MSB_num_restart = ReadEEPROM(0x20); LSB_num_restart = RedEEPROM(0x21); if ((MSB_num_restart == 0xFF)&(LSB_num_restart == 0xFF)){ /* blank eeprom */ MSB_num_restart = 0 ; LSB_num_restart = 0 ; WriteEEPROM ( 0x20, 0); WriteEEPROM ( 0x21, 0); } else { if (LSB_num_restart == 0xFF){ MSB_num_restart = MSB_num_restart +1; WriteEEPROM ( 0x20, MSB_num_restart); WriteEEPROM ( 0x21, 0); LSB_num_restart = 0; } else { LSB_num_restart = LSB_num_restart +1; WriteEEPROM ( 0x21, LSB_num_restart); } } EnablePullups(); /* enable pull-up on PORTB */ while(1) { USBTasks(); /* USB Tasks */ ProcessIO(); /* See user\user.c & .h */ } } void ProcessIO(void) { if((usb_device_state < CONFIGURED_STATE)||(UCONbits.SUSPND==1)) return; if(getsUSBUSART(input_buffer,1)){ ... if(input_buffer[0] == '6') /* 0x36 */ { if(mUSBUSARTIsTxTrfReady()){ mRele_1_On(); /* turn on relay number 1*/ putrsUSBUSART("I"); /* acknoledge */ } } ... } }

The ProcessIO() function waits for a command. On this example when the board receives the "6" character on virtual RS232 (the USB) the first relay will turn on and answer with "I" character. You can also read how many time the board has been turned on, this counter is saved on the eeprom memory inside the PIC microcontroller.

Software description

Here is an example of software written with Visual Basic. From left ToolBox import the "SerialPort" control. We will use 3 functions of this control, which are: Open, Write, Read

Let's start opening the serial port, with SerialPort.Open.

The SerialPort.Open needs a serial port number to be opened; it is the number
of the virtual serial port (usually it is COM4).
After the virtual serial port is opened, you can send commands using
SerialPort.Write function, for example if you want to turn on the first relay:
SerialPort.Write("6")

To read the answer from the board and to acknowledge the command received
use the SerialPort.Read function

scheda rele visual basic

Command protocol

Send 0 = 0x30 = 48 => Turn on Led and answer  "A" Send 2 = 0x32 = 50 => Turn off Led and answer  "C" Send 5 = 0x35 = 53 => if the puss button is pressed, answer  "G"  otherwise "H" Send 6 = 0x36 = 54 => Turn on Relay 1 and answer  "I" Send 7 = 0x37 = 55 => Turn off Relay 1 and answer  "L" Send 8 = 0x38 = 56 => Turn on Relay 2 and answer  "M" Send 9 = 0x39 = 57 => Turn off Relay 2 and answer  "N" Send a = 0x61 = 97 => Turn on Relay 3 and answer  "O" Send b = 0x62 = 98 => Turn off Relay 3 and answer  "P" Send c = 0x63 = 99 => Turn on Relay 4 and answer  "Q" Send d = 0x64 = 100 => Turn off Relay 4 and answer  "R" Send e = 0x65 = 101 => if input 1 is on, answer  "S" otherwise "T" Send f = 0x66 = 102 => if input 2 is on, answer  "U" otherwise "V" Send g = 0x67 = 103 => if input 3 is on, answer  "W" otherwise "Y" Send h = 0x68 = 104 => if input 4 is on, answer  "J" otherwise "K" Send i = 0x69 = 105 => if input 5 is on, answer  "Z" otherwise "a" Send l = 0x6C = 108 => if input 6 is on, answer  "b" otherwise "c" Send m = 0x6D = 109 => if input 7 is on, answer  "d" otherwise "e" Send n = 0x6E = 110 => if input 8 is on, answer  "f" otherwise "g" Send o = 0x6F = 111 => Turn on Relay 5 and answer  "H" Send p = 0x70 = 112 => Turn off Relay 5 and answer  "B" Send q = 0x71 = 113 => Turn on Relay 6 and answer  "D" Send r = 0x72 = 114 => Turn off Relay 6 and answer  "E" Send s = 0x73 = 115 => Turn on Relay 7 and answer  "F" Send t = 0x74 = 116 => Turn off Relay 7 and answer  "G" Send u = 0x75 = 117 => Turn on Relay 8 and answer  "X" Send v = 0x76 = 118 => Turn off Relay 8 and answer  "m" 

You can also read how many time the board has been powered up:

Send w = 0x77 = 119 => Read the Most significant part of number of restart Send z = 0x7A = 122 => Read the Least significant part of number of restart 
SW_PC_Relay_USB.zip289.38 KB

No comments: