View previous topic :: View next topic |
Author |
Message |
The_Todd
Joined: 11 Feb 2007 Posts: 5 Location: Santa Clara University
|
PIC18F4520 Development Kit Comment & Question Exercise 7 |
Posted: Sun Feb 11, 2007 6:23 pm |
|
|
First of all, I just bought the development kit with the student discount and am really enjoying it. I'm working through the exercise book, and am have a few troubles with example 7.
The example reads a POT on A0 and looks like this:
Code: | #include <prototype.h>
#include <utility.c>
#define cutoff 128 //2.5 volts
#define neutral_zone 25 // .5 Volts
void main() {
int reading;
setup_adc_ports( RAO_ANALOG );
setup_adc( ADC_CLOCK_INTERNAL);
set_adc_channel( 0 );
while(true) {
reading = read_adc();
if(reading<cutoff>(cutoff+neutral_zone/2))
light_one_led(RED);
else
light_one_led(YELLOW);
}
}
|
I believe that RA0_ANALOG is an error because it is not defined in the H file, after doing some digging, replacing RA0_ANALOG with AN0 makes the program work correctly (FUN!), I thought I should let you know for the next printing.
Here is my real question:
Also with the Further Study in Exercise 7 part A, it suggests adding ADC=16 to the #device ICD=TRUE line. "this will change the range to 0-1023".
I did that, but the range is definitely not 0-1023. Putting a watch on the reading variable, I get various values with the POT between 0-192. I am guessing this is because the reading variable is a INT (maximum 255, right?), so I change int reading; to long reading;, but then I get values between 128-38976.
Can someone help me with this?
Thanks,
Todd |
|
|
The_Todd
Joined: 11 Feb 2007 Posts: 5 Location: Santa Clara University
|
|
Posted: Sun Feb 11, 2007 7:05 pm |
|
|
So with a little more digging i've found that the answer to my question is that there is another error in exercise 7. Anyways, it should suggest adding ADC=12, as this will return a range of 0-1023, while ADC=16 will return 0-65472. It should also suggest changing reading to a long.
CCS was having some trouble "stepping over" reading = read_adc(), but "run to cursor" to the next line returned good values.
Todd |
|
|
Storic
Joined: 03 Dec 2005 Posts: 182 Location: Australia SA
|
|
Posted: Sun Feb 11, 2007 7:33 pm |
|
|
you have to ask yourself, are the question deliberately incorrect so you have to debug/find the fault to make it work.
forces you to understand the answer over parrot fashion learning.
I have always learn't more by mistakes than getting it right 1st time.
Andrew _________________ What has been learnt if you make the same mistake? |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1907
|
|
Posted: Sun Feb 11, 2007 7:36 pm |
|
|
Storic wrote: | I have always learn't more by mistakes than getting it right 1st time.
Andrew |
Amen. |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Sun Feb 11, 2007 8:24 pm |
|
|
The_Todd,
Thanks for the info for those here who might benefit. It sounds like you are coming along nicely!
However, as you might have noticed at the top of the web page, it says CCS doesn't monitor this page and to send bug reports to support@ccsinfo.com
I think they might appreciate the feedback on the kit....
Hang in there and, as you said earlier, have fun!!! |
|
|
|