Public feed #724574

codigo fuente del adc0804

public java v1 · immutable
Raw Edit
·published 2007-10-03 18:08 UTC
rendered paste body
codigo fuente en visual basic'variable que contiene la direccion base del puertoPrivate DIR_PUERTO As Integer'Declaracion de funcionesPrivate Declare Function GetPortBit Lib "IO.DLL" (ByVal Port As Integer, ByVal Bit As Byte) As BooleanPrivate Declare Function IsDriverInstalled Lib "IO.DLL" () As Boolean'Funciones que habilitan/deshabilitan bits del puerto.Private Declare Function SetPortBit Lib "IO.DLL" (ByVal Port As Integer, ByVal Bit As Byte) As BooleanPrivate Declare Function NotPortBit Lib "IO.DLL" (ByVal Port As Integer, ByVal Bit As Byte) As BooleanPrivate Declare Function ClrPortBit Lib "IO.DLL" (ByVal Port As Integer, ByVal Bit As Byte) As BooleanPrivate Sub Form_Load()'Veo si cargo el driverIf IsDriverInstalled() = True Then    DIR_PUERTO = &H378Else    MsgBox "Driver no instalado.", vbInformation + vbOKOnly + vbSystemModal, "Problemas!"EndEnd IfEnd SubPrivate Sub LeerP_Click()'Limpio el estado del puertoFor X = 0 To 7 Step 1    aux = SetPortBit(DIR_PUERTO, X)    aux = ClrPortBit(DIR_PUERTO, X)Next'Configuro el puerto paralelo como entrada'poniendo el BIT 5 del port de control en 1aux = SetPortBit(DIR_PUERTO + 2, 5) '--- Ordeno la conversion A/D----'selecciono el chip'poniendo CS (Pin 1 del chip) a masa:C0=0 (Pin 1 del LPT)aux = SetPortBit(DIR_PUERTO + 2, 0)'Pongo WR (pin  3 del chip) bajo: C2=0 (pin 16 del LPT)aux = SetPortBit(DIR_PUERTO + 2, 2)'Espero al menos 100 nsFor X = 0 To 100000Next'Pongo WR (pin  3 del chip) en alto: C2=1 (pin 16 LPT)aux = ClrPortBit(DIR_PUERTO + 2, 2)'Leo y muestro Bits de controlFor X = 0 To 7 Step 1    If GetPortBit(DIR_PUERTO + 2, X) = True Then        Text3(X).Text = CStr(1)    Else        Text3(X).Text = CStr(0)    End IfNextFor X = 0 To 100000Next'Le doy tiempo a que INTR (pin 5 del chip) se ponga en altoFor X = 0 To 100000Next'Pongo RD (pin 2 del chip) en bajo: C3=1 (pin 1 LPT)aux = ClrPortBit(DIR_PUERTO + 2, 3)'Espera al menos 200 nsFor X = 0 To 100000Next'Leo los 8 bits del puertoValorD = 0For X = 0 To 7 Step 1    'Bits de datos    If GetPortBit(DIR_PUERTO, X) = True Then        Text1(X).Text = CStr(1)        ValorD = ValorD + 2 ^ (7 - X)    Else        Text1(X).Text = CStr(0)    End If         'Bits de control    If GetPortBit(DIR_PUERTO + 2, X) = True Then        Text3(X).Text = CStr(1)    Else        Text3(X).Text = CStr(0)    End IfNext X'Calculo el valor porcentual, etcValorP = ValorD / 256 * 100ValorV = ValorD / 256 * 5'La siguiente linea calcula la temperatura a mostrar.'Para calibrar lo que mide el sensor, podemos variar el'valor que se resta al final (2.73).ValorT = ValorV / 0.01 - 273End SubPrivate Sub LimpiarP_Click()'Limpio el estado del puertoaux = ClrPortBit(DIR_PUERTO + 2, 5)For X = 0 To 7 Step 1    aux = SetPortBit(DIR_PUERTO, X)    aux = ClrPortBit(DIR_PUERTO, X)    aux = ClrPortBit(DIR_PUERTO + 2, X)Next'Leo los 8 bits del puertoValorD = 0For X = 0 To 7 Step 1    'Bits de datos    If GetPortBit(DIR_PUERTO, X) = True Then        Text1(X).Text = CStr(1)        ValorD = ValorD + 2 ^ X    Else        Text1(X).Text = CStr(0)    End If    'Bits de control        If GetPortBit(DIR_PUERTO + 2, X) = True Then        Text3(X).Text = CStr(1)    Else    Text3(X).Text = CStr(0)    End If    NextEnd SubPrivate Sub Timer1_Timer()Dim X As IntegerDim aux As Integer'acquiring different bits and there values in different registers in the parallel port'For X = 0 To 7 Step 1'If GetPortBit(DIR_PUERTO, X) = True Then'Text1(X).Text = CStr(1)'Else'Text1(X).Text = CStr(0)'End If'If GetPortBit(DIR_PUERTO + 1, X) = True Then'Text2(X).Text = CStr(1)'Else'Text2(X).Text = CStr(0)'End If'aux = SetPortBit(DIR_PUERTO + 2, 32)'If GetPortBit(DIR_PUERTO + 2, X) = True Then'Text3(X).Text = CStr(1)'Else'Text3(X).Text = CStr(0)'End If'Next XEnd Subcodigo fuente en vb.netOption Strict OffOption Explicit OnFriend Class Frm_Register_Analyzer	Inherits System.Windows.Forms.Form	'variable que contiene la direccion base del puerto	Private DIR_PUERTO As Short		'Declaracion de funciones	Private Declare Function GetPortBit Lib "IO.DLL" (ByVal Port As Short, ByVal Bit As Byte) As Boolean	Private Declare Function IsDriverInstalled Lib "IO.DLL" () As Boolean	'Funciones que habilitan/deshabilitan bits del puerto.	Private Declare Function SetPortBit Lib "IO.DLL" (ByVal Port As Short, ByVal Bit As Byte) As Boolean	Private Declare Function NotPortBit Lib "IO.DLL" (ByVal Port As Short, ByVal Bit As Byte) As Boolean	Private Declare Function ClrPortBit Lib "IO.DLL" (ByVal Port As Short, ByVal Bit As Byte) As Boolean	Private Sub Frm_Register_Analyzer_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load        'Veo si cargo el driver        If IsDriverInstalled() = True Then            DIR_PUERTO = &H378S        Else            MsgBox("Driver no instalado.", MsgBoxStyle.Information + MsgBoxStyle.OkOnly + MsgBoxStyle.SystemModal, "Problemas!")            End        End If    End Sub		Private Sub LeerP_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles LeerP.Click		Dim aux As Object		Dim X As Object		'Limpio el estado del puerto		For X = 0 To 7 Step 1						aux = SetPortBit(DIR_PUERTO, X)						aux = ClrPortBit(DIR_PUERTO, X)		Next 				'Configuro el puerto paralelo como entrada		'poniendo el BIT 5 del port de control en 1				aux = SetPortBit(DIR_PUERTO + 2, 5)								'--- Ordeno la conversion A/D----		'selecciono el chip		'poniendo CS (Pin 1 del chip) a masa:C0=0 (Pin 1 del LPT)				aux = SetPortBit(DIR_PUERTO + 2, 0)								'Pongo WR (pin  3 del chip) bajo: C2=0 (pin 16 del LPT)				aux = SetPortBit(DIR_PUERTO + 2, 2)				'Espero al menos 100 ns		For X = 0 To 100000		Next 						'Pongo WR (pin  3 del chip) en alto: C2=1 (pin 16 LPT)				aux = ClrPortBit(DIR_PUERTO + 2, 2)		'Leo y muestro Bits de control		For X = 0 To 7 Step 1						If GetPortBit(DIR_PUERTO + 2, X) = True Then				Text3(X).Text = CStr(1)			Else				Text3(X).Text = CStr(0)			End If		Next 				For X = 0 To 100000		Next 								'Le doy tiempo a que INTR (pin 5 del chip) se ponga en alto		For X = 0 To 100000		Next 						'Pongo RD (pin 2 del chip) en bajo: C3=1 (pin 1 LPT)				aux = ClrPortBit(DIR_PUERTO + 2, 3)				'Espera al menos 200 ns		For X = 0 To 100000		Next 				'Leo los 8 bits del puerto        ValorD.Text = CStr(0)		For X = 0 To 7 Step 1			'Bits de datos						If GetPortBit(DIR_PUERTO, X) = True Then				Text1(X).Text = CStr(1)								ValorD.Text = CStr(CDbl(ValorD.Text) + 2 ^ (7 - X))			Else				Text1(X).Text = CStr(0)			End If			'Bits de control						If GetPortBit(DIR_PUERTO + 2, X) = True Then				Text3(X).Text = CStr(1)			Else				Text3(X).Text = CStr(0)			End If					Next X				'Calculo el valor porcentual, etc		ValorP.Text = CStr(CDbl(ValorD.Text) / 256 * 100)		ValorV.Text = CStr(CDbl(ValorD.Text) / 256 * 5)		'La siguiente linea calcula la temperatura a mostrar.		'Para calibrar lo que mide el sensor, podemos variar el		'valor que se resta al final (2.73).		ValorT.Text = CStr(CDbl(ValorV.Text) / 0.01 - 273)	End Sub		Private Sub LimpiarP_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles LimpiarP.Click		Dim X As Object		Dim aux As Object		'Limpio el estado del puerto				aux = ClrPortBit(DIR_PUERTO + 2, 5)				For X = 0 To 7 Step 1						aux = SetPortBit(DIR_PUERTO, X)						aux = ClrPortBit(DIR_PUERTO, X)						aux = ClrPortBit(DIR_PUERTO + 2, X)		Next 				'Leo los 8 bits del puerto		ValorD.Text = CStr(0)		For X = 0 To 7 Step 1			'Bits de datos						If GetPortBit(DIR_PUERTO, X) = True Then				Text1(X).Text = CStr(1)								ValorD.Text = CStr(CDbl(ValorD.Text) + 2 ^ X)			Else				Text1(X).Text = CStr(0)			End If			'Bits de control									If GetPortBit(DIR_PUERTO + 2, X) = True Then				Text3(X).Text = CStr(1)			Else				Text3(X).Text = CStr(0)			End If								Next 	End Sub		Private Sub Timer1_Timer()        Dim X As Short        Dim aux As Short		        'acquiring different bits and there values in different registers in the parallel port        For X = 0 To 7 Step 1            If GetPortBit(DIR_PUERTO, X) = True Then                Text1(X).Text = CStr(1)            Else                Text1(X).Text = CStr(0)            End If            If GetPortBit(DIR_PUERTO + 1, X) = True Then                Text2(X).Text = CStr(1)            Else                Text2(X).Text = CStr(0)            End If            aux = SetPortBit(DIR_PUERTO + 2, 32)            If GetPortBit(DIR_PUERTO + 2, X) = True Then                Text3(X).Text = CStr(1)            Else                Text3(X).Text = CStr(0)            End If        Next X    End SubEnd Class