jim 1
Define SIMULATION_WAITMS_VALUE = 1 'make this =0 for programming a PIC
Define ADC_CLOCK = 3 'default value is 3
Define ADC_SAMPLEUS = 10 'default value is 20
Define LCD_BITS = 8 'allowed values are 4 and 8 - the number of data interface lines
Define LCD_DREG = PORTD
Define LCD_DBIT = 0 '0 or 4 for 4-bit interface, ignored for 8-bit interface
Define LCD_RSREG = PORTC
Define LCD_RSBIT = 0
Define LCD_EREG = PORTC
Define LCD_EBIT = 1
Define LCD_RWREG = 0 'set to 0 if not used, 0 is default
Define LCD_RWBIT = 0 'set to 0 if not used, 0 is default
Define LCD_COMMANDUS = 100 'delay after LCDCMDOUT, default value is 5000
'Define LCD_DATAUS = 10 'delay after LCDOUT, default value is 50
'Define LCD_INITMS = 1 'delay used by LCDINIT, default value is 100
'the last three Define directives set the values suitable for simulation; they should be omitted for a real device
'------------------------------------
'setup temp variables
Dim abc4 As Byte
Dim abc3 As Byte
Dim abc2 As Byte
Dim abc1 As Byte
Dim abc0 As Byte
Dim temp3 As Word
Dim binval As Word
Dim val0 As Word
Dim vref1 As Word '+++++++++++++++
'ADCON0 = %01000001 'adcchan0
'ADCON1 = %10001011 'Dis clk div,,,an0 > an3 analaog rest dig
TRISA = %00000001
TRISB = %11111111 'all inputs on portb
TRISC = %11111100
'-----------------------------------------------------
Lcdinit
Lcddefchar 0, 31, 31, 31, 31, 31, 31, 31, 31
Lcddefchar 1, %00011111, %00011111, %00011111, %00011111, %00011111, %00011111, %00011111, %00011111
Lcdout 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
Lcdcmdout LcdLine2Home
Lcdout 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
WaitMs 1000
Lcdcmdout LcdHome
Lcdout "**Initialising**"
Lcdcmdout LcdLine2Home
Lcdout "*****FLIPS******"
WaitMs 750
Lcdcmdout LcdHome
Lcdout " PLEASE "
Lcdcmdout LcdLine2Home
Lcdout " WAIT "
WaitMs 750
main0:
Lcdcmdout LcdClear
main:
Gosub readadc
Goto main
End
'scale the 5V input to 4.88v using a resistive divider
readadc:
'read adc word
Adcin 0, val0
vref1 = val0 '+++++++++++++++++ make a copy
binval = val0 'rename for the bin2asc subr
Gosub bin2asc
Lcdcmdout LcdHome
Lcdout "Percent: ", abc3, abc2, abc1, ".", abc0, "%"
If vref1 < 102 Then
vref1 = 0
Goto show2
Endif
If vref1 > 757 Or vref1 = 757 Then
vref1 = 200
Goto show2
Endif
vref1 = vref1 - 102
vref1 = vref1 * 80
vref1 = vref1 / 262
show2:
binval = vref1
Gosub bin2asc
Lcdcmdout LcdLine2Home
Lcdout "Volume:", abc2, abc1, ".", abc0, "L"
Return
bin2asc:
'abc4 = binval / 10000' add these 4 lines for 16 bit conv
'temp3 = binval Mod 10000
'abc3 = temp3 / 1000
'temp3 = binval Mod 1000
abc3 = binval / 1000 'delete for 16 bit conv
temp3 = binval Mod 1000 'delete for 16 bit
abc2 = temp3 / 100
temp3 = temp3 Mod 100
abc1 = temp3 / 10
abc0 = temp3 Mod 10
abc4 = abc4 Or 0x30
abc3 = abc3 Or 0x30
abc2 = abc2 Or 0x30
abc1 = abc1 Or 0x30
abc0 = abc0 Or 0x30
WaitMs 250
Return
jim 2:
'04 Feb 2010 Forum
Define LCD_BITS = 4
Define LCD_DREG = PORTB
Define LCD_DBIT = 4
Define LCD_RSREG = PORTB
Define LCD_RSBIT = 3
Define LCD_EREG = PORTB
Define LCD_EBIT = 2
Define LCD_RSREG = PORTB
Define LCD_RWBIT = 1
Define LCD_LINES = 2
Define LCD_CHARS = 16
Define SIMULATION_WAITMS_VALUE = 1 'make this =0 for programming a PIC
'------------------------------------
'setup temp variables
Dim ascbfr4 As Byte
Dim ascbfr3 As Byte
Dim ascbfr2 As Byte
Dim ascbfr1 As Byte
Dim ascbfr0 As Byte
Dim temp3 As Word
Dim binval As Word
Dim volt0 As Word
Dim vref1 As Word
Dim intrcnt As Byte
Dim adcavg1 As Word
'''''''''''''''''''
T1CON.T1OSCEN = 1
T1CON.TMR1CS = 0
T1CON.TMR1ON = 1
T1CON.T1CKPS0 = 1 'tmr1 prescaler /256 gives a 8 microsec clock period
T1CON.T1CKPS1 = 1
TMR1H = 0xfe ''0xcf 'FFFF - CF2C = 30D3 == 12500 * 8 = ~ 0.1 sec/intr
TMR1L = 0xfe ''0x2c
INTCON.GIE = 1
INTCON.PEIE = 1
PIE1.TMR1IE = 1
PIR1.TMR1IF = 0
'''''''''''''''''''''''''''''''
ADCON0 = %01000001 'fosc/8
ADCON1 = %10000100 'Dis clk div,,,an0 > an3 analaog rest dig
TRISA = %00001111
TRISB = %00000001 'reserve portb.0 for ext interrupts, if required
TRISC = %11111111
'-----------------------------------------------------
Lcdinit
Enable 'enable interrupts
main0:
Lcdcmdout LcdClear
main: 'loop
If intrcnt > 4 Then
intrcnt = 0
binval = adcavg1 / 5 'rename for the bin2asc subr
vref1 = binval 'make a copy
adcavg1 = 0
Gosub bin2asc
'show average of 5 as percentage
Lcdcmdout LcdLine1Home
Lcdout "Percent: ", ascbfr3, ascbfr2, ascbfr1, ".", ascbfr0, "%"
If vref1 < 102 Then
vref1 = 0
Goto show2
Endif
If vref1 > 757 Or vref1 = 757 Then
vref1 = 200
Goto show2
Endif
vref1 = vref1 - 102
vref1 = vref1 * 80
vref1 = vref1 / 262
show2:
binval = vref1
Gosub bin2asc
Lcdcmdout LcdLine2Home
Lcdout "Volume:", ascbfr2, ascbfr1, ".", ascbfr0, "L"
Endif
Goto main
End
On Interrupt
Save System
PIR1.TMR1IF = 0
TMR1H = 0xfd ''0xcf
TMR1L = 0xfd ''0x2c
intrcnt = intrcnt + 1
Gosub readadc
adcavg1 = adcavg1 + volt0
Resume
'scale the 5V input to 4.88v using a resistive divider
readadc:
'read adc word
Adcin 0, volt0
Return
'you can convert any binary value from 0000h to fffFh to 0000 to 65535 decimal
'just name the binary word as binval and call this subr and the ASCII
'result will be in ascbfr4,3,2,1,0, ready for your LCD or UART
'just pop the DP in the output to the LCD [as shown above]
bin2asc:
'ascbfr4 = binval / 10000 'add these 4 lines for 16 bit conv
'temp3 = binval Mod 10000
'ascbfr3 = temp3 / 1000
'temp3 = binval Mod 1000
ascbfr3 = binval / 1000 'delete for 16 bit conv
temp3 = binval Mod 1000 'delete for 16 bit
ascbfr2 = temp3 / 100
temp3 = temp3 Mod 100
ascbfr1 = temp3 / 10
ascbfr0 = temp3 Mod 10
'results are BCD so
'convert to ASCII for LCD or UART
ascbfr4 = ascbfr4 Or 0x30
ascbfr3 = ascbfr3 Or 0x30
ascbfr2 = ascbfr2 Or 0x30
ascbfr1 = ascbfr1 Or 0x30
ascbfr0 = ascbfr0 Or 0x30
Return