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.

problem in execution program with keypad

Status
Not open for further replies.

asoum

Newbie level 4
Newbie level 4
Joined
May 12, 2014
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
50
Hi, I'm working in a project in which i used a keypad to enter value.
After entering this value, i want to return to execute the rest of my program, but the problem is that the execution still blocked on the function of keypad :(
I will appreciate your help.

Code:


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <main.h> 
#define LCD_ENABLE_PIN PIN_D0 
#define LCD_RS_PIN PIN_D2 
#define LCD_RW_PIN PIN_D1 
#define LCD_DATA4 PIN_D6 
#define LCD_DATA5 PIN_D5 
#define LCD_DATA6 PIN_D4 
#define LCD_DATA7 PIN_D3 
#define buzzer PIN_D7 
#use fast_io(a) 
#include <lcd.c> 
#include <flex_kbd.c> 
char k; 
unsigned long int lecture; 
float v, Ifuite,Imax; 
  
int j=60; 
 
void keypad() 
{ 
   lcd_init(); 
   kbd_init(); 
              
     lcd_gotoxy(1,1); 
     printf(lcd_putc,"Entrer les"); 
     lcd_gotoxy(1,2); 
     printf(lcd_putc,"parametres"); 
     delay_ms (1500) ; 
     lcd_init(); 
     printf(lcd_putc,"Imax=%c",k); 
      
      
    while (TRUE) 
                { 
                    k=kbd_getc(); 
                    if(k!=0)                                      
                    lcd_putc(k); 
                    if(k=='#')                  
                    lcd_putc("\fTension\n");                    
                    if (k=='*')                  
                    lcd_putc("\fTemps\n");                    
                    
                } 
                    
                
}  
 
void test() 
{ 
          if ( Ifuite < Imax) 
            { 
                            
                 output_low(pin_c0); 
                 lcd_init(); 
                 printf(lcd_putc,"test OK \r"); 
                 output_high(pin_c1); 
                 delay_ms(1500); 
                 output_low(pin_c1); 
            } 
            
          else if ( Ifuite >= Imax) 
            
           {          
                 output_low(pin_c0); 
                 lcd_init(); 
                 printf(lcd_putc,"test NOT OK \r"); 
                 //output_high(pin_d7); 
                 output_high(pin_c2); 
                 delay_ms(1500);          
                 output_low(pin_c2); 
} 
            
 
  
void main() 
{ 
     lcd_init(); 
     printf(lcd_putc,"Bienvenue \r"); 
     delay_ms (1500) ; 
     lcd_init(); 
     printf(lcd_putc,"sagemcom \r"); 
     delay_ms (1500) ; 
      
     keypad(); 
    
     lcd_init(); 
     printf(lcd_putc,"start test \n"); 
     delay_ms (1500); 
     printf(lcd_putc,"test en cours \r"); 
     output_high(pin_C0); 
     delay_ms (1500); 
      
    setup_adc_ports(AN0); 
    setup_adc(ADC_CLOCK_INTERNAL); 
    lcd_init(); 
    set_adc_channel(0); 
    delay_us(20); 
   while(true) 
   { 
    
   lecture=read_adc(); 
   delay_us(20); 
   v=lecture*0.0048828125; 
   delay_us(10); 
   ifuite=v/1.5; 
   delay_us(10); 
   lcd_init(); 
   lcd_gotoxy(1,1); 
   printf(lcd_putc,"I=%fmA",Ifuite); 
   lcd_gotoxy(1,2); 
   printf(lcd_putc,"Ttest=%d",j); 
   j=j-1; 
          if(j==0) 
                   { 
                    
                    lcd_init(); 
                    test(); 
                    
                   } 
   } 
    
 
}

 
Last edited by a moderator:

hello asoum,
check carefully the function "keypad", there is a infinite while loop in that. No matter what u do, the control will stay in the loop "while(TRUE)" bcoz the loop condition is always true just like we see in other embedded codes "while(1)". Usually, this kind of infinite loops should be in MAIN function. change the while loop in keypad function like this, that may help:
Code:
while (TRUE) 
                { 
                    k=kbd_getc(); 
                    if(k!=0)                                      
                    lcd_putc(k); 
                    else if(k=='#')                  
                    lcd_putc("\fTension\n");                    
                    else if (k=='*')                  
                    lcd_putc("\fTemps\n");                    
                    break;
                }
The break line will break the infinite while loop when key is found out but if u want to only exit until key is detected, then before break put else.
On the other hand, i suggest making keypad function interrupt based, that way, u need not poll the change in keys. And initialization should be only once, until there is some fatal error. Every time u enter the keypad function, there is init for keyboard and lcd. check the example codes online on different sites and tutorial.
 

you have infinity condition in keypad function. so it is always true so it get halted over there.
 

ok thanks :) and haw can i store a value of current entered by keypad, displayed on lcd,because in test i need this value to do the comparaison
 

use 1 bit variable and use one unsigned char variable.
first put the value which is pressed and then raise the bit and monitor in main loop while condition. and after check lower the bit. here what i meant to say
Code:
bit pressed;
unsigned char key_val;

(get the numver which is pressed in key_val)

void main()
{
if(pressed)
{
pressed = 0;
// do whatever you want with key val;
}
}
 

as ebmpic said, u can poll for change in input bit. Or u can enable interrupt and make circuit arrangement for keypad to generate external interrupt for keypad and then handle the debounce delay with getting character value. U have to use global variable to store value of key and use that global variable to access key value.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top