ADC + USB implementation on pic18f4550

Status
Not open for further replies.

prongs1911

Newbie level 6
Joined
Jun 15, 2012
Messages
11
Helped
4
Reputation
8
Reaction score
4
Trophy points
1,283
Activity points
1,352
I am trying to sample an analog input and send the ADC output via USB. The device gets detected but a message pops up saying that "the device is broken or not functioning". The device worked properly when i used it to echo back a sent message. I am using PROTEUS simulator to test my code and Realterm to read the device on PC.
This was my original code.
Code:
#include "p18f4550.h"
 #include "./USB/usb.h"
 #include "./USB/usb_function_cdc.h"
 #include "main.h"
 #include "adc.h"
 #include "delays.h"
void user(void)
  {
           //Blink the LEDs according to the USB device status
         BlinkUSBStatus();
         //             User Application USB tasks
         if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return;
 //-- clear adc interrupt and turn off adc if in case was on prerviously---
       TRISAbits.TRISA5=1; //RA5/AN4 as analog input
       CloseADC();
       Delay10TCYx(5);
 /**********Iitialize ADC***********
 ADC configured for:
                * FOSC/64 as conversion clock
    * Result is right justified
    * Aquisition time of 20 TAD
    * Channel 4 for sampling
    * ADC interrupt off
    * ADC reference voltage from VDD & VSS
 **********************************************/
        OpenADC(ADC_FOSC_64 & ADC_RIGHT_JUST & ADC_20_TAD, ADC_CH4 & ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS,
                               7);
       SetChanADC(ADC_CH4);
       Delay10TCYx(5);
       ConvertADC();
        while(BusyADC());
       Delay10TCYx(3);
             if(USBUSARTIsTxTrfReady())
             {                          USB_In_Buffer[0] = ADRESL;       // pack the ADC result to the packet buffer
                      USB_In_Buffer[1] = ADRESH;       //   LSB first
                      putUSBUSART(USB_In_Buffer,2);
               }        
         CDCTxService();}
But someone suggested that the delays may be the cause of problem, since in USB polling the USBDeviceTasks() has to be called every 1.8ms. So I implemented state machine and changed my code to this, but the problem still persists.
Code:
#include "p18f4550.h"
 #include "./USB/usb.h"
 #include "./USB/usb_function_cdc.h"
 #include "main.h"
 #include "adc.h"
 #include "delays.h"
 extern unsigned int ADCstate;
 BYTE lsByte;
 BYTE msByte;
 void user(void)
 {
         //Blink the LEDs according to the USB device status
         BlinkUSBStatus();
         // User Application USB tasks
         if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return;
 /**********Iitialize ADC***********
 ADC configured for:
                * FOSC/64 as conversion clock
    * Result is right justified
    * Aquisition time of 20 TAD
    * Channel 4 for sampling
    * ADC interrupt off
    * ADC reference voltage from VDD & VSS
 **********************************************/
 switch(ADCstate)
 {
 case 0: OpenADC(ADC_FOSC_64 & ADC_RIGHT_JUST & ADC_2_TAD, 
 ADC_CH0 & ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS, 
 ADC_2ANA);
 ADCstate++;
 break;
 case 1:        SetChanADC(ADC_CH0);
 ADCstate++;
 break;
 case 2:        ConvertADC();
 ADCstate++;
 break;
 case 3: while(BusyADC());
 ADCstate++;
 break;
 case 4: lsByte = ADRESL;
 msByte = ADRESH;
 ADCstate++;
 break;
 case 5: CloseADC();
 ADCstate++;
 break;
 case 6:if(USBUSARTIsTxTrfReady())
                                {
                                USB_In_Buffer[0] = lsByte;       // pack the ADC result to the packet buffer
                                        USB_In_Buffer[1] = msByte;       //   LSB first
                        putUSBUSART(USB_In_Buffer,2);
                }
 ADCstate=0;
 break;
 default: ADCstate=0;
 break;
                        }
         CDCTxService();
 }
Please Help.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…