Elio Magnus
Newbie level 4
- Joined
- Mar 9, 2011
- Messages
- 7
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Location
- Porto Alegre - RS - Brazil
- Activity points
- 1,347
I had difficulties in operating the AD with the MCP3201 PicBasic and I did not find much information. After much work I get it to work so I am making available to people who need help.
I also accept tips to improve my system.
' Program:
' Versão 1.0
' 04/03/2011 (Printer 9600kBps)
Device = 18F4550
Declare XTAL 20
ALL_DIGITAL = TRUE
Declare HSERIAL_BAUD 9600
Declare HSERIAL_RCSTA %10000000
Declare HSERIAL_TXSTA %00100000
Declare HSERIAL_CLEAR = On
Symbol STROBE = PORTE.0 ' strobe diplay
Symbol SCK = PORTB.1 ' Clock pin
Symbol SI = PORTB.0 ' Data in pin
Symbol SO = PORTB.0 ' Data out pin
Symbol RS = PORTB.4 ' R/S display
Symbol E = PORTB.2 ' Enable display
Symbol DQ = PORTA.0 ' One-wire data pin
Symbol Strobe11 = PORTA.5 ' Strobe 11
Symbol Strobe10D = PORTA.4 ' Strobe 10D
Symbol Strobe10C = PORTA.3 ' Strobe 10B
Symbol Strobe10B = PORTA.2 ' Strobe 10C
Symbol Strobe10A = PORTA.1 ' Strobe 10A
Symbol CS1 = PORTE.1 ' CS_AD
TXSTA=$24 ' Printer
RCSTA= $80 ' Printer
SPBRG= 129 ' Printer
'**********************************
'**********************************
Output STROBE ' Set Strobe to output (LCD display)
Output E ' Set Enable to output (LCD display)
Output RS ' Set RS to output (LCD display)
Output Strobe11
Output Strobe10D
Output Strobe10C
Output Strobe10B
Output Strobe10A
Output Relogio
Output CS1
Read_adc1:
PORTB.7 = ABC.0 'Select input port in CD4051 (ABC)
PORTB.6 = ABC.1
PORTB.5 = ABC.2
DelayMS 10
SHOut SO, SCK, msbfirst, [Placa] ' Send data to enable 4051
PulsOut Strobe11, 100 ' Enable Strobe11 to 4094 to select 4051
DelayMS 250
High CS1
DelayMS 1
Low CS1
DelayUS 10
SHIn SI, SCK, msbpost, [Tensao\13] 'read msb to lsb
SHIn SI, SCK, lsbpost, [Tensao\12] 'read lsb to msb (true value)
High CS1
DelayUS 1
DelayMS 1
SHOut SO, SCK, msbfirst, [255] ' Send data to disable 4051
PulsOut Strobe11, 100 ' Enable Strobe11 to 4094 to disable 4051
Volt[ABC] = Tensao * 7 ' 3000/4096 = 0.73 -> 0.7 * 4096 = 2867 = V reference
Volt[ABC] = Volt[ABC] / 10
TVolt = Volt[ABC]
Return
I also accept tips to improve my system.
' Program:
' Versão 1.0
' 04/03/2011 (Printer 9600kBps)
Device = 18F4550
Declare XTAL 20
ALL_DIGITAL = TRUE
Declare HSERIAL_BAUD 9600
Declare HSERIAL_RCSTA %10000000
Declare HSERIAL_TXSTA %00100000
Declare HSERIAL_CLEAR = On
Symbol STROBE = PORTE.0 ' strobe diplay
Symbol SCK = PORTB.1 ' Clock pin
Symbol SI = PORTB.0 ' Data in pin
Symbol SO = PORTB.0 ' Data out pin
Symbol RS = PORTB.4 ' R/S display
Symbol E = PORTB.2 ' Enable display
Symbol DQ = PORTA.0 ' One-wire data pin
Symbol Strobe11 = PORTA.5 ' Strobe 11
Symbol Strobe10D = PORTA.4 ' Strobe 10D
Symbol Strobe10C = PORTA.3 ' Strobe 10B
Symbol Strobe10B = PORTA.2 ' Strobe 10C
Symbol Strobe10A = PORTA.1 ' Strobe 10A
Symbol CS1 = PORTE.1 ' CS_AD
TXSTA=$24 ' Printer
RCSTA= $80 ' Printer
SPBRG= 129 ' Printer
'**********************************
'**********************************
Output STROBE ' Set Strobe to output (LCD display)
Output E ' Set Enable to output (LCD display)
Output RS ' Set RS to output (LCD display)
Output Strobe11
Output Strobe10D
Output Strobe10C
Output Strobe10B
Output Strobe10A
Output Relogio
Output CS1
Read_adc1:
PORTB.7 = ABC.0 'Select input port in CD4051 (ABC)
PORTB.6 = ABC.1
PORTB.5 = ABC.2
DelayMS 10
SHOut SO, SCK, msbfirst, [Placa] ' Send data to enable 4051
PulsOut Strobe11, 100 ' Enable Strobe11 to 4094 to select 4051
DelayMS 250
High CS1
DelayMS 1
Low CS1
DelayUS 10
SHIn SI, SCK, msbpost, [Tensao\13] 'read msb to lsb
SHIn SI, SCK, lsbpost, [Tensao\12] 'read lsb to msb (true value)
High CS1
DelayUS 1
DelayMS 1
SHOut SO, SCK, msbfirst, [255] ' Send data to disable 4051
PulsOut Strobe11, 100 ' Enable Strobe11 to 4094 to disable 4051
Volt[ABC] = Tensao * 7 ' 3000/4096 = 0.73 -> 0.7 * 4096 = 2867 = V reference
Volt[ABC] = Volt[ABC] / 10
TVolt = Volt[ABC]
Return