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 CCS Technical Support

Please help for ADC voltage measurement
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ahliang530



Joined: 18 Nov 2010
Posts: 9

View user's profile Send private message MSN Messenger

Please help for ADC voltage measurement
PostPosted: Tue Jan 18, 2011 4:25 am     Reply with quote

I have no problem when compiling but I face error when I simulate in the proteus simulation. Once I click start button it show 3 errors:

1. ADC conversion started before 'wait' time has expired following previous conversion or channel change.

2. PORTA<0> is not configured as an analog input.

3. Voltage references for ADC conversion yield a 0V range.

But the simulation still running smoothly with show all the thing I wish to be done. I have not yet download to my PIC and try but I hope I can solve this problem b4 i try on it to avoid PIC damage.

Below is the link for my simulation photo (so sorry that I not really know how to show photo in the forum).

Thanks for helping
Code:

#include <16F877A.h>
#device ADC=12
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=10000000)

#include <LCDDRIVER.c>

void main()
{
    float   analin, disvolts, current;
    int limit=0 , i ,d ,s;

   SET_TRIS_C(0xFF);
   SET_TRIS_A(0xFF);

   SETUP_ADC_PORTS(ALL_ANALOG);
   setup_adc(ADC_CLOCK_DIV_8);


   
    while(1)
    {
         
         i = input(PIN_C0);
         d = input(PIN_C1);
         s = input(PIN_C2);

            lcd_init();
                  lcd_putc("\f");
        if(s==1)
         
         {
         
            printf(lcd_putc,"Key in limit A:%d",limit);
         
            
         if(i==0)
                  
               {

                  limit++;
               }

         else if(d==0)
               {
   
                  limit--;
               }
         }

      else if ( s == 0)
         while(1)
         {


             SET_ADC_CHANNEL(1);
               analin = read_adc();
            delay_ms(100);
                 disvolts = (analin)/204.6;
            current = (disvolts*240);
         
            if ( disvolts >= limit)
         {   
      
                 lcd_init();
               lcd_putc("\f");
            printf(lcd_putc,"voltage over:%d",limit);
                 lcd_putc("\n");
            printf(lcd_putc,"voltage now:%1.4f",disvolts);
            delay_ms(100);
            OUTPUT_HIGH(PIN_B1);
            OUTPUT_HIGH(PIN_B2);
         }
         
            
            else
         {
            lcd_init();
               lcd_putc("\f");
            printf(lcd_putc,"voltage now:%1.4f",disvolts);
         }
             
            }

   }
}



http://flic.kr/p/9bcfn4
temtronic



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

View user's profile Send private message

PostPosted: Tue Jan 18, 2011 6:34 am     Reply with quote

Well, your program is a good example as to why I never use Proteus !!

First, it should never have compiled as line two is wrong.
hint: read the ADC section of the PIC datasheet.

Second, as you area novice, get rid of the TRIS() statements and let the compiler take care of creating the correct code.

Third, you should fully 'setup' the PIC by disabling any internal peripherals that you are not using. This includes comparators, ADC pins, I2C, etc.

Fourth, it looks like you're running the lcd_init function inside the 'forever' loop.
It only needs to be called once, at the beginning of main but before the forever loop.

I create a 'setupPIC()' function, which defines pins,enable/disable peripherals, and calls 'one time' setups (like your LCD_INIT), as well as clears/sets I/O pins to my defaults. This setupPIC() function is then executed once, at the top of Main, before the forever loop.

Also be aware that floating point math uses a LOT of memory! Replacing it with integer math with really improve overall speed and use of memory.

hope this helps
ahliang530



Joined: 18 Nov 2010
Posts: 9

View user's profile Send private message MSN Messenger

PostPosted: Tue Jan 18, 2011 7:14 am     Reply with quote

temtronic wrote:
Well, your program is a good example as to why I never use Proteus !!

First , it should never have compiled as line two is wrong.
hint: read the ADC section of the PIC datasheet..

Second, as you area novice, get rid of the TRIS() statements and let the compiler take care of creating the correct code.

Third, you should fully 'setup' the PIC by disabling any internal peripherals that you are not using.This includes comparators,ADC pins,I2C,etc.

Fourth, it looks like you're running the lcd_init function inside the 'forever' loop.
It only needs to be called once, at the beginning of main but before the forever loop.

I create a 'setupPIC()' function, which defines pins,enable/disable peripherals, and calls 'one time' setups(like your LCD_INIT),as well as clears/sets I/O pins to my defaults. This setupPIC() function is then executed once,at the top of Main, before the forever loop.

Also be aware that floating point math uses a LOT of memory! Replacing it with integer math with really improve overall speed and use of memory.

hope this helps


what wrong with my line 2 statement that u mention, can u tell me?

may i ignore the TRIS and let the compiler determine itself?

can u please give me some further example for setupPIC function as u mention ?

last question .. is that mean i can proceed to download my code and ignore the error without damage my PIC ?
dyeatman



Joined: 06 Sep 2003
Posts: 1934
Location: Norman, OK

View user's profile Send private message

PostPosted: Tue Jan 18, 2011 7:42 am     Reply with quote

Rather than telling you the answer to the ADC question, YOU need to learn
how to research these things yourself. Read Page 127 of the datasheet.
How many bits resolution is the ADC?
_________________
Google and Forum Search are some of your best tools!!!!
ahliang530



Joined: 18 Nov 2010
Posts: 9

View user's profile Send private message MSN Messenger

PostPosted: Tue Jan 18, 2011 8:13 am     Reply with quote

dyeatman wrote:
Rather than telling you the answer to the ADC question, YOU need to learn
how to research these things yourself. Read Page 127 of the datasheet.
How many bits resolution is the ADC?


i get what u means .. thanks for your advice

so is that TRIS statement i can just skip it?

and then can i download my code into my PIC with those error (my budget is limited.. so i am worry for PIC damage)
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Tue Jan 18, 2011 9:21 am     Reply with quote

It is basically impossible to harm a PIC with code.

It is what you connect externally to it, that can do harm:
Supplies reversed.
Connections to pins carrying voltages outside the ratings.
Induced voltages throwing 'back' through the chip (when switching things like inductors).
Connecting loads without thinking about the currents they may draw (LED's without load resistors for example).

Obviously, beyond this, if you have external circuitry that if turned 'on' in the wrong pattern could cause problems (an H bridge for example), which you then turn on both the 'highside', and 'lowside' transistor at the same time. However unlikely to hurt the PIC, unless something else blows up, and shorts power through the PIC....

Yes, basically with CCS, _unless_ you are doing something where you specifically 'need' to override the default behaviour, for 90+ % of code, you can just ignore TRIS, and let the compiler handle it.

Now remember that the ADC input, is a capacitor, and understand why you need to wait after connecting it to a voltage, before taking the reading.

Best Wishes
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Tue Jan 18, 2011 9:40 am     Reply with quote

Another thing you might want to add is a small delay (~10ms) between these lines:

Code:
             SET_ADC_CHANNEL(1);
               analin = read_adc();


you need to allow some time for the sample and hold capacitor to charge.
else you will get wrong reads...

... just my two cents..

hope that helps..

G

Edit: i just noticed somebody already pointed this out...
_________________
CCS PCM 5.078 & CCS PCH 5.093
ahliang530



Joined: 18 Nov 2010
Posts: 9

View user's profile Send private message MSN Messenger

PostPosted: Wed Jan 19, 2011 7:01 am     Reply with quote

Gabriel wrote:
Another thing you might want to add is a small delay (~10ms) between these lines:

Code:
             SET_ADC_CHANNEL(1);
               analin = read_adc();


you need to allow some time for the sample and hold capacitor to charge.
else you will get wrong reads...

... just my two cents..

hope that helps..

G

Edit: i just noticed somebody already pointed this out...


thanks for your advice .. i will try it out on my PIC ..
temtronic



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

View user's profile Send private message

PostPosted: Wed Jan 19, 2011 8:47 am     Reply with quote

hay Ttelmah

You can actually have a PIC survive the 'reversed power pins ' error....

...at least if it's only in backwards for 5-6 seconds !
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Wed Jan 19, 2011 9:17 am     Reply with quote

and if the supply is current limited....

Best Wishes Very Happy
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Wed Jan 19, 2011 11:27 am     Reply with quote

"The most expensive component will always blow first to protect the fuse" or words to that effect Laughing

mikey
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
ahliang530



Joined: 18 Nov 2010
Posts: 9

View user's profile Send private message MSN Messenger

PostPosted: Sun Jan 23, 2011 10:33 am     Reply with quote

temtronic wrote:
Well, your program is a good example as to why I never use Proteus !!

First, it should never have compiled as line two is wrong.
hint: read the ADC section of the PIC datasheet.

Second, as you area novice, get rid of the TRIS() statements and let the compiler take care of creating the correct code.

Third, you should fully 'setup' the PIC by disabling any internal peripherals that you are not using. This includes comparators, ADC pins, I2C, etc.

Fourth, it looks like you're running the lcd_init function inside the 'forever' loop.
It only needs to be called once, at the beginning of main but before the forever loop.

I create a 'setupPIC()' function, which defines pins,enable/disable peripherals, and calls 'one time' setups (like your LCD_INIT), as well as clears/sets I/O pins to my defaults. This setupPIC() function is then executed once, at the top of Main, before the forever loop.

Also be aware that floating point math uses a LOT of memory! Replacing it with integer math with really improve overall speed and use of memory.

hope this helps


I try on my board .. but its not working
the LCD show a raw of dark box with showing ntg ...
I need HELP :(
temtronic



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

View user's profile Send private message

PostPosted: Sun Jan 23, 2011 10:59 am     Reply with quote

You seem to call lcdinit() several times???

I only call mine once, in the 'startup routine', after the code to setup the peripherals,variables,etc.

It'll be easier to debug if..
You create a new program just to test the LCD.
The classic 'Hello World' program.

That way you can verify the LCD runs properly.

Start off with small programs, build on what you learn and keep backups !

oh, and you still have #device adc=12 which is NOT correct and must be fixed.
ahliang530



Joined: 18 Nov 2010
Posts: 9

View user's profile Send private message MSN Messenger

PostPosted: Sun Jan 23, 2011 12:08 pm     Reply with quote

temtronic wrote:
You seem to call lcdinit() several times???

I only call mine once, in the 'startup routine', after the code to setup the peripherals,variables,etc.

It'll be easier to debug if..
You create a new program just to test the LCD.
The classic 'Hello World' program.

That way you can verify the LCD runs properly.

Start off with small programs, build on what you learn and keep backups !

oh, and you still have #device adc=12 which is NOT correct and must be fixed.



Code:
#include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)


#include <LCDDRIVER.c>

void main()
{




    while(1)
    {
         

                lcd_init();
               delay_ms(10);
                lcd_putc("\f");
               lcd_gotoxy(1,1);
               printf(lcd_putc,"Hi, Milo 520");
         
            
   
}

}


my code is just simple as this .. but its not working ..
frustrated :(
temtronic



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

View user's profile Send private message

PostPosted: Sun Jan 23, 2011 12:15 pm     Reply with quote

Usually you have 'setup' or 'configuration' code before 'main', to tell the compiler what the I/O pins are to be used for,disabling interrupts,configuring onboard perihpherals,etc..

Since you haven't defined any I/O pins, perhaps there's a problem with the LCDDRIVER.C that you're using.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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