Autor Tema: como cambiar de puerto b al puerto d del pic 18f452??  (Leído 3475 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado juanjuan19

  • PIC16
  • ***
  • Mensajes: 120
como cambiar de puerto b al puerto d del pic 18f452??
« en: 16 de Marzo de 2012, 22:17:38 »
hola amigos del foro, como estan?les cuento que estoy haciendo un sistemita que mide temperatura y humedad con unpic 18f, el tema es que quiero pasar las lineas que estan multiplexadas entre el teclado y el lcd al puerto d del pic, la verdad no pude hacerlo funcionar, solo me funciona en el puerto b del pic, les paso la libreria que encontre en internet y la modifique segun pense serviria pero nada:
///////////////////////////////////////////////////////////////////////////
////                     KBD_LIB.C by Redraven                         ////
////                                                                   ////
////                     Derived from KBDD.C                           ////
////                  Generic keypad scan driver                       ////
////                                                                   ////
////  kbd_init()   Must be called before any other function.           ////
////                                                                   ////
////  c = kbd_getc(c)  Will return a key value if pressed or /0 if not ////
////                   This function should be called frequently so as ////
////                   not to miss a key press.                        ////
////                                                                   ////
///////////////////////////////////////////////////////////////////////////
////        (C) Copyright 1996,1997 Custom Computer Services            ////
//// This source code may only be used by licensed users of the CCS C   ////
//// compiler.  This source code may only be distributed to other       ////
//// licensed users of the CCS C compiler.  No other use, reproduction  ////
//// or distribution is permitted without written permission.           ////
//// Derivative programs created using this software in object code     ////
//// form are not restricted in any way.                                ////
////////////////////////////////////////////////////////////////////////////

////////////////// The following defines the keypad layout on port D

// Un-comment the following define to use port B
#define use_portd_kbd TRUE

//#define PUERTO_PIC18 0xF83
// Make sure the port used has pull-up resistors (or the LCD) on
// the column pins


#if defined(__PCH__)
#if defined use_portd_kbd
   #byte kbd = 0xF83              // This puts the entire structure
#else
   #byte kbd = 0xF83              // This puts the entire structure
#endif
#else
#if defined use_portd_kbd
   ////#byte kbd = 6                  // on to port B (at address 6)
////#else
   #byte kbd = 8                  // on to port D (at address 8)
#endif
#endif

#if defined use_portd_kbd
////   #define set_tris_kbd(x) set_tris_b(x)
////#else
   #define set_tris_kbd(x) set_tris_d(x)
#endif

//Keypad connection:   (for example column 0 is B0)

#define COL0 (1 << 0) // PIN_D0
#define COL1 (1 << 1) // PIN_D1
#define COL2 (1 << 2) // PIN_D2
#define COL3 (1 << 3) // PIN_D3

#define ROW0 (1 << 4) // PIN_D4
#define ROW1 (1 << 5) // PIN_D5
#define ROW2 (1 << 6) // PIN_D6
#define ROW3 (1 << 7) // PIN_D7


#define ALL_ROWS (ROW0|ROW1|ROW2|ROW3)


#define ALL_PINS (ALL_ROWS|COL0|COL1|COL2|COL3)


// Keypad layout:

char const KEYS[4][4] = {{'1','2','3','A'},
                         {'6','5','4','B'},
                         {'7','8','9','C'},
                         {'*','0','#','D'}};


#define KBD_DEBOUNCE_FACTOR 33    // Set this number to apx n/333 where
                                  // n is the number of times you expect
                                  // to call kbd_getc each second

void kbd_init() {
}

char kbd_getc( ) {
   static byte kbd_call_count;
   static short int kbd_down;
   static char last_key;
   static byte col;

   byte kchar;
   byte row;

   kchar='\0';
   if(++kbd_call_count>KBD_DEBOUNCE_FACTOR) {
       switch (col) {
         case 0   : set_tris_kbd(ALL_PINS&~COL0);
                    kbd=~COL0&ALL_PINS;
                    break;
         case 1   : set_tris_kbd(ALL_PINS&~COL1);
                    kbd=~COL1&ALL_PINS;
                    break;
         case 2   : set_tris_kbd(ALL_PINS&~COL2);
                    kbd=~COL2&ALL_PINS;
                    break;
         case 3   : set_tris_kbd(ALL_PINS&~COL3);
                    kbd=~COL3&ALL_PINS;
                    break;
       }

       if(kbd_down) {
         if((kbd & (ALL_ROWS))==(ALL_ROWS)) {
           kbd_down=false;
           kchar=last_key;
           last_key='\0';
         }
       } else {
          if((kbd & (ALL_ROWS))!=(ALL_ROWS)) {
             if((kbd & ROW0)==0)
               row=0;
             else if((kbd & ROW1)==0)
               row=1;
             else if((kbd & ROW2)==0)
               row=2;
             else if((kbd & ROW3)==0)
               row=3;
             last_key =KEYS[row][col];
             kbd_down = true;
          } else {
             ++col;
             if(col==4)
               col=0;
          }
       }
      kbd_call_count=0;
   }
  set_tris_kbd(ALL_PINS);
  return(kchar);
}

y ahora les muestro el inicio del main de mi programa:
void main(void)
{
    //port_b_pullups(true);      
    
    kbd_init();
    set_tris_c(0b00000011); //configura RB7 como salida el resto como entrada
    set_tris_d(0x0F);
    RC5=0; //inicializo el bit RB7 a 0;
    set_tris_e(0b11111111);
    output_low(pin_c4);
    output_low(pin_c3);
    output_low(pin_c2);
    
 me:
  
   lcd_init();
   lcd_clear();
   lcd_gotoxy(1,1);
   char data[3],clave[3];
   char kk;
   int i;
 
 //va a preguntar la clave y para que , les agradesco cualquier ayuda, el tema es que quiero cambiar d epuerto para que tener libre la interrupcion RB0, ya que me parece piola para zafar de otro porblema que se me aparecio al momento de pulir mi programa, el tema es que cuando pongo una tecla el programa no sale y no responde, y por eso quiero usar la interrupcion RB0 y hacerlo salir a la fuerza de donde se encuentre, gracias anticipadamente!!!!!!!!!!!!!

Desconectado MLO__

  • Colaborador
  • DsPIC33
  • *****
  • Mensajes: 4581
Re: como cambiar de puerto b al puerto d del pic 18f452??
« Respuesta #1 en: 17 de Marzo de 2012, 01:52:45 »
Hola.

El PORTB tiene resistencias de pullup internas ... el PORTD no ... de pronto se soluciona conectandolas en el diseno.

Saludos
El papel lo aguanta todo

Desconectado juanjuan19

  • PIC16
  • ***
  • Mensajes: 120
Re: como cambiar de puerto b al puerto d del pic 18f452??
« Respuesta #2 en: 19 de Marzo de 2012, 22:19:58 »
Hola, MLO__, gracias por tomarte la molestia de leer mi consulta y responder, sabes que, probe poniendole unas resistencias a las entradas de los pines en el puerto d del pic, pero aun asi no me ingresaba los datos que hacia por teclado, te muestro la libreria original que encontre en internet y luego la que modifique:

///////////////////////////////////////////////////////////////////////////
////                     KBD_LIB.C by Redraven                         ////
////                                                                   ////
////                     Derived from KBDD.C                           ////
////                  Generic keypad scan driver                       ////
////                                                                   ////
////  kbd_init()   Must be called before any other function.           ////
////                                                                   ////
////  c = kbd_getc(c)  Will return a key value if pressed or /0 if not ////
////                   This function should be called frequently so as ////
////                   not to miss a key press.                        ////
////                                                                   ////
///////////////////////////////////////////////////////////////////////////
////        (C) Copyright 1996,1997 Custom Computer Services            ////
//// This source code may only be used by licensed users of the CCS C   ////
//// compiler.  This source code may only be distributed to other       ////
//// licensed users of the CCS C compiler.  No other use, reproduction  ////
//// or distribution is permitted without written permission.           ////
//// Derivative programs created using this software in object code     ////
//// form are not restricted in any way.                                ////
////////////////////////////////////////////////////////////////////////////

////////////////// The following defines the keypad layout on port D

// Un-comment the following define to use port B
#define use_portb_kbd TRUE

// Make sure the port used has pull-up resistors (or the LCD) on
// the column pins


#if defined(__PCH__)
#if defined use_portb_kbd
   #byte kbd = 0xF81              // This puts the entire structure
#else
   #byte kbd = 0xF83              // This puts the entire structure
#endif
#else
#if defined use_portb_kbd
   #byte kbd = 6                  // on to port B (at address 6)
#else
   #byte kbd = 8                  // on to port D (at address 8)
#endif
#endif

#if defined use_portb_kbd
   #define set_tris_kbd(x) set_tris_b(x)
#else
   #define set_tris_kbd(x) set_tris_d(x)
#endif

//Keypad connection:   (for example column 0 is B0)

#define COL0 (1 << 0) // PIN_B0
#define COL1 (1 << 1) // PIN_B1
#define COL2 (1 << 2) // PIN_B2
#define COL3 (1 << 3) // PIN_B3

#define ROW0 (1 << 4) // PIN_B4
#define ROW1 (1 << 5) // PIN_B5
#define ROW2 (1 << 6) // PIN_B6
#define ROW3 (1 << 7) // PIN_B7


#define ALL_ROWS (ROW0|ROW1|ROW2|ROW3)


#define ALL_PINS (ALL_ROWS|COL0|COL1|COL2|COL3)


// Keypad layout:

char const KEYS[4][4] = {{'1','2','3','A'},
                         {'4','5','6','B'},
                         {'7','8','9','C'},
                         {'*','0','#','D'}};


#define KBD_DEBOUNCE_FACTOR 33    // Set this number to apx n/333 where
                                  // n is the number of times you expect
                                  // to call kbd_getc each second

void kbd_init() {
}

char kbd_getc( ) {
   static byte kbd_call_count;
   static short int kbd_down;
   static char last_key;
   static byte col;

   byte kchar;
   byte row;

   kchar='\0';
   if(++kbd_call_count>KBD_DEBOUNCE_FACTOR) {
       switch (col) {
         case 0   : set_tris_kbd(ALL_PINS&~COL0);
                    kbd=~COL0&ALL_PINS;
                    break;
         case 1   : set_tris_kbd(ALL_PINS&~COL1);
                    kbd=~COL1&ALL_PINS;
                    break;
         case 2   : set_tris_kbd(ALL_PINS&~COL2);
                    kbd=~COL2&ALL_PINS;
                    break;
         case 3   : set_tris_kbd(ALL_PINS&~COL3);
                    kbd=~COL3&ALL_PINS;
                    break;
       }

       if(kbd_down) {
         if((kbd & (ALL_ROWS))==(ALL_ROWS)) {
           kbd_down=false;
           kchar=last_key;
           last_key='\0';
         }
       } else {
          if((kbd & (ALL_ROWS))!=(ALL_ROWS)) {
             if((kbd & ROW0)==0)
               row=0;
             else if((kbd & ROW1)==0)
               row=1;
             else if((kbd & ROW2)==0)
               row=2;
             else if((kbd & ROW3)==0)
               row=3;
             last_key =KEYS[row][col];
             kbd_down = true;
          } else {
             ++col;
             if(col==4)
               col=0;
          }
       }
      kbd_call_count=0;
   }
  set_tris_kbd(ALL_PINS);
  return(kchar);
}

//////*************************************************************///////////
///////*************************************************************//////////
//////ahora viene la que modifique yo//////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
////                     KBD_LIB.C by Redraven                         ////
////                                                                   ////
////                     Derived from KBDD.C                           ////
////                  Generic keypad scan driver                       ////
////                                                                   ////
////  kbd_init()   Must be called before any other function.           ////
////                                                                   ////
////  c = kbd_getc(c)  Will return a key value if pressed or /0 if not ////
////                   This function should be called frequently so as ////
////                   not to miss a key press.                        ////
////                                                                   ////
///////////////////////////////////////////////////////////////////////////
////        (C) Copyright 1996,1997 Custom Computer Services            ////
//// This source code may only be used by licensed users of the CCS C   ////
//// compiler.  This source code may only be distributed to other       ////
//// licensed users of the CCS C compiler.  No other use, reproduction  ////
//// or distribution is permitted without written permission.           ////
//// Derivative programs created using this software in object code     ////
//// form are not restricted in any way.                                ////
////////////////////////////////////////////////////////////////////////////

////////////////// The following defines the keypad layout on port D

// Un-comment the following define to use port B
#define use_portd_kbd TRUE

//#define PUERTO_PIC18 0xF83
// Make sure the port used has pull-up resistors (or the LCD) on
// the column pins


#if defined(__PCH__)
#if defined use_portd_kbd
   #byte kbd = 0xF83              // This puts the entire structure
////#else
////   #byte kbd = 0xF83              // This puts the entire structure
#endif
#else
#if defined use_portd_kbd
   ////#byte kbd = 6                  // on to port B (at address 6)
////#else
   #byte kbd = 8                  // on to port D (at address 8)
#endif
#endif

#if defined use_portd_kbd
////   #define set_tris_kbd(x) set_tris_b(x)
////#else
   #define set_tris_kbd(x) set_tris_d(x)
#endif

//Keypad connection:   (for example column 0 is B0)

#define COL0 (1 << 0) // PIN_D0
#define COL1 (1 << 1) // PIN_D1
#define COL2 (1 << 2) // PIN_D2
#define COL3 (1 << 3) // PIN_D3

#define ROW0 (1 << 4) // PIN_D4
#define ROW1 (1 << 5) // PIN_D5
#define ROW2 (1 << 6) // PIN_D6
#define ROW3 (1 << 7) // PIN_D7


#define ALL_ROWS (ROW0|ROW1|ROW2|ROW3)


#define ALL_PINS (ALL_ROWS|COL0|COL1|COL2|COL3)


// Keypad layout:

char const KEYS[4][4] = {{'1','2','3','A'},
                         {'6','5','4','B'},
                         {'7','8','9','C'},
                         {'*','0','#','D'}};


#define KBD_DEBOUNCE_FACTOR 33    // Set this number to apx n/333 where
                                  // n is the number of times you expect
                                  // to call kbd_getc each second

void kbd_init() {
}

char kbd_getc( ) {
   static byte kbd_call_count;
   static short int kbd_down;
   static char last_key;
   static byte col;

   byte kchar;
   byte row;

   kchar='\0';
   if(++kbd_call_count>KBD_DEBOUNCE_FACTOR) {
       switch (col) {
         case 0   : set_tris_kbd(ALL_PINS&~COL0);
                    kbd=~COL0&ALL_PINS;
                    break;
         case 1   : set_tris_kbd(ALL_PINS&~COL1);
                    kbd=~COL1&ALL_PINS;
                    break;
         case 2   : set_tris_kbd(ALL_PINS&~COL2);
                    kbd=~COL2&ALL_PINS;
                    break;
         case 3   : set_tris_kbd(ALL_PINS&~COL3);
                    kbd=~COL3&ALL_PINS;
                    break;
       }

       if(kbd_down) {
         if((kbd & (ALL_ROWS))==(ALL_ROWS)) {
           kbd_down=false;
           kchar=last_key;
           last_key='\0';
         }
       } else {
          if((kbd & (ALL_ROWS))!=(ALL_ROWS)) {
             if((kbd & ROW0)==0)
               row=0;
             else if((kbd & ROW1)==0)
               row=1;
             else if((kbd & ROW2)==0)
               row=2;
             else if((kbd & ROW3)==0)
               row=3;
             last_key =KEYS[row][col];
             kbd_down = true;
          } else {
             ++col;
             if(col==4)
               col=0;
          }
       }
      kbd_call_count=0;
   }
  set_tris_kbd(ALL_PINS);
  return(kchar);
}




Desde ya gracias anticipadamente, saludos!!!!!!!!
 

Desconectado jukinch

  • Colaborador
  • PIC24F
  • *****
  • Mensajes: 608
Re: como cambiar de puerto b al puerto d del pic 18f452??
« Respuesta #3 en: 21 de Marzo de 2012, 10:21:41 »
Hola a ambos.
      En la directiva de preprocesado que tiene la librería original dice:

// Un-comment the following define to use port B  (descomentar el siguiente define para usar portb
#define use_portb_kbd TRUE

y en la librería tuya lo cambiaste a portd:

// Un-comment the following define to use port B
#define use_portd_kbd TRUE


Eso no debes cambiarlo. Sólo comentarlo. Debés dejarlo asi:
//#define use_portb_kbd TRUE

Para que el compilador elija solo. Ya que con eso hace una compilación condicional. dependiendo del puerto que elijas.

Saludos.
      Jukinch

modificación: resalté con negritas
« Última modificación: 21 de Marzo de 2012, 11:14:52 por jukinch »
"Divide las dificultades que examinas en tantas partes como sea posible para su mejor solución." -René Descartes

Desconectado jukinch

  • Colaborador
  • PIC24F
  • *****
  • Mensajes: 608
Re: como cambiar de puerto b al puerto d del pic 18f452??
« Respuesta #4 en: 21 de Marzo de 2012, 11:20:54 »
Juanjuan19: te recomiendo postear tu código completo usando geSHi (eso que se ve arriba a la derecha al postear. Elegí código en lenguaje c y en medio de los corchetes entre code y barra code poné tu código.
te va a salir esto: (sin espacios )
[ code=c]  [ /code ]

y entre medio ponés tu código.  Así queda más prolijo y fácil de leer.  ;-)

[ ....]
tu codigo
[...]

           Saludos.
               Jukinch
"Divide las dificultades que examinas en tantas partes como sea posible para su mejor solución." -René Descartes

Desconectado juanjuan19

  • PIC16
  • ***
  • Mensajes: 120
Re: como cambiar de puerto b al puerto d del pic 18f452??
« Respuesta #5 en: 21 de Marzo de 2012, 18:39:51 »
               Jukinch:
recien veo tu mensaje, lo acomodo y lo posteo, gracias por la recomendacion y por constestar

           Un gran saludo.
                          juanjuan19


 

anything