![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
nfs
Joined: 28 Apr 2004 Posts: 18
|
where's the bug in this code? |
Posted: Fri May 21, 2004 9:23 am |
|
|
sorry, i've a great problem with my code, it simulate successfully with ISIS simulator but when i wrote it to the PIC never seems to be right.
so if sone find the problem plz tell me
Code: |
#include <16F876.h>
/* Set configuration bits in the PIC processor */
#fuses XT, NOPROTECT, NOPUT, NOWDT, NOBROWNOUT, NOLVP, NOCPD, NOWRT
//crystal, no watchdog timer, no low programming, no powerup timer, noprotect=memory protection off
#device adc=10
#device *=16
#use delay (clock=4000000) /* sets appropriate compiler constants 1µsecond instruction cycle */
#use standard_io ( a )
#use standard_io ( b )
#use standard_io ( c )
#define led1 PIN_A1 //led jaune commutateur '0'
#define led2 PIN_A2 //led verte commutateur '0'
#define led3 PIN_A3 //led rouge commutateur '0'
#define Vppfluct 1003 //seuil autorisé de fluctuation de la tension
#define Vppmin 973 //seuil pour lequel la tension est passée du niveau haut à un niveau plus bas*****
#define Vctq 921 //seuil de chute de la tension
#define tension_maxi_touche_0 34
#define tension_mini_touche_0 0
#byte INTCON = 0x0b //registre des interruptions
#byte PIE1 = 0x8c //registre contenant les autorisations d'interruptions
#byte PIR1 = 0x0c
#byte ADCON0 = 0x1f //registre du convertisseur
#byte ADCON1 = 0x9f //rôle des pins+justification du resultat de la conversion
#bit GO_DONE = ADCON0.2
#bit ADON = ADCON0.0
#bit ADIE = PIE1.6
#bit ADIF = PIR1.6
#define TMR1IE = PIE1.0
#define TMR1IF = PIR1.0
int16 ADCvaleur;
int32 ADCvtot;
int32 Counter1;
int32 Counter2;
int8 counter4;
int32 counter3 =499;
int32 cnt;
#int_TIMER2
TIMER2_isr()
{
Counter1++;
Counter2--;
}
void InitialiseADC ()
{
setup_port_a( RA0_ANALOG );
setup_adc( ADC_CLOCK_DIV_8 );
set_adc_channel( 0 );
delay_us(10);
}
void main(void)
{
long ADCvaleur;
int32 ADCvtot;
OUTPUT_B(0x00);// clear all pins on port b
ADCON0=0x81; // fosc/32, RA0, a/d off, a/d operating
ADCON1=0x8E; // an0 is a/d input, rest port a digital, internal ref, right justified
INTCON=0xc0; // enable global interrupts and peripherals
InitialiseADC();
if (ADIF=1)
ADIF = 0;
ADCvaleur = 0;
ADCvtot = 0;
Counter1 == 0;
setup_timer_2(T2_DIV_BY_4,5,5);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
ADCvaleur=read_ADC();
while (Counter1<19999)
{
ADCvaleur=read_ADC();
if (ADCvaleur>Vppfluct)
{
output_high (led1);
}
else
if (ADCvaleur=Vppfluct)
{
output_low(led1);
disable_interrupts (INT_TIMER2);
delay_ms(10);
ADCvaleur=read_ADC();
if ((ADCvaleur<=tension_maxi_touche_0) && (ADCvaleur>=tension_mini_touche_0))
{
counter1==0;
setup_timer_2(T2_DIV_BY_4,5,5);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
ADCvaleur=read_ADC();
while ((counter1<=counter3) && (ADCvaleur<=tension_maxi_touche_0) && (ADCvaleur>=tension_mini_touche_0))/* increment count */
{
ADCvaleur = Read_ADC(); /* read the ADC */
ADCvtot = ADCvtot + ADCvaleur; /* add it to total, for later averaging */
}
disable_interrupts (INT_TIMER2); /* don't allow interrupts */
ADCvtot /= (long)500; /* division par 500 échantillons pour calculer la moyenne des tensions relevée */
if ((ADCvtot<=tension_maxi_touche_0) && (ADCvtot>=tension_mini_touche_0))
{
continue;
}
else
{
output_high(led3);
}
counter1==0;
setup_timer_2(T2_DIV_BY_4,10,5);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
ADCvaleur=read_ADC();
if ((ADCvaleur >= ADCvtot+ADCvtot*0.1))
{
disable_interrupts(INT_timer2);
delay_ms(20);
counter2==200;
setup_timer_2(T2_DIV_BY_4,15,5);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
ADCvaleur=read_ADC();
if ((ADCvaleur>=Vppfluct) && (counter2>=0))
{
output_high(led2);
disable_interrupts(INT_timer2);
disable_interrupts(GLOBAL);
}
else
output_high(led3);
}
else
output_high(led3);
}
else
output_high(led3);
}
}
if ((counter1++==19999)&&(ADCvaleur>=Vppfluct))
{
disable_interrupts (INT_TIMER2);
output_high(led3);
output_low(led1);
disable_interrupts(GLOBAL);
}
}
|
i know that is so longer but i can't delete parts cause it can be significant
thanks for your time _________________ nfs
Last edited by nfs on Mon May 24, 2004 5:08 am; edited 1 time in total |
|
![](templates/subSilver/images/spacer.gif) |
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Fri May 21, 2004 10:41 am |
|
|
Just looking at your code, quickly, I can see that you are making a common mistake in swapping '=' and '=='. It looks like you are trying to assign '0' (zero) to Counter1 in several parts of your code but you are using '==' (equality) instead. Try looking through your code and make sure you have the correct '=' or '==' in each statement.
Ronald |
|
![](templates/subSilver/images/spacer.gif) |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Fri May 21, 2004 12:40 pm |
|
|
Ronald is right, your major error is mixing '=' and '==' (at least 5 times).
Another bug:
Code: |
if ((ADCvaleur>=Vppfluct) && (counter2>=0))
|
counter2 is an unsigned int32, so Counter2 is doing nothing.
I suggest to change it to:
Code: |
if ((ADCvaleur>=Vppfluct) && (counter2>0))
|
and then also
Code: |
int8 Counter2;
#int_TIMER2
TIMER2_isr()
{
Counter1++;
if (Counter2)
Counter2--;
}
|
Carlo |
|
![](templates/subSilver/images/spacer.gif) |
guest Guest
|
re |
Posted: Tue May 25, 2004 10:25 am |
|
|
i have the same idea than those who posted. you can verify if you have a infinished boucle.
bye |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|