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

what is the problem

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



Joined: 28 Apr 2004
Posts: 18

View user's profile Send private message

what is the problem
PostPosted: Thu Apr 29, 2004 8:29 am     Reply with quote

i use the ccs_compiler and i try to make a program who read the voltage from the ADC, compare it to a value and send me response throug led: this is the source:

#include <16F876.h>
/* Set configuration bits in the PIC processor */
#fuses XT, NOPROTECT, NOPUT, NOWDT, NOBROWNOUT, NOLVP, NOCPD, NOWRT

#device adc=10
#device *=16
#use delay (clock=4000000) #use standard_io ( a )
#use standard_io ( b )
#use standard_io ( c )
#define led1 PIN_A1
#define led2 PIN_A2
#define led3 PIN_A3
#define Vpp 1024
#define Vppmin 973
#define Vss 0
#define Vctq 921
#define tension_maxi_touche_0 34
#define tension_mini_touche_0 0
#define tension_maxi_touche_R 790
#define tension_mini_touche_R 750
#define tension_maxi_touche_plus 523
#define tension_mini_touche_plus 463
#define tension_maxi_touche_moins 287
#define tension_mini_touche_moins 245
#bit ADFM_BIT=0x9F.7
#bit PCFG3_BIT=0X9F.3
#bit PCFG2_BIT=0X9F.2
#bit PCFG1_BIT=0X9F.1
#bit PCFG0_BIT=0X9F.0
#byte ADCON0 = 0x1f
#byte ADCON1 = 0x9f
#bit start_ad = ADCON0.2
int16 us;
int16 ADCvaleur;
static char cADCflag; /* global variable */

void InitialiseADC (unsigned char ADC_Channel)
{
setup_port_a( RA0_ANALOG );
setup_adc( ADC_CLOCK_DIV_8 );
set_adc_channel( 0 );
}


void main( void )
{
long ADCvaleur;
int i, k;
output_low (led1);
output_low (led2);
output_low (led3);

InitialiseADC(0);
ADFM_BIT=1;
PCFG3_BIT=1;
PCFG2_BIT=1;
PCFG1_BIT=1;
PCFG0_BIT=0;


output_high (led1);
delay_ms(5000);
start_ad = 1;
ADCvaleur = 0x0000;
ADCvaleur = (unsigned int16)(read_ADC(0));
delay_us(50);
if (ADCvaleur<=921)
{
output_low (led1);
output_high(led2);
delay_ms(400);
output_low(led2);
}
else
{
output_high (led3);
delay_ms(500);
output_low (led3);
}
}

thanks
_________________
nfs
Couch



Joined: 20 Jan 2004
Posts: 9
Location: Ottawa, Ontario Canada

View user's profile Send private message

PostPosted: Thu Apr 29, 2004 8:39 am     Reply with quote

nfs

post the version of compiler you are using, and what specific problem are you seeing (eg. not compiling, wrong a/d value)? Add more info to your post.
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Thu Apr 29, 2004 8:41 am     Reply with quote

Few of us are going to wade through your code without some hint as to what we should look for. Can you tell us what goes wrong? Does it compile? Do the LEDs ever light? Do they ever go out?

Consider adding some diagnostic output, maybe from a software UART to an unused pin to tell you what the PIC thinks it is doing. Just printing the name of a subroutine every time you enter it can work wonders.

Well one thing does stand out... I don't see any looping structure. It seems you code will run once though and STOP. If you want to keep checking the A/D over and over you will have to loop your code so it keeps running.
_________________
The search for better is endless. Instead simply find very good and get the job done.
nfs



Joined: 28 Apr 2004
Posts: 18

View user's profile Send private message

re
PostPosted: Thu Apr 29, 2004 8:42 am     Reply with quote

version 3.168
and it compiled succesfully but when i want to simulate it the ADC unit isn't working. i simulate it with ISIS and i program it on the PIC.
_________________
nfs
ttelmah
Guest







Re: re
PostPosted: Thu Apr 29, 2004 9:28 am     Reply with quote

nfs wrote:
version 3.168
and it compiled succesfully but when i want to simulate it the ADC unit isn't working. i simulate it with ISIS and i program it on the PIC.

Comments:
1) You are using the CCS function to set the result format, and the channel, then 'overriding' these manually. Go with one or the other. Either go completely manual, and set the data yourself, or rely on the CCS functions. As it is, it is very difficult to really predict how the code will work...
2) The same applies with the ADC operation. You are triggering a conversion, not waiting for the conversion to complete, and then issuing a 'read_adc' command (which normally expects to issue a conversion itself...).
3) Do you know that the ISIS simulator can simulate the ADC correctly?.
Many simulators have restrictions (MicroChip's own simulator in MPLAB, cannot simulate all peripherals).
Simplify the code. Use the Microchip functions (they do 99.9% work, and make it much easier to understand what you are trying to do). If it still doesn't work, try it in a chip instead...

Best Wishes
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Thu Apr 29, 2004 11:00 am     Reply with quote

Make sure you have a while(1) statement in there so the code does not fall off the end and cause the PIC to fall asleep.

Try this code and see if it works for you.

Code:

#include <16F876.h>
/* Set configuration bits in the PIC processor */
#fuses XT, NOPROTECT, NOPUT, NOWDT, NOBROWNOUT, NOLVP, NOCPD, NOWRT

#device adc=10
#device *=16
#use delay (clock=4000000)

#define led1 PIN_A1
#define led2 PIN_A2
#define led3 PIN_A3
#define Vpp 1024
#define Vppmin 973
#define Vss 0
#define Vctq 921
#define tension_maxi_touche_0 34
#define tension_mini_touche_0 0
#define tension_maxi_touche_R 790
#define tension_mini_touche_R 750
#define tension_maxi_touche_plus 523
#define tension_mini_touche_plus 463
#define tension_maxi_touche_moins 287
#define tension_mini_touche_moins 245

#bit ADFM_BIT=0x9F.7
#bit PCFG3_BIT=0X9F.3
#bit PCFG2_BIT=0X9F.2
#bit PCFG1_BIT=0X9F.1
#bit PCFG0_BIT=0X9F.0

#byte ADCON0 = 0x1f
#byte ADCON1 = 0x9f
#bit start_ad = ADCON0.2
#bit GO_DONE = ADCON0.2

int16 us;
int16 ADCvaleur;
static char cADCflag; /* global variable */

void InitialiseADC ()
{
setup_port_a( RA0_ANALOG );
setup_adc( ADC_CLOCK_DIV_8 );
set_adc_channel( 0 );
}


void main( void )
{
long ADCvaleur = 0;
int i, k;
output_low (led1);
output_low (led2);
output_low (led3);

InitialiseADC();
ADFM_BIT=1;

output_high (led1);
delay_ms(5000);
ADCvaleur = read_ADC();

while(GO_DONE)
{
;
}

if (ADCvaleur<=921)
{
output_low (led1);
output_high(led2);
delay_ms(400);
output_low(led2);
}
else
{
output_high (led3);
delay_ms(500);
output_low (led3);
}

while(1)
{
;
}
}

SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Thu Apr 29, 2004 11:54 am     Reply with quote

Even the code RNIELSON supplied will only read the A/D once each time power is applied. This MIGHT be what he wants... but I doubt it. I would assume he wants a while (1) around the whole sampling and display code so the lights will change as the voltage varies.
_________________
The search for better is endless. Instead simply find very good and get the job done.
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Thu Apr 29, 2004 2:58 pm     Reply with quote

True. I put the while() at the end so he could see if the lights would change after the 5 second delay.

If at first you don't succeed, PUNT!
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