![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
morebudwiser
Joined: 17 Oct 2005 Posts: 26
|
do while loop not looping |
Posted: Tue Jul 15, 2008 4:49 pm |
|
|
hi i want to go round a do while loop until the reading from my adc falls within a certain range. Here is the code for the loop i am using but seems to run down the code then not execute the loop. Is this code correct ? many thanks morebudwiser
ver4.065
Code: |
do
{
value=read_adc_value(0); // Read (AIN1+, AIN1-) differential inputs
printf(lcd_putc,"\fValue = %6ld\n", value)
Voltage = (float)(value * 2.5 )/65535 * 4 + 0.0265 ;
printf(lcd_putc, "%3.3f\n", Voltage);
delay_ms(1000);
}while((value >= 7024)&&(value <= 7029) ); do while reading is not in range
|
|
|
![](templates/subSilver/images/spacer.gif) |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Wed Jul 16, 2008 2:00 am |
|
|
"until the reading from my adc falls within a certain range."
"while((value >= 7024)&&(value <= 7029) );"
The loop will run while value is in the range 7024 to 7029 inclusive. Your statement says you want to exit the loop when this happens!
Try
while((value < 7024) || (value > 7029) );
If this is not the case then please show output from you routine so we know if vallue is in range or not!
Also, please show the rest of the code. In paticular we need to see the definition of value. Is it an int16, signed int16 or something else ? |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Wed Jul 16, 2008 2:27 am |
|
|
Lots of other comments:
value=read_adc(0);
What 'type' is value?.
Get out of the habit of using fixed numbers like '0' to feed the functions. Use the CCS defines instead. There is no guarantee, that in a years time, they won't change the value needed by the function, to perform a particular operation - this has happened in the past. The whole point of the defines, is to allow values to change in future releases, without people having to re-write code. Also to allow code to move to other chips,that may require different values. Hence _use them_. '0' is not a legitimate value, an may well cause this function to hang. Either leave the entry blank and use the default 'pause and read' operation, or use the 'ADC_START_AND_READ' define.
Now, you 'comment' "Read (AIN1+, AIN1-) differential inputs". Where is the setup to do this?. What is the chip?. Most PICs don't support differential ADC operation.
What is the accuracy of this chips ADC?. Have you got the higher resolution mode selected in the setup?. Otherwise the value will not be in the expected range.
You are using the external Vref apparently (from the 2.5 in your formula). Again where is your setup.
Again, what is the chip?. Only a couple of PICs offer more than 10bit accuracy on the ADC (0-1023 counts), yet you are looking for an output from the ADC function around 1756.
Your test is reversed as already pointed out.
The range is too small to ever be reliable. If this is a chip with a high accuracy ADC, the range would correspond to counts of just 1756, and 1757 on the ADC. A range of just 38uV!... If this is a normal PIC, with the 10bit ADC, left justified to give a 16bit result, the test cannot work. The output will jump in steps of 64 counts setup this way, with 'resolvable steps' of 2.4mV, and the smallest change in the final numbers possible, is 256 counts (with your *4)....
The higher accuracy ADC's, in most cases, are _audio_ ADC's. These do not guarantee good linearity over their range. You are unlikely to get exactly the number you expect for a particular voltage working to this sort of accuracy...
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
morebudwiser
Joined: 17 Oct 2005 Posts: 26
|
|
Posted: Wed Jul 16, 2008 1:07 pm |
|
|
hi thanks for the reply s, you were correct the logic in the do while loop was incorrect your suggested code fix the problem |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|