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.

pic16f877 + keypad + two 7-segment (help me)

Status
Not open for further replies.

suleymanqx

Newbie level 6
Joined
Apr 29, 2013
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,360
Hi guys! I need a help, what should I do to send first pressed number to first 7-seg(portb) second pressed number to second 7-seg(portc) ?



Code:
#include <16f877.h>  

#fuses XT,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOWRT,NODEBUG,NOCPD

#use delay (clock=4000000) 

#use fast_io(b)
#use fast_io(c)
#use fast_io(d)

#byte   portb=0x06
#byte   portc=0x07   

#define sut1   pin_d0 
#define sut2   pin_d1 
#define sut3   pin_d2 
#define sut4   pin_d3 

#define sat1   pin_d4 
#define sat2   pin_d5 
#define sat3   pin_d6 
#define sat4   pin_d7

char tus=0;

//******************************
char keypad_read()
{
   output_d(0x00);

   output_high(sat1);
   if (input(sut1))   // 1. sütun okunuyor
      { delay_ms(20); tus=1; }
   if (input(sut2))   // 2. sütun okunuyor
      { delay_ms(20); tus=2; }
   if (input(sut3))   // 3. sütun okunuyor
      { delay_ms(20); tus=3; }
   if (input(sut4))   // 4. sütun okunuyor
      { delay_ms(20); tus=0xA; }
   output_low(sat1); // 1. satýr lojik-0 yapýlýyor

   output_high(sat2); // 2. satýr lojik-1 yapýlýyor
   if (input(sut1))   // 1. sütun okunuyor
      { delay_ms(20); tus=4; }
   if (input(sut2))   // 2. sütun okunuyor
      { delay_ms(20); tus=5; }
   if (input(sut3))   // 3. sütun okunuyor
      { delay_ms(20); tus=6; }
   if (input(sut4))   // 4. sütun okunuyor
      { delay_ms(20); tus=0xB; }
   output_low(sat2); // 2. satýr lojik-0 yapýlýyor

   output_high(sat3); // 3. satýr lojik-1 yapýlýyor
   if (input(sut1))   // 1. sütun okunuyor
      { delay_ms(20); tus=7; }
   if (input(sut2))   // 2. sütun okunuyor
      { delay_ms(20); tus=8; }
   if (input(sut3))   // 3. sütun okunuyor
      { delay_ms(20); tus=9; }
   if (input(sut4))   // 4. sütun okunuyor
      {delay_ms(20); tus=0x0C; }
   output_low(sat3); // 3. satýr lojik-0 yapýlýyor

   output_high(sat4); // 3. satýr lojik-1 yapýlýyor
   if (input(sut1))   // 1. sütun okunuyor
      { delay_ms(20); tus=0xE; }
   if (input(sut2))   // 2. sütun okunuyor
      { delay_ms(20); tus=0; }
   if (input(sut3))   // 3. sütun okunuyor
      { delay_ms(20); tus=0xF; }
   if (input(sut4))   // 4. sütun okunuyor
      {delay_ms(20); tus=0xD; }
   output_low(sat4); // 3. satýr lojik-0 yapýlýyor

   return tus;
}

/****************************/

void main ( )
{
   setup_psp(PSP_DISABLED);       
   setup_timer_1(T1_DISABLED);   
   setup_timer_2(T2_DISABLED,0,1);
   setup_adc_ports(NO_ANALOGS);  
   setup_adc(ADC_OFF);             
   setup_CCP1(CCP_OFF);
   setup_CCP2(CCP_OFF);

   set_tris_b(0x00);
   set_tris_c(0x00);  
   set_tris_d(0x0F);  

   output_b(0x00);
   output_c(0x00); 

   while(1)  
   {
      portb=keypad_read(); 
  }
}
 

anboli

Full Member level 2
Joined
Mar 9, 2012
Messages
144
Helped
4
Reputation
8
Reaction score
4
Trophy points
1,298
Activity points
2,512
create one flag with bit type variable

set the flag to send the first number to 1st 7-segment, and clear the flag to send the second number to 2nd 7-segment.

check the the flag to send the data to 7-segment. if yes===> 1st display On.
No===> 2nd display On,
 

suleymanqx

Newbie level 6
Joined
Apr 29, 2013
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,360
create one flag with bit type variable

set the flag to send the first number to 1st 7-segment, and clear the flag to send the second number to 2nd 7-segment.

check the the flag to send the data to 7-segment. if yes===> 1st display On.
No===> 2nd display On,
thank you bro!
Can you write it for me and where I have to write it in code please?
 

anboli

Full Member level 2
Joined
Mar 9, 2012
Messages
144
Helped
4
Reputation
8
Reaction score
4
Trophy points
1,298
Activity points
2,512
- - - Updated - - -

#include <16f877.h>

#fuses XT,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOWRT,NODEBUG,NOCPD

#use delay (clock=4000000)

#use fast_io(b)
#use fast_io(c)
#use fast_io(d)

#byte portb=0x06
#byte portc=0x07

#define sut1 pin_d0
#define sut2 pin_d1
#define sut3 pin_d2
#define sut4 pin_d3

#define sat1 pin_d4
#define sat2 pin_d5
#define sat3 pin_d6
#define sat4 pin_d7

char tus=0;
int select_seg=1;
//******************************
char keypad_read()
{
output_d(0x00);

output_high(sat1);
if (input(sut1)) // 1. sütun okunuyor
{ delay_ms(20); tus=1; }
if (input(sut2)) // 2. sütun okunuyor
{ delay_ms(20); tus=2; }
if (input(sut3)) // 3. sütun okunuyor
{ delay_ms(20); tus=3; }
if (input(sut4)) // 4. sütun okunuyor
{ delay_ms(20); tus=0xA; }
output_low(sat1); // 1. satýr lojik-0 yapýlýyor

output_high(sat2); // 2. satýr lojik-1 yapýlýyor
if (input(sut1)) // 1. sütun okunuyor
{ delay_ms(20); tus=4; }
if (input(sut2)) // 2. sütun okunuyor
{ delay_ms(20); tus=5; }
if (input(sut3)) // 3. sütun okunuyor
{ delay_ms(20); tus=6; }
if (input(sut4)) // 4. sütun okunuyor
{ delay_ms(20); tus=0xB; }
output_low(sat2); // 2. satýr lojik-0 yapýlýyor

output_high(sat3); // 3. satýr lojik-1 yapýlýyor
if (input(sut1)) // 1. sütun okunuyor
{ delay_ms(20); tus=7; }
if (input(sut2)) // 2. sütun okunuyor
{ delay_ms(20); tus=8; }
if (input(sut3)) // 3. sütun okunuyor
{ delay_ms(20); tus=9; }
if (input(sut4)) // 4. sütun okunuyor
{delay_ms(20); tus=0x0C; }
output_low(sat3); // 3. satýr lojik-0 yapýlýyor

output_high(sat4); // 3. satýr lojik-1 yapýlýyor
if (input(sut1)) // 1. sütun okunuyor
{ delay_ms(20); tus=0xE; }
if (input(sut2)) // 2. sütun okunuyor
{ delay_ms(20); tus=0; }
if (input(sut3)) // 3. sütun okunuyor
{ delay_ms(20); tus=0xF; }
if (input(sut4)) // 4. sütun okunuyor
{delay_ms(20); tus=0xD; }
output_low(sat4); // 3. satýr lojik-0 yapýlýyor

return tus;
}

/****************************/

void main ( )
{
setup_psp(PSP_DISABLED);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_CCP1(CCP_OFF);
setup_CCP2(CCP_OFF);

set_tris_b(0x00);
set_tris_c(0x00);
set_tris_d(0x0F);

output_b(0x00);
output_c(0x00);

while(1)
{
if(select_seg)
{
if(keypad_read()>0)
{
Portb = keypad_read();
select_seg=0;
}
if(!select_seg)
{
if(keypad_read()>0)
{
Portc = keypad_read();
select_seg=1;
}
}
}
}
 

anboli

Full Member level 2
Joined
Mar 9, 2012
Messages
144
Helped
4
Reputation
8
Reaction score
4
Trophy points
1,298
Activity points
2,512
what was showing now?
 

suleymanqx

Newbie level 6
Joined
Apr 29, 2013
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,360
first pressed number to first 7-seg(portb), second pressed number to second 7-seg(portc)
 

anboli

Full Member level 2
Joined
Mar 9, 2012
Messages
144
Helped
4
Reputation
8
Reaction score
4
Trophy points
1,298
Activity points
2,512
modify the program like this.

change the while loop and declare the i variable;
int i=0;

while(1)
{
i = keypad_read();
if(select_seg)
{
if(i>0)
{
Portb = keypad_read();
select_seg=0;
i=0;
}
if(!select_seg)
{
if(i>0)
{
Portc = keypad_read();
select_seg=1;
i=0;
}
}
}
 

suleymanqx

Newbie level 6
Joined
Apr 29, 2013
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,360
thank you bro, but it doesn't work. first pressed number writes to portb then freezes

Code:
#include <16f877.h>     // Kullanýlacak denetleyicinin baþlýk dosyasý tanýtýlýyor.

// Denetleyici konfigürasyon ayarlarý
#fuses XT,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOWRT,NODEBUG,NOCPD

#use delay (clock=4000000) // Gecikme fonksiyonu için kullanýlacak osilatör frekansý belirtiliyor.

#use fast_io(b)
#use fast_io(c) //Port yönlendirme komutlarý B portu için geçerli
#use fast_io(d) //Port yönlendirme komutlarý C portu için geçerli

#byte   portb=0x06
#byte   portc=0x07   // C portu "tus" ismine eþitleniyor.

#define sut1   pin_d0 // sut1 ifadesi pin_d0 ifadesine eþitleniyor
#define sut2   pin_d1 // sut2 ifadesi pin_d1 ifadesine eþitleniyor
#define sut3   pin_d2 // sut3 ifadesi pin_d2 ifadesine eþitleniyor
#define sut4   pin_d3 // sut3 ifadesi pin_d2 ifadesine eþitleniyor

#define sat1   pin_d4 // sat1 ifadesi pin_d4 ifadesine eþitleniyor
#define sat2   pin_d5 // sat2 ifadesi pin_d5 ifadesine eþitleniyor
#define sat3   pin_d6 // sat3 ifadesi pin_d6 ifadesine eþitleniyor
#define sat4   pin_d7 // sat4 ifadesi pin_d7 ifadesine eþitleniyor

int i=0;
int select_seg=1;
char tus; // karakter tipinde deðiþken tanýmlanýyor




//*********** Keypad Tarama Fonksiyonu *********
char keypad_read() // Fonksiyon ismi
{
   output_d(0x00); // D portu çýkýþý sýfýrlanýyor

   output_high(sat1); // 1. satýr lojik-1 yapýlýyor
   if (input(sut1))   // 1. sütun okunuyor
      { delay_ms(20); tus=1; }
   if (input(sut2))   // 2. sütun okunuyor
      { delay_ms(20); tus=2; }
   if (input(sut3))   // 3. sütun okunuyor
      { delay_ms(20); tus=3; }
   if (input(sut4))   // 4. sütun okunuyor
      { delay_ms(20); tus=0xA; }
   output_low(sat1); // 1. satýr lojik-0 yapýlýyor

   output_high(sat2); // 2. satýr lojik-1 yapýlýyor
   if (input(sut1))   // 1. sütun okunuyor
      { delay_ms(20); tus=4; }
   if (input(sut2))   // 2. sütun okunuyor
      { delay_ms(20); tus=5; }
   if (input(sut3))   // 3. sütun okunuyor
      { delay_ms(20); tus=6; }
   if (input(sut4))   // 4. sütun okunuyor
      { delay_ms(20); tus=0xB; }
   output_low(sat2); // 2. satýr lojik-0 yapýlýyor

   output_high(sat3); // 3. satýr lojik-1 yapýlýyor
   if (input(sut1))   // 1. sütun okunuyor
      { delay_ms(20); tus=7; }
   if (input(sut2))   // 2. sütun okunuyor
      { delay_ms(20); tus=8; }
   if (input(sut3))   // 3. sütun okunuyor
      { delay_ms(20); tus=9; }
   if (input(sut4))   // 4. sütun okunuyor
      {delay_ms(20); tus=0x0C; }
   output_low(sat3); // 3. satýr lojik-0 yapýlýyor

   output_high(sat4); // 3. satýr lojik-1 yapýlýyor
   if (input(sut1))   // 1. sütun okunuyor
      { delay_ms(20); tus=0xE; }
   if (input(sut2))   // 2. sütun okunuyor
      { delay_ms(20); tus=0; }
   if (input(sut3))   // 3. sütun okunuyor
      { delay_ms(20); tus=0xF; }
   if (input(sut4))   // 4. sütun okunuyor
      {delay_ms(20); tus=0xD; }
   output_low(sat4); // 3. satýr lojik-0 yapýlýyor

     
   return tus; // Fonksiyon "tus" deðeri ile geri döner
   
}

/********* ANA PROGRAM FONKSÝYONU********/

void main ( )
{
   setup_psp(PSP_DISABLED);        // PSP birimi devre dýþý
   setup_timer_1(T1_DISABLED);     // T1 zamanlayýcýsý devre dýþý
   setup_timer_2(T2_DISABLED,0,1); // T2 zamanlayýcýsý devre dýþý
   setup_adc_ports(NO_ANALOGS);    // ANALOG giriþ yok
   setup_adc(ADC_OFF);             // ADC birimi devre dýþý
   setup_CCP1(CCP_OFF);
   setup_CCP2(CCP_OFF);

   set_tris_b(0x00);
   set_tris_c(0x00);   // B portu komple çýkýþ
   set_tris_d(0x0F);   // Yüksek deðerlikli 4 bit çýkýþ, düþük deðerlikli 4 bit giriþ

   output_b(0x00);
   output_c(0x00); // Ýlk anda B portu çýkýþý sýfýrlanýyor
   
   
   
   while(1)  // Sonsuz döngü
   {
    i = keypad_read();
if(select_seg)
{
if(i>0)
{
Portb = keypad_read();
select_seg=0;
i=0;
}
if(!select_seg)
{
if(i>0)
{
Portc = keypad_read();
select_seg=1;
i=0;
}
}
}
   }
}
 

anboli

Full Member level 2
Joined
Mar 9, 2012
Messages
144
Helped
4
Reputation
8
Reaction score
4
Trophy points
1,298
Activity points
2,512
whats happening while pressing the second switch and in second display?
 

M.Imran Ali

Member level 5
Joined
Sep 28, 2012
Messages
86
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,298
Activity points
1,813
It is better to use portb change interrupt rather than scaning have a look on it

#include <16F877A.h>
#device adc=16

#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O

#use delay(clock=20000000)
#include <LCD.C>
#include <defines.c>
#BIT FLAG = STATUS.RBIF


char key[4][3]= { '1','2','3',
'4','5','6',
'7','8','9',
'*','0','#'};


#int_RB
void RB_isr(void)
{
BYTE COL;
BYTE temp;
int i,j,k;
delay_ms(20);
COL=PORTB;
bit_clear(STATUS, RBIF);
output_bit( PIN_B0, 1);
if(temp!=COL) {i=0; FLAG=0;}
output_bit( PIN_B1, 1);
if(temp!=COL) {i=1; FLAG=0;}
output_bit( PIN_B2, 1);
if(temp!=COL) {i=2; FLAG=0;}

for(k=4;k<=7;k++){

if( bit_test(COL,k)) {j=k; break;}

}

j=j-4;

lcd_gotoxy(17 ,1);
printf(lcd_putc , "%c" , key[j]);
delay_ms(500);


}



void main()
{
lcd_init();
port_b_pullups (true);
enable_interrupts(INT_RB);
enable_interrupts(GLOBAL);






lcd_gotoxy(1 , 1 );
lcd_putc("character is");

set_tris_b(0xf0);
output_bit( PIN_B0, 0);
output_bit( PIN_B1, 0);
output_bit( PIN_B2, 0);


while(TRUE)
{
output_bit( PIN_B0, 0);
output_bit( PIN_B1, 0);
output_bit( PIN_B2, 0);

}

}


it works fine . In defines file define Registers of pic16f877a to make them use in pic c CSS compiler
LCD on port D and keypad on port b. u can use seven segment instead of LCD.
 

suleymanqx

Newbie level 6
Joined
Apr 29, 2013
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,360
whats happening while pressing the second switch and in second display?
Nothing heppens. just first pressed digit sends to portb then doesn't any reaction in second pressed digits

- - - Updated - - -

It is better to use portb change interrupt rather than scaning have a look on it

Code:
#include <16F877A.h>
#device adc=16

#FUSES NOWDT                 	       //No Watch Dog Timer
#FUSES HS                    	              //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOBROWNOUT            	     //No brownout reset
#FUSES NOLVP                 	             //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O

#use delay(clock=20000000)
#include <LCD.C>
#include <defines.c>
#BIT  FLAG = STATUS.RBIF

       
 char key[4][3]= { '1','2','3',
                         '4','5','6', 
                         '7','8','9',
                         '*','0','#'};
       

#int_RB
void  RB_isr(void) 
{
       BYTE COL;
       BYTE temp;
       int i,j,k;
       delay_ms(20);
       COL=PORTB;
       bit_clear(STATUS, RBIF);
       output_bit( PIN_B0, 1);
       if(temp!=COL) {i=0; FLAG=0;}
         output_bit( PIN_B1, 1);
         if(temp!=COL) {i=1; FLAG=0;}
         output_bit( PIN_B2, 1);
         if(temp!=COL) {i=2; FLAG=0;}
        
        for(k=4;k<=7;k++){
        
        if( bit_test(COL,k)) {j=k; break;}
        
        }
         
         j=j-4;
         
         lcd_gotoxy(17 ,1);
         printf(lcd_putc , "%c" , key[i][j]);
         delay_ms(500);
       
       
}



void main()
{
   lcd_init();
   port_b_pullups (true);
   enable_interrupts(INT_RB);
   enable_interrupts(GLOBAL);
  


   
   
   
   lcd_gotoxy(1 , 1 );
   lcd_putc("character is");
   
   set_tris_b(0xf0);
   output_bit( PIN_B0, 0);
   output_bit( PIN_B1, 0);    
   output_bit( PIN_B2, 0);

   
   while(TRUE)
   {
   output_bit( PIN_B0, 0);
   output_bit( PIN_B1, 0);
   output_bit( PIN_B2, 0);

   }

}

it works fine . In defines file define Registers of pic16f877a to make them use in pic c CSS compiler
LCD on port D and keypad on port b. u can use seven segment instead of LCD.

Thank you bro but I'm not advanced programmer) I'm electrical engineer without programming. So I need a simple code to do this job for making another project with 7-segments.

The anboli's code is perfect for me but it doesnt work properly
 

suleymanqx

Newbie level 6
Joined
Apr 29, 2013
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,360
Done

Code:
#include <16f877.h>
#fuses XT,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOWRT,NODEBUG,NOCPD
#use delay (clock=2000000) 

#use fast_io(b)
#use fast_io(c)
#use fast_io(d)

#byte   portb=0x06
#byte   portc=0x07

#define sut1   pin_d0 
#define sut2   pin_d1 
#define sut3   pin_d2 
#define sut4   pin_d3 

#define sat1   pin_d4 
#define sat2   pin_d5
#define sat3   pin_d6
#define sat4   pin_d7

char tus,ptus,ekran;

char keypad() 
{
   output_d(0x00);
   tus=0;
   output_high(sat1);
   if (input(sut1))  
      { delay_ms(20); tus=1; }
   if (input(sut2))  
      { delay_ms(20); tus=2; }
   if (input(sut3))   
      { delay_ms(20); tus=3; }
   if (input(sut4))  
      { delay_ms(20); tus=0xA; }
   output_low(sat1); 

   output_high(sat2); 
   if (input(sut1))   
      { delay_ms(20); tus=4; }
   if (input(sut2))   
      { delay_ms(20); tus=5; }
   if (input(sut3))   
      { delay_ms(20); tus=6; }
   if (input(sut4))   
      { delay_ms(20); tus=0xB; }
   output_low(sat2); 

   output_high(sat3); 
   if (input(sut1))   
      { delay_ms(20); tus=7; }
   if (input(sut2))   
      { delay_ms(20); tus=8; }
   if (input(sut3))   
      { delay_ms(20); tus=9; }
   if (input(sut4))   
      {delay_ms(20); tus=0x0C; }
   output_low(sat3); 

   output_high(sat4); 
   if (input(sut1))  
      { delay_ms(20); tus=0xE; }
   if (input(sut2))   
      { delay_ms(20); tus=0; }
   if (input(sut3))   
      { delay_ms(20); tus=0xF; }
   if (input(sut4))   
      {delay_ms(20); tus=0xD; }
   output_low(sat4); 
     
   return tus; 
   
}

void goster_b()
      {      
      ekran=tus;
      if(ekran==255){ekran=0;}output_b(tus);            
      }
void goster_c()
      {
      ekran=tus;
      if(ekran==255){ekran=0;}output_c(tus);            
      }

void main ( )
{
   setup_psp(PSP_DISABLED);        
   setup_timer_1(T1_DISABLED);     
   setup_timer_2(T2_DISABLED,0,1); 
   setup_adc_ports(NO_ANALOGS);    
   setup_adc(ADC_OFF);             
   setup_CCP1(CCP_OFF);
   setup_CCP2(CCP_OFF);

   set_tris_b(0x00);
   set_tris_c(0x00);   
   set_tris_d(0x0F);   

   output_b(0x00);
   output_c(0x00); 
   
   int k;
   
   while(1)  
   {
     keypad();
     if(tus>0)
     {
     k++;
     if(k==1){
     goster_b();
     ptus=tus;
     do{delay_ms(10);keypad();}while(tus==ptus);
     }
     
     if(k==2){
     goster_c();
     ptus=tus; k=0; 
     do{delay_ms(10);keypad();}while(tus==ptus);
     }
    }
  } 
}
 

anboli

Full Member level 2
Joined
Mar 9, 2012
Messages
144
Helped
4
Reputation
8
Reaction score
4
Trophy points
1,298
Activity points
2,512
i missed one curly brace in that loop.

so only it getting freeze..


while(1) // Sonsuz döngü
{
i = keypad_read();
if(select_seg)
{
if(i>0)
{
Portb = keypad_read();
select_seg=0;
i=0;
}
}
if(!select_seg)
{
if(i>0)
{
Portc = keypad_read();
select_seg=1;
i=0;
}
}

}


now its work properly..
sure...
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top