|
|
View previous topic :: View next topic |
Author |
Message |
Wind
Joined: 15 Jun 2009 Posts: 5
|
impossible to enter while() loop ! |
Posted: Fri Jul 17, 2009 2:35 pm |
|
|
Hi there,
I've wrote a code for my PIC16F877 based card, I'm using some pins as Inputs and others as outputs,
inputs : PIN_B6, PIN_A2 and PIN_A5
outputs : PIN_B1, PIN_C1 and PIN_D4
The purpose of my program is simple, according to the status of each inputs, I have to activate/deactivate some outputs.
To read the inputs, I'm using two different ways :
-the read of PIN_B6 is done with the RB interruption routine (see the code below, it is working well)
-the read of PIN_A2 & PIN_A5 should be done in the while(true) loop of the main, but, for some reason I don't know, the program enters while(true) in the beginning and never enter again,
Then I can't read the status of PIN_A2 & PIN_A5.
I've put some instructions in the while(true) loop like printf() but never of them was executed.
I've tried my code for real in my microcontroller card and I got the same behaviour of the Proteus/Isis simulator.
What should I do to make the program entering the while() loop ?
Your help will be much appreciated, thanks in advance .
Compiler version : PCW 4.057
PIC : 16F877
compiling with : 0 errors, 0 warnings.
here is my code :
Code: | #include <16F877.h>
#device adc=8
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 20000000)
#use rs232 (baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#byte Portb = 6 // Address of PortB for 16F-series PICs
#define SysClock 20000000
#define Switch_deb input_state(PIN_A5)
#define Switch_press input_state(PIN_A2)
#define EVB PIN_D4
#define LED_ALR PIN_B1
#define Stop_pompe PIN_E1 // IO2
#define buzzer PIN_C1
float freq=3000,Cont_CCP_2_f;
long Cont_CCP_2;
int lect_int=0;
int1 switch_deb_activ=0,switch_press_activ=0,int_PORTB,sortie=1;
#priority rb,ccp2
#int_CCP2 //---------------COMPARAISON MODE (Buzzer)---------------
void CCP2_isr ( )
{
CCP_2=CCP_2 + Cont_CCP_2;
sortie=!sortie;
output_bit(buzzer,sortie);
}
#int_rb
void rb_isr ( )
{
int8 cb;
cb = Portb;
lect_int = Portb>>6;
int_PORTB = 1;
if (lect_int==1) //Detecting falling edge
{
printf("\n\rAlarme Pompe activ");
output_high(EVB);
output_high(LED_ALR);
enable_interrupts(INT_CCP2);
}
if (lect_int==0) //Detecting rising edge
{
printf("\n\rAlarme Pompe desactiv");
output_low(EVB);
output_low(LED_ALR);
disable_interrupts(INT_CCP2);
}
}
//-------------------Procedures--------------
void calcul_reg_cpp2() // calculating CCP2 register
{
Cont_CCP_2_f = SysClock/(2*freq*4*8); //8 = timer's 1 prescaler
Cont_CCP_2= (long) Cont_CCP_2_f ;
}
//-------------------------------THE MAIN PROGRAM-------------------
void main()
{
char cb;
set_tris_a(0x3F);
set_tris_e(0);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_spi(FALSE);
setup_ccp2(CCP_COMPARE_INT);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
cb = Portb; // Read PortB to clear mismatch condition
clear_interrupt(INT_RB);
enable_interrupts(INT_RB);
enable_interrupts(INT_TIMER1);
disable_interrupts(INT_CCP2);
enable_interrupts(GLOBAL);
//delay_ms(500); Caused blocking
calcul_reg_cpp2();
switch_press_activ=0;
switch_deb_activ=0;
while(true)
{
//-----receiving a logic Level from switch numuber 1
if( (Switch_deb==0) && !switch_deb_activ)
{
printf("\n\rSwitch debit actif");
output_high(EVB);
output_high(LED_ALR);
enable_interrupts(INT_CCP2);
output_high(Stop_pompe);
switch_deb_activ = 1;
}
if( (Switch_deb==1) && switch_deb_activ )
{
printf("\n\rSwitch debit desactive");
output_low(EVB);
output_low(LED_ALR);
disable_interrupts(INT_CCP2);
output_low(Stop_pompe);
switch_deb_activ=0;
}
//-----receiving a logic Level from switch numuber 2
if( (Switch_press==0) && !switch_press_activ)
{
printf("\n\rSwitch Pression actif");
output_high(EVB);
output_high(LED_ALR);
enable_interrupts(INT_CCP2);
output_high(Stop_pompe);
switch_press_activ = 1;
}
if( (Switch_press==1) && switch_press_activ )
{
printf("\n\rSwitch Pression desactive");
output_low(EVB);
output_low(LED_ALR);
disable_interrupts(INT_CCP2);
output_low(Stop_pompe);
switch_press_activ=0;
}
}
}
|
Here is a screen capture of my circuit.
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jul 17, 2009 3:48 pm |
|
|
Add more printf statements to show the activitity of your program.
Put in one near the start of main(), to show if the program begins.
Put another one just before the while() loop begins, to show that it
got that far. Put another one at the top of the while() loop, to show
that it executes the loop.
Code: | void main()
{
char cb;
set_tris_a(0x3F);
set_tris_e(0);
printf("Start\n\r"); // *** TESTING ***
.
.
.
printf("Before while\n\r"); // *** TESTING ***
while(true)
{
printf("Top of while\n\r"); // *** TESTING *** |
|
|
|
Wind
Joined: 15 Jun 2009 Posts: 5
|
|
Posted: Fri Jul 17, 2009 11:09 pm |
|
|
Hi Mr PCM,
thank you very much for your support.
I've add those three instructions, put the new code.hex into my PIC and supply it with power. then hyerterminal showed me
'start'
'Befo' (incomplete)
and then messages from the RB interruption routine (depending on RB6 status).
it's obvious now, the program do not execute the while() loop. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Jul 18, 2009 1:42 am |
|
|
Do some more testing. Disable global interrupts by commenting out
the following line in your code.
Quote: | enable_interrupts(GLOBAL); |
If it starts working, then debug it by finding out which interrupt is
causing the problem. Re-enable Global interrupts, and comment out the
line that enables INT_RB interrupts. If it now fails, then that's the
problem area to investigate some more.
If you're not sure where the problem is, one method to debug a program
is to comment out certain features or sections of the code, until it starts
working. |
|
|
Ttelmah Guest
|
|
Posted: Sat Jul 18, 2009 4:16 am |
|
|
The obvious reason for failure, is that there is no handler for 'int_timer1'.....
You must _never_ enable an interrupt, without some provision for handling it....
Best Wishes |
|
|
Wind
Joined: 15 Jun 2009 Posts: 5
|
|
Posted: Sat Jul 18, 2009 10:01 pm |
|
|
Hi,
now it's working correctly.:-)
I commented enable timer1 interruption ,in fact, I don't have
to enable it when I use ccp module. thank you very much Ttelmah.
;-) |
|
|
|
|
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
|