jueves, 18 de julio de 2013

Implementación de la rutina de un código para desarrollar una aplicación tipo Hardware-Software atreves del IDE de visual Basic y el código en firmware en PIC C necesarios para establecer una comunicación USB con un dispositivo HID LAB 8

MATERIALES : 




- PBC 
- LEDS
- POTENCIOMETROS
- RESISTENCIAS
- MOTOR
- RS232

- PROTOBOARD

- COMPUTADOR 
- VISUAL BASIC 2010

CODIGO FIRMWARE :

#include <18F2550.h>
#device adc=8//La resolución del módulo A/D es de 8 bits.
#fuses HS,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL3,CPUDIV1,VREGEN

#use delay (clock=12000000)//12Mhz

#USE RS232 (baud=9600,bits=8,parity=N,xmit=PIN_C6,rcv=PIN_C7)//Defincion RS-232

#use standard_io(a)
#use fast_io(b)// Aqui están las señales del encoder

#byte port_a      = 0xF80
#byte port_b      = 0xF81



// CCS Library dynamic defines
#DEFINE USB_HID_DEVICE  TRUE //Tells the CCS PIC USB firmware to include HID handling code.
#define USB_EP1_TX_ENABLE  USB_ENABLE_INTERRUPT   //turn on EP1 FOR IN bulk/interrupt transfers
#define USB_EP1_TX_SIZE    64  //allocate 64 bytes in the hardware FOR transmission
#define USB_EP1_RX_ENABLE  USB_ENABLE_INTERRUPT   //turn on EP1 FOR OUT bulk/interrupt transfers
#define USB_EP1_RX_SIZE    64  //allocate 64 bytes in the hardware FOR reception

// CCS USB Libraries
#include <pic18_usb.h>   //Microchip 18Fxx5x hardware layer FOR usb.c
#include <usb_desc_hid 8-byte.h>   //USB Configuration and Device descriptors FOR this UBS device
#include <usb.c>        //handles usb setup tokens and get descriptor reports

void usb_debug_task(VOID)
{
   STATIC int8 last_connected;
   STATIC int8 last_enumerated;
   INT8 new_connected;
   INT8 new_enumerated;
   new_connected = usb_attached ();
   new_enumerated = usb_enumerated ();
   last_connected = new_connected;
   last_enumerated = new_enumerated;
}


// ----------------------------------Declaración de variables----------------------------------

int dato4;
int dato1;


//Variables USB-----------------------------------

int16 tx;
//int16 tx1=0;
#byte txl=43
#byte txh=44
#byte tx1l=45
#byte tx1h=46
#byte port_a      = 0xF80
#byte port_b      = 0xF81


int8 in_data[8];
int8 out_data[8];



// ----------------------------------Declaración de los prototipos de las funciones----------------------------------


#INT_TIMER1
void int_TMR1()//retardo programado a un segundo
{
}
#int_RDA
RDA_isr()
{
 dato4=GETC();
}


void main(){

   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);
   set_tris_b(0x00);
   SETUP_ADC_PORTS(AN0|VSS_VDD);
   setup_adc(ADC_CLOCK_INTERNAL);
   
   usb_init();
  
      for (;;) {

      usb_task ();
      usb_debug_task ();
      usb_get_packet (1, in_data, 8);

            txh=(in_data[1]);
            txl=(in_data[2]);
            tx1h=(in_data[3]);
           dato1=(in_data[4]);
           PUTC(dato1);
           output_b(dato1);           
            out_data[0]=30 ;
            //out_data[1]=q;
            //out_data[2]=q1;
            set_adc_channel(0);
            delay_us(20);
            dato4 = read_adc();
            out_data[3]=dato4;
            usb_put_packet (1, out_data, 8, USB_DTS_TOGGLE);
            ENABLE_INTERRUPTS(int_timer0);
            set_timer0(tx);

      }
      

}


CODIGO SOFTWARE:

Public Class Form1
    Dim val1, y1, y2 As Int16
    Dim sensor, setpoint As Byte
    Dim cuenta, dato4 As Byte
    Dim bajoconta, bajo, bajoy1, bajoy2 As Byte
    Dim uno, dos, tres, cuatro, cinco, seis, siete, ocho As Byte

    Private Sub HIDComm1_ConnectSuccess(ByVal sender As System.Object, ByVal e As AxVBHIDComm.__HIDComm_ConnectSuccessEvent) Handles HIDComm1.ConnectSuccess
        Me.Label4.Text = "Control de port B" & Me.HIDComm1.Manufacturer
    End Sub

    Private Sub HIDComm1_Disconnected(ByVal sender As System.Object, ByVal e As AxVBHIDComm.__HIDComm_DisconnectedEvent) Handles HIDComm1.Disconnected
        Me.Label4.Text = "Esperando conexion"
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.HIDComm1.Connect()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim buffer() As Byte
        ReDim buffer(8)
        If HIDComm1.Connected = False Then
            HIDComm1.Connect()
        End If

    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        Dim outbuffer() As Byte
        ReDim outbuffer(8)

        Dim inbuffer() As Byte
        ReDim inbuffer(8)

        outbuffer(0) = 1
        outbuffer(1) = 15
        outbuffer(2) = 2
        outbuffer(3) = 1
        outbuffer(4) = cuenta
        outbuffer(5) = y1
        'outbuffer(6) = 0
        'outbuffer(7) = 0
        HIDComm1.WriteTo(outbuffer, 8)
        inbuffer = HIDComm1.ReadFrom(8)
        dato4 = inbuffer(3)
        Label3.Text = dato4
        ProgressBar1.Value = dato4
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        HIDComm1.Uninit()
        End
    End Sub

    Private Sub ProgressBar1_Click(sender As Object, e As EventArgs) Handles ProgressBar1.Click

    End Sub

    Private Sub GroupBox1_Enter(sender As Object, e As EventArgs)

    End Sub

    Private Sub Label1_Click(sender As Object, e As EventArgs)

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        cuenta = 70
        Label1.Text = 3410

    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        cuenta = 140
        Label1.Text = 5500
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        cuenta = 185
        Label1.Text = 7250
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        cuenta = 255
        Label1.Text = "APAGADO"
    End Sub

    Private Sub Label1_Click_1(sender As Object, e As EventArgs) Handles Label1.Click

    End Sub

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        cuenta = 20
        Label1.Text = "ENCENDIDO"
    End Sub

    Private Sub GroupBox1_Enter_1(sender As Object, e As EventArgs) Handles GroupBox1.Enter

    End Sub
End Class
 



No hay comentarios:

Publicar un comentario en la entrada