Autor Tema: Iniciandome  (Leído 1838 veces)

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

Desconectado x-ufo

  • PIC10
  • *
  • Mensajes: 6
Iniciandome
« en: 08 de Mayo de 2008, 12:47:32 »
Hola:
Antes de nada felicitaros por el foro, me parece magnifico.
Mi pregunta posiblemente os parezca abstante tonta, pero me estoy iniciando y no consigo hacerlo funcionar en condiciones.
Utilizo el CCS y quiero que mi pic 16F876 lea los datos de un inclinometro, tengo todo resuelto, salvo un campo. Os cuento, el inclinometro tiene una salida que solo me proporciona un 1 o un 0, pero no se porque no consigo leerlo, me podriais poner un ejemplo para leer un valor de un pin de un dispositivo? muchas gracias

Desconectado jfmateos2

  • Moderador Local
  • DsPIC30
  • *****
  • Mensajes: 3145
Re: Iniciandome
« Respuesta #1 en: 08 de Mayo de 2008, 13:29:31 »
Bienvenido x-ufo. Aquí te pongo un ejemplo del propio CCS en el que una tira de LEDs se iluminan en un sentido u otro según el estado de un botón.
Código: [Seleccionar]
/////////////////////////////////////////////////////////////////////////
////                             EX_8PIN.C                           ////
////  This program shows how to use the pins on a PIC12C508/9 or a   ////
////  PIC12C671/2 for general I/O.  When run, the program will run up////
////  and down the LEDs.  If you hold down the push button, the      ////
////  program will reverse its direction.                            ////
////                                                                 ////
////  Configure the CCS prototype card as follows:                   ////
////             12C508/671 pin   Protoboard                         ////
////                  1                +5V                           ////
////                  2                LED 5                         ////
////                  3                LED 4                         ////
////                  4                Push Button                   ////
////                  5                LED 3                         ////
////                  6                LED 2                         ////
////                  7                LED 1                         ////
////                  8                gnd                           ////
////                                                                 ////
////  This example will work with the PCB and PCM compilers.  The    ////
////  following conditional compilation lines are used to include a  ////
////  valid device for each compiler.  Change the device and clock   ////
////  for your hardware if needed.                                   ////
/////////////////////////////////////////////////////////////////////////
////        (C) Copyright 1996,2003 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.              ////
/////////////////////////////////////////////////////////////////////////


#if defined(__PCB__)
#include <12C508.h>
#fuses INTRC,NOWDT,NOPROTECT, NOMCLR
#use delay(clock=4000000)
#define GP0 PIN_B0
#define GP1 PIN_B1
#define GP2 PIN_B2
#define GP3 PIN_B3
#define GP4 PIN_B4
#define GP5 PIN_B5

#elif defined(__PCM__)
#include <12C671.h>
#fuses INTRC,NOWDT,NOPROTECT, NOMCLR,NOLVP
#use delay(clock=4000000)
#define GP0 PIN_A0
#define GP1 PIN_A1
#define GP2 PIN_A2
#define GP3 PIN_A3
#define GP4 PIN_A4
#define GP5 PIN_A5

#endif


void cycle_forward (void)
{
   output_low (GP5);
   output_high (GP0);
   delay_ms (70);
   output_low (GP0);
   output_high (GP1);
   delay_ms (70);
   output_low (GP1);
   output_high (GP2);
   delay_ms (70);
   output_low (GP2);
   output_high (GP4);
   delay_ms (70);
   output_low (GP4);
   output_high (GP5);
   delay_ms (70);
}

void cycle_backward (void)
{
   output_low (GP0);
   output_high (GP5);
   delay_ms (70);
   output_low (GP5);
   output_high (GP4);
   delay_ms (70);
   output_low (GP4);
   output_high (GP2);
   delay_ms (70);
   output_low (GP2);
   output_high (GP1);
   delay_ms (70);
   output_low (GP1);
   output_high (GP0);
   delay_ms (70);
}

void main() {
#if defined(__PCB__)
   setup_counters (RTCC_INTERNAL,RTCC_DIV_2);
                               // See .h file for other special
                               // options for setup_counters
#else
   setup_counters (RTCC_INTERNAL,RTCC_DIV_2);
#endif


   while (TRUE) {
      if (!input (GP3))
   cycle_forward ();
      else
   cycle_backward ();
   }
}

Desconectado firepic

  • Colaborador
  • PIC24H
  • *****
  • Mensajes: 1130
    • JC Servicios
Re: Iniciandome
« Respuesta #2 en: 08 de Mayo de 2008, 14:21:14 »
Bienvenido x-ufo!  :-)
En el ejemplo de jfmateos2, la parte que lee el valor (si es 0 o 1) es la que está abajo:

   while (TRUE)
   {
      if (!input (GP3))
         cycle_forward ();
      else
         cycle_backward ();
   }

Eso quiere decir que cuando GP3 (que si ves arriba del código representa a RA3) está en 0 (FALSE), pues al hacer !GP3 (negación) devolverá el valor TRUE y entrará en la función cycle_forward, y pues, de lo contrario (si RA3=1, el resultado será FALSE) entrará en cycle_backward.

La instrucción como ves es input(pin_que_quieres_evaluar);
Esa instrucción te devuelve el estado del pin.

Ok nos leemos!  :mrgreen:

"Por la presunción solo se ocasiona una lucha, pero con los que consultan juntos hay sabiduría" (Proverbios 13:10).
Visita Mi Sitio Web


 

anything