Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

programming idea plz - mikroC w/ 16f877 - push button to choose function

Status
Not open for further replies.

janosandi

Full Member level 4
Joined
Jan 23, 2010
Messages
210
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,298
Activity points
2,788
hello guys
im newly using timer1 interrupt on overflow with mikroC
in the interrupt function i call a display function to drive 4 digits 7 segment displays
& its ok
i want to select between 4 functions by push button then by another push button select tht function to do the job

any idea will b appriciated

John
 

Hi,

Please be more descriptive:
* what microcontroller
* where exactely is your problem
* show your code
* what tests did you make, what did you expect, and what happened instead?
* ask specific questions

Klaus
 

Avoid any unnecessary, particularly time consuming code in the interrupt function and the functions called by it. Use a variable to select which function is called in the interrupt, set the variable outside interrupt.
 

Hello guys thx for your reply
im using mikroC with 16f877 & its my first time use timer interrupts & im already new with mikroC

Here is my code
Code:
unsigned char mode;

char select = 0, Digitt1, Digitt2, Digitt3, Digitt4;
unsigned char common_cathode_mask[21] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71, 0x73, 0x50, 0x5C, 0x37, 0x40};
//                                      0      ,1    ,2    ,3    ,4    ,5    ,6    ,7    ,8    ,9    ,A    ,b    ,C    ,d    ,E    ,F    ,P    ,r    ,o    ,N    ,-
//char digit[2];
char digit[4];

//Timer1
//Prescaler 1:1; TMR1 Preload = 63536; Actual Interrupt Time : 2 ms
//Place/Copy this part in declaration section

void InitTimer1() {
    //T1CON = 0x01;
    T1CON = 0x01;
    TMR1IF_bit = 0;
    TMR1H = 0xF8;
    TMR1L = 0x30;
    TMR1IE_bit = 1;
    INTCON = 0xC0;
}
void machoff() {
     Digitt1 = 20;
     Digitt2 = 20;
     Digitt3 = 20;
     Digitt4 = 20;
}

void  dispFunction() {
         digit[0] = common_cathode_mask[Digitt1];
         digit[1] = common_cathode_mask[Digitt2];
         digit[2] = common_cathode_mask[Digitt3];
         digit[3] = common_cathode_mask[Digitt4];
      switch(select) {

          case 0:
                PORTC = digit[0];
                PORTD = 0x01;
                break;
          case 1:
                PORTC = digit[1];
                PORTD = 0x02;
                break;
          case 2:
                PORTC = digit[2];
                PORTD = 0x04;
                break;
          case 3:
                PORTC = digit[3];
                PORTD = 0x08;
                break;

      };
}
void Dopen() {
     Digitt1 = 0;
     Digitt2 = 16;
     Digitt3 = 14;
     Digitt4 = 19;
     PORTB.RB5 = 1;
     dispFunction();
}
void Pro1() {
     mode = 1;
     Digitt1 = 16;
     Digitt2 = 17;
     Digitt3 = 18;
     Digitt4 = 1;
     dispFunction();
}

void Pro2() {
     mode = 2;
     Digitt1 = 16;
     Digitt2 = 17;
     Digitt3 = 18;
     Digitt4 = 2;
     dispFunction();
}

void Pro3() {
     mode = 3;
     Digitt1 = 16;
     Digitt2 = 17;
     Digitt3 = 18;
     Digitt4 = 3;
     dispFunction();
}

void Pro4() {
     mode = 4;
     Digitt1 = 16;
     Digitt2 = 17;
     Digitt3 = 18;
     Digitt4 = 4;
     dispFunction();
}

void ProSelect() {
     if(mode == 1) {
     Pro1();
     }
     if(mode == 2) {
     Pro2();
     }
     if(mode == 3) {
     Pro3();
     }
     if(mode == 4) {
     Pro4();
     }
}


void Interrupt(){
    if(TMR1IF_bit) {
      TMR1H = 0xF8;
      TMR1L = 0x30;
      PORTD = 0x00;
}
//      dispFunction();
      if(PORTB.B0 == 1) Dopen();
            else if(PORTB.B0 == 0){
            mode = 3; //
            ProSelect();
            }

      TMR1IF_bit = 0;
      if(++select == 4)select = 0;
}
void main() {

    CMCON = 0x07;
    ADCON1 = 0x87;

    TRISA = 0x00;
    TRISB = 0xFF;
    TRISC = 0x00;
    TRISD = 0x00;
    TRISE = 0x00;

    PORTA = 0x00;
    PORTB = 0x00;
    PORTC = 0x00;
    PORTD = 0x00;
    PORTE = 0x00;

//    mode = 3;

    InitTimer1();
}
in the interrupt function when PORTB.B0 = 0 i have open on 4Digits & when its 1 then i call Function Proselect with mode = 3 which call function Pro3
did i understand it right all of my work must be in the interrupt routine ?
main its just for declarations in my code ?
now i want to move between Pro1,Pro2,Pro3,Pro4 with any PORTB.B4 pins
when press the display moves to Pro1 then pro2 ......
then with PORTB.B5 i go inside Pro? fnctions which i select then execute it
my code is just something to learn the new way of coding
i used with assembly to call a sub routine then stay there
with timer interrupt it always call interrupt function

so how to stay in any function i call ?

sorry for my english also
any idea will help

i've already learned Assembly coding here with help of this forum members

thx
John
 

Hi,

did i understand it right all of my work must be in the interrupt routine ?
FvM wrote:
Avoid any unnecessary, particularly time consuming code in the interrupt

My recommendation:
The ISR must be short in time.
Therefore just update all necessary variables and set a flag..to show that new data is available.
But the processing of the data (display) should be done in main loop.

Klaus
 

Hi,


FvM wrote:


My recommendation:
The ISR must be short in time.
Therefore just update all necessary variables and set a flag..to show that new data is available.
But the processing of the data (display) should be done in main loop.

Klaus
thx Klaus i'll try to move the display function to main loop
with my code wht should i do to make it better ?
 

would you plz answer my first question?
how do i change between functions with push button?

thx guys
 

You aren't incrementing variable "mode" anywhere.

thts my problem i dont know where to increment it i've just put mode = ? to test if it works to call another function
maybe i must increment it it the function which i call so the next call must go to the next function?
& do i have to make a do loop to stay in the function which i call ?

thx
 

When an interrupt takes place, the microprocessor automatically goes to a isr.

in your case, the interrupt is caused timer counter over/underflow and you set a flag within the isr (service routine) timer_interrupt=1 if timer_interrupt=0 else timer_interrupt=2;

you return from the isr and process outside, if timer_interrupt==1, the work to be done you wanted on this interrupt. And you also reset the timer_interrupt=0;

If you find timer_interrupt==2, it means that another interrupt has taken place before you could process the first one. It should not happen.
 

When an interrupt takes place, the microprocessor automatically goes to a isr.

in your case, the interrupt is caused timer counter over/underflow and you set a flag within the isr (service routine) timer_interrupt=1 if timer_interrupt=0 else timer_interrupt=2;

you return from the isr and process outside, if timer_interrupt==1, the work to be done you wanted on this interrupt. And you also reset the timer_interrupt=0;

If you find timer_interrupt==2, it means that another interrupt has taken place before you could process the first one. It should not happen.

thx for your time
so i got it as i guess
when timer1 interrupt occurs it goes automatilcally to interrpt Function or isr.
i've used a do loop to stay in a function & i thought when timer1 interrupt occurs it should refresh the display function of driving 7 segments but it didnt work
Code:
void Interrupt(){
    if(TMR1IF_bit) {
      TMR1H = 0xF8;
      TMR1L = 0x30;
      PORTD = 0x00;
}
      if(PORTB.B0 == 1) Dopen();
            if(PORTB.B4 == 0){
            mode = 3; //
            ProSelect();
            }
      TMR1IF_bit = 0;
      if(++select == 4)select = 0;
}
when i press PORTB.B4 it goes to proselect with mode = 3 which calls Pro3
but with the do loop i supposed tht the display will refresh on every timer overflow
but it didnt
Code:
void Pro3() {
     do {
     mode = 3;
     Digitt1 = 16;
     Digitt2 = 17;
     Digitt3 = 18;
     Digitt4 = 3;
     dispFunction();
     
     }while(1);
}
or in this situation it stays in Pro3 without going to interrupt ?

thx for your ideas
John
 
Last edited:

The setup of the interrupt function is not correct. (the function starting with void interrupt...) I have not done much work with PIC and I have to read...

Your codes are not having any comments. What you are trying to do?
 

The setup of the interrupt function is not correct. (the function starting with void interrupt...) I have not done much work with PIC and I have to read...

Your codes are not having any comments. What you are trying to do?

i think the interrupt function is ok coz 4 digits r working great
but im looking for method to change between 4 functions by push button.
like:
1st press function1
2nd press function2
3rd press function3
.......
thx for your idea
one more question:
if i use delay in other place of code does it stop timer1 interrupt ?
 

Hi,

Translate my "textual description" to your code:

on press (edge): function = function +1
If function > 4 then function = 1

Klaus
 

if i use a do loop..while(1) endless loop.
is it ok with timer interrupt ? does it jump to the interrupt funcion when timer1 overflow then go back to the endless loop ?
 

Hi,

interrupt basics?

when an interrupt happens,
* it safes the address of the next command
* it jumps to the ISR
* it processes the ISR
* it comes to the end of the ISR
* it fetches the safed address
* jumps to the address and goes on as if there was no interrupt (but delayed)

Before the interrupt you may be in an endless loop, you may be in idle state or anywhere else in the code....
...and it comes back and processes the next command...

There are many documents, tutorials and (I asumme) videos that describe how interrupts work. Go through them.

Klaus
 
thx Klaus

i'll search for interrupt tutorial
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top