Device 18F4523
Declare Xtal 20
Config_Start
OSC = HS ; HS oscillator
PWRT = OFF ; PWRT disabled
BOREN = OFF ; Brown-out Reset disabled in hardware and software
WDT = OFF ; WDT disabled (control is placed on the SWDTEN bit)
MCLRE = OFF ; RE3 input pin enabled; MCLR disabled
LVP = OFF ; Disabled
Debug = OFF ; Background debugger disabled, RB6 and RB7 configured as general purpose I/O pins
Config_End
'ADCON2.7 = 1
PortB_Pullups = 1
Declare Adin_Res 12 ' 10-bit result required
Declare Adin_Tad 32_FOSC ' Set the ADC's clock source
Declare Adin_Stime 50 ' Allow 50us sample time
Declare LCD_Type 0 ' Type of LCD Used is Alpha
Declare LCD_DTPin PORTD.4 ' The control bits B4,B5,B6,B7
Declare LCD_RSPin PORTD.2 ' RS pin on B2
Declare LCD_ENPin PORTD.3 ' E pin on B3
Declare LCD_Interface 4 ' Interface method is 4 bit
CCP1_Pin = PORTC.2
CCP2_Pin = PORTC.1
Dim ADC_Loops As Word
Dim Temp As Word
Dim port As Byte
Dim incremento As Float
Dim ADC_Result1 As Word
Dim ADC_Result3 As Word
ADCON1 = %11001011 ' Set all to analogue inputs (PORTA)
TRISA = $FF ' Declare porta as all inputs
TRISB = $FF
DelayMS 150
TRISD.0=0
PORTD.0=0
Dim Result1 As Float
Dim Result3 As Float
Dim ADC_Total1 As Float
Dim ADC_Total3 As Float
Dim Last_Result1 As Float
Dim Last_Result3 As Float
Dim ax As Float
Dim az As Float
Dim inclina As Float
Dim inclinamm As Float
Dim inclinammPf As Float
Dim diferencia As Word
Dim mp1 As Byte
Dim brillo As Byte
incremento=0.1
ADC_Result1=0
ADC_Result3=0
brillo=0
inclinammPf =0
ADC_Loops =800
Print $FE,1
mp1=0
Print $FE,$40,$07,$05,$07,$00,$00,$00,$00,$00 ' Custom character for Degree
MenuP:
incremento=0.1
'PORTd.0 =0
Print $FE,1
Print At 1, 1, "Menu P"
If mp1=1 Then
Print At 2, 1, "Contrast"
End If
If mp1=2 Then
Print At 2, 1, "Pos. F"
End If
If mp1=3 Then
Print At 2, 1, "Inc A"
End If
If PORTB=254 Then
If mp1>1 Then
mp1=mp1-1
Else
mp1=3
End If
DelayMS 600
End If
If PORTB=251 Then
If mp1<3 Then
mp1=mp1+1
Else
mp1=1
End If
DelayMS 600
End If
If PORTB=253 Then
If mp1=1 Then
DelayMS 200
GoTo menubrillo
End If
If mp1=2 Then
DelayMS 200
GoTo menuPosfinal
End If
If mp1=3 Then
DelayMS 200
GoTo menuPosActual
End If
End If
DelayMS 50
GoTo MenuP
menubrillo:
Print $FE,1
Print At 1, 1, "C.Contraste"
Print At 2,1, Dec brillo
If PORTB=254 Then
If brillo>1 Then
brillo=brillo-3
Else
brillo=0
End If
DelayMS 400
End If
If PORTB=251 Then
If brillo<30 Then
brillo=brillo+3
Else
brillo=30
End If
DelayMS 400
End If
HPWM 1,brillo,1000
DelayMS 100
If PORTB=253 Then
mp1=0
DelayMS 100
GoTo MenuP
End If
GoTo menubrillo
menuPosfinal:
If PORTB=254 Then
If inclinammPf >-88.8889 Then
inclinammPf=inclinammPf-incremento
Else
inclinammPf=-88.8889
End If
DelayMS 100
End If
If PORTB=251 Then
If inclinammPf < 1600 Then
inclinammPf=inclinammPf+incremento
Else
inclinammPf=1600
End If
DelayMS 100
End If
' HPWM 2,inclinammPf,1000
DelayMS 100
If PORTB=253 Then
mp1=0
DelayMS 100
GoTo MenuP
End If
If PORTB=250 Then
If incremento < 100 Then
incremento=incremento *10
DelayMS 300
Else
incremento=0.1
DelayMS 300
End If
End If
Print $FE,1
Print At 1, 1, "Ingr Pos"
Print At 2, 1, Dec1 inclinammPf, " Mils"
GoTo menuPosfinal
menuPosActual:
'GoTo menup
ADC_Total1 = 0 ' Clear summing register
ADC_Total3 = 0
' Loop for a pre-determined number of times
For Temp = 1 To ADC_Loops
ADC_Result1 = ADIn 1 ' Grab a new ADC value
ADC_Total1 = ADC_Total1 + ADC_Result1 ' Sum it to the total register
DelayUS 2 ' Allow internal capacitors to discharge
ADC_Result3 = ADIn 3 ' Grab a new ADC value
ADC_Total3 = ADC_Total3 + ADC_Result3 ' Sum it to the total register
DelayUS 2
Next Temp
' Determin the average of all the equations
ADC_Result1 = ADC_Total1 / ADC_Loops
ADC_Result3 = ADC_Total3 / ADC_Loops
Result1 = ADC_Result1 *( 5.07/ 1023) ' Scale it to volts
Result3 = ADC_Result3 *( 5.07/ 1023)
ax= (Result1 - 0.03)/0.8
az= (Result3 - 0.03)/0.8
inclina= ATan (ax/az)
inclina=inclina * 57.2957795 *4.20023
inclina= 232.666 - inclina
If inclina>-7 And inclina <92 Then
If inclina >= 0 Then
inclina=inclina
inclinamm=inclina*17.7778
Else
inclina=inclina*1.506
inclinamm=inclina*17.7778
End If
If Last_Result1 <> inclina Then ' Check if value has changed
' Print At 1, 1, Dec2 inclina,0, " " ' If it has, display new data
Print At 1, 1, Dec1 inclinamm," mils " ' If it has, display new data
Print At 2, 1, Dec1 inclinammPf," mils "
Last_Result1 = inclina ' and update last value
EndIf
Else
Print At 1, 1, "fuera de rang"
End If
'If Last_Result3 <> az Then ' Check if value has changed
' Print At 2, 1, Dec4 az, " Z" ' If it has, display new data
' Last_Result3 = az ' and update last value
'EndIf
If PORTB=253 Then
mp1=0
DelayMS 50
GoTo MenuP
End If
diferencia= inclinammPf -inclinamm
If diferencia =0 Then
HPWM 2,255,1000
Else
HPWM 2,0,1000
End If
GoTo menuPosActual