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

18F6520

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



Joined: 20 Apr 2007
Posts: 111

View user's profile Send private message Send e-mail

18F6520
PostPosted: Fri Jul 06, 2007 5:48 am     Reply with quote

I want use PIC 18F6520 and I would like know if my code below is ok.
When I compile there are no errors but when I simulate on my lcd just appear 00000.

tks

nina


#include <18F6520.h>
#device ADC=16
#use delay(clock=4000000)
#FUSES HS, NOPROTECT, NOPUT, NOBROWNOUT, NOWDT, NOCPD
#include <flex_lcd.c>
#use fast_io (A)
#use fast_io (B)
#use fast_io (D)


void main(void)
{

float pot,r,t,b;

lcd_init();
enable_interrupts(GLOBAL);
setup_port_a(ALL_ANALOG);
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);

do{
set_adc_channel(0);
pot=read_adc();
r = pot*5;
t = r/4096;
b = (t*260/5);

if (b > 100)
{
output_high(PIN_D0);
}
if (b < 100)
{
output_low(PIN_D0);
}


lcd_gotoxy(1,1);
delay_ms(500);
printf(lcd_putc,"Shunt: %f",b);
delay_ms(500);
}while(true);

}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jul 06, 2007 2:27 pm     Reply with quote

Here's a demo program to display the voltage on pin A0 on an LCD.
Use it as a guide to writing your own program.
http://www.ccsinfo.com/forum/viewtopic.php?t=28842&start=7


The program that you posted has several problems.

1. It's missing the NOLVP fuse.

2. You enable global interrupts, but you don't have any need to do
this, because you don't have an interrupt service routine.

3. You enable "fast_io" mode for port D, but then you don't have a
statement to set the TRIS for port D. But in fact, there is no need
for fast_io mode to be declared in your program at all.

4. You have "#device ADC=16" specified, but this will left-justify the
10-bit result from the A/D. In other words, the result will be
multiplied by 32. I don't know why you're doing this. I didn't
try to understand your math. My suggestion is just look at the
example in the link above.

5. There are other minor issues, such as you're setting the adc channel
inside the do-while() loop. You don't need to do this. You're always
using the same channel, so you could just set it one time, before you
enter the loop.
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