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 support@ccsinfo.com

PIC18f4520 analog; library terms are incorrect in booklet

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



Joined: 27 Mar 2008
Posts: 3

View user's profile Send private message

PIC18f4520 analog; library terms are incorrect in booklet
PostPosted: Tue Jul 15, 2008 9:32 am     Reply with quote

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);
}
}
guest1
Guest







PostPosted: Tue Jul 15, 2008 9:50 am     Reply with quote

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.
guest1
Guest







Thanx
PostPosted: Tue Jul 15, 2008 11:04 am     Reply with quote

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);
}
}
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