[SOLVED] Timer 0 interrupt and keypad interfacing problem for pic18f4520 in mikroc pro basic

Status
Not open for further replies.

djc

Advanced Member level 1
Joined
Jan 27, 2013
Messages
402
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Location
India
Visit site
Activity points
4,554
Hi,

I wrote a program for pic18f4520 interfacing with 4*4 matrix keypad and 15 FND and 74hc595 using Timer 0 interrupt. Issue is display is running fine on timer interrupt but key detection is not working. However if i disable timer interrupt and display key status separately its working fine. Can anybody plz tell me what is the probem. Plz guide. Here is my code
Code:
#define ROW0    PORTB.B1
#define ROW1    PORTB.B3
#define ROW2    PORTB.B5
#define ROW3    PORTB.B7
#define COL0    RB0_bit
#define COL1    RB2_bit
#define COL2    RB4_bit
#define COL3    RB6_bit
#define Select_DS     PORTD.B7
#define Select_Clk    PORTD.B6
#define Data_DS       PORTD.B5
#define Data_Clk      PORTD.B4

#define TIMER_START_VALUE 0

unsigned int arr_small[10] = {24,222,52,148,210,145,17,220,16,144};
 unsigned  int arr_large[10] = {17,125,35,37,77,133,129,61,1,5};
 unsigned  int arr_med[10] = {20,126,140,44,102,37,5,124,4,36};
 unsigned  int Select_FND[4] = {1,2,4,8};//{16,32,64,128,256,512,1024,2048,4096,8192,16384,32768};
 unsigned int  decimalNumber,remainder,quotient,decimalNumber_1,remainder_1,quotient_1;
 unsigned int  binaryNumber[100],binaryNumber_1[100],i=1,j,k,l_1=0,l=0,l_3=0,l_4=0,m=0,count=0,key,flag=0;
 unsigned int  key_fnd_0, key_fnd_1, key_fnd_2, key_fnd_3;
unsigned int Data=0,FND_0=255,FND_1=255,FND_2=255,FND_3=255,FND_4,FND_5,FND_6,FND_7,FND_8,FND_9,FND_10,FND_11,FND_12,FND_13,FND_14,FND_15;

/*void Sel_FND(int select)
{

     quotient = select_fnd[select];
          i=1;
          while(quotient!=0){
                             binaryNumber[i++]= quotient % 2;
                             quotient = quotient / 2;
                             }

                             for(j = i-1;j> 0;j--){
                             Select_DS = binaryNumber[j];
                             Select_Clk = 1;
                             Delay_us(10);
                             Select_Clk = 0;
                             }
           Select_Clk = 1;
           Delay_us(10);
           Select_Clk = 0;

}*/

void Key_Detect(int Data)
{
       quotient = Data;//arr_small[key];
           i=1;
                     while(quotient!=0){
                             binaryNumber[i++]= quotient % 2;
                             quotient = quotient / 2;
                             }
                             for(i=i;i<=8;i++)
                             {
                                 binaryNumber[i] = 0;
                             }
                             for(j = i-1;j> 0;j--){
                             Data_DS = binaryNumber[j];
                             Data_Clk = 1;
                             Delay_us(10);
                             Data_Clk = 0;
                             Delay_us(10);
                             }
                 Data_Clk = 1;
                 Delay_us(10);
                 Data_Clk = 0;
                 //Delay_ms(1000);
}


void interrupt()
{

      if(INTCON.TMR0IF){
      TMR0L = TIMER_START_VALUE;
      T0IF_bit = 0;
      }

      Select_DS = 1;
          Delay_us(2);

      Key_Detect(arr_small[FND_0]);
                 Select_Clk = 1;
                 Delay_us(2);
                 Select_DS = 0;
                 Select_Clk = 0;

                 Select_Clk = 1;
                 Delay_us(2);
                 Select_DS = 0;
                 Select_Clk = 0;

     Key_Detect(arr_small[FND_1]);
                 Select_Clk = 1;
                 Delay_us(2);
                 Select_DS = 0;
                 Select_Clk = 0;

     Key_Detect(arr_small[FND_2]);
                 Select_Clk = 1;
                 Delay_us(2);
                 Select_DS = 0;
                 Select_Clk = 0;
                 //Delay_ms(1000);

     Key_Detect(arr_small[FND_3]);
                 Select_Clk = 1;
                 Delay_us(2);
                 Select_DS = 0;
                 Select_Clk = 0;


     Key_Detect(arr_large[FND_5]);
                 Select_Clk = 1;
                 Delay_us(2);
                 Select_DS = 0;
                 Select_Clk = 0;
     //Data = arr_large[FND_6];
     Key_Detect(arr_large[FND_6]);
                 Select_Clk = 1;
                 Delay_us(2);
                 Select_DS = 0;
                 Select_Clk = 0;
     //Data = arr_large[FND_7];
     Key_Detect(arr_large[FND_7]);
                 Select_Clk = 1;
                 Delay_us(2);
                 Select_DS = 0;
                 Select_Clk = 0;
     //Data = arr_large[FND_8];
     Key_Detect(arr_large[FND_8]);
                 Select_Clk = 1;
                 Delay_us(2);
                 Select_DS = 0;
                 Select_Clk = 0;
                 

     Key_Detect(arr_med[FND_9]);
                 Select_Clk = 1;
                 Delay_us(2);
                 Select_DS = 0;
                 Select_Clk = 0;
                 
                 Select_Clk = 1;
                 Delay_us(2);
                 Select_DS = 0;
                 Select_Clk = 0;
     //Data = arr_med[FND_10];
     Key_Detect(arr_med[FND_10]);
                 Select_Clk = 1;
                 Delay_us(2);
                 Select_DS = 0;
                 Select_Clk = 0;
     //Data = arr_med[FND_11];
     Key_Detect(arr_med[FND_11]);
                 Select_Clk = 1;
                 Delay_us(2);
                 Select_DS = 0;
                 Select_Clk = 0;
     //Data = arr_med[FND_12];
     Key_Detect(arr_med[FND_12]);
                 Select_Clk = 1;
                 Delay_us(2);
                 Select_DS = 0;
                 Select_Clk = 0;
     //Data = arr_med[FND_13];
     Key_Detect(arr_med[FND_13]);
                 Select_Clk = 1;
                 Delay_us(2);
                 Select_DS = 0;
                 Select_Clk = 0;
     //Data = arr_med[FND_14];
     Key_Detect(arr_med[FND_14]);
                 Select_Clk = 1;
                 Delay_us(2);
                 Select_DS = 0;
                 Select_Clk = 0;
     //Data = arr_med[FND_15];
     Key_Detect(arr_med[FND_15]);
                 Select_Clk = 1;
                 Delay_us(2);
                 Select_DS = 0;
                 Select_Clk = 0;

}



void main() {

     TRISD = 0x00;            //PortD as output
     PORTD = 0x00;            //Initial value
     TRISB = 0b01010101;
     INTCON = 0xc0;

     TMR0ON_bit = 0;       //Stop timer 0 during set up
     TMR0IE_bit = 1;      // Enable TMRO interrupt
     T08BIT_bit = 1;      // Enable 8 bit control
     T0CS_bit = 0;        // use internal clock to trigger timer to count

     PSA_bit = 0;         // Use the prescaler to slow the timer down

     // prescaler
     // 111 = 1:256 Prescale value
     // 110 = 1:128 Prescale value
     // 101 = 1:64 Prescale value
     // 100 = 1:32 Prescale value
     // 011 = 1:16 Prescale value
     // 010 = 1:8 Prescale value
     // 001 = 1:4 Prescale value
     // 000 = 1:2 Prescale value
     T0PS0_bit = 0;
     T0PS1_bit = 0;
     T0PS2_bit = 0;

     TMR0L = TIMER_START_VALUE;

     TMR0ON_bit = 1;      // start the timer
     flag = 2;
     /*FND_0 = flag;
     FND_1 = flag;
     FND_2 = flag;
     FND_3 = flag;*/
     FND_4 = 4;           //Default values on FND 5 to 15
     FND_5 = 5;
     FND_6 = 6;
     FND_7 = 7;
     FND_8 = 8;
     FND_9 = 9;
     FND_10 = 0;
     FND_11 = 1;
     FND_12 = 2;
     FND_13 = 3;
     FND_14 = 4;
     FND_15 = 5;

      while(1){
            while(1){                                //Keypad displayroutine

            ROW0 = 0;
            ROW1 = 1;
            ROW2 = 1;
            ROW3 = 1;
            Delay_ms(50);
                         while(COL0==0){
                         do
                         {
                            Delay_ms(5);
                         }while(COL0!=1);
                         key=1;
                         count++;
                         //Sel_FND(1);
                         //Key_Detect(arr_small[1]);
                         }
                         
                         //if (Button(&PORTB, 2, 1, 0))
                         //{
                          //FND_6 = 1;
                         //}

                         while(COL1==0){
                         do
                         {
                         Delay_ms(5);
                         }while(COL1!=1);
                         key=2;
                         count++;
                         //Sel_FND(2);
                         //Key_Detect(arr_small[2]);
                         }
                         while(COL2==0){
                         do
                         {
                         Delay_ms(5);
                         } while(COL2!=1);
                         key=3;
                         count++;
                         //Sel_FND(4);
                         //Key_Detect(arr_small[3]);
                         }
                         while(COL3==0){
                         do
                         {
                         Delay_ms(5);
                         }while(COL3!=1);
                         key=0;
                         count++;
                         //Sel_FND(8);
                         //Key_Detect(arr_small[0]);
                         }
                         ROW0 = 1;
                         ROW1 = 0;
                         ROW2 = 1;
                         ROW3 = 1;
                         //PORTB.B3 = 0;
                         Delay_ms(5);
                         while(COL0==0){
                         do
                         {
                          Delay_ms(5);
                         }while(COL0!=1);
                         key=4;
                         count++;
                         }
                         while(COL1==0){
                         do
                         {
                         Delay_ms(5);
                         }while(COL1!=1);
                         key=5;
                         count++;
                         }

                         while(COL2==0){
                         do
                         {
                         Delay_ms(5);
                         } while(COL2!=1);
                         key=6;
                         count++;
                         }

                         while(COL3==0){
                         do
                         {
                         Delay_ms(5);
                         }while(COL3!=1);
                         key=0;
                         count++;
                         }

                         ROW0 = 1;
                         ROW1 = 1;
                         ROW2 = 0;
                         ROW3 = 1;
                         //PORTB.B5 = 0;
                         Delay_ms(5);

                         while(COL0==0){
                         do
                         {
                          Delay_ms(5);
                         }while(COL0!=1);
                         key=7;
                         count++;
                         }

                         while(COL1==0){
                         do
                         {
                         Delay_ms(5);
                         }while(COL1!=1);
                         key=8;
                         count++;
                         }

                         while(COL2==0){
                         do
                         {
                         Delay_ms(5);
                         } while(COL2!=1);
                         key=9;
                         count++;
                         }

                         while(COL3==0){
                         do
                         {
                         Delay_ms(5);
                         }while(COL3!=1);
                         key=0;
                         count++;
                         }

                         ROW0 = 1;
                         ROW1 = 1;
                         ROW2 = 1;
                         ROW3 = 0;
                         //PORTB.B7 = 0;
                         Delay_ms(5);

                         while(COL0==0){
                         do
                         {
                          Delay_ms(5);
                         }while(COL0!=1);
                         key=0;
                         count++;
                         }

                         while(COL1==0){
                         do
                         {
                         Delay_ms(5);
                         }while(COL1!=1);
                         key=0;
                         count++;
                         }

                         while(COL2==0){
                         do
                         {
                         Delay_ms(5);
                         } while(COL2!=1);
                         key=0;
                         count++;
                         }

                         while(COL3==0){
                         do
                         {
                         Delay_ms(5);
                         }while(COL3!=1);
                         key=0;
                         count++;
                         }

                         if(count==1){
                          key_fnd_0 = key;
                          FND_0 = key_fnd_0;
                         }
                         else if(count==2){
                         key_fnd_1 = key;
                         FND_1 = key_fnd_1;
                         }
                         else if(count==3){
                         key_fnd_2 = key;
                         FND_2 = key_fnd_2;
                         }
                         else if(count==4){
                         key_fnd_3 = key;
                         FND_3 = key_fnd_3;

                         }
                         if(count>4){
                         FND_5 = 0;
                         count=0;
                         }

                 }
          }
}
 

Hi,

No absolutely no problem in circuit diagram, hardware is through, moreover i don't have ckt diagram, direct hardware.

- - - Updated - - -

Hi,

No absolutely no problem in circuit diagram, hardware is through, moreover i don't have ckt diagram, direct hardware.
 

Hi djc,
i see some general issue..
RB0,RB1,RB2,RB3 shares the analog channels for A/D converter. This should be switched to digital mode.

Also what is the frequency of operation?
 
Last edited:

Hi murugesh,

Yes you are right, i am changing those pins to digital in picFLASH burner. I have connected 8Mhz crystal. I think my interrupt routine is getting bigger than main routine. To drive 15 FND i have to give 15 clock cycles otherwise it will not work. But in interrupt routine another subroutine is being called to convert decimal number into binary values to send it on 74hc595. That must be eating more time. Because keypad routine is not getting executed at all. Any other efficient way to convert decimal no to binary and send it to 74hc595.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…