Sie sind auf Seite 1von 3

//////////////////////////////////////////////////////////

//***** ENVIAR EL VALOR DEL ADC POR EL PUERTO USB********


//////////////////////////////////////////////////
#include <18F4550.h>
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#DEVICE ADC=8
#use delay(clock=48000000)
////////////////////////////////////////////////////////////////////////////////
///////////////////
//
// CCS Library dynamic defines. For dynamic configuration of the CCS Library
// for your application several defines need to be made. See the comments
// at usb.h for more information
//
////////////////////////////////////////////////////////////////////////////////
///////////////////
#define USB_HID_DEVICE FALSE // deshabilitamos el uso de las directivas HID
#define USB_EP1_TX_ENABLE USB_ENABLE_BULK // turn on EP1(EndPoint1) for IN bulk/
interrupt transfers
#define USB_EP1_RX_ENABLE USB_ENABLE_BULK // turn on EP1(EndPoint1) for OUT bulk
/interrupt transfers
#define USB_EP1_TX_SIZE 32 // size to allocate for the tx endpoint 1 buffer
#define USB_EP1_RX_SIZE 32 // size to allocate for the rx endpoint 1 buffer
////////////////////////////////////////////////////////////////////////////////
///////////////////
//
// Include the CCS USB Libraries. See the comments at the top of these
// files for more information
//
////////////////////////////////////////////////////////////////////////////////
///////////////////
#include <pic18_usb.h> // Microchip PIC18Fxx5x Hardware layer for CCS's PIC USB
driver
#include <header.h> // Configuracin del USB y los descriptores para este disposit
ivo
#include <usb.c> // handles usb setup tokens and get descriptor reports
#define Enciende Output_High
#define Apaga Output_Low
#define Conmuta Output_Toggle
#define RecCommand recbuf[0]
#define LedParam recbuf[1]
#define COMMAND_FIRMWARE 99
#define COMMAND_LEDS 88
#define COMMAND_ADC 01
#BYTE
#BYTE
#BYTE
#BYTE
#BYTE

port_a
port_b
port_c
port_d
port_e

=
=
=
=
=

0xf80
0xf81
0xf82
0xf83
0xf84

#define uno bit_test(port_d,1)


#define dos bit_test(port_d,2)
#define tre bit_test(port_d,3)
const int8 Lenbuf = 32;
////////////////////////////////////////////////////////////////////////////////
///////////////////

//
// RAM, RAM, RAM
//
////////////////////////////////////////////////////////////////////////////////
///////////////////
char Version[] = "RADAMIS";
int8 recbuf[Lenbuf];
int8 sndbuf[Lenbuf];
////////////////////////////////////////////////////////////////////////////////
///////////////////
//
// M A I N
//
////////////////////////////////////////////////////////////////////////////////
///////////////////
void antirebote();
void main(void) {
int adc0[];
int i,j,k;
char voltage[9];
setup_adc_ports(AN0|VSS_VDD);//para escoger el canal cero y el vss y vdd
SETUP_ADC(ADC_CLOCK_INTERNAL);//la frecuencia de muestreo es 2-6us
SET_ADC_CHANNEL(0);
delay_ms(500);
usb_init();
usb_task();
usb_wait_for_enumeration();
disable_interrupts(global);
port_d=0;
i=0;
j=0;
k=0;
while (TRUE){
if (uno == 0 && i==0)
{
antirebote();
i=1;
bit_set(port_b,6);
}
if (uno == 0 && i==1)
{
antirebote();
i=0;
bit_clear(port_b,6);
}
if (dos == 0 && j==0)
{
antirebote();
j=1;
bit_set(port_b,5);
}

if (dos == 0 && j==1)


{
antirebote();
j=0;
bit_clear(port_b,5);
}
if (tre == 0 && j==0)
{
antirebote();
k=1;
bit_set(port_b,4);
}
if (tre == 0 && j==1)
{
antirebote();
k=0;
bit_clear(port_b,4);
}
if(RecCommand==COMMAND_LEDS){
output_b(LedParam);
}
if(usb_enumerated()){
if (usb_kbhit(1)){
usb_get_packet(1, recbuf, Lenbuf);
if(RecCommand==COMMAND_FIRMWARE){
usb_put_packet(1,Version,5,USB_DTS_TOGGLE);
}
if(RecCommand==COMMAND_ADC){
adc0 = read_adc(); //para leer el valor del adc0
sprintf(voltage, "%f", (float)adc0 * 0.19607843); //esta instruccion nos
permite convertir el valor del adc en un string.
voltage[4] = '\0';
usb_put_packet(1,voltage,4,USB_DTS_TOGGLE);//con esta instruccion mandam
os el arrglo que se creo en la instruccion anterior
}
}
}
}
}
void antirebote(void)
{
while(uno==0){ }
while(dos==0){ }
while(tre==0){ }
delay_ms(30);
return;
}

Das könnte Ihnen auch gefallen