Microcontroladores PIC en Linux.
¿Quieres reaccionar a este mensaje? Regístrate en el foro con unos pocos clics o inicia sesión para continuar.

GCBasic: Librería Comparadores para PIC

Ir abajo

para - GCBasic: Librería Comparadores para PIC Empty GCBasic: Librería Comparadores para PIC

Mensaje por Pikitin Miér 18 Feb 2009 - 14:28

Aquí les dejo una librería para comparadores con función establecer Vref.
En principio está pensada para PIC16F87X, pero se puede usar en otros PICs, para esto la función: comp_conf hay que usarla directamente con el valor que podemos ver en los datasheets:

comp_conf b'010'

comp_conf 2

comp_conf 0x02

Para utilizarla basta con ponerlas en el directorio "include" y añadir
la correspondiente linea #include librería.h en el programa donde se
vaya a usar.
Aunque podemos hacerlo para no tener que añadir la
linea #include cada vez que las vayamos a usar; para esto hay que
colocar la librería en "include/lowlevel" y editar el archivo:
lowlevel.dat para añadirle el nombre de la librería, de esta forma el
compilador comprobará si se usan las funciones sin necesidad de añadir
la linea #include.

El driver y el ejemplo también está en la seccion de descargas.

Driver:
Código:
'________________________________________________________________________________
'
'                LIBRERIA COMPARADORES PARA PIC16F87X Y COMPATIBLES
'
'                              (asm optimizado)
'
'                            Nombre: comp-87X.h
'________________________________________________________________________________
'
'
'    This library is free software; you can redistribute it and/or
'    modify it under the terms of the GNU Lesser General Public
'    License as published by the Free Software Foundation; either
'    version 2.1 of the License, or (at your option) any later version.
'
'    This library is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
'    Lesser General Public License for more details.
'
'    You should have received a copy of the GNU Lesser General Public
'    License along with this library; if not, write to the Free Software
'    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
'
'________________________________________________________________________________
'
'                      FUNCIONES DISPONIBLES (ejemplos):
'________________________________________________________________________________
'
'
'IMPORTANTE: CUALQUIER CAMBIO EN LA CONFIGURACION DE COMPARADPRES
'            DESABILITA LAS INTERRUPCIONES POR COMPARADORES,
'            EN ESE CASO HABRÁ QUE REHABILITARLAS.
'
'
'Establece comparadores a: 2 comparadores con Vref interna:
'
'      comp_conf TWO_COMP_VREF
'
'
'Establece canales multiplexados a RA0 RA1:
'
'      comp_mpl RA0_RA1
'
'
'Establece Vref a 60% de Vdd:
'
'      comp_vref  VREF_60
'
'
'Invierte la salida del comparador1:
'
'      comp_inv  C1_INV
'
'
'Lee comparador1:
'
'      value = comp1_read
'
'
'Lee comparador2:
'
'      value2 = comp2_read
'
'
'Deshabilita comparadores:
'
'      comp_off
'
'________________________________________________________________________________
'
'                    DEFINICIONES (opciones disponibles):
'________________________________________________________________________________
'
' Configuración de comparadores

#define COMP_RESET          0X00
#define COMP1_OUT            0X01
#define TWO_COMP            0X02
#define TWO_COMP_OUT        0X03
#define TWO_COMP_COMREF      0X04
#define TWO_COMP_COMREF_OUT  0X05
#define TWO_COMP_VREF        0x06
#define COMP_OFF            0X07


' Multiplexor: usados bits 0-3

#define RA0_RA1        0X00
#define RA2_RA3        0X08


' Invertir salidas: usados bits 4-5 = CMCON.C1INV-C2INV

#define NO_INV        0X00
#define C1_INV        0X10
#define C2_INV      0X20
#define C1_C2_INV    0X30


' Definiciones Vref, voltajes aproximados para Vpp=5v

#define VREF_0    0X20    'V = 0,000
#define VREF_5    0X21    'V = 0,208
#define VREF_6    0X21    'V = 0,208
#define VREF_7    0X22    'V = 0,417
#define VREF_8    0X22    'V = 0,417
#define VREF_9    0X22    'V = 0,417
#define VREF_10  0X22    'V = 0,417
#define VREF_11  0X23    'V = 0,625
#define VREF_12  0X23    'V = 0,625
#define VREF_13  0X23    'V = 0,625
#define VREF_14  0X23    'V = 0,625
#define VREF_15  0X24    'V = 0,833
#define VREF_16  0X24    'V = 0,833
#define VREF_17  0X24    'V = 0,833
#define VREF_18  0X24    'V = 0,833
#define VREF_19  0X25    'V = 1,042
#define VREF_20  0X25    'V = 1,042
#define VREF_21  0X25    'V = 1,042
#define VREF_22  0X25    'V = 1,042
#define VREF_23  0X26    'V = 1,250
#define VREF_24  0X26    'V = 1,250
#define VREF_25  0X26    'V = 1,250
#define VREF_26  0X26    'V = 1,250
#define VREF_27  0X01    'V = 1,406
#define VREF_28  0X01    'V = 1,406
#define VREF_29  0X27    'V = 1,458
#define VREF_30  0X27    'V = 1,458
#define VREF_31  0X02    'V = 1,562
#define VREF_32  0X02    'V = 1,562
#define VREF_33  0X28    'V = 1,667
#define VREF_34  0X03    'V = 1,719
#define VREF_35  0X03    'V = 1,719
#define VREF_36  0X04    'V = 1,875
#define VREF_37  0X04    'V = 1,875
#define VREF_38  0X04    'V = 1,875
#define VREF_39  0X04    'V = 1,875
#define VREF_40  0X05    'V = 2,031
#define VREF_41  0X05    'V = 2,031
#define VREF_42  0X2A    'V = 2,083
#define VREF_43  0X06    'V = 2,188
#define VREF_44  0X06    'V = 2,188
#define VREF_45  0X2B    'V = 2,292
#define VREF_46  0X2B    'V = 2,292
#define VREF_47  0X07    'V = 2,344
#define VREF_48  0X07    'V = 2,344
#define VREF_49  0X08    'V = 2,500
#define VREF_50  0X08    'V = 2,500
#define VREF_51  0X08    'V = 2,500
#define VREF_52  0X09    'V = 2,656
#define VREF_53  0X09    'V = 2,656
#define VREF_54  0X2D    'V = 2,708
#define VREF_55  0X2D    'V = 2,708
#define VREF_56  0X0A    'V = 2,812
#define VREF_57  0X0A    'V = 2,812
#define VREF_58  0X2E    'V = 2,917
#define VREF_59  0X0B    'V = 2,969
#define VREF_60  0X0B    'V = 2,969
#define VREF_61  0X0C    'V = 3,125
#define VREF_62  0X0C    'V = 3,125
#define VREF_63  0X0C    'V = 3,125
#define VREF_64  0X0C    'V = 3,125
#define VREF_65  0X0D    'V = 3,281
#define VREF_66  0X0D    'V = 3,281
#define VREF_67  0X0D    'V = 3,281
#define VREF_68  0X0E    'V = 3,438
#define VREF_69  0X0E    'V = 3,438
#define VREF_70  0X0E    'V = 3,438
#define VREF_71  0X0F    'V = 3,594
#define VREF_72  0X0F    'V = 3,594
#define VREF_73  0X0F    'V = 3,594
#define VREF_74  0X0F    'V = 3,594
#define VREF_75  0X0F    'V = 3,594


#define comp1_read CMCON.C1OUT
#define comp2_read CMCON.C2OUT

'________________________________________________________________________________
'
'                              FUNCTIONS
'________________________________________________________________________________

sub comp_conf(set_config) #NR
    BCF PIE2,CMIE            ; Desactiva interrupciones comparadores
    BCF PIR1,CMIF            ; Borra flag interrupciones comparadores
    MOVF set_config,W
    MOVWF  CMCON            ; Establece modo de operación
end sub


sub comp_mpl(set_switch) #NR
    BCF PIE2,CMIE            ; Desactiva interrupciones comparadores
    BCF PIR1,CMIF            ; Borra flag interrupciones comparadores
    MOVF set_switch,W
    BCF CMCON,CIS            ; Borra anteriores selecciones
    IORWF CMCON,F            ; Establece canales multiplexados
end sub


sub comp_vref(set_cvr) #NR
    MOVF set_cvr,W
    MOVWF  CVRCON            ; Establece valor de Vref
    BSF  CVRCON,7            ; Habilita módulo Vref
end sub


sub comp_inv(set_inv) #NR
    BCF PIE2,CMIE          ; Desactiva interrupciones comparadores
    BCF PIR1,CMIF          ; Borra flag interrupciones comparadores
    MOVF set_inv,W
    BCF  CMCON,4          ; Borra anteriores selecciones
    BCF  CMCON,5
    IORWF CMCON,F          ; Establece canales invertidos
end sub


sub comp_off
    BCF PIE2,CMIE            ; Desactiva interrupciones comparadores   
    BCF PIR1,CMIF            ; Borra flag interrupciones comparadores
    MOVLW    0x07
    MOVWF  CMCON            ; Establece modo off = 111
    MOVLW    0x00
    MOVWF  CVRCON            ; Deshabilita modulo Vref
end sub


Ejemplo de uso:
Código:
'________________________________________________________________________________
'
'              Ejemplo de uso del driver adc-87X.h
'
'
' Un led conectado a PORTB.7 se encenderá cuando el voltaje en PORTA.0 < 60% Vdd
'
'________________________________________________________________________________
'

'General hardware configuration
 
#chip 16F876A, 20
 
#config HS_OSC, WDT_OFF, LVP_OFF, PWRTE_ON



#include comp-87X.h
 
'el driver comp-87X.h debe estar en la carpeta "iclude" de GcBasic



#define LED1  PORTB.7

'________________________________________________________________________________

DIR A0 IN

DIR B7 OUT

comp_conf TWO_COMP_VREF

comp_mpl RA0_RA1

comp_vref  VREF_60
 
do

    LED1 = comp1_read
 
loop

Pikitin
veterano
veterano

Mensajes : 623
Fecha de inscripción : 26/11/2008

http://linuxmicros.blogspot.com/

Volver arriba Ir abajo

Volver arriba

- Temas similares

 
Permisos de este foro:
No puedes responder a temas en este foro.