|
|
View previous topic :: View next topic |
Author |
Message |
Khansokhua
Joined: 06 Nov 2021 Posts: 92
|
PIC16F877A int_ext -2 |
Posted: Wed Dec 01, 2021 12:59 pm |
|
|
Code: | #include <Timerlar.h>
#fuses XT
#use delay(clock=4M)
int i;
const int segment[8]= {0x06,0x5B,0x4F,0x66,0x6D,0x7C,0x07,0x7F};
const int led[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
#int_ext
void kesme()
{
output_c(segment[i]);
output_d(led[i]);
if(input(pin_B0)) // Also, I think " if I trigger RB0, I am also trigger int ext simultaneously. I am in doubt about it.
{
// goto cycle;
}
while(1);
}
void main()
{
// cycle:
output_a(0x02);
output_float(pin_B0);
while(1)
{
for(i=0;i<=7;i++)
{
output_d(led[i]);
delay_ms(200);
}
ext_int_edge(H_TO_L);
enable_interrupts(int_ext);
enable_interrupts(GLOBAL);
}
}
|
Greetings, Here is the issue:"While leds are going by turns, I apply int_ext interrupt. Then I see which led was "high" last time on segment display. Everything's okay till here. Now, when I want to apply second time interrupt, I want leds to continue flowing from point that was shown on segment display last time. What can I do at this point ? I tried to make with "goto" but did not work, I received many errors. Resepcts |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Dec 01, 2021 2:27 pm |
|
|
What is the purpose of your project ?
What do you want it to do ?
Explain the desired operation of your project. |
|
|
PrinceNai
Joined: 31 Oct 2016 Posts: 480 Location: Montenegro
|
|
Posted: Wed Dec 01, 2021 4:04 pm |
|
|
Well, if I understood correctly, int_ext stops the leds the first and every odd time and shows the last pattern somewhere, while every even time interrupt is triggered the main resumes with displaying dancing numbers on another port. If I'm correct with my assumptions, you could easily introduce a variable, say "flow" and toggle it every time in your interrupt. Based on this variable you either stop or resume what is displayed. Since I'm a fan of state machines, you could do it with that. Toggle the variable in your interrupt routine and let the main decide via switch statement what it needs to do. Show static info on one port or change the numbers on another.
Of course there is a debouncing problem, where every press of the button could lead to many interrupts. |
|
|
Khansokhua
Joined: 06 Nov 2021 Posts: 92
|
|
Posted: Wed Dec 01, 2021 6:33 pm |
|
|
Quote: |
What is the purpose of your project ?
What do you want it to do ?
Explain the desired operation of your project. |
I am practicing just to learn.
While leds are flowing constantly. I apply int_ext interrupt and leds are being stopped. I read the led's pin number on 7 segment display. When I apply second interrupt I want leds to continue flow on main function. Sorry for my english. |
|
|
Khansokhua
Joined: 06 Nov 2021 Posts: 92
|
|
Posted: Wed Dec 01, 2021 6:47 pm |
|
|
What is the simple way to go out from int_ext function at second trigger?It is weird because every time I press RB0 I trigger int_ext.Maybe we can disable? |
|
|
Khansokhua
Joined: 06 Nov 2021 Posts: 92
|
|
Posted: Wed Dec 01, 2021 7:14 pm |
|
|
Code: | #int_ext
void kesme()
{
output_c(segment[i]);
output_d(led[i]);
disable_interrupts(int_ext);
disable_interrupts(GLOBAL);
if(input(buton))// pin_b0
{
output_c(0xFF);
}
while(1);
} |
It doesn't work. I have just tried, maybe. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Thu Dec 02, 2021 1:07 am |
|
|
As written, the code flow can never leave the interrupt handler. Once it has triggered, it sits forever in the
while(1);
at the end of the interrupt routine.
The interrupt code can never then be called again.....
The code has to leave the interrupt handler, before this can be called again.
The processor can only do one thing. |
|
|
PrinceNai
Joined: 31 Oct 2016 Posts: 480 Location: Montenegro
|
|
Posted: Sun Dec 05, 2021 4:46 am |
|
|
Untested, but it should work:
Code: |
int i = 0;
const int segment[8]= {0x06,0x5B,0x4F,0x66,0x6D,0x7C,0x07,0x7F};
const int led[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
#define RUN 0 // state 0
#define FREEZE 1 // state 1
int8 Run_Or_Freeze = RUN; // state machine variable, we start with a running pattern
int8 ms_counter = 0;
#INT_EXT
// changes state variable Run_Or_Freeze on every press of the button from RUN to FREEZE and vice versa
void EXT_isr(void) {
if(Run_Or_Freeze == RUN){ // if we were in a RUN state, change to FREEZE
Run_Or_Freeze = FREEZE;
}
else{
Run_Or_Freeze = RUN; // or if in FREEZE, change to RUN
}
}
// end interupt
void main(){
ext_int_edge(H_TO_L); // define interrupts
clear_interrupt(INT_EXT);
enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL);
// disable display on port_c here!!!
while(TRUE){
// state machine
switch(Run_Or_Freeze){ //decide what to do, RUN or FREEZE. int_ext decides that.
// do the RUN code
case RUN:{
// 200ms are broken down to 40 5ms chunks to be able to exit RUN state as soon (or max. 5ms later) as int_ext is triggered
// otherwise it would finish the current 200ms pattern in "RUN" state before changing state to "FREEZE" state and so freeze only on the next pattern, when i variable would be already incremented.
if(ms_counter < 40){ // display the same pattern 40x for 5ms
// disable display on port_c here or write something like "-" on it!!!
output_d(led[i]);
ms_counter++;
delay_ms(5);
} // if
else{
ms_counter = 0; // reset counter
i++; // increment i for the next pattern
if(i == 8){ // if we went through all the patterns, start from 0
i = 0;
}
} // else
break; // exit to switch statement
} // RUN case
// do the FREEZE code
case FREEZE:{
// since I don't know your connections, it is a must here to enable or turn on your led segment via some pin to see anything written to port_c!!!!!
output_c(segment[i]);
output_d(led[i]); // display frozen state on port_c and port_d until next int_ext happens
break;
} // FREEZE case
} // switch brace
} // while true brace
} // main brace
|
|
|
|
|
|
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
|