View previous topic :: View next topic |
Author |
Message |
nfs
Joined: 28 Apr 2004 Posts: 18
|
what is the problem |
Posted: Thu Apr 29, 2004 8:29 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
Couch
Joined: 20 Jan 2004 Posts: 9 Location: Ottawa, Ontario Canada
|
|
Posted: Thu Apr 29, 2004 8:39 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Thu Apr 29, 2004 8:41 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
nfs
Joined: 28 Apr 2004 Posts: 18
|
re |
Posted: Thu Apr 29, 2004 8:42 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
ttelmah Guest
|
Re: re |
Posted: Thu Apr 29, 2004 9:28 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Thu Apr 29, 2004 11:00 am |
|
|
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)
{
;
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Thu Apr 29, 2004 11:54 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Thu Apr 29, 2004 2:58 pm |
|
|
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! |
|
![](templates/subSilver/images/spacer.gif) |
|