View previous topic :: View next topic |
Author |
Message |
x_Stickler_x
Joined: 27 Mar 2008 Posts: 3
|
PIC18f4520 analog; library terms are incorrect in booklet |
Posted: Tue Jul 15, 2008 9:32 am |
|
|
Hello people of CCS forum or CCS,
I am having a noob issue and I can't figure out why my simple ADC program will not work. I did it straight from the PIC developer kit (PIC18F4520). I talked with my electronics engineer and he said the written portion of the book was incorrect to the 18f4520.h library. I have looked up the correct ports and library terms and it still is not working. My leds do not light nor do I get anything from the analog. Can anyone help me?
program:
Code: | #include <18f4520.h>
#device ICD=TRUE
#fuses HS,NOLVP,NOWDT
#use delay (clock=20000000)
#define GREEN_LED PIN_A5
#define YELLOW_LED PIN_B4
#define RED_LED PIN_B5
#define PUSH_BUTTON PIN_A4
#define cutoff 128 //2.5 volts
#define neutral_zone 25 //0.5 volts
light_one_led(int led)
{
output_high (GREEN_LED);
output_high (YELLOW_LED);
output_high (RED_LED);
switch(led)
{
case 0 : output_low (GREEN_LED); break;
case 1 : output_low (YELLOW_LED); break;
case 2 : output_low (RED_LED); break;
}
}
void main()
{
int reading;
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
while (TRUE)
{
reading = read_adc();
if (reading<(cutoff-neutral_zone/2))
light_one_led(GREEN_LED);
else if (reading>(cutoff+neutral_zone/2))
light_one_led(RED_LED);
else
light_one_led(YELLOW_LED);
}
} |
|
|
![](templates/subSilver/images/spacer.gif) |
guest1 Guest
|
|
Posted: Tue Jul 15, 2008 9:50 am |
|
|
You should be passing the function light_one_led an integer with the value 0, 1 or 2. I'm not sure what you are actually passing it, it depends on the #defines of pin_* in the header file. |
|
![](templates/subSilver/images/spacer.gif) |
guest1 Guest
|
Thanx |
Posted: Tue Jul 15, 2008 11:04 am |
|
|
THANK YOU!!!
This works now. Kudos!!!
#include <18f4520.h>
#device ICD=TRUE
#fuses HS,NOLVP,NOWDT
#use delay (clock=20000000)
#define GREEN_LED PIN_A5
#define YELLOW_LED PIN_B4
#define RED_LED PIN_B5
#define PUSH_BUTTON PIN_A4
#define cutoff 128 //2.5 volts
#define neutral_zone 25 //0.5 volts
light_one_led(int led)
{
output_high (GREEN_LED);
output_high (YELLOW_LED);
output_high (RED_LED);
switch(led)
{
case 0 : output_low (GREEN_LED); break;
case 1 : output_low (YELLOW_LED); break;
case 2 : output_low (RED_LED); break;
}
}
void main()
{
int reading;
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
while (TRUE)
{
reading = read_adc();
if (reading<(cutoff-neutral_zone/2))
light_one_led(0);
else if (reading>(cutoff+neutral_zone/2))
light_one_led(2);
else
light_one_led(1);
}
} |
|
![](templates/subSilver/images/spacer.gif) |
|