|
|
View previous topic :: View next topic |
Author |
Message |
hoangtuanthien
Joined: 25 May 2013 Posts: 7
|
Program just run in the interrupt program. |
Posted: Sun Jun 23, 2013 8:51 am |
|
|
please help me know the reason why my program just run in the interrupt program, it can not perform the commands in the while(true) of the main program.
code:
Code: | #include <18f4550.h>
#include <math.h>
int16 dem;
int8 i;
//int16 dem1;
char const gt0[12]={47,30,32,32,43,38,35,43,26,37,36,38};
char const gtmin[12]={20,10,14,18,18,17,17,17,13,17,17,17};
char const gtmax[12]={56,40,61,55,55,54,54,54,55,54,54,54};
char const gocmax[12]={135,160,180,135,140,140,135,125,170,120,145,120};
char const flagsv[12]={1,0,1,0,1,0,0,1,0,1,1,0};
char sv[12];
void dunglen();
#int_TIMER0
void TIMER0_isr(void)
{
set_timer0(216);
dem++;
// dem1++;
//if(dem1==10000)
//{
// dem1=0;
// sv[0]++;
// output_toggle(pin_c2);
//}
}
int8 translate(int8 gt0,int8 gtmin, int8 gtmax,int8 gocmax,int flag,signed int8 gocset)
{
float tam,tang;
int8 gtri,tam1;
int bit;
tang=abs(gocset*((gtmax-gtmin)/gocmax));
tam=modf(tang,&tam1);
if (tam>=0.5){tang=tam1+1;}
else if(tam<0.5){tam=tam1;}
if (gocset>=0){bit=1;}
if(gocset<0){bit=0;}
if(flag&&bit==1){gtri=(gt0+tang);}
else if(flag&&bit==0){gtri=(gt0-tang);}
return gtri;
}
#int_TIMER1
void TIMER1_isr(void)
{
}
void main()
{
set_timer0(216);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_2|RTCC_8_bit); //128 us overflow
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1); //16.3 ms overflow
enable_interrupts(INT_TIMER0);
//enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
while(true)
{
if(dem==500)
{
dem=0;
output_d(0x3f);
output_e(0x07);
output_high(pin_a4);
output_b(0x30);
output_toggle(pin_c1);
}
if(dem==sv[0]){output_low(pin_d0);}
if(dem==sv[1]){output_low(pin_d1);}
if(dem==sv[2]){output_low(pin_d2);}
if(dem==sv[3]){output_low(pin_d3);}
if(dem==sv[4]){output_low(pin_d4);}
if(dem==sv[5]){output_low(pin_d5);}
if(dem==sv[6]){output_low(pin_e2);}
if(dem==sv[7]){output_low(pin_e1);}
if(dem==sv[8])output_low(pin_e0);
if(dem==sv[9])output_low(pin_a4);
if(dem==sv[10])output_low(pin_b5);
if(dem==sv[11])output_low(pin_b4);
for(i=1;i<=11;i++)sv[i]=0;
dunglen();
}
}
void dunglen()
{
// int8 i;
// for(i=0;i<=11;i++)
// {
sv[0]=translate(gt0[0],gtmin[0],gtmax[0],gocmax[0],flagsv[0],30);
// sv[0]=45;
// }
}
|
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Sun Jun 23, 2013 9:57 am |
|
|
Well depending on the processor's speed( unknown here), it could be spending ALL of it's time within the ISR.
No where in your program do you actually set the processor speed( clock).
Kinda interesting that it actually runs.....
hth
jay |
|
|
hoangtuanthien
Joined: 25 May 2013 Posts: 7
|
|
Posted: Sun Jun 23, 2013 10:49 am |
|
|
actually, in my program the first command is #include <timer.h> and in file timer.h set the processor speed (clock). it's here
Code: | #include <18F4550.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES PLL1 //No PLL PreScaler
#FUSES CPUDIV1 //No System Clock Postscaler
#FUSES USBDIV //USB clock source comes from PLL divide by 2
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES NOPUT //No Power Up Timer
#FUSES NOBROWNOUT //No brownout reset
#FUSES BORV20 //Brownout reset at 2.0V
#FUSES VREGEN //USB voltage regulator enabled
#FUSES PBADEN //PORTB pins are configured as analog input channels on RESET
#FUSES LPT1OSC //Timer1 configured for low-power operation
#FUSES MCLR //Master Clear pin enabled
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES ICPRT //ICPRT enabled
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOCPB //No Boot Block code protection
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#use delay(clock=16000000,crystal =16000000)
|
|
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Sun Jun 23, 2013 10:56 am |
|
|
Set up some diagnostic pin wobbles.
Then you will be able to see what's happening with a 'scope.
Ttelmah is probably pointing you in the right direction.
In your timer0 ISR you're setting timer0 to 216.
Timer0 defaults to 8 bit mode, ie counts to 255 then 0.
It's going to overflow in 80 machine cycles with div_by_2.
Same order as time to get in and out of the ISR!
Mike |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Sun Jun 23, 2013 12:36 pm |
|
|
Temtronic, not me....
However, exactly what I'd probably have pointed out.
Best Wishes |
|
|
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Sun Jun 23, 2013 12:47 pm |
|
|
One of the Mr. "T" guys :-) I definitely agree with the adding a "pin wobble" (never heard it described that way before, but it is perfect). Set an unused pin high at the start of the ISR, then set it low just before exiting the ISR. That allows you to see that the ISR is indeed executing as well as seeing how much time is being spent in there as opposed to in main.
mikey _________________ mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3 |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Sun Jun 23, 2013 12:56 pm |
|
|
"pin wobble", phrase, a 'blimey' term used to descibe an I/O pin that 'toggles' to give an indication that the program is running. Principally used by blokes with roots in merry old England!
cheers
Jay
on the other side of the pond |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Sun Jun 23, 2013 1:17 pm |
|
|
However it won't allow you to see how much time is taken in the ISR, rather than the main, since there are about 60 instructions of overhead 'outside' the handler code....
The timing will actually be that timer0, will zero, and the handler will be called. By the time it arrives at the code, the timer will have incremented to perhaps 8. The the routine sets it to 216, which reset the prescaler. It'll count from here, while the increment, clearing the interrupt flag, and resetting the registers takes place. Approximately 40 instructions, so it'll have counted to perhaps 236. So it should then have about 40 instruction times in the main before the next call. So about 25% of the time in the main.
With the processor running very slow, start by remembering that sv will all probably be 0xff at the start. Then the interrupt is executing all the time, so dem will change between the successive tests. Then the maths in dunglen, will take an age, so it'll hardly ever actually reach a test, and then the 'matching' value will almost certainly be missed.
Best Wishes |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Sun Jun 23, 2013 2:43 pm |
|
|
Apologies to both Mr T's.
I'm preparing to run a golf competition tomorrow, so not really paying full attention. You get the idea.
It is Sunday, and the forecast is good.
Seriously though; I was thinking in terms of putting 'wobbles' on different pins in both ISR and main, to get more 'information' out of the system.
Mike
PS. Glad the term was liked. It was the best an old guy could do.
I was typing slowly whilst the OP responded with all the fuses. |
|
|
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Sun Jun 23, 2013 7:10 pm |
|
|
I agree that setting it high at the start of the ISR then low at the end does not include the overhead code, but it will give a very good indication if a) it is being called as expected and b) if there is lots of free time for main or if you are in trouble :-) Definitely going to add the "wobble pin" definition to my vocabulary (if I can get it to stick in this old brain ! - it is the perfect descriptor)
mikey _________________ mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3 |
|
|
|
|
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
|