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

Multiplication

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



Joined: 01 Feb 2018
Posts: 42

View user's profile Send private message

Multiplication
PostPosted: Wed Mar 28, 2018 12:21 pm     Reply with quote

In channel 0 voltage = 5v
In channel 1 voltage = 2v

Quote:
set_adc_channel(0);
k=read_adc();
set_adc_channel(1);
a=read_adc();
y=k*a;
According to the above code the y should be 10v but it's not 10v. What is the problem?
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Wed Mar 28, 2018 12:59 pm     Reply with quote

I don't understand what you're trying to do!

To me 5V * 2V = 10V^2 (I.e 10 volt squared.)

What answers are you getting with your hardware?

Show us a short complete code which illustrates the problem.

Mike
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Mar 28, 2018 1:18 pm     Reply with quote

Post your variable declarations for y, k, a.
newguy



Joined: 24 Jun 2004
Posts: 1907

View user's profile Send private message

PostPosted: Wed Mar 28, 2018 1:20 pm     Reply with quote

What are "k", "a", and "y" defined to be? If they are defined as "int" and you're dealing with a 16- or 18-series PIC, then "int" = "int8" = "byte". Define your variables as being "unsigned int16" and see what happens.
temtronic



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

View user's profile Send private message

PostPosted: Wed Mar 28, 2018 5:11 pm     Reply with quote

We also don't know which PIC, how the ADC is setup (Vref comes to mind as well as clock speed) and what your answer to y=k*a is, other than you say it's not 10V.
There also could be HUGE errors if you're using floating point math, or some other part of the program could be affecting the 'y' result BEFORE it is transmitted to you. Now IF the 'y' answer is going to a PC, perhaps the PC program that is receiving the data is misinterpreting the data, or the transmission between the unknown PIC and the PC is bad.
We can probably think of 100 different and each possibly correct reasons for the 'error', but we NEED to see your entire program to locate it for you.

Jay
PROMOD



Joined: 01 Feb 2018
Posts: 42

View user's profile Send private message

PostPosted: Wed Mar 28, 2018 8:04 pm     Reply with quote



I wold like measure the instantaneous power at t1 for the above voltage and current wave. For this I write the following code.

Code:
#include<18f4431.h>
#device adc=10
#fuses xt,nowdt,nolvp
#use delay(clock=40000000)
#include "flex.c"

float k,a,r=0,p=0,average,energy,energy_consumed; int16 i=0,q=0;

#int_timer2
void pwm_interrupt()
{
set_adc_channel(0);
k=read_adc();
k=k/204;
set_adc_channel(1);
a=read_adc();
a=a/204;
i=i+1;
}

void main()
{
lcd_init();
setup_timer_2(t2_div_by_16,98,16);
enable_interrupts(global);
enable_interrupts(int_timer2);
setup_adc_ports(all_analog);
setup_adc(adc_clock_internal);
r=k*a;
printf(lcd_putc,"EC=%f",k);

while(1)
  {
   if(i>q)
     {
      r=r+k*a;
      q=i;
     }
   if(i==8)
     {
      average=r/i;
      energy=average/50;
      energy_consumed=energy_consumed+energy;
      lcd_gotoxy(1,1);
      printf(lcd_putc,"EC=%f",energy_consumed);
      i=0;
      q=0;
     }
  }
}

Is it ok?
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Thu Mar 29, 2018 2:49 am     Reply with quote

No, it's not OK.

First, the Pic can't handle negative inputs, so you need to offset your analogue inputs to roughly half your reference voltage.
Second, your making it hard work by using floats throughout, use integers and either use the decimal adjusted print or convert to floats at the end.
Third, I showed a solution to this problem recently on this forum. Follow this link:-
https://www.ccsinfo.com/forum/viewtopic.php?t=56115&start=0&postdays=0&postorder=asc&highlight=

Mike
temtronic



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

View user's profile Send private message

PostPosted: Thu Mar 29, 2018 5:09 am     Reply with quote

In addition to Mike's comments...
this...
setup_adc(adc_clock_internal);
is WRONG

Please consult table 20-2 ,in the ADC section of the datasheet as to why it is wrong.
As a sidenote, I'd like to know HOW you decided 'internal' was a valid option, as we see it posted here a lot. If it's from some Mcrochip document, please post a link to it,as it is in error.

Also ,as a general comment, you should add comments at the end of each line to tell others what the code is doing. While some code may be obvious,most isn't , like why /204 in the ISR, why energy/50 in main?
You could have 'bad math' where /202 is correct, not /204 but we can't tell you as we don't know the reason for those numbers. While it may be obvious to you now, come Fall, you'll be wondering too !

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19496

View user's profile Send private message

PostPosted: Thu Mar 29, 2018 6:04 am     Reply with quote

Unfortunately Jay, a couple of the old CCS examples use it.
These were ones just trying to show 'operation' without worrying about accuracy, and they didn't want to have to work out the correct divisor for different clock rates.

Then it gets posted here a lot.

It's also OK on some of the newest PIC's (particularly PIC24's).

So it keeps getting used. I think it's going to have to evolve into a 'sticky', but people don't seem to read these either.... Sad
PROMOD



Joined: 01 Feb 2018
Posts: 42

View user's profile Send private message

PostPosted: Fri Mar 30, 2018 10:51 am     Reply with quote

temtronic wrote:

setup_adc(adc_clock_internal);
is WRONG
Hello! Could you please explain it a little bit more? instead of it which clock should I use?
temtronic



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

View user's profile Send private message

PostPosted: Fri Mar 30, 2018 12:34 pm     Reply with quote

Which adc clock you use, has to be determined by you, the programmer and the designer of the product. If you look at the ADC section of the datasheet, table 20-2 shows a selection of adc clock speeds. Given the 40MHz of the PIC clock, you have 2 choices. Which one YOU decide has to be based upon how YOU use the ADC and what sort of analog input you've designed. Chapter 20.3 tells about the 'math', as well there's a chart or two.
Generally speaking, it's best to 'buffer' the signal going to the PIC's ADC section. That can be as simple as a single channel 'rail to rail' input AND output opamp though I always use 4 channel opamps. Similar price and greater PCB options...like the client decides they NEED another sensor input !
Unlike digital where there are 3 states and easy to understand, proper analog design and PCB layout can take decades to master (if THAT is possible).
The good new for you is there's ONLY 2 clock choices, select one, run 10,000 samples and then run the other, run 10,000 more examples and decide which is best for you.

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