Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Issue in PIC18F2553 USB

Status
Not open for further replies.

embdev4

Newbie level 1
Newbie level 1
Joined
Apr 1, 2011
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,293
Hi,
I’m using PIC18F2553 for the ADC recording purpose. What I want to do is, get a few ADC values of different channels into a buffer and then send it to Computer through USB in CDC mode. I’m taking ADC values at an interval of 8KHz(125usec) with help of Timer0. The Timer Interrupt is set at Low Priority and the USB Interrupt at High Priority.
I’m using 2 buffers of 256B each. At start first buffer fills up, and when filled up, sent to USB. In the meantime, the second buffer starts filling up, and when filled up, it is sent to USB. So, the process continues.
But the issue, I’m struck in is that I do not receive any data on Terminal when the Timer is run at this speed. Although it gets enumerated and I can open the port also, do not receive anything. When I run Timer at Low Speed, I start getting both the buffers alternatively (as desired).
So what could be the solution out to this issue because I don’t think that USB does not get enough time to send its data as one buffer is sent after an interval of 30*125usec.

CODE:

Code:
void YourLowPriorityISRCode()
	{
		//Check which interrupt flag caused the interrupt.
		//Service the interrupt
		//Clear the interrupt flag
		//Etc.
		 

	            if(INTCONbits.TMR0IF == 1)        //Timer0 ISR
            {
                    INTCONbits.TMR0IF = 0;

//					adc_calculation();
//					adc_calculation_one_ch();
					new_adc_calculation();

					TMR0H=0XFD;
					TMR0L=0X8F;
            }
	}	//This return will be a "retfie", since this is in a #pragma interruptlow section 


int main(void)
{   
    InitializeSystem();
	TRISA = 0xFF;
	TRISB = 0xFF;

	memset(adc_buff,0x00,256);
	memset(adc_buff_2,0x00,256);

	ADCON1=0x00;
	ADCON2 = 0xB5;
        ADCON0bits.ADON = 1;           // Turn on ADC module
                       // internal Vref=4.53V 

   #if defined(USB_INTERRUPT)
       if(USB_BUS_SENSE && (USBGetDeviceState() == DETACHED_STATE))
       {
           USBDeviceAttach();
       }
   #endif

     while(USBGetDeviceState() != CONFIGURED_STATE);
     UserInit();

    while(1)
    {
	    if(buffers_full_F==1)
		{	
		   send_adc1();
		   buffers_full_F=0;
		   clear_adc_buffers();
		}

    }//end while

}//end main


static void InitializeSystem(void)
{ 
    #if defined(USE_USB_BUS_SENSE_IO)
    tris_usb_bus_sense = INPUT_PIN; // See HardwareProfile.h
    #endif
    

    #if defined(USE_SELF_POWER_SENSE_IO)
    tris_self_power = INPUT_PIN;	// See HardwareProfile.h
    #endif
    

    USBDeviceInit();	//usb_device.c.  Initializes USB module SFRs and firmware
    					//variables to known states.
}//end InitializeSystem


void UserInit(void)
{  
   RCONbits.IPEN=1;	  //enable priorities

   INTCON2=0x00 ;   //low priority to timer0

   T0CON=0x08;

   TMR0H=0X00;
   TMR0L=0X00;

   INTCONbits.TMR0IE = 1; //Enable Timer0 Interrupt
   T0CONbits.TMR0ON=1;  //Start Timer
   
   
}//end UserInit


void send_adc1(void)
{    
    // User Application USB tasks
    if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return;


	if(USBUSARTIsTxTrfReady())
	{
		if(buff_sent_F==1)
		{	
	       	       putUSBUSART(adc_buff,250); 
		}		
		else
		{
			putUSBUSART(adc_buff_2,250); 
		}
	}

        while(cdc_trf_state != CDC_TX_READY)
		{
    		CDCTxService();
		}
}



void new_adc_calculation(void)
{
	char i=0, *ptr;
	float adc;
    unsigned int ADC_data=0;

	tick++;

//--------------------------------------------------------------------------------------
	//start channel 4 adc
    ADCON0bits.CHS0 = 0;          // Select ADC channel 
	ADCON0bits.CHS1 = 0;          // Select ADC channel 
    ADCON0bits.CHS2 = 1;          // Select ADC channel 
	ADCON0bits.CHS3 = 0;          // Select ADC channel 

//	for (i=0; i<50; i++){i=i;}     // aquisition delay
	ADCON0bits.GO_DONE = 1;           // GO
	while (ADCON0bits.GO_DONE){i=i;}	// wait until conversion is complete
	ADC_data = ADRESL + ( ADRESH * 256)	;		// Read ADC register
	adc=(ADC_data * Vdd/4096);   
                
    ptr=(char*)&ADC_data;

   if(buff_sent_F==0)
   {
	   	adc_buff[2+2*(tick-1)]=*ptr++;    
	   	adc_buff[3+2*(tick-1)]=*ptr;
   }
   else
   {
	   	adc_buff_2[2+2*(tick-1)]=*ptr++;    
	   	adc_buff_2[3+2*(tick-1)]=*ptr;
   }

//--------------------------------------------------------------------------------------
	//start channel 8 adc
    ADCON0bits.CHS0 = 0;          // Select ADC channel 
	ADCON0bits.CHS1 = 0;          // Select ADC channel 
    ADCON0bits.CHS2 = 0;          // Select ADC channel 
	ADCON0bits.CHS3 = 1;          // Select ADC channel 

//	for (i=0; i<50; i++){i=i;}     // aquisition delay
	ADCON0bits.GO_DONE = 1;           // GO
	while (ADCON0bits.GO_DONE){i=i;}	// wait until conversion is complete
	ADC_data = ADRESL + ( ADRESH * 256)	;		// Read ADC register
	adc=(ADC_data * Vdd/4096);         //CHANGE VDD as per our requirement    
                
    ptr=(char*)&ADC_data;
   
    if(buff_sent_F==0)
    {
   		adc_buff[63+2*(tick-1)]=*ptr++;    
   		adc_buff[64+2*(tick-1)]=*ptr;
	}
	else
	{
   		adc_buff_2[63+2*(tick-1)]=*ptr++;    
   		adc_buff_2[64+2*(tick-1)]=*ptr;
	}

//--------------------------------------------------------------------------------------
	//start channel 9 adc
    ADCON0bits.CHS0 = 1;          // Select ADC channel 
	ADCON0bits.CHS1 = 0;          // Select ADC channel 
    ADCON0bits.CHS2 = 0;          // Select ADC channel 
	ADCON0bits.CHS3 = 1;          // Select ADC channel  

//	for (i=0; i<50; i++){i=i;}     // aquisition delay
	ADCON0bits.GO_DONE = 1;           // GO
	while (ADCON0bits.GO_DONE){i=i;}	// wait until conversion is complete
	ADC_data = ADRESL + ( ADRESH * 256)	;		// Read ADC register
	adc=(ADC_data * Vdd/4096);  
                
    ptr=(char*)&ADC_data;

    if(buff_sent_F==0)
    {	
    	adc_buff[124+2*(tick-1)]=*ptr++;    
    	adc_buff[125+2*(tick-1)]=*ptr;
	}
	else
	{
		adc_buff_2[124+2*(tick-1)]=*ptr++;    
    	adc_buff_2[125+2*(tick-1)]=*ptr;
	}

//--------------------------------------------------------------------------------------
	//start channel 10 adc
    ADCON0bits.CHS0 = 0;          // Select ADC channel 
	ADCON0bits.CHS1 = 1;          // Select ADC channel 
    ADCON0bits.CHS2 = 0;          // Select ADC channel 
	ADCON0bits.CHS3 = 1;          // Select ADC channel 

//	for (i=0; i<50; i++){i=i;}     // aquisition delay
	ADCON0bits.GO_DONE = 1;           // GO
	while (ADCON0bits.GO_DONE){i=i;}	// wait until conversion is complete
	ADC_data = ADRESL + ( ADRESH * 256)	;		// Read ADC register
	adc=(ADC_data * Vdd/4096);  
                
    ptr=(char*)&ADC_data;
    if(buff_sent_F==0)
    {
	    adc_buff[185+2*(tick-1)]=*ptr++;    
	    adc_buff[186+2*(tick-1)]=*ptr;
	}
	else
	{
	    adc_buff_2[185+2*(tick-1)]=*ptr++;    
	    adc_buff_2[186+2*(tick-1)]=*ptr;
	}
//--------------------------------------------------------------------------------------

	if(tick==30)
	{
	    if(buff_sent_F==0)
	    {
			adc_buff[0]='S';
			adc_buff[1]='T';
	
			adc_buff[62]='@';
			adc_buff[123]='@';
			adc_buff[184]='@';
	
			strcpypgm2ram(&adc_buff[245],(const rom far char *)"END\n\n");

		}
		else
		{
			adc_buff_2[0]='M';
			adc_buff_2[1]='M';
	
			adc_buff_2[62]='@';
			adc_buff_2[123]='@';
			adc_buff_2[184]='@';
	
			strcpypgm2ram(&adc_buff_2[245],(const rom far char *)"END\n\n");


		}
				
		buff_sent_F=!buff_sent_F;
		
		tick=0;
		buffers_full_F=1;
	}

}


void clear_adc_buffers(void)
{
    if(buff_sent_F==1)
	{
		memset(adc_buff,0x00,256);
	}
	else
	{
		memset(adc_buff_2,0x00,256);
	}
}
 

void packet_TX(char HB,char LB)
{
int res=0,temp,k;
int arr[4];

// Calculate Decimal Value of {HB,LB}
for (k=1;k<=128;k=k*2)
{
temp = LB;
temp = temp & k;
res += temp;
}
for (k=1;k<=2;k=k*2)
{
temp = HB;
temp = temp & k;
temp = temp * 256;
res += temp;
}

k = 0;
if (res==0)
{
for (k=0;k<4;k++)
brr[k]='0';
}
else
{
while(res!=0)
{
arr[k] = res%10;
brr[k] = arr[k]+48;
res = res/10;
k++;
}
}
flag = 1;
count = k-1;
TXREG = brr[count];
PIE1bits.TXIE=1;
}
plz tell me the algo of this cod
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top