CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

USB AND interrupts INT_AD problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
AKIOM



Joined: 16 May 2014
Posts: 11

View user's profile Send private message

USB AND interrupts INT_AD problem
PostPosted: Fri May 16, 2014 7:34 pm     Reply with quote

I would like to convert analog to digital in sleep mode to reduce noise. Then, I send the value via usb port. When I enable INT_AD, PIC18f4550 can not send anything to computer. Even, I script sleep mode and left only enable INT_AD, it only see as com-port on my computer (I use usb cdc module) but it cannot send anything. Please help me.
Note that if I enable other interrupts such as INT global or INT_RA, my PIC can send data via usb (condition of the test does not get PIC in sleep mode). I use all day to find this solution by searching from internet but I can not.
I use PIC18f4550 and 5.024 ccs complier

Thank you very much

Code:

/**************************************************************************
 * File :
 * Purpose : Test ADC SLEEP MODE AND THEN SEND VIA USB
 **************************************************************************/

#include <18F4550.h>
#device adc=10
#use delay (clock= 48M)

#include <stdlib.h>
#include <usb_cdc.h>

#fuses HSPLL ,PLL5 ,USBDIV ,CPUDIV1 ,VREGEN, NOLVP, NOWDT, NOPROTECT 

/***********************************************************************
 *   Constants
 ***********************************************************************/
#define Vbe 0.00488758553 //->(5/1023)

/***********************************************************************
 FUNCTION: Mainprogram
 ***********************************************************************/


void main()
{   
   setup_adc_ports(AN0);
   setup_adc(ADC_CLOCK_INTERNAL);
   int16 ADC_Value;
   float volt;

   while(true)
   {
      enable_interrupts(INT_AD);
      set_adc_channel(0);
      clear_interrupt(INT_AD);
      read_adc(ADC_START_ONLY);
      sleep();
      delay_cycles(1);
      ADC_value=read_adc(ADC_READ_ONLY);
      disable_interrupts(INT_AD);
      volt=ADC_Value* (float) Vbe;
     
      usb_cdc_init();
      usb_init(); //init USB and enable USB interrupt.
      usb_task();
   
   //// %ld unsigned lohg decimal
   if (usb_enumerated())//Returns TRUE if the device has been enumerated(specified)by the PC.
      {
     printf(usb_cdc_putc, "\n\rval: %ld",ADC_Value);// %ld unsigned lohg decimal
     printf(usb_cdc_putc, "\n\rval: %f",volt);// %ld unsigned long decimal
     
      }     
   
   }
}
oxo



Joined: 13 Nov 2012
Posts: 219
Location: France

View user's profile Send private message

PostPosted: Fri May 16, 2014 10:41 pm     Reply with quote

Data sheet p35.

All clocks are disabled in sleep mode.

What do you think this means for the usb controller and the adc?
AKIOM



Joined: 16 May 2014
Posts: 11

View user's profile Send private message

I read datasheet p35 as you suggest
PostPosted: Sat May 17, 2014 1:23 am     Reply with quote

I read datasheet p35 as you suggest. The detail is when the PIC sleep, it use internal oscillator so adc work. After, it finish reads Analog signal, it should be waked by INT_AD. All in all, I test my code by script *"sleep mode"*(pic does not sleep" and left only enable INT_AD but it does not send any data to usb port.
oxo



Joined: 13 Nov 2012
Posts: 219
Location: France

View user's profile Send private message

PostPosted: Sat May 17, 2014 1:34 am     Reply with quote

But the usb will not have a clock. You cannot sleep with usb active.
Ttelmah



Joined: 11 Mar 2010
Posts: 19447

View user's profile Send private message

PostPosted: Sat May 17, 2014 2:56 am     Reply with quote

You basically cannot do this.

USB, requires the USB peripheral is always active, once enumerated (unless the host puts the device to sleep). As Oxo has told you the master oscillator stops, so the USB peripheral stops.....

Use the main CPU clock with a suitable divider. The internal RC clock is not 'better' in terms of noise. It is only better because using it you can stop the CPU (which then potentially reduces noise from this), but with USB running you _cannot_ do this. With good supply design, decoupling, and a well designed ground plane round the CPU, you can quite easily reach the design limits of the PIC ADC with the internal clock.
AKIOM



Joined: 16 May 2014
Posts: 11

View user's profile Send private message

PostPosted: Sat May 17, 2014 6:29 am     Reply with quote

Dear oxo and Ttelmah
Thank both of you very much for answering me.
I have the question in my mind about my code.
Code:

read_adc(ADC_START_ONLY);
sleep();
delay_cycles(1);
ADC_value=read_adc(ADC_READ_ONLY);

This process is wake up cpu when reading ADC finish. After that, I start usb from initial process with
Code:

      usb_cdc_init();
      usb_init(); //init USB and enable USB interrupt.
      usb_task();

This process runs infinite loop after finishing adc converter. So, I think it can enable USB peripheral active.

I have to do this (sleep) because I test ADC by reading from battery 1.2 V and the value is +-5 point (230-240) ->1. 2/5*1023. This error is common error or not. I use supply from USB and use C 0.1uf decoupling USB supply. I should use external supply and regulator or not. Another question is I want more accuracy how can I do?. My project want 10 Kbps ADC sampling rate. Please advise me, I am a new user about PIC.
Thank you very much.
Ttelmah



Joined: 11 Mar 2010
Posts: 19447

View user's profile Send private message

PostPosted: Sat May 17, 2014 9:17 am     Reply with quote

Forget about sleeping the chip.

To get _accuracy_, you want an external Vref.

Currently your accuracy will be <10% if that. The USB supply at the PC, must be +/-5% (it's allowed to be worse than this for USB3....), but add your cables and connectors, and +5/-10% is more typical. It is not suitable for use as a reference voltage.
Your count will easily vary by 20 or more counts as the USB supply varies, and with different PC's. A bandgap reference feeding the ADC Vref is the way to get accuracy.
No point in sleeping at all.

The fastest CPU clock speed you can legally use for full accuracy on the internal ADC on this chip is 40MHz. This is actually difficult to do with USB on this chip. Best is to run with HSPLL, and CPUDIV2, which from a crystal that is a multiple of 4MHz (with the correct PLL ratio selected), then give the CPU at 32MHz. This does a conversion in 22uSec, with 7.6uSec required between conversions for a maximum rate just over 33KHz. However you are unlikely to be able to do anything like this rate for any duration.
First the PIC can't store many values.
Then when USB requires servicing, there will be 100+uSec pauses as this is handled.
To get this sort of rate and feed it to USB with a steady throughput, you need a chip with possibly DMA.

So if this is just a short 'burst' of readings, then OK. But if you want a long period of readings, you have not got the hardware to handle it....
AKIOM



Joined: 16 May 2014
Posts: 11

View user's profile Send private message

PostPosted: Sat May 17, 2014 10:58 am     Reply with quote

Ttelmah, Thank you very much again.

After I read your suggestion. To accurate ADC, It should have
1. External Voltage Reference for ADC
2. External supply with voltage regulator
3. Decoupling capacitor from supply
4. Ground plane around PIC
5. External Oscillator for ADC Clock.
6. Do not use sleep mode for reducing noise while connecting USB

For my project, I read your suggestion. The conclusion is

1. It is hard to use ADC at sampling rate 10 kbps and send via USB port. by using PIC18F4550.

If I really change my PIC, what PIC I should start.

I go to microchip website and try to select the PIC that has USB mode and
DMA. I think dspic33EP256MU806' spec is suitable for my project. I do not
know this PIC is proper for me. I am just a starter and do not know much about micro-controller. Please advise me. Thank you very much.
temtronic



Joined: 01 Jul 2010
Posts: 9202
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat May 17, 2014 5:53 pm     Reply with quote

You should tell us WHAT analog input you're sampling that needs 10KHz.

Also along with previous replies...., you'll need real GOOD PCB layouts,proper decoupling, shielded cables, etc. to get accurate ADC readings !


hth
jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19447

View user's profile Send private message

PostPosted: Sun May 18, 2014 2:54 am     Reply with quote

and seriously, you need to walk before you can ride.

You need to start much smaller.

Set yourself a series of parts to do one step at a time.

Before starting, if all you want is to sample a voltage 10000* per second, there are any number of USB modules on the market already that can do this without building anything. Some of the data logger units do exactly this.

Once you are sure you need to build something:

First get an actual PIC project working, sending data to a serial port.
Then perhaps try USB.
Then try running the chip without decoupling, then with, and realise why this is _essential_.
Then try some ADC readings, and see what the noise is like, and study the effect of decoupling round the chip, and using an external Vref (and no, you don't need 'an external supply with voltage regulator'). The Vref is all that is needed here.
Then depending on what your signal is, look at what analog preprocessing may be necessary. Remember the PIC ADC needs a relatively low impedance source, and the ADC is linear. Is this what your signal can generate, and requires?.
Only once you are happy with your ability to do all this, and understand the effect of things round the chip, look at what processor may best suit your needs.
AKIOM



Joined: 16 May 2014
Posts: 11

View user's profile Send private message

PostPosted: Sun May 18, 2014 6:48 am     Reply with quote

Dear all
Thank you very much for suggestions.

My project is measure voltage signal from photo-detector array.
Ttelmah, I very appreciate your suggestion. I will start walking as you advise me. All suggestion is very useful for me.

AKIOM
Ttelmah



Joined: 11 Mar 2010
Posts: 19447

View user's profile Send private message

PostPosted: Sun May 18, 2014 1:25 pm     Reply with quote

Seriously, you need to do a lot of basic learning, before starting this project.

Now a photodiode array, covers a huge range of different devices, and what is needed for the preprocessing, will depend on the nature of the device, and the lighting intensities involved. You'll need a constant current source feeding the diodes, and a buffer amplifier to feed the ADC. You need to get this processing right before even beginning to think about the PIC circuit. One way, could be to use two PIC's, and a small amount of dual port memory. Then one PIC samples the signal at a constant rate transferring the data to the memory, and the other handles sending this to the USB.
However I have to repeat the comment about the number of off the shelf units that can do this. If you want less than a few hundred units, it'll be a lot quicker and cheaper to use one of these. The PIC solution becomes worth developing, when the costs of development can be amortised over a lot of units, The sort of unit I'm talking about is the NI USB-6008.
temtronic



Joined: 01 Jul 2010
Posts: 9202
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun May 18, 2014 4:18 pm     Reply with quote

You should tell us the make/model of this 'array' or a 'link' to the datasheet. Someone here might have already used it or similar.

hth
jay
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group