prongs1911
Newbie level 6
Hi,
I am trying to send data from the inbuilt ADC of pic18f4550 to PC using USB. I successfully wrote a code for ADC to send data over serial port for testing.I have also modified the USB CDC-BASIC DEMO from microchip framework to echo back the data sent from PCand I was able to send and recieve data using realterm. But I have not been able to combine both. What I tried was, I wrote the ADC part of the code in void user(void) after if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return;,but i was not getting any output on Realterm.
Is this the correct approach or do I need to do something else?
Please help. I have to submit this project in another 5 days.
ADC code:
USB-CDC echo code:
I am trying to send data from the inbuilt ADC of pic18f4550 to PC using USB. I successfully wrote a code for ADC to send data over serial port for testing.I have also modified the USB CDC-BASIC DEMO from microchip framework to echo back the data sent from PCand I was able to send and recieve data using realterm. But I have not been able to combine both. What I tried was, I wrote the ADC part of the code in void user(void) after if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return;,but i was not getting any output on Realterm.
Is this the correct approach or do I need to do something else?
Please help. I have to submit this project in another 5 days.
ADC code:
Code:
#include <p18f4550.h>
#include <delays.h>
#include <adc.h>
#include <usart.h>
#include <stdlib.h>
#pragma config FOSC = HSPLL_HS // 20 MHz crystal.
#pragma config PLLDIV = 5 // Divide by 5 to provide the 96 MHz PLL with 4 MHz input.
#pragma config CPUDIV = OSC1_PLL2 // Divide 96 MHz PLL output by 2 to get 48 MHz system clock.
#pragma config USBDIV = 2 // "USB clock source comes from the 96 MHz PLL divided by 2."
#pragma config FCMEN = OFF // "Fail-Safe Clock Monitor disabled."
#pragma config IESO = OFF // "Oscillator Switchover mode disabled."
#pragma config PWRT = OFF // "PWRT disabled."
#pragma config WDT = OFF // "HW Disabled - SW Controlled."
#pragma config MCLRE = ON // "MCLR pin enabled; RE3 input pin disabled."
#pragma config BOR = ON // Brown Out Reset enabled in hardware. In case of supply voltage drop, BOR resets the device to prevent erratic CPU behavior.
#pragma config BORV = 3 // BOR occurs at 2V (1.2V below the minimum required voltage for the HSPLL oscillator mode).
#pragma config VREGEN = ON //USB Voltage Regulator
#pragma config LVP = OFF // Disable low voltage ICSP
#pragma config ICPRT = OFF // Disable dedicated programming port (44-pin devices)
#pragma config CP0 = OFF // Disable code protection
#define LEDPin LATDbits.LATD2 //Define LEDPin as PORT D Pin 1
#define LEDTris TRISDbits.TRISD2 //Define LEDTris as TRISD Pin 1
void main()
{
int delay;
unsigned char result=0;
unsigned char lsByte=0;
unsigned char msByte=0;
unsigned int ADCstate = 0;
LEDTris = 0;//Set LED Pin data direction to OUTPUT
LEDPin = 1;//Set LED Pin
ADCON1 = 0b00001110;//VSS,VDD ref. AN0 analog only
ADCON0 = 0x00;//clear ADCON0 to select channel 0 (AN0)
ADCON2 = 0b00001110;//ADCON2 setup: Left justified, Tacq=2Tad, Tad=64*Tosc (or Fosc/64)
ADCON0bits.ADON = 0x01;//Enable A/D module
OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE &
USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW, 255);
while(1)
{
switch(ADCstate)
{
case 0: SetChanADC(ADC_CH0);
ADCstate++;
break;
case 1: ConvertADC();
ADCstate++;
break;
case 2: if(BusyADC())
{ break;
}
else
{ ADCstate++;
break;
}
case 3: lsByte = ADRESL;
msByte = ADRESH;
ADCstate++;
break;
case 4:
putcUSART(msByte);
ADCstate=0;
break;
default: ADCstate=0;
break;
}
}
}
Code:
#include "p18f4550.h"
#include "./USB/usb.h"
#include "./USB/usb_function_cdc.h"
#include "main.h"
void user(void)
{
BYTE numBytesRead;
//Blink the LEDs according to the USB device status
BlinkUSBStatus();
// User Application USB tasks
if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return;
if(USBUSARTIsTxTrfReady())
{
numBytesRead = getsUSBUSART(USB_Out_Buffer,64);
if(numBytesRead != 0)
{
BYTE i;
for(i=0;i<numBytesRead;i++)
{
switch(USB_Out_Buffer[i])
{
case 0x0A:
case 0x0D:
USB_In_Buffer[i] = USB_Out_Buffer[i];
break;
default:
USB_In_Buffer[i] = USB_Out_Buffer[i];// + 1;
break;
}
}
putUSBUSART(USB_In_Buffer,numBytesRead);
}
}
CDCTxService();
}