Escolar Documentos
Profissional Documentos
Cultura Documentos
Application Notes
All application notes in one file! Thanks to Michael Akers.
Download application_notes.chm (600 KB)
Send mail to mark@mcselec.com with questions or comments about this web site.
Last modified: juli 11, 2000
Our Company
We design professional hardware and software for embedded systems.
Our core business is designing BASIC compilers for the 8051 and AVR.
Our products are low priced and affordable for hobbyists.
Contact Information
Telephone
+31 75 6148799
FAX
+31 75 6144189
Postal address
MCS Electronics
G. Brautigamstraat 11, 1506WL
Zaandam
HOLLAND
Electronic mail
General Information: info@mcselec.com
Sales: info@mcselec.com
Webmaster: mark@mcselec.com
Send mail to mark@mcselec.com with questions or comments about this web site.
Last modified: juni 01, 2000
Home
MCS News Page
MCS Products Page
BASCOM-AVR
BASCOM LT
BASCOM-8051
SimmStick
PLM-24
CANDIP
ATMEGA103
ISP Programmer
F2 KIT and TIO-16
MCS Flashprogrammer
SE512-514
Test PCB
FPQP
Downloads
Pricelist
Resellers
Application Notes
AN 01, Evaluation board for the AT89C2051 and AT89C4051
AN 02, Reading the TLC2543 A/D converter
AN 03, A Garden Watering Program
AN 04, Reading the Dallas temperature sensor DS1821
AN 05, Reading the AT PC Keyboard
AN 06, Reading the DS1820 temperature sensor
AN 07, Big Digit ThermoMeter
AN 08 , Flash Programmer
AN 09, Controlling the X9CMME
AN 10, BASCOM-LT and BASCOM-8051 Hardware simulation
AN 11, Using an I2C clock device with the PCF8583.
AN 12, Using the TIMER interrupt to make a clock with
LED-display.
Send mail to mark@mcselec.com with questions or comments about this web site.
Last modified: mei 17, 1999
You may search our web site for all documents containing matching words or
patterns.
Search for:
Send mail to mark@mcselec.com with questions or comments about this web site.
Last modified: mei 17, 1999
Please tell us what you think about our web site, company,
products, or services. Please provide contact information so
that we will be able to reach you in case we have any
questions.
When you have a question it is better to send an
email because the results of the feedback page are
only read once in a few weeks.
Comments
Category
Web site
Contact Information
Name
Send mail to mark@mcselec.com with questions or comments about this web site.
Last modified: maart 02, 2000
Argentina
Dinastia Soft
Oscar H. Gonzalez
Roca 2239 (1714)
Ituzaingo
Buenos Aires
Argentina
Phone: +54-1-4621-0237
Fax : +54-1-4621-0237
Email : dinastiasof@infovia.com.ar
WWW : http://www.dinastiasoft.com.ar
Austria
RIBU ELEKTRONIK GMBH
Muehlgasse 18
A-8160 Weiz
Phone : 03172-64800
Fax : 03172-64806
Email : office@ribu.at
WWW :http://www.ribu.at
Brazil
WF AUTOMAO INDSTRIA COMRCIO SERVIOS LTDA ME
Canada
Zanthic Technologies Inc
Steve Letkeman
75 Vintage Meadows Place. S.E.
Medicine Hat, Alberta
T1B 4G8 Canada
Phone: 403-526-8318
Fax : 403-528-9708
Email : zanthic@zanthic.com
WWW: http://www.zanthic.com
LAMIA s.r.o.
Antonin Straka
Porici 20a
Blansko
678 01
CZECH REPUBLIC
Phone: +00420-506-418726
Fax : +00420-506-53988
France
SARL OPTIMINFO
M. Belouet
France
Phone: +(33) 03 80 57 14 17
Fax : +(33) 03 80 57 14 17
Email : Optiminfo@libertysurf.fr
WWW: www.optiminfo.com
Germany
Elektronikladen Mikrocomputer GmbH
Martin Danne
Wilhelm.-Mellies-Str. 88
D- 32758 Detmold
GERMANY
Phone: +49 5232-8171
Fax : +49 5232-86197
E-Mail: sales@elektronikladen.de
WWW: www.elektronikladen.de
Vertriebsbros in Hamburg, Berlin, Leipzig, Frankfurt, Mnchen
Hungary
CODIX Ltd, Hungary
Imre Gaspar
Atilla u 1-3,
H-1013 Budapest
HUNGARY
Italy
Grifo(R)
Salvatore Damino
Via dell'Artigiano 8/6
40016 S.Giorgio di Piano BO
ITALY
Phone: +39 (51) 892.052
Fax : +39 (51) 893.661
Email : tech@grifo.it
WWW: http://www.grifo.com (Englisch)
WWW: http://www.grifo.it (Italian)
Korea
SAMPLE Electronics Co.
Junghoon Kim
306 Jeshin 43-22 Shinkey Youngsan Seoul Korea
Postal code 140-090
Phone: 82-2-707-3882
Fax : 82-2-707-3884
Email : info@sample.co.kr
WWW: http://www.sample.co.kr
Pakistan
ORRIS MICRO SYSTEM
Malik Muhammad Nawaz Awan
15/Y, TARIQ BIN ZIAD COLONY, SAHIWAL.
PAKISTAN
Phone: 0441-66982
Email : oms@brain.net.pk
Poland
RK-SYSTEM
Robert Kacprzycki
CHELMONSKIEGO 30
05-825 GRODZISK MAZ.
POLAND.
Sweden
LAWICEL
Lars Wictorsson
Klubbgatan 3
SE-282 32 TYRINGE
SWEDEN
Phone: +46 (0)451 59877
Fax : +46 (0)451 59878
WWW: http://www.lawicel.com
Email : info@lawicel.com
Spain
Ibercomp
Miquel Zuniga
C/. del Parc, numero 8 (bajos)
E-07014
Palma de Mallorca
Spain
Phone: +34 (9) 71 45 66 42
Fax : +34 (9) 71 45 67 58
Email: ibercomp@atlas-iap.es
WWW: http://www.ibercomp.es
Turkey
IBD Limited Sirketi
379/1 Sokak A-Blok No: 2/101
AFA Sanayi Carsisi - II.Sanayi
35100 Bornova - Izmir
Phone: 0090-232-4627477 - 78
Fax: 0090-232-4627545
E-mail : ibdltd@superonline.com
E-mail : ibd@ibd-ltd.com
E-mail : sales@ibd-ltd.com
E-mail : faruk@ibd-ltd.com
WWW : www.ibd.com.tr
UK
TECHMAIL SOLUTIONS LTD
Dogan Ibrahim
14 Dunvegan Road
Eltham
London SE9 1SA
Phone: 0171 343 5242 or 0181 488 9689
Fax : 0171 821 6744
Email: dogan@dircon.co.uk
WWW www.users.dircon.co.uk/~dogan/dogan/
UK
QUASAR ELECTRONICS
Simon Neil
Unit 14 Sunningdale
BISHOP'S STORTFORD
Herts
CM23 2PA
UNITED KINGDOM
TEL: +44 (0)1279 306504
FAX: +44 (0)870 7064222
Email: simon@quasarelectronics.com
WWW http://www.quasarelectronics.com/home.htm
USA
DonTronics
Don McKenzie P.O.
Box 595
Tullamarine 3043
Australia
Email don@dontronics.com
WWW http://www.dontronics.com
USA
Techniks, Inc.
Frank Capelle
PO Box 463
Ringoes, NJ 08551
USA
Phone: 908-788-8249
Fax: 908-788-8837
Email: Techniks@techniks.com
WWW: http://www.techniks.com
USA
M. Akers Enterprises
Michael W. Akers, US BASCOM support
3800 Vineyard Avenue #E
Pleasanton, CA 94566-6734
USA
Phone: +1-925-640-3600
Fax: +1-925-640-3600
Email: info@mwakers.com
WWW: http://www.mwakers.com
USA
Rhombus
David H. Lawrence
1909 Old Mountain Creek Road
Greenville, SC 29609
USA
Phone: +1-864-233-8330
Fax: +1-864-233-8331
Email: webmaster@rhombusinc.com
WWW: http://www.rhombusinc.com
Send mail to mark@mcselec.com with questions or comments about this web site.
Last modified: juni 27, 2000
Links
Here is a list of links that can be of great interest
Gianni made a complete project based on the 89C2051 and bascom-8051 , in fact
few basic statement and lot of asm. But squeeze everything in 2Kb is quite hard .
http://www.geocities.com/SiliconValley/Lab/9128/gianni/analizzatore_nicd/nicd.html
Sorry but whole documentation is in italian.
Nico H.C.J. Veth, pa0nhc has a site for Radio Amateurs : www.pa0nhc.net
A new AVR Mega board is developed by Robert Bastubbe.
http://www.opticompo.com/letatwork_en.html
Roland Walter's AVR Programmer. Works with BASCOM-AVR too.
http://www.rowalt.de/mc/
Roland also has other information about AVR on his site available.
Grifo FMO52 special board with monitor. Supported by BASCOM.
NCS Neff Computer Systeme . 80535 uP board. Work very well.
Holger Fritz At89S8252 board. Very nice design with I2C controller chip.
Custom Soft and hardware design
Links to capable people that can help you on commercial base with your project.
Ben's HobbyCorner , 80535 projects with ASM code, a free monitor program and
PCB's. A must for the 80535 user!
Michael Wste is a journalist and Radio Amateur. so he has Radio Amateur related
info. He also founded the :
BASCOM software pool. Take a look at the code and code snippets or send some
code yourself! Contains many nice examples.
Michael Akers has an ftp site with BASCOM related samples and data sheets. The ftp
site also contains the BASCOM update files. Look at
ftp://ftp.mwakers.com/pub/clients/Bascom
Michael is also one of our US resellers.
Zlatko & Zeljko did a great job on interfacing IDE disks. Check it out on
http://www.angelfire.com/de2/zel/
Iguana Labs runned by James Atwell. He sells the PG302 programmer supported by
BASCOM.
Erich Linsmeier have done a lot of projects with BASCOM. Most of them are Radio
Amateur related.
-(HAM) T7F-Control (a controlunit for a 70cm-Amateur-transceiver
with AD-Converter and EEPROM, and LCD)
-Video-RGB-Converter for TFT-LCD (I2C controlled Videoprocessor)
-Controlunit for Philips-TV-Tuner via I2C
-VPS-Controller, displays information from line 16 (videosignal) in
a 2line LCD Cardreader (see application notes)
-I2C-Stamp (an controller and EEPROM, which sends cont. or one
time I2C-Command)
Contrive has also a complementary branch, raised through the years for internal
purposes and next proposed to the market.
The Manufacturing service Team, the service bureau for PCB manufacturers
(photoplotting, CNC, CAM...), with 10 years
experience have now the capability to produce 3 days guaranteed PCB prototypes, 1
o 2 sides thru-plated,
tin-plated, needs one day more if you want solder-mask and/or component
silkscreen.
http://space.tin.it/economia/gpedruz/
Eddie Mcmullen has a site with exiting products.
ATISP provides an in circuit programming system for Atmel AT89S and AT90S
microcontroller based systems.
The ATISP PCB provides the required circuitry for downloading code via the PC
parallel port to the user's target
system.
ATISP PCB is connected to the parallel port via a user supplied 25 pin point to point
modem cable. A user cable is
also required to connect the 4 programming signals that are brought out on a 10 pin
header on ATISP into the user
target system. Software comes with full source code.
No dongle is required.
Telesystems company
A collection of links devoted to microcontrollers.
Support and detail information about PICPROG Microcontrollers and
E(E)PROM production quality programmer.
Telesystems company:
Telesystems products:
Send mail to mark@mcselec.com with questions or comments about this web site.
Last modified: juli 11, 2000
Send mail to mark@mcselec.com with questions or comments about this web site.
Last modified: januari 21, 2000
'
' EXAMPLE PROGRAM FOR DOMO - I
'
' READ ANALOG INPUTS
'
' (C) 1999 IBERCOMP SA - Writen by MIGUEL ZUIGA
'
$large
Config Lcdpin , Db4 = P2.4 , Db5 = P2.5 , Db6 = P2.6 , Db7 = P2.7 , E = P2.1 , Rs
= P2.2
Config Sda = P1.0
Config Scl = P1.1
Cls
Lcd " "
Locate 1 , 1
Lcd " ANALOG INPUTS "
Do
State = 9
Read_analog 0
Locate 2 , 3
Lcd State ; " "
Locate 2 , 1
Lcd " "
Waitms 500
Locate 2 , 1
Send mail to mark@mcselec.com with questions or comments about this web site.
Last modified: juli 20, 1999
Download avanzado.zip
Send mail to mark@mcselec.com with questions or comments about this web site.
Last modified: juli 20, 1999
Send mail to mark@mcselec.com with questions or comments about this web site.
Last modified: juli 20, 1999
' =========================================================================================
'
' Program: ATPCKBD.BAS
' Author: Dusko Djuricin
' E-mail: djdusko@eunet.yu
'
' This program demonstrates how to interface AT PC keyboard to 8031 microcontrollers family.
' Program was originally written and tested for ATMEL 89C2051. For keyboard interfacing it
' uses only two I/O lines with two pull up resistors. At the output, through serial channel,
' it sends ASCII code of pressed key. Other used I/O lines are not necessary.
' Complete description of PC keyboard operation, could be found at the following address:
'
' http://www.geocities.com/SiliconValley/Bay/8302/keybrd.htm#1
'
' =========================================================================================
Dim B1 As Byte , B2 As Byte , N As Byte , Status As Byte , Key As Byte
Dim Caps As Bit , Caps_lock As Bit , Ctrl As Bit , Alt As Bit , Tmp As Bit
Dim Temp As String * 1
Declare Sub Tx_byte(b1 As Byte)
Declare Sub Rx_byte
Declare Sub Kbd_status
Kbd_data Alias P1.1 ' connected with 4K7 pull up resistor
Kbd_clk Alias P1.0 ' connected with 4K7 pull up resistor
Buzzer Alias P3.7 ' for details see BASCOM51 documentation
Res_sw Alias P3.5 ' optional output (see code)
Dim Frequency As Const 2048
Dim Duration As Const 50
Dim L_shift As Const &H12
file:///F|/FrontPage Webs/Content/server/an05.htm (1 of 8) [15-7-2000 23:50:23]
AN 05, Reading the AT PC Keyboard
Dim R_shift As Const &H59
Dim Ctrl_key As Const &H14
Dim Alt_key As Const &H11
Dim Caps_key As Const &H58
Dim Scroll_key As Const &H7E
'---------------------------[Beginning of Program]-------------------------------
Reset Kbd_clk ' disable the Keyboard
Waitms 100
Do
Tx_byte &HFF ' reset the Keyboard and wait
Rx_byte ' till the Keyboard answer with
If B1 = &HFA Then Exit Do ' acknowledge byte
If B1 = &HAA Then Exit Do ' or Power On Self Test OK code
Loop
Print
Print "*** AT PC Keyboard Interface ***"
Print " author: Dusko Djuricin"
Print " E-mail: djdusko@eunet.yu"
Print "********************************"
Print
' KBD LED: Status.0 - Scroll Lock, Status.1 - Num Lock, Status.2 - Caps Lock
Status = 2 ' initially Num Lock On
Kbd_status
Do
' KEY variable will hold ASCII value of pressed key
Key = 0 ' initial value
Rx_byte ' read scan code from PC keyboard into B1
Select Case B1
Case Ctrl_key:
Set Ctrl ' mark that CTRL key is pressed
B1 = 0
Case Alt_key:
Set Alt ' same as above but for ALT key ...
B1 = 0
Case L_shift:
Set Caps ' same as above but for Left SHIFT key ...
B1 = 0
Case R_shift:
Set Caps ' same as above but for Right SHIFT key ...
B1 = 0
Case &HE0: ' extended codes are not handled!
file:///F|/FrontPage Webs/Content/server/an05.htm (2 of 8) [15-7-2000 23:50:23]
AN 05, Reading the AT PC Keyboard
B1 = 0
Case &HF0: ' BREAK CODE is detected (key depressed)
Rx_byte ' read again to see which key is depressed
If B1 = L_shift Then ' and keep tracking flags...
Reset Caps
Elseif B1 = R_shift Then
Reset Caps
End If
If B1 = Ctrl_key Then
Reset Ctrl
Elseif B1 = Alt_key Then
Reset Alt
End If
B1 = 0
Case Caps_key: ' in a case of CAPS LOCK key
Caps_lock = Caps_lock Xor 1 ' there must be calculated status of it's LED
Status = Status Xor 4
Kbd_status ' and then we must send it to keyboard
B1 = 0
Case Scroll_key: ' I use combination of Ctrl + Alt + ScrLock
Tmp = Ctrl And Alt ' keys to generate reset signal...
If Tmp = 1 Then
Reset Res_sw ' ...for my host controller
Status = Status Xor 1 ' (Scroll Lock LED will lit and buzzer beeps)
Kbd_status
Sound Buzzer , Duration , 512
Waitms 100
Sound Buzzer , Duration , 512
Waitms 100
Sound Buzzer , Duration , 512
Waitms 100
Set Res_sw
Status = Status Xor 1 ' turn off Scroll Lock LED
Kbd_status
End If
B1 = 0
Case Else: ' handling of "printable" characters
If B1 > 131 Then ' this program handles only lower part
B1 = 0 ' of ASCII code table (with exception of
End If ' function keys - see lookup table)
If Caps = 1 Then
Key = Lookup(b1 , With_shift) ' one table is for SHIFT + key combination
Else
file:///F|/FrontPage Webs/Content/server/an05.htm (3 of 8) [15-7-2000 23:50:23]
AN 05, Reading the AT PC Keyboard
Key = Lookup(b1 , No_shift) ' and another is for key WITHOUT SHIFT
End If
If Caps_lock = 1 Then
If Caps = 0 Then ' if CAPS LOCK was ON and there is no
Temp = Chr(key) ' SHIFT + key combination, then convert
Temp = Ucase(temp) ' pressed key into upper case letter
Key = Asc(temp)
End If
End If
End Select
If B1 <> 0 Then
Sound Buzzer , Duration , Frequency ' beep
Print Chr(key); ' and print pressed key
End If
Waitms 10 ' dummy delay ...
Loop
'-----------------------[Receive Byte from Keyboard]----------------------------
Sub Rx_byte
Set Kbd_clk ' enable Keyboard
mov {n},#0
jb Kbd_clk,*+0 ' wait CLK to become zero
' START bit
jnb Kbd_data,L_cont ' if START bit is zero continue
sjmp .L_error ' if not exit via error label
' DATA bits
!L_cont:
Incr N ' this is counter of received bits
acall zero2one
mov a,{b1} ' prepare buffer
mov c,Kbd_data ' read DATA bit
rrc a ' read it into the buffer
mov {b1},a ' store buffer value
mov a,{n}
cjne a,#8,L_cont ' if it isn't last bit repeat
' PARITY bit
acall zero2one
mov a,{b2} ' prepare buffer
mov c,Kbd_data ' read ODD PARITY bit
rrc a ' store it into the buffer
' STOP bit
acall zero2one
mov c,Kbd_data ' read STOP bit
file:///F|/FrontPage Webs/Content/server/an05.htm (4 of 8) [15-7-2000 23:50:23]
AN 05, Reading the AT PC Keyboard
rrc a ' store it into the buffer
rrc a ' now align bits to right...
rrc a
rrc a
rrc a
rrc a
rrc a
anl a,#3 ' extract valuable bits and
mov {b2},a ' store to buffer
' check STOP bit
anl a,#2
jz .L_error
' PARITY checking
mov a,{b1}
mov c,psw.0 ' this is EVEN PARITY
rlc a
anl a,#1
xch a,{b2}
anl a,#1
xrl a,{b2}
jz .L_error
sjmp .L_end
L_error: ' exit in case of an error
Waitms 1 ' wait end of transmission
mov a,#0
mov {b1},a
L_end:
Reset Kbd_clk ' stop the Keyboard
End Sub
'------------------------[Transmit Byte to Keyboard]----------------------------
Sub Tx_byte(b1 As Byte)
B2 = 8
Reset Kbd_clk ' break the Keyboard
Delay ' (safety reasons)
Delay
Delay
Delay
Delay
Delay
Reset Kbd_data ' request to send
Set Kbd_clk ' enable the Keyboard
' START BIT
file:///F|/FrontPage Webs/Content/server/an05.htm (5 of 8) [15-7-2000 23:50:23]
AN 05, Reading the AT PC Keyboard
acall zero2one
' DATA BITS
mov a,{b1}
!L_tx_data:
rrc a
mov Kbd_data,c
acall zero2one
djnz {b2},L_tx_data
' PARITY BIT
mov a,{b1} ' calculate parity bit
mov c,psw.0 ' this is Even parity
cpl c ' and Keyboard needs Odd parity
mov Kbd_data,c ' send parity bit
acall zero2one
' STOP BIT
Set Kbd_data ' send stop bit
acall zero2one
' KBD ACK BIT
acall zero2one
Reset Kbd_clk ' stop the keyboard
End Sub
'------------------[ASM Subroutine wait for falling edge]-----------------------
!zero2one:
jnb Kbd_clk,*+0 ' wait till CLK rise
jb Kbd_clk,*+0 ' now wait till CLK fall down
ret
'------------[Subroutine for sending LED status to the Keyboard]----------------
Sub Kbd_status:
Tx_byte &HED
Rx_byte
Tx_byte Status
Rx_byte
End Sub
'---------------------[Scan Code to ASCII conversion table]---------------------
' Here are two lookup tables, one for handling ordinary keys and other for
' handling combination of SHIFT + key. I gave (under block remark) same tables
' written as strings, so matching with ASCII codes could be easier.
'
' Note that function keys (F1 to F12) are coded as one byte. F1 is coded as 129,
' F2 gives 130, F3 gives 131 and so on. This was OK for my application and it is
' because of simplicity.
Send mail to mark@mcselec.com with questions or comments about this web site.
Last modified: juli 20, 1999
' ----------------------------------------------------------------
'Author : Bojan Ivancic
' Email : nicrodesign@siol.net
' Example measuring temperature using Dallas DS1820,
' with calculated for 0.1 C resolution and with 8-bit CRC!
' ----------------------------------------------------------------
$CRYSTAL = 12000000 ' we are using this frequency
declare sub Read1820
declare sub CRCit
declare sub Temperature
dim bd(9) as byte
dim i as byte , tmp as byte
dim CRC as byte
dim T as integer , T1 as integer
dim v as byte
Config 1wire = P1.0 ' DS1820 on pin 12 (pull up)
cursor off noblink
cls
locate 1 , 1 : lcd "DEMO for DS1820"
locate 2 , 1 : lcd "T="
do
Temperature
waitms 250
loop
End
'//////////////////////////////////////////////////////////////////////////////
sub Temperature ' actual measuring
1wwrite &hcc : 1wwrite &h44 ' start measure
waitms 300 ' wait for end of conversion
Read1820 ' read 9 bytes
if Err = 1 then ' if there is no sensor
locate 2 , 4 : lcd "-- " ' we put "-- " on LCD
else
if CRC = 0 then ' sensor present, check CRC
locate 2 , 4 : lcd T ' CRC OK, print T*10 on LCD
else
locate 2 , 4 : lcd "** " ' CRC NOT OK, "** " on LCD
end if
end if
end sub
'//////////////////////////////////////////////////////////////////////////////
sub Read1820 ' reads sensor ans calculate
' T for 0.1 C
Send mail to mark@mcselec.com with questions or comments about this web site.
Last modified: augustus 20, 1999
Send mail to mark@mcselec.com with questions or comments about this web site.
Last modified: augustus 20, 1999
Flash Programmer
This application note is for a parallel printer port based Flash programmer for the At89Cx051.
Programmer PCB:
Send mail to mark@mcselec.com with questions or comments about this web site.
Last modified: augustus 29, 1999
Download an09.bas
Send mail to mark@mcselec.com with questions or comments about this web site.
Last modified: augustus 29, 1999
Users of the MCS Flashprogrammer can use the programmer to simulate PORT 1 (port 3 not supported).
You can make a cable which connects P1 of the flashprogrammer (with a piggy-back) to the
target system(the AT89C2051 socket).
Take care not to insert a chip into the programmer when used as a simulator!
I used a dontronics DT003 power-board as a basis, a DT203 led/button board for the leds and a
dontronics prototypeboard to connect P1 of the flashprogrammer to the datalines of the SIMM-bus.
For info on these boards http://www.dontronics.com
Dim a As Byte
P1 = 1
For a = 1 to 8
Rotate P1 , Left
Next
For a = 1 to 8
Rotate P1 , Right
Next
End
Schematic:
Send mail to mark@mcselec.com with questions or comments about this web site.
Last modified: augustus 29, 1999
Schematic:
Send mail to mark@mcselec.com with questions or comments about this web site.
Last modified: augustus 29, 1999
Send mail to mark@mcselec.com with questions or comments about this web site.
Last modified: december 12, 1999
Keyboard decoder
The following example was donated by Mike Gill.
' Reads a standard 3X4 keypad and assembles No in Digits
variable, then displays
' Led used is just to show program running 2K2 from port pin via
Led to +
' Setup for 89S8252. Use appropriate port assignments for other
processors
' Uses Active Low, so no extra components needed. Chip internal
tieups are utilised
' Mike Gill . B M Electronics UK 01422 839321
Startup:
Config DeBounce = 40 ' Set Key Pad Debounce to 40 Ms
Config Lcd = 16 * 1
Cursor OFF
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'OUTPUTS All Active High
StatusLed Alias P2.7 ' Operational Status
Col1 Alias P2.6 ' Keyboard Column Drivers
Col2 Alias P2.5
Col3 Alias P2.4 ' Add Another Col for 4X4 Kpad
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'INITIAL OUTPUT STATES
StatusLed = 1
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'INPUTS All Active Low
Row1 Alias P2.0 ' Keyboard Rows (Read)
Row2 Alias P2.1
Row3 Alias P2.2
Row4 Alias P2.3
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'VARIABLES
Dim Check As Byte ' Detects Key Released
Dim KeyRead As Byte ' Actual Button Value
Dim Digits As Word ' Actual Dialled Number
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'INITIALISE VARIABLES
Digits = 0
KeyRead = 0
Check = 0
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
KeyScan: ' Keypad Read Routine (Assignments can be changed
for different connections)
StatusLed = 0 ' Status Led OFF
Col1 = 0 : Col2 = 1 : Col3 = 1 ' Turns Columns On (Low) 1 by 1
KeyRead = 1 : DeBounce Row1,0,GotKey,Sub ' Gosub if Key
Pressed
(Reads LOW Input)
KeyRead = 4 : DeBounce Row2,0,GotKey,Sub
KeyRead = 7 : DeBounce Row3,0,GotKey,Sub
KeyRead = 11 : DeBounce Row4,0,GotKey,Sub ' CALL Button *
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Col1 = 1 : Col2 = 0 : Col3 = 1 ' Assign KeyRead to Any No You
Like <256
KeyRead = 2 : DeBounce Row1,0,GotKey,Sub
KeyRead = 5 : DeBounce Row2,0,GotKey,Sub
KeyRead = 8 : DeBounce Row3,0,GotKey,Sub
KeyRead = 0 : DeBounce Row4,0,GotKey,Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Col1 = 1 : Col2 = 1 : Col3 = 0 ' Add Another Section After this
for 4X4 Kpad
KeyRead = 3 : DeBounce Row1,0,GotKey,Sub
KeyRead = 6 : DeBounce Row2,0,GotKey,Sub
KeyRead = 9 : DeBounce Row3,0,GotKey,Sub
KeyRead = 12 : DeBounce Row4,0,GotKey,Sub ' CANCEL Button
#
WaitMs 50 ' Status Led OFF Time
StatusLed = 1 ' Turns Status Led ON
WaitMs 15 ' Status Led ON Time (Only to show Program Running)
Goto KeyScan
End
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
GotKey:
CheckAgain: ' Reads P2 Directly and Waits for all Keys Released
Check = P2 And 15 ' Appropriate Port
If Check <> 15 Then : Goto CheckAgain : End If ' Check for Key
Release
If Keyread = 12 Then : Goto StartUp : End If ' CANCEL Button
If Keyread = 11 Then : Goto CallNumber : End If ' CALL Button
Digits = Digits * 10 ' Assemble Dialling No in Digits Variable
Digits = Digits + Keyread
Cls : Lcd Digits ' Display Accumulating Number
KeyRead = 0 ' Clear Keypad Read Variables
Check = 0
Return
End
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
CallNumber: ' What You Like Here. When call key pressed
Goto StartUp
Download keypad.bas
Send mail to mark@mcselec.com with questions or comments about this web site.
Last modified: augustus 29, 1999
Download flm.bas
Send mail to mark@mcselec.com with questions or comments about this web site.
Last modified: september 02, 1999
LCD VU Meter
The following example was donated by Ger Langezaal.
Note that BASCOM-8051 was used and that it doesn't work
with
BASCOM LT without modifications , since arrays are used.
Note: Use a 47K resistor from the input to P6.0 for input
protection.
Send mail to mark@mcselec.com with questions or comments about this web site.
Last modified: september 02, 1999
'--------------------------------------------------------------
' (c) 1999 Lex Bolkesteijn
'--------------------------------------------------------------
' file: GP2D02.BAS
'
' Sample program to use a Sharp GP2D02 distance sensor
' with a 89C2051. The GP2D02 is non lineair. The shorter
' the distance between object and sensor the higher the
output.
'
Cls
Do
Reset P1.0 'Start cycle
Waitms 70 'time to calculate
Set P1.0
Shiftin P1.1 , P1.0 , D , 0 'clock bits in MSB first on falling
edge
Set P1.0 'set powerdown for sensor
Waitms 2 'minimum delay for sensor
Cls
T = D / 16 'we use a 16 char. display (255/x)= 16 max
Lcd "Distance : " ; D 'display the distance
Lowerline
If T > 0 Then 'else string will fail
S = String(t , 61)
Lcd S
End If
Loop
End
Send mail to mark@mcselec.com with questions or comments about this web site.
Last modified: september 02, 1999
New : 19-9-1998
Erich has enhanced the program and even provided a PCB
in laserjet image format.
Here is the content of the readme file which is in the !ic.exe
file.
Hello BASCOM-USER!
Send mail to mark@mcselec.com with questions or comments about this web site.
Last modified: oktober 11, 1999
StartUp:
Config WatchDog = 2048
Start WatchDog
Config SCL = P2.0 ' I2C Bus Pin Assignments
Config SDA = P2.1
Config Lcd = 16 * 2
WaitMs 50 ' Allow Display time to Initialise
$Baud = 300 ' Output to paging TX
$Crystal = 12000000
Cursor OFF
Cls
Lcd "B M Electronics" ' Start Up Message, What you like
Dim Check As Bit ' Check for Call
Dim DelayTime As Byte ' Used in Loop for Time Between
Calls
Dim AlarmBlips As Byte ' Counter for No of Bleeps on
Sounders
Dim I2cAddress As Byte ' Address to Read
Dim I2cRead As Byte ' Data from PCF8574
Dim I2cStage As Byte ' Individual PCF8574 Location
Dim RealNo As Byte ' Actual Position Number
Dim Result As Byte ' Result of Input Present Test
Dim Message As String * 16 ' Displayed Pager/Lcd Message
StatusLed Alias P0.0 ' Inverted 1=OFF 0=ON
Sounder Alias P2.2 ' True 1=ON 0=OFF
BackLight Alias P2.3 ' True
Relay Alias P2.4 ' True
Sounder = 0 ' Sounder OFF
Relay = 0 ' Relay OFF (If Used)
BackLight = 0 ' Back Light OFF
Check = 0 ' Reset Call Check Bit for Immediate Page
StatusLed = 1 ' OFF
MainLoop:
Gosub ScanInputs
Reset WatchDog
StatusLed = 0 ' Status Led ON
WaitMs 10 ' Led Flash Time (Once Per Scan of 32 Points)
StatusLed = 1 ' Status Led OFF - Disable if faster scan
needed
WaitMs 150
If Check = 1 Then : Gosub DelayIt : Goto StartUp : End
If
Goto MainLoop
End
Display:
Reset WatchDog
BackLight = 1 ' Display Back Light ON
Check = 1 ' Enables Loop Delay Counter
StatusLed = 0 ' Flashes Led
Message = LookupStr(RealNo , List)
Cls
Lcd "Number Calling.." ' Print to Lcd
Lowerline
Lcd Message ' Alarm message to Lcd
Reset WatchDog
Print "L0012000A" ; Message ' TX Message to Pagers Or
other serial 'device
Gosub Alarm
Return
End
Alarm:
For AlarmBlips = 0 to 5 ' Modify for faster scan
Reset WatchDog
Sounder = 1 ' Alarm ON
Wait 1
Reset WatchDog
Sounder = 0 ' Alarm OFF
Wait 1
Next AlarmBlips
Return
End
List:
Data "One " , "Two " , "Three " , "Four " ' Change these for
whatever
Data "Five " , "Six " , "Seven " , "Eight "
Data "Nine " , "Ten " , "Eleven " , "Twelve "
Data "Thirteen " , "Fourteen " , "Fifteen " , "Sixteen "
Data "Seventeen " , "Eighteen " , "Nineteen " , "Twenty "
Data "Twenty One " , "Twenty Two " , "Twenty Three " ,
"Twenty Four "
Data "Twenty Five " , "Twenty Six " , "Twenty Seven " ,
"Twenty Eight "
Data "Twenty Nine " , "Thirty " , "Thirty One " , "Thirty Two
"
End
Send mail to mark@mcselec.com with questions or comments about this web site.
Last modified: oktober 10, 1999
Send mail to mark@mcselec.com with questions or comments about this web site.
Last modified: oktober 23, 1999
Send mail to mark@mcselec.com with questions or comments about this web site.
Last modified: december 11, 1999
' *************************************************
' * MATRIXINPUT V1.0 07-12-99
' *
' * Author: John van der Putte
' * E-mail: joconiki@hotmail.com
' * icq: 47666030
' *
' * Program:
' * This program uses a 3x4 keyboard matrix to input
' * ASCII. The output is placed on the LCD-display and
' * will be saved in the variable TEXT.
' * If one key is pushed several times the characters
' * of that key rotates at the cursor. When no key is
' * pressed the cursor shifts to the next display-
' * position after a while. This time can easely be changed.
' *
' * The keys have the following layout:
' *
' * 1 2 3
' * .,?!-& ABC DEF
' *
' * 4 5 6
' * GHI JKL MNO
' *
' * 7 8 9
' * PQRS TUV WXYZ
' *
' * * 0 #
' * <BACKSPACE> <SPACE> <ENTER>
' *
' *
' *
' * The procedure which looks to the keyboard matrix returns the
' * following values:
' * no key = 17
Matrix_input:
Index = 0 ' reset index
Keyold = 16 ' reset keyold (16 is used to prevent automatic cursormovement)
Count = 0 ' reset count
Send mail to mark@mcselec.com with questions or comments about this web site.
Last modified: december 11, 1999
'-------------------------------------------------------------------------------
' (c) Copyright 2000 Rhombus Greenville SC
'Code can be used by others providing this header is included in the source.
'===============================================================================
'Getting started in Real-Time Control using BASCOM Part 1
'===============================================================================
'INTRO
'For those who have not yet applied themselves to real-time control using 8-bit
' MCU/MPUs, then these notes and segments of code may help you get started.
'Or if you normally avoid Interrupts, then this first part may change that by
' showing how Timer0 can be expanded into as many timers as you wish, and with
' very little code.
'I am experienced with the 51 at assembler level but new to the Bascom compiler.
' What surprises me the most is that despite the convenience of using a
' compiler, it does not appear to be restricting in any way - hence I hope to
' avoid the use of any assembler code.
'
'Whilst most of what will be described was written a good 15-25 years ago, I
' have checked and it does appear to fit in neatly with present day theory for
' simple real-time control and is classified as 'Co-operative Multi-tasking'.
' There is no forced switching of tasks as with a RTOS, and hence all CPU time
' is devoted to 'getting the job done' and no storage is needed to hold all the
' intermediate states of pre-empted tasks.
'OVERVIEW OF MACHINE CONTROL
'All but the most simple machines require multiple tasks operating in parallel.
' Each task consist of a series of states(steps) with clearly defined logic that
' determines the transition from one state to the next.For example a tank drain
' valve is open, the low level float switch indicates empty but it is known that
' an additional 40 secs needs to be timed for a complete drain and to finally
' close the valve. In parallel a safety locking sequence must be performed on
' another part of the machine before a steam valve is opened.
'
'It is that sort of parallel control that is easily handled by PLCs where there
' is a continuous loop scanning all Inputs, then based on pre-defined logic so
' each task progresses from one stage(step) to the next and resulting in a new
' set of Outputs. With fast loops of Inputing, Processing and Outputing the
' tasks are effectively being controlled in parallel.
'
'When I/O is added that cannot be included in that main I/O loop, such as
' operator interfaces and host serial communication, then the software must use
' interrupts to take whatever time is needed from the main I/O loop to service
' the needs of those asynchronous events. It was at this stage of the PLC's
' history that the limitations of relay ladder logic became more than apparent.
' The PLC solutions to handle these new demands were far from elegant,whereas
' for MPU/MPCs the solutions are a natural and can be achieved very simply
' using the Bascom compiler.
'The methods to be described here have handled slow process control as mentioned
' above, and together with both serial & operator interfaces, They have also
' allowed an 11Mhz 8052 to precisely control a knitting machine's 100+ pneumatic
' outputs in perfect sync with needles flying by at 800 per second, and as the
' operator keyed in a new batch and the host collected production data.
'
'The sharing of CPU time is based on a priority ordered list of tasks and with
' all asyncronous or time critical events being interrupt driven. The main I/O
' tasks are prompted by timer set flags (only one in example) which can also
' serve to distribute CPU demands. Analog inputs can also benefit from using a
' timer rate related to the supply frequency. When each task is completed it
' returns execution to the top of the list.
'Interrupt routines are kept at an absolute minimum and any excessive processing
' needs are off-loaded to the main loop. This holds interrupt service latencies
' to a minimum and avoids the need for either hardware or software priority
' handling to satisfy critical timing issues.
'An example of off-loading interrupt servicing time would be to merely flag the
' timer event shown below, avoid all needs for stacking (it would not change the
' value of any registers, nor even the status flags), and carry out the updates
' in the main loop. For simplicity, the timer ISR below does its processing
' during the interrupt and that will normally be OK but for the most demanding
' applications.
'
'Timer0 ISR code is very small and should be self explicit. In order to show it
' working there is a main loop using those new timers as inputs, a little logic
' for reloading them, and their run status is output to a set of pins to allow
' scoping. For a steady trace the timers are running very fast at a resolution
' of 2mS. If using the Timer0 expansion code in your own applications then it is
' only a matter of defining a new constant for loading the timer.
'Using P3.5 as a scope trigger will show P3.2.3.4 effectively operating in
' parallel and from the same Timer0.
'Part 2 will add transparent keypad input to change the timer values whilst they
' are running & without affecting their operation until the Enter key finalises
' their new value and they snap to the new timing.
'----- INITIALISATION
Dim Timers(4) As Byte , Tic_cnt0 As Byte , Isr_temp As Byte , Io_flag As Bit
Const 2ms = -1793 ' (2/1000)*(11059200/12)-50reload
Config Timer0 = Timer , Gate = Internal , Mode = 1 '16 bit,own code reloads
On Timer0 Timer_0_int
Enable Interrupts 'enable the use of interrupts
Enable Timer0
Priority Set Timer0 'highest priority
Counter0 = 2ms
Start Timer0
'----- MAIN LOOP
'Dumb code to show a set of timers (Timer0 expanded) each running with their own
' individual values, and controlling their own output(a port pin) and effectively
' in parallel.
Main_loop:
Do
'--Test for I/O prompt
If Io_flag = 1 Then Goto Io_control 'Dummy machine control
'--Test for waiting Host messages 'Nothing currently implemented
'If Msg_flag Then Goto Msg_rtn
'--Test for keypad activity
'If Key_flag Then GoTo Key_rtn
Loop
'----- MACHINE CONTROL
Io_control:
'The 3 stages of Read_Inputs/Process/Write_Outputs
'All inputs here are Timer values and hence readily available internally.
Send mail to mark@mcselec.com with questions or comments about this web site.
Last modified: februari 29, 2000
Fill_write_array:
For I = 1 To 16
Temp2 = Mid(text , I , 1)
Ar(i) = Asc(temp2)
Next I
Return
Send mail to mark@mcselec.com with questions or comments about this web site.
Last modified: maart 01, 2000
Send mail to mark@mcselec.com with questions or comments about this web site.
Last modified: maart 01, 2000
The following document describes how you can create your own bitmaps: convert bitmap.pdf
The document writing software to control t6963c based displays.pdf , you can download for
reference.
'******************************************************************
'INIT
'******************************************************************
$regfile = "REG51.DAT" 'your uC
$crystal = 11059200 'your crystal. Not
important to control the display
'as long if you don't
exceed an instruction time of 200ns
'VARIABELS DISPLAY AND TO SHOW GRAPHICS
Dim Data_display As Byte
Dim Status As Byte
Dim Mask_status As Byte
Dim Dta_1 As Byte
Dim Dta_2 As Byte
Dim Cmdo_1 As Byte
Dim Adres_pointer As Integer
Dim Dummy_adres_pointer As Integer
Dim Dummy As Integer
'VARIABELS USED TO SHOW TEXT
Send mail to mark@mcselec.com with questions or comments about this web site.
Last modified: mei 02, 2000
LAB PLC
Lab PLC is written by Mirko Bjelica.
He wrote it for his exam and after reading this project you will admit that he deserves the 10 he got
for his work!
This project is well suited for educational purposes. Mirko supplied all the source and is willing to
help if you have a question. Of course he also likes to know if his design is useful for you.
Now what is this LAB PLC you might wonder?
LAB PLC is a PLC Compiler written in VB DOS.
The PLC software is written in BASCOM-8051 and runs in a small 20 pins 8051 chip.
Send mail to mark@mcselec.com with questions or comments about this web site.
Last modified: juni 01, 2000
C.
Send mail to mark@mcselec.com with questions or comments about this web site.
Last modified: oktober 10, 1999
Tempbyte1 = Max_bytes
Call Clearbytes(tempbyte1) ' clear output byte array
Call Test_shift_reg() ' test and set shift reg length
If Max_shift_len = 0 Then
Print "Control Board Failure !!"
Else
Print "Control Board OK !!"
End If
Do ' main loop
String_input = Space(max_in_len) ' clear input string to spaces
Input String_input ' read input string
Raw_echo = String_input ' form echo string
String_input = Trim(string_input) ' trim lead and trail spaces
Opcode = Left(string_input , 1) ' extract opcode
Opcode = Ucase(opcode) ' convert to upper case
Temp = Mid(string_input , 2 , Max_in_len) ' get numeric string
Temp = Trim(temp)
Value = Val(temp) ' convert string to numeric
If Value > Max_shift_len Then ' setting or clearing beyond bit range?
Opcode = " " ' yes. set a bogus command code
End If
Print
Select Case Opcode
Case "S" ' Set a bit
If Value = 0 Then ' improper bit value or format
Print "ERR 7"
Else
Set Set_clear_flag ' flag to set a bit
Call Set_clear_bit() ' go set the bit in array
Call Send_bytes() ' send to shift regs
Call Latchbytes() ' latch the bit pattern to outputs
Send mail to mark@mcselec.com with questions or comments about this web site.
Last modified: december 11, 1999
Introduction:
Some years ago I got the idea of making a PCB with a
plotter and finally found an old Tektronix A3 flatbed plotter
from the early 80'.
But I never really got it to work for that purpose I had in
mind, mainly because of the strange old GDU coordinate
system used on Tektronix Plot10 and Plot50 graphic work
station from that period.
Now I have gone a step further doing quick PCB's and for
that I needed a X-Y machine.
So the old Tektronix got an Atmel 90S2313 chip
implemented, and voila...
So thanks to MCS electronic for there fantastic compiler. :o)
Software/Hardware:
I don't think this software is usable in this form for other
than myself, but if you can use fragments of it, maybe the
GPIB protocol procedure, you are welcome to do so.
In this case, I'll be glad when you post me a message.
Send mail to mark@mcselec.com with questions or comments about this web site.
Last modified: februari 26, 2000
How it works...
When you select the chip (with CS = 0) from the MCU sending a
low by pinb.3, the ADC "wakes up", and it is ready to take a
measurement and output it one bit at a time through D0. The
MCU controls this process through the clock pinb.2. Each pulse of
this clock indicates to the ADC to place a bit on D0. The MCU
grabs that bit, and adds it to a byte variable in which it will
accumulate all eight bits.
'aduc0831 example
Dim As Byte 'hold each bit coming from the ADC
Do
Reset Clk
Reset Cs 'select chip
Gosub Pulse
A = 0 : T = 0
For Temp = 1 To 8 'start a new measure
Next
Set Cs
Print T
Wait 1
Loop
End
Pulse:
Set clk
Waitus 10 ' 10 us wait
Reset Clk
Return
Send mail to mark@mcselec.com with questions or comments about this web site.
Last modified: juli 11, 2000
For the proper work of PLC device its necessary to put in his
memory a program on which it will work. It can be accomplished in
several ways, and one, that I used, is to use personal computer for
downloading program into PLC. This is of course possible if PC has
installed proper software and in further reading I will explain how to use
software that I made. Application is created using programming language
Visual Basic 1.0 for DOS. Its been realized to maximally simplify
functions entering by only selecting them and not entering logic
functions, inputs, outputs or memory markers by writing names from
keyboard. Next table shows codes of logic functions, inputs, outputs and
memory markers. Of course, this codes are completely useless for user,
but they could be useful for future upgrades or if someone would wish to
make other versions of this software (Windows version for example).
FUNCTION SHORTCUT
NEW Ctrl+N
LOAD F3
SAVE F2
EXIT Ctrl+X
COMPILE F7
NEXT F6
PREVIOUS F5
TRANSFER F9
DELETE F8
Table2. Shortcut codes
It is very easy to use LabPLC. When you turn on switch PR1 you
are connecting 24V power supply to PLC and LED L1 (POWER ON)
goes on. I used 24V because it is a standard for industrial voltage levels
for supply and for control signals. At the same time LED L2 starts to
blink and that signifies that PLC is READY for data transmission.
Transmission between PC and PLC goes with a speed of 4800 bits per
second with 8 data bits, none parity and 1 stop bit. When transfer ends,
L2 goes off and L11 (READY) starts to shine. From that moment PLC
starts cyclical program execution. Changing logic states on inputs would
directly affect logic states on outputs and its controlled by the logic,
implemented true program.
EXAMPLES
Example1:
MARKER 0
INPUT 1
START
K1
OUT 1
INPUT 2
STOP
MARKER 1
MARKER0
MARKER3
INPUT 1
LEFT OUT1
K1
`
MARKER5
INPUT 2 MARKER1
RIGHT
MARKER6
OUT2
INPUT 3 K2
STOP
MARKER2 MARKER4
OUT1
K1
INPUT 1 MARKER0
STOP
OUT2
K2
INPUT 2
START
MARKER1 MARKER3
OUT3
MARKER2 K3
R5 560R K2
K11 1K L4 IN3
1 R11 R12
TxD
Q1 1
U1 220R 560R
R3 BC547
AT89C2051
K12 22K R4 19 IN3 R28
FROM PC RxD
1
1K 3
P3.1 (TxD)
P1.7
560R K3
18 L5 IN2
P1.6 R13 R14
VCC 2
P3.0 (RxD) 1
VCC 17 220R 560R
1 P1.5
K14
TRANSFER GROUND C1 16 IN2 R29
P1.4
1uF 560R K4
SW1 R2 15 IN1
P1.3 L6
RESET 100R 1 R15 R16
RST/VPP 1
14 220R 560R
P1.2
R1 C2
10K 27p 4 13 IN1 U2A
XTAL2 P1.1 (AIN2)
1 2
1
XTAL 12 L7
P1.0 (AIN0)
12MHz SN7404 K5
C3 R17 OUT4
VCC
27p 5 11 560R
XTAL1 P3.7 OUT4 U2B
U3 3 4
PR1 1
K9 LM317T VCC 9 L8
P3.5 (T1)
3 2 8 SN7404 K6
1 Vin Vout P3.4 (T0) R18
POWER 7 OUT3
C8 Adj R24 C9 R25 P3.3 (INT1) VCC
+24V 6 560R
100nF 820R 100nF 560R P3.2 (INT0) OUT3 U2C
R33 5 6
1
C4 560R 1
K10 L9
ROUND 1000uF/35V SN7404 K7
R22 L1 R19 OUT2
2K2 VCC
1
+5V
560R
L2 OUT2 U2D
READY 9 8
1
L10
R23 SN7404 K8
270R Q3 R20 OUT1
VCC
BC214B 560R
R32 L11 OUT1
2K2 DATA RECEIVED
C6
R34
560R
47uF
U2E U2F
SN7404 C7 SN7404
11 10 13 12 VCC
47uF
R30 R31
3K3 3K3
file:///F|/FrontPage Webs/Content/server/download/appnotes/vu_032.bas
Config Lcdbus = 4
Config Lcd = 16 * 2
Config Timer0 = Timer , Gate = Internal , Mode = 2
Enable Timer0
Temp_byte1 = 0
Temp_byte2 = 0
Temp_byte3 = 0
Temp_byte4 = 0
Temp_byte5 = 0
Ad_value = 0
Vu_value = 0
Vu_flag = 1 ' Enable Vu_meter Interrupt
Peak_hold_flag = 1 ' Enable Peak Hold
Start Timer0
End
'
Vu_meter:
Vu_bar = ""
Ad_value = Getad(0 , 0) ' A/D Ch#1 ,range 0-5Volt
Vu_value = Vu(ad_value) ' Get log value
Temp_byte1 = Vu_bars - Vu_value
Temp_byte1 = Temp_byte1 \ 2 ' Number of fill spaces
Temp_byte2 = Vu_value \ 2 ' Number of Vu bars
Temp_byte3 = Vu_value Mod 2 ' Bar position odd or even
If Temp_byte2 > 0 Then Vu_bar = String(temp_byte2 , 5) ' Assemble Vu bar
If Temp_byte3 = 1 Then Vu_bar = Vu_bar + Chr(6) ' Odd then add 1/2 Vu bar
If Temp_byte1 > 0 Then Vu_bar = Vu_bar + String(temp_byte1 , 32)
Locate 2 , 1 : Lcd Vu_bar ' Show Vu bar
'-------------------------------------------------------------------------------
Claus Khnel
The sensor GP2DO2 contains an infrared transmitting diode, a position sensitive photo
detector, some optics and a signal processing unit in a compact casing of 29 x 14 x 14,4
mm3. Only four pins give the electrical contact to the sensor. Two of them are power supply
and the other two builds a simple serial interface for the data handling during the
measurement. Fig. 1 gives an impression of the Sharp's sensor GP2DO2.
SHARP
On the left side of the case there is the IR LED and on the right side is the position sensitive
photo detector behind an optical lens. The method of distance measurement is very simple.
Fig. 2 shows the method.
The IR LED transmits a bundled beam to the object plane to be measured. From the photo
detector a reflected beam will be received. The angle of the received beam is dependant of
the distance of the object plane. For two different object planes the situation is displayed in
Fig. 2. If the photo detector is able to process a position sensitive input signal then there is
an information of the distance of an object given.
Photo diodes with a big light sensitive area have position sensitivity on principle. Based on
this physical effect there was position sensitive photo detectors developed. Fig. 3 displays
the principle of a position sensitive photo diode.
P
I
N
I1 I2
If there is a spot irradiation in the centre of the p-layer then both currents I1 and I2 will have
the same value. If the spot irradiation goes to the left the current I1 wills grow and the current
I2 will drop with the same value. The difference between the currents I1 and I2 describe the
location of a spot irradiation on the light sensitive surface (between the contacts) of the
position sensitive photo diode.
To process the measured distance the signal processing unit has to exploit the difference in
the currents I1 and I2. After an analog-to-digital conversion the result of measurement is
ready for serial transmission to a microcontroller or other processing unit.
The daylight (or the light of the environment in more general terms) may not influence
the measuring procedure.
The very high dynamic range of the measuring signal requires signal compression for
working with normal voltages for power supply.
Disregarding special circuit techniques in bipolar integrated circuit technology (like I2L) that
convert an irradiation into a digital signal directly than one can find circuits based on
logarithmic current-to-voltage converters always again. For logarithmic current-to-voltage
converters there are excellent technological solutions with a very good performance.
Fig. 4 shows the principle of a circuit for position sensitive current-to-voltage conversion.
-UT ln(I1/I0)
FD1
VEE R1 R2
VCC
FD2
VCC UT ln(I1/I2)
R3
VEE
I2 -UT ln(I2/I0) R4
VEE
R1 = R2 = R3 = R4 = R
In Fig. 4 the position sensitive photo detector is described through the photo diodes FD1 and
FD2. Both anodes are connected with identical circuits for current-to-voltage conversion. The
diodes in the opamp's feedback give a logarithmic behaviour of the current-to-voltage
conversion.
To achieve the required accuracy the diodes are build by bipolar transistors with a grounded
base. In a good approach the following equation is valid:
VBE
I C I 0 e xp ( ) (1)
VT
VT describes the temperature voltage proportional to the absolute temperature by eq. (2)
kT
V=
T with k = Boltzmann's constant and e = elementary charge (2)
e
I0 describes very strong temperature dependant saturation current of the bipolar transistor.
For the output voltage of the logarithmic current-to-voltage converter the following equation
is valid:
The collector current IC is identically to the current I1 in the upper circuit part of fig. 4 and to
I2 in the lower part. The third opamp processes the difference of the two output voltages.
There is only one condition for accuracy - the resistor values have to be equal.
I
VO = VT ln ( 1 ) (4)
I2
The influence of the saturation current I0 is cancelled in eq. (4). It remains only the influence
of the temperature voltage. Because this influence is linear it can be eliminated easily.
Changes of intensity (daylight) will be suppressed because they affect both currents. For
further digital processing the output voltage in eq. (4) is to convert to digital.
With the timing diagram in fig. 5 some statements to the inner processes for distance
measurement can be derived.
Vout
MSB LSB
For controlling the measurement and data handling the two lines Vin and Vout are available.
Vin is to drive by an open-drain output. Vout gives an output compatible to CMOS and/or
TTL [1].
To start the distance measurement the control line Vin must drop to Lo for a time of 70 msec
in minimum. In this time the IR LED transmits 16 pulses in the direction of the measuring
After this measuring phase the result can be called from the sensor. The control line Vin has
in this phase the function of a synchronous clock input. Starting with the most significant bit
(MSB) the eight data bits are available at Vout.
The control program for a connected microcontroller has a very simple structure (Fig. 6).
Start
Initialisation I/O-Port
Start of measurement
by Vin = Lo
no
Measurement ready?
yes
End
Controlling the sensor requires only few resources of a connected microcontroller. Quick
results for evaluation (for example) can be achieved by microcontrollers programmable in
VCC
10u//47n
D1
LED
C1
U2
UB1
VCC
U3
GND
R1
NC 680 +5VDC
U1
D7
VO D6 GND
D5
VCC D4 SERIAL
D3
VIN D2 GND
D1
GND D0 +5VDC
Q1
GP2DO2 BS1_IC LCD_BACKPACK
MOSFET N
GND
All three units of the whole circuit are powered from the same supply voltage of +5
VDC. I/O line D0 transmits the data to the LCD serial backpack [2]. I/O line D1 drives the
LED to visualize the start of measurement. The input Vin of the sensor GP2DO2 is
connected to the FET Q1, to fulfill the requirements for an open-drain driver. Over I/O line
D3 the serial result of distance measurement will be readout.
The following listing shows the control program for cyclic distance measurements in PBASIC
for BASIC Stamp I. Changes for BASIC Stamp II should be a minimum.
LCD_clear:
serout LCD,baud,(254,1,254)
serout LCD,baud,("Count:")
return
LCD_print:
serout LCD,baud,(254,136,254,#count," ")
return
On the start of each cycle the LED flashes. In the subroutine sensor_control happens
the start of measurement followed from the serial readout of the result. After the readout the
result is stored in the variable Count. The subroutine LCD_print works like a "print-at"
instruction for the LCD and writes the result as decimal number to a defined position. After a
waiting period of one second the whole procedure will be repeated.
The following table shows test results for a reflector build from white copy paper.
Distance in cm 10 20 30 40 50 60 70 >80
Counts on display 147 91 71 55 47 44 42 40
An important parameter describing the sensitivity of the sensor is the output distance
characteristic Vo. This parameter will be represented by an output change as a result of a
distance change from 80 cm to 20 cm. The values of Vo are between 45 and 65 (typical
55). The measured value here was 51 counts and so a little bit smaller as the typical value.
The measuring distance range is limited to distances from 10 cm to 80 cm. Distances
smaller then 10 cm give angles for the received beam which cannot proceed.
Literature:
'******************************************************************************
' (c) Jan Petersen
' Orevej 6
' DK 4760 Vordingborg
' jan_p@post8.tele.dk
'------------------------------------------------------------------------------
' Date: 24.02.2000
' Compiler: Bascom AVR 1.0.8b
' File: Serial2Gpib.bas
' Description: RS232 to GPIB converter +
' HPGL to GDU converter
'
' This program is made specific for my Tektronix 4662 flatbed plotter
' with GPIB (IEE488) interface, and a strange GDU coordinate system..!
' Now it communicate whit a HPGL device-driver trough a normal
' RS 232 serialport. Hey man...
'------------------------------------------------------------------------------
'
' Input:
' RS232 :19200N81R
' Handshake :RTS/CTS specific for (Eagle CAM processor)
'
' Output:
' GPIB :DATA[0..7] (Inverted !Activ Low!)
' Handshake :DAV (DataValid)
' Handshake :NDAC (NoDataAccepted)
' Handshake :NRFD (NotReadyForData)
'------------------------------------------------------------------------------
'
' HPGL commands: Tektronix 4662 Plotter Commands:
' PU; (PenUp) M[cr]
' PD; (PenDown) D[cr]
' PA xxx,yyy; (PlotAbsolut) Dxxx,yyy[cr]
' VS xx; (VelocitySpeed) BYxx[cr]
' SP 0; (SelectPen) End Of File so goto home = M150,100[cr]
'
' GDU = GraphicDataUnit
' HPGL = HeweletPackardGraphicLanguage
'
' 1 GDU = 2.54 mm
' 1 HPGL unit = 0.025 mm
'
' 100 HPGL units = 1 GDU
' 1 HPGL units = 0.01 GDU
'
' All coordinates are divided by 100
'
' A typical input format would look like this (Eagle Cam processor-HPGL driver)
'
' IN; IP 0,0,100,100; SC 0,100,0,100;
' VS 120
' SP 2;
' PU;
' PA 19,18;
' PD;
' PA 1035,18;
' PU; SP 0; PA 0,0;
'
' Converted output to Tektronix plotter
'
' BY120
' M0.19,0.18
' D10.35,0.18
' M150.100
'******************************************************************************
$regfile = "2313def.dat"
$crystal = 7372800 '7.372800 MHz crystal
$baud = 19200 '@ 19.2 Kbaud error is 0.0%
Select Case A
Case 10 : Gosub _reset 'linefeed
Case 13 : Gosub _reset 'carriage return
Case 32 : Gosub _space 'space
Case 44 : Gosub _comma 'comma
Case 59 : Gosub _semicolon 'semicolon
Case Else : Result = Result + Chr(a) 'add the character to string
End Select
_space:
' SPACE
'***************************************************
Select Case Result
Case "VS" : Command = "V" 'VelocitySpeed
Case "SP" : Command = "S" 'SelectPen
Case "PU" : Command = "M" 'PenUp
Gosub _reset
Return
_comma:
' COMMA
'***************************************************
Value_x = Result 'value_x = X-coordinate
Result = "" 'clear Result
Return
_semicolon:
' SEMICOLON
'***************************************************
Select Case Command
Case "V" : Gosub _vs 'Velocity
Case "S" : Gosub _sp 'Select Pen
Case "R" : Gosub _reset '/Dummy Chr...!
End Select '/SP 0 command was detected
'/in the _sp SUB
'/means ..EOF..
Select Case Result
Case "PU" : Command = "M" 'Move
Case "PD" : Command = "D" 'Draw
Case Else : Gosub _make_string 'value_y is in result
End Select
Gosub _reset
Return
_vs:
' PEN SPEED (velocity speed)
'***************************************************
If Result <> "0" Then
Result = "BY" + Result + Chr(13)
Gosub _gpib_out 'BYxx[cr]
End If
_sp:
' SELECT PEN
' if a "SP 0" command is recived
' it means "MOVE HOME" and EOF
'***************************************************
If Result = "0" Then
Result = "M150," + "100" + Chr(13)
Gosub _gpib_out 'M150,100[cr]
Command = "R" '/R means EOF
'/and is at dummy Chr
'/used in _semicolon SUB
Else
Command = "M"
End If
Gosub _reset
Return
_make_string:
' Prepare the string to send to GPIB
'***************************************************
If Result = "" Then Return
Gosub _divide
Value_x = Result 'value_X = value_x / 100
_reset:
' Reset some variable
' This SUB saves lot of ROM Space.. :o)
'***************************************************
Result = "" 'saves some program memory
Value_x = "" 'when variables are cleared
Value_y = "" 'in one SUB
Return
_divide:
' divide by 100
' SINGLE aritmic don't work yet, so we do It like this..!
'***************************************************
High_ = "" : Low_ = ""
If L > 2 Then H = L - 2
B = Val(low_)
If B = 0 Then Result = High_ 'ONLY real
Return
_gpib_out:
' Output data to GPIB port
' Data is inverted
' NRFS,NDAC and DAV is handshake
'***************************************************
L = Len(result) 'how long is the string..?
End
1DWLRQDO6HPLFRQGXFWRU/0
'LJLWDO7HPSHUDWXUH6HQVRU
Attention
The information contained in this document is neither supported, nor sanctioned, by any of the
corporations referenced within. This document and the information it contains is solely the opinion
and view of its creator. Use of the information contained in this document is at the risk of the user of
the information. Neither the creator of the document, nor M. Akers Enterprises, can or will, be held
accountable for any damages or loss of profits resulting from the use of this information.
7KH/0
What exactly is the LM75
2YHUYLHZ
The LM75 was created by National Semiconductor Corporation to fill a crying need in the
PC industry for detecting over temperature conditions in a personal computer. This device
has a built-in 9 bit ADC that will convert the analog thermal reference to a digital value
usable by the PC. But for this document we are going to explore how to access this device
using a Atmel 89C4051 flash microcontroller using the MCS Electronics BASCOM-8051
basic language compiler.
2
Communication with the LM75 is through the I C interface (created by the Phillips
Corporation). This is a 2-wire communications protocol used to communicate serially with
various types of devices similarly configured. The LM75, designed as a slave device, can
2
be configured through the I C interface to alert, through various methods, the PC system
that an over temperature condition has happened.
1
At certain times, direct quotations or excerpts of the LM75 Datasheet will be reproduced
in this document. The information will be displayed with a gray background, and will
contain the page of the LM75 Datasheet. The LM75 Datasheet will, at all times, be the
sole authority. If there is a conflict of information, the LM75 Datasheet will be considered
the correct source.
6FRSH
It is the intent of this document to show the relative ease to which the LM75 Digital
Temperature Sensor can be utilized. This author has reviewed several programs that use
different programming languages to interact with the LM75 DTS. Except for the MCS
Electronics BASCOM-8051 Basic Compiler, all other high level languages require a large
amount of code to perform a simple task (although the compiled object code is as small as
can be). BASCOM-8051 is 99% syntax compatible with Microsoft Qbasic, thus anyone
who can program using MS Qbasic can program a microcontroller. MCS Electronics
2
BASCOM-8051 Basic Compiler has built-in commands for handling the I C protocol,
making the coding process much easier.
The programming examples given in this document will be presented as subroutines that
you can use in your project. Appendix A will contain an example program that will read the
temperature from the LM75 DTS and display it on a LCD display module.
7KH/0
The easiest way to think of the LM75 is as a digital thermal alarm clock. You can read the
thermal time, set the thermal alarm, and its thermal snooze button. You can configure the
device to switch on a fan or an audible alarm. As seen in the diagram below the LM75 is a
fairly simple device.
Figure 1. Block diagram of LM75 device. (extracted from page 1 of the LM75 Datasheet)
The Lm75 has four registers that you can read and write to, depending upon what you
want the device to do. Mostly though, you will be reading the temperature. Upon power up
the device is set to the default mode:
n Comparator Mode
n TOS = 80 C
n THYST = 75 C
n Pointer = 00
2
The LM75 registers are accessible through the I C port. This port is comprised of Pin 1
SDA (Serial DAta) and Pin 2 SCL (Serial CLock). Also, since this device is addressable
2
(you can have up to eight devices on the I C bus), you have three address pins; Pin 7 A0,
Pin 6 A1, and Pin 5 A2. For purposes of simplicity, A0 to A2 are considered tied to
ground. A thorough reading of the LM75 Datasheet will aquatint you with the operational
fundamentals of the device.
There are some apparent errors in the LM75 Datasheet. As stated, the errors are only
apparent and not actual. The errors, if they are errors, are in the omission of information.
The following is how the information should have been presented:
Address word format: (the waveforms in the LM75 Datasheet are correct)
1 0 0 1 A2 A1 A0 R/W
MSB LSB
Many people who read the LM75 Datasheet often make the mistake of thinking that the
registers in the LM75 are accessed as a 16-bit word. Not, as the waveforms clearly
delineate, as two 8-bit words, in high byte, low byte, order. So most people make the
2
mistake of using the read word or write word I C function when communicating with the
LM75. Although the registers are 16-bit, the access method is 8-bit!
&RQFOXVLRQ
In the next section, I will be going through each register and, with code, explain how to
access and interpret the information.
Section
/P,2
Basic Communications
7HPSHUDWXUH
In the table above is the accurate way to think of the temperature register. Remember that
upon power-up the device is in the default mode, (Pointer = 00) read temperature. With
this in mind, lets look at the table above.
The temperature register is 16 bits wide. Only bits D7 through D15 mean anything. The
High Byte of the temperature word contains the Sign bit (D15) and the whole value
temperature, and the Low Byte contains the fractional temperature (D7). The rest of the
bits in the Low Byte have no meaning (D0 to D6).
To read the LM75 temperature will require sending the address and read command to the
LM75 and then reading the High Byte and then the Low Byte. In addition, the temperature
data is in twos compliment form. This means that when the temperature goes below 0C
the sign bit is set and the data is now a twos compliment of the actual temperature value.
Twos compliment simply means that the information is negated. (Real clear isnt it?) As an
example, zero is the compliment of one, and one is the compliment of zero. The
compliment of falling down is falling up. Get the picture? The math for it though is a little
tricky. In doing the math we loose 1 LSB, so it must be replaced when converting back.
Original 00011001
Compliment 11100111
As seen in the example above the compliment to 00011001 (Hex 19) is 11100111 (Hex
E7). But if we negate 11100111 we get 00011000 (Hex 18). But we know that we lost 1
LSB when the conversion was performed, so we just add it back. So 00011000 (Hex 18) +
1 gives us 00011001 (Hex19), the original number. (Believe it or not, this gives first year
Computer Science students NIGHTMARES!)
So with the above in mind, lets get to the code! In BASCOM-8051 syntax, you must
declare the Subroutines and the variables associated with them.
Lm75read = &B10010001
Lm75write = &B10010000
Sub Readlm75(Lm75Addr)
Call Setaddr Lm75addr, 0
I2cstart Start the I2C process.
I2cwbyte Lm75read, 8 Send the LM75 address and read Info.
I2crbyte Lm75high , 8 Get 8 bits and get ACK from LM75
I2crbyte Lm75low , 9 Get 8 bits and send NACK to LM75
I2Cstop Stop the I2C system
If Lm75high > 127 Then
Lm75high = Not Lm75high Flip the bits Bob!
Incr Lm75high Add 1 to the value
Lm75sign = 1 Yup, we be negative!
End If
Lm75low = Lm75low And &B10000000 Mask out the lower 7 bits.
If Lm75Low = &B10000000 Then
Lm75low = 5
End If
Lm75read = &B10010001 Reset Lm75read
End Sub
This leaves the variables Lm75high with the whole temperature value, Lm75low with the
fractional temperature value, and the Lm75sign bit denoting the polarity of the temperature
value.
3RLQWHU
P7 P6 P5 P4 P3 P2 P1 P0
0 0 0 0 0 0 Register
Select
As seen above the Pointer register is 8 bits in length. The Pointer register is used to Point
to the register that you would like to read from or write to. The only bits that we need to
concern ourselves with are P0 and P1. Bits P2 to P7 are for test purposes only, and a
write operation into these registers could damage or destroy the Lm75. So, leave them
alone.
&RQILJXUDWLRQ
D7 D6 D5 D4 D3 D2 D1 D0
0 0 0 Fault O.S. Cmp/Int Shutdown
Queue Polarity
The Configuration register controls the O.S polarity, whether or not the output is in
Comparator or Interrupt mode, fault queue (for noisy environments), and a way to put the
device to sleep.
6KXWGRZQ0RGH
To put the device in shutdown mode D0 is set. The device goes into a quiescent but
nominally active state and will draw around 1 A. The I C bus remains active and you can
2
still Read/Write the Configuration, THYST, and TOS registers. Resetting theis bit will bring the
device back up to full operation.
&RPSDUDWRU,QWHUUXSW0RGH
With D1 = 0 the device is in comparator mode. When the temperature goes above the
TOS value, the O.S. will go low. When the temperature falls below the THYST value then the
O.S. goes high. This assumes that the Polarity bit is low. With D1=1, the device is in
Interrupt mode. When the temperature goes above the TOS value, the O.S. will start
pulsing low. To reset the Interrupt the temperature must be below TOS and any device read
function will then reset the Interrupt.
263RODULW\
With D2=0 (default) the O.S. output is Active Low, when D2=1 then O.S. output is Active
High.
)DXOW4XHXH
D4 D3 Number of Faults
0 0 1 (default)
0 1 2
1 0 4
1 1 6
As the temperature nears the TOS level, any noise will cause the device to false trigger.
The Fault Queue allows you to determine how many times a false trigger happens before
the device triggers for real.
Sub Getconfig(lm75addr)
Call Setaddr Lm75addr , 2
I2cstart
I2cwbyte Lm75write Send write address
I2cwbyte &B00000001 , 8 Set Pointer to point to Configuration register
I2cstart Restart I2C
I2cwbyte Lm75read Send read address
I2crbyte Lm75config , 9 Read the config register
I2cstop
Resetpointer Reset the pointer to 00
End Sub
Sub Setconfig(lm75addr)
Call Setaddr Lm75addr , 1
I2cstart Start I2C
I2cwbyte Lm75write Send write address
I2cwbyte &B00000001 , 8 Set Pointer to point to Configuration register
I2cwbyte Lm75config Send the Lm75config byte to write
I2cstop
Resetpointer Reset the pointer to 00
End Sub
726DQG7+<67
TOS Register
THYST Register
The TOS and THYST registers are exactly like the Temperature register except that you can
write to them. Everything that pertains to the temperature register pertains to the TOS and
THYST registers.
On power-up the TOS register contains &B0101000000000000 (Hex 5000) or 80C. And
the THYST register contains &B0100101100000000 (Hex 4800) or 75C. The following
four subroutines will show how to set and get data on these registers.
Sub Gettos(lm75addr)
Call Setaddr Lm75addr , 2
I2cstart
I2cwbyte Lm75write Send write address
I2cwbyte &B00000011 , 8 Set Pointer to Tos
I2cstart
I2cwbyte Lm75read Send read address
I2crbyte Lm75toshi , 8 Read Tos high byte
I2crbyte Lm75toslo , 9 Read Tos low byte
I2cstop
Resetpointer Reset the pointer to 00
End Sub
Sub Settos(lm75addr)
Call Setaddr Lm75addr , 1
I2cstart
I2cwbyte Lm75write Send write address
I2cwbyte &B00000011 Set Pointer to Tos
I2cwbyte Lm75toshi Send Tos high byte
I2cwbyte Lm75toslo Send Tos low byte
I2cstop
Resetpointer Reset the pointer to 00
End Sub
Sub Getthyst(lm75addr)
Call Setaddr Lm75addr , 2
I2cstart
I2cwbyte Lm75write Send write address
I2cwbyte &B00000010 , 8 Set pointer to Thyst
I2cstart
I2cwbyte Lm75read Send read address
I2crbyte Lm75thysthi , 8 Read Thyst high byte
I2crbyte Lm75thystlo , 9 Read Thyst low byte
I2cstop
Resetpointer Reset the pointer to 00
End Sub
Sub Setthyst(lm75addr)
Call Setaddr Lm75addr , 1
I2cstart
I2cwbyte Lm75write Send write address
I2cwbyte &B00000010 Set pointer to Thyst
I2cwbyte Lm75thysthi Send Thyst high byte
I2cwbyte Lm75thystlo Send Thyst low byte
I2cstop
Resetpointer Reset the pointer to 00
End Sub
&RQFOXVLRQ
2
Well, thats it! Not as hard as you were thinking was it. Using the I C interface code in
BASCOM-8051 makes accessing and controlling the LM75 a snap.
Appendix A contains the full core code file, along with a schematic of a circuit that will work
with the code. All you need to do now is complete the program to read and write to the
LM75.
$
Appendix
/0&RUH&RGH
------------------------------------------------------------------------------
Program Name : Lm75Full.bas
Program Date : October 15,1999
Program Written By : M. Akers Enterprises
Michael W. Akers
3800 Vineyard Ave. #E
Pleasanton, California 94566
Voice: +1 925 484 4750
Email: mwakers@home.com
Program Purpose : This program will demonstrate how to interface to,
and communicate With The National Semiconductor LM75
Digital Temperature Sensor.
Target Processor : Atmel 89C52
------------------------------------------------------------------------------
Programmer Date Comments
------------------- ---------- --------------------------------------------
Michael Akers 10/15/99 Initial creation of program.
-----------------------------------------------------------------------------
Define the processor (the regfile goes here!)
$regfile = "8052.DAT"
Define all meta-commands that must beinserted before all other commands.
Define all subroutines
Declare Sub Readlm75(lm75addr As Byte)
Declare Sub Getconfig(lm75addr As Byte)
Declare Sub Setconfig(lm75addr As Byte)
Declare Sub Gettos(lm75addr As Byte)
Declare Sub Settos(lm75addr As Byte)
Declare Sub Gethyst(lm75addr As Byte)
Declare Sub Sethyst(lm75addr As Byte)
Declare Sub Setaddr(lm75addr As Byte , Flagrw As Byte)
Declare Sub Resetpointer()
Declare Sub Val2temp(lm75tmphi As Byte , Lm75tmplo As Byte , Lm75tmpsign As Bit)
Define all variables and constants
Dim Lm75read As Byte Read address base
Dim Lm75write As Byte Write address base
Dim Lm75addr As Byte Lm75 Address
Dim Lm75high As Byte Lm75 Temperature High Byte
Dim Lm75low As Byte Lm75 Temperature Low Byte
Dim Lm75sign As Bit Lm75 Temperature Sign Bit
Dim Lm75config As Byte Lm75 Configuration
Dim Lm75toshi As Byte Lm75 Tos high byte
Dim Lm75toslo As Byte Lm75 Tos low byte
Dim Lm75tossign As Bit Lm75 Tos sign bit
Dim Lm75thysthi As Byte Lm75 Thyst high byte
Dim Lm75thystlo As Byte Lm75 Thyst low byte
Dim Lm75thystsign As Bit Lm75 Thyst sign bit
Dim Flagrw As Byte Read/Write Flag
Dim Lm75tmphi As Byte Lm75 Temp hi byte
Dim Lm75tmplo As Byte Lm75 Temp lo byte
Dim Lm75tmpsign As Bit Lm75 Temp sign bit
Define all Configurations and Pin assignments
Config Sda = P1.0
Config Scl = P1.1
Config I2cdelay = 1
Config Lcd = 40 * 4
Config Lcdpin , Db4 = P1.4 , Db5 = P1.5 , Db6 = P1.6 , Db7 = P1.7 , E = P1.3 , Rs = P1.2
Initialize variables as needed.
Lm75read = &B10010001
Lm75write = &B10010000
Program start.
Start:
Goto Start
Program end.
Begin subroutine section.
Sub Readlm75(lm75addr)
Call Setaddr Lm75addr , 0
I2cstart Start the I2C process.
I2cwbyte Lm75read Send the LM75 address and read Info.
I2crbyte Lm75high , 8 Get 8 bits and get ACK from LM75
I2crbyte Lm75low , 9 Get 8 bits and send NACK to LM75
I2cstop Stop the I2C system
Call Val2temp Lm75high , Lm75low , Lm75sign Convert the value (if needed)
Lm75high = Lm75tmphi
Lm75low = Lm75tmplo
Lm75sign = Lm75tmpsign
Lm75read = &B10010001 Reset Lm75read
End Sub
Sub Getconfig(lm75addr)
Call Setaddr Lm75addr , 2
I2cstart
I2cwbyte Lm75write Send write address
I2cwbyte &B00000001 , 8 Set pointer register to point
at the configuration register.
I2cstart Restart I2C
I2cwbyte Lm75read Send read address
I2crbyte Lm75config , 9 Read the config register
I2cstop
Resetpointer Reset the pointer to 00
End Sub
Sub Setconfig(lm75addr)
Call Setaddr Lm75addr , 1
I2cstart Start I2C
I2cwbyte Lm75write Send write address
I2cwbyte &B00000001 , 8 Set Pointer to point to Config register
I2cwbyte Lm75config Send the Lm75config byte to write
I2cstop
Resetpointer Reset the pointer to 00
End Sub
Sub Gettos(lm75addr)
Call Setaddr Lm75addr , 2
I2cstart
I2cwbyte Lm75write Send write address
I2cwbyte &B00000011 , 8 Set Pointer to Tos
I2cstart
I2cwbyte Lm75read Send read address
I2crbyte Lm75toshi , 8 Read Tos high byte
I2crbyte Lm75toslo , 9 Read Tos low byte
I2cstop
Resetpointer Reset the pointer to 00
End Sub
Sub Settos(lm75addr)
Call Setaddr Lm75addr , 1
I2cstart
I2cwbyte Lm75write Send write address
I2cwbyte &B00000011 Set Pointer to Tos
I2cwbyte Lm75toshi Send Tos high byte
I2cwbyte Lm75toslo Send Tos low byte
I2cstop
Resetpointer Reset the pointer to 00
End Sub
Sub Getthyst(lm75addr)
Call Setaddr Lm75addr , 2
I2cstart
I2cwbyte Lm75write Send write address
I2cwbyte &B00000010 , 8 Set pointer to Thyst
I2cstart
I2cwbyte Lm75read Send read address
I2crbyte Lm75thysthi , 8 Read Thyst high byte
I2crbyte Lm75thystlo , 9 Read Thyst low byte
I2cstop
Resetpointer Reset the pointer to 00
End Sub
Sub Setthyst(lm75addr)
Call Setaddr Lm75addr , 1
I2cstart
I2cwbyte Lm75write Send write address
I2cwbyte &B00000010 Set pointer to Thyst
I2cwbyte Lm75thysthi Send Thyst high byte
I2cwbyte Lm75thystlo Send Thyst low byte
I2cstop
Resetpointer Reset the pointer to 00
End Sub
Sub Setaddr(lm75addr , Flagrw)
If Lm75addr <> 0 Then
Lm75addr = Lm75addr * 2 Shift the address 1 bit left
Select Case Flagrw
Case 0 : Lm75read = Lm75read + Lm75addr Add the offset to the read address
Case 1 : Lm75write = Lm75write + Lm75addr Add the offset to the write address
Case 2 :
Lm75read = Lm75read + Lm75addr Add the offset to read and write address
Lm75write = Lm75write + Lm75addr
Case Else
End Select
End If
End Sub
Sub Resetpointer()
Only call this routine from within a subroutine that has called Setaddr()!
I2cstart
I2cwbyte Lm75write Send write address
I2cwbyte &B00000000 , 9 Set pointer to 00
I2cstop
Lm75read = &B10010001
Lm75write = &B10010000
End Sub
Sub Val2temp(lm75tmphi , Lm75tmplo , Lm75tmpsign)
This routine will convert the hi and lo bytes into a temperature value
If Lm75tmphi > 127 Then
Lm75tmphi = Not Lm75tmphi
Incr Lm75tmphi
Lm75tmpsign = 1
End If
Lm75tmplo = Lm75tmplo And &B10000000
If Lm75tmplo = &B10000000 Then
Lm75tmplo = 5
End If
End Sub
Sub Temp2val(lm75tmphi , Lm75tmplo , Lm75tmpsign)
This routine will convert a temperature value into the prober hi and lo byte values.
If Lm75tmpsign = 1 Then
Lm75tmphi = Not Lm75tmphi
Incr Lm75tmphi
End If
If Lm75tmplo <> 0 Then
Lm75tmplo = &B10000000
End If
End Sub
Insert include files here.
End of subroutines and actual end of program.
+5
U1
31 39
EA/VP P0.0 38
P0.1
14
19 37
OSC1 X1 P0.2 36
P0.3 35
Vcc 18 P0.4 34
R1 X2 P0.5 33
1 8 10K P0.6 32
N/C OUT +5 9 P0.7
24 MHz Os cil lato r RESET 21
Gnd
P2.0 22
C1 12 P2.1 23
CO100- 24 0.1uf 13 INT0 P2.2 24
14 INT1 P2.3 25
7
R2 R3 R4 15 T0 P2.4 26
5K 5K 330 R5 T1 P2.5 27
330 1 P2.6 28
2 P1.0 P2.7
RS 3 P1.1 17
EN 4 P1.2 RD 16
DB 4 5 P1.3 WR 29
LCD Module DB 5 6 P1.4
P1.5
PSEN
ALE/P
30
DB 6 7 11
DB 7 8 P1.6 TXD 10
P1.7 RXD
AT89C52
+5
U2 U3 U4 U5
8
8
7 7 7 7
+VS
+VS
+VS
+VS
6 A0 1 6 A0 1 6 A0 1 6 A0 1
5 A1 SDA 3 5 A1 SDA 3 5 A1 SDA 3 5 A1 SDA 3
A2 O.S. A2 O.S. A2 O.S. A2 O.S.
GND
GND
GND
GND
2 2 2 2
SCL SCL SCL SCL
LM75_S LM75_S LM75_S LM75_S
4
4
U6 U7 U8 U9
8
8
7 7 7 7
+VS
+VS
+VS
+VS
6 A0 1 6 A0 1 6 A0 1 6 A0 1
5 A1 SDA 3 5 A1 SDA 3 5 A1 SDA 3 5 A1 SDA 3
A2 O.S. A2 O.S. A2 O.S. A2 O.S.
GND
GND
GND
GND
2 2 2 2
SCL SCL SCL SCL
LM75_S LM75_S LM75_S LM75_S
4
4
M. Akers Enterprises
Title
LM75 Demo (handles 8 LM75 devices)
This soft/hardware is made specific for my Tektronix 4662 flatbed plotter with
GPIB (IEE488) interface, and a strange GDU coordinate system..!
Now it communicate with a HPGL device-driver trough a normal RS 232 serialport.
Introduction:
Some years ago I got the idea of making PCB with a plotter and finally found a old
Tektronix A3 flatbed plotter from the early 80'.
But I newer really got it to work for that purpose I had in mind, mainly because of the
strange old GDU cordinate system used on Tektronix Plot10 and Plot50 graphic work
station from that periode.
Later on I got a newer Roland DXY990 plotter with Serial/Parallel interface and have
made many fine PCB since.
I plot direct on the copper foil with a permanent ink pen, and the quality is very good.
A quick, easy and clean way of doing some rapidly prototype boards.
Now I have gone a step further doing quick PCB's and for that I need a X-Y machine.
So the old Tektronix got a Atmel 90S2313 chip implemented, and voila...
So thanks to MCS electronic for there fantastic compiler. :o)
I now just need to finish the milling machine to replace the pen on the plotter, and
I'am ready to Countour Milling a board with the Eagle Layout-editor and CAM processor
I use. And for that purpose the Tektronix plotter is ideal with it's robust motor system
and very smoth microstep drive.
Software/Hardware:
I don't think this software is usable in this form for other than myself, but if you
can use fragments of it, maby the GPIB protocol procedure, you are welcome to do so.
In this case, I'll be glad when you post me a message.
I have included lots of comments in the source code, so there isn't mutch to say about
it, except for the size of the hex file generated.! 7EC (2028) is the actual
ROMIMAGE size, and It took me some time to get it fit in.
Lot of memory was used every time a string needed to be cleared... (Result="")
By collecting this in a single Sub routine instead, up to 300 byte was saved...!
Files Contents:
Serial2Gpib.ZIP ---Serial2Gpib.bas
---Serial2Gpib.gif
---Serial2Gpib.sch
---Serial2Gpib.txt
Posted at:
MCS electronic --- http://www.mcselec.com/applicat.htm
Bascom Users Software Pool --- http://www.qsl.net/dg5dbz/bascompool/
Links:
MCS electronic --- http://www.mcselec.com/
Eagle Layout Editor --- http://www.cadsoft.de/
Jan Petersen --- http://home8.inet.tele.dk/jan_p
Author:
Jan Petersen
Orevej 6
DK 4760 Vordingborg
Denmark
EMail: jan_p@post8.tele.dk
Date: 24.02.2000
Tempbyte1 = Max_bytes
Call Clearbytes(tempbyte1) ' clear output byte array
End Select
Loop
Sub latchbytes()
Set Latch_out ' latch the shift reg to outputs
Waitms 1
Reset Latch_out
End Sub
Sub Send_bytes()
For I = Arry_end Downto 0 ' send byte array to shift regs
Tempbyte1 = Bytes(i)
Shiftout Data_out , Clock_out , Tempbyte1 , 0 , 8 , Shift_delay
Next I
End Sub
End
'Author : P. Huijssen
' The Netherlands
'This program shows a bitmap and some text on a graphic display type : TLX1741-C3M
'which uses the T6963 controller.
'The display has a screenresolution of 240x128 pixels
'The display uses a data/command line to determine of a databyte or command byte is sent.
'In this application it's connected to A14 which makes it easy te sent data or a command.
'Data is sent to &H8000 (C/D line low)
'Commands/statuscheck written/read at &HC000 (C/D line high)
'The software is really an experiment and may not match the 'rules' of proper programming
'All of the used settings regarding the display are very well described in the datasheet
'The displayscreen should show 'TOSHIBA' in large characters (bitmap) and two lines of
text
'******************************************************************
'INIT
'******************************************************************
'*****************************************************************
'*****************************************************************
'START PROGRAMME
'*****************************************************************
'*****************************************************************
End
'**************************
'DISPLAY SETUP
'************************
Set_display:
For dummy = 1 To 7
Read Dta_1
Data_display = Dta_1
Gosub Writed
Read Dta_2
Data_display = Dta_2
Gosub Writed
Read Cmdo_1
Data_display = Cmdo_1
Gosub Writec
Next Dummy
Return
Dta_init:
'**********************************************************
'CLEAR SCREEN (ERASE GRAPHIC+TEXT RAM)
'**********************************************************
Display_blank:
Next Dummy
Next Dummy
Return
'*********************************************************
' PLACE ADRES POINTER DISPLAY
'*********************************************************
Set_adres: 'set adres display
Return
'*********************************************************
'PLACE TEXT.
'*********************************************************
Show_text:
Return
'*****************************************************
'WRITE COMMAND AND DATA NAAR DISPLAY + STATUSCHECK
'*****************************************************
Writec: 'write display command
Gosub Status_check
Out &H3800 , Data_display
Return
'***********
Writed: 'write display data
Gosub Status_check
Out &H3000 , Data_display
Return
'***********
Status_check: 'check status controller
Status_1:
Status = Inp(&H3800)
Mask_status = Status And 3
If Mask_status = 3 Then : Return
End If
Goto Status_1
'*****************************************************************
'SHOW BITMAP
'*****************************************************************
Show_bitmap:
Gosub Display_blank
Adres_pointer = 0
Gosub Set_adres
Restore Bitmap_data
Next Dummy
Return
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H04 , &H00 , &H01 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H04 , &H00 , &H01 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H04 , &H00 , &H01 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H04 , &H00
Data &H01 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H04 , &H00 , &H01 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &HFE , &H00 , &H00 , &H00 , &H7F , &HC0 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H04 , &H00 ,
&H01 , &H03 , &HFF , &HFF
Data &HFF , &HC0 , &H07 , &HFF , &HC0 , &H00 , &H01 , &HFF , &HF0 , &H00 , &H7C , &H00 ,
&H00 , &HF8 , &H0F , &H00
Data &H7F , &HFF , &HE0 , &H00 , &H00 , &H3F , &H00 , &H00 , &H04 , &H00 , &H01 , &H03 ,
&HFF , &HFF , &HFF , &HC0
Data &H1F , &HFF , &HF0 , &H00 , &H03 , &HFF , &HFC , &H00 , &H7C , &H00 , &H00 , &HF8 ,
&H0F , &H00 , &H7F , &HFF
Data &HF8 , &H00 , &H00 , &H3F , &H00 , &H00 , &H04 , &H00 , &H01 , &H03 , &HFF , &HFF ,
&HFF , &HC0 , &H7F , &HFF
Data &HFC , &H00 , &H1F , &HFF , &HFF , &H00 , &H7C , &H00 , &H00 , &HF8 , &H0F , &H00 ,
&H7F , &HFF , &HFE , &H00
Data &H00 , &H3F , &H80 , &H00 , &H04 , &H00 , &H01 , &H03 , &HFF , &HFF , &HFF , &HC0 ,
&HFF , &H03 , &HFE , &H00
Data &H1F , &HC0 , &H7F , &H80 , &H7C , &H00 , &H00 , &HF8 , &H0F , &H00 , &H7F , &HFF ,
&HFF , &H00 , &H00 , &H7F
Data &H80 , &H00 , &H04 , &H00 , &H01 , &H00 , &H00 , &H3C , &H00 , &H01 , &HFC , &H00 ,
&H7F , &H00 , &H3F , &H00
Data &H1F , &HC0 , &H7C , &H00 , &H00 , &HF8 , &H0F , &H00 , &H7C , &H00 , &H3F , &H80 ,
&H00 , &H7B , &H80 , &H00
Data &H04 , &H00 , &H01 , &H00 , &H00 , &H3C , &H00 , &H03 , &HF0 , &H00 , &H1F , &H00 ,
&H3C , &H00 , &H07 , &HC0
Data &H7C , &H00 , &H00 , &HF8 , &H0F , &H00 , &H7C , &H00 , &H0F , &HC0 , &H00 , &HFB ,
&HC0 , &H00 , &H04 , &H00
Data &H01 , &H00 , &H00 , &H3C , &H00 , &H03 , &HE0 , &H00 , &H0F , &H80 , &H7C , &H00 ,
&H03 , &HC0 , &H7C , &H00
Data &H00 , &HF8 , &H0F , &H00 , &H7C , &H00 , &H07 , &HC0 , &H00 , &HF3 , &HC0 , &H00 ,
&H04 , &H00 , &H01 , &H00
Data &H00 , &H3C , &H00 , &H07 , &HC0 , &H00 , &H07 , &HC0 , &H7C , &H00 , &H03 , &HC0 ,
&H7C , &H00 , &H00 , &HF8
Data &H0F , &H00 , &H7C , &H00 , &H07 , &HC0 , &H01 , &HF3 , &HE0 , &H00 , &H04 , &H00 ,
&H01 , &H00 , &H00 , &H3C
Data &H00 , &H07 , &H80 , &H00 , &H07 , &HC0 , &H7C , &H00 , &H03 , &HE0 , &H7C , &H00 ,
&H00 , &HF8 , &H0F , &H00
Data &H7C , &H00 , &H03 , &HC0 , &H01 , &HE1 , &HE0 , &H00 , &H04 , &H00 , &H01 , &H00 ,
&H00 , &H3C , &H00 , &H0F
Data &H80 , &H00 , &H03 , &HE0 , &H7C , &H00 , &H03 , &HE0 , &H7C , &H00 , &H00 , &HF8 ,
&H0F , &H00 , &H7C , &H00
Data &H03 , &HC0 , &H01 , &HE1 , &HF0 , &H00 , &H04 , &H00 , &H01 , &H00 , &H00 , &H3C ,
&H00 , &H1F , &H80 , &H00
Data &H03 , &HF0 , &H7C , &H00 , &H00 , &H00 , &H7C , &H00 , &H00 , &HF8 , &H0F , &H00 ,
Data &HF0 , &H00 , &H03 , &HE0 , &H04 , &H00 , &H01 , &H00 , &H00 , &H3C , &H00 , &H07 ,
&H80 , &H00 , &H07 , &HC0
Data &HF8 , &H00 , &H01 , &HF0 , &H7C , &H00 , &H00 , &HF8 , &H0F , &H00 , &H7C , &H00 ,
&H01 , &HF0 , &HF0 , &H00
Data &H03 , &HE0 , &H04 , &H00 , &H01 , &H00 , &H00 , &H3C , &H00 , &H03 , &HE0 , &H00 ,
&H0F , &H80 , &HFC , &H00
Data &H03 , &HE0 , &H7C , &H00 , &H00 , &HF8 , &H0F , &H00 , &H7C , &H00 , &H03 , &HE1 ,
&HF0 , &H00 , &H01 , &HE0
Data &H04 , &H00 , &H01 , &H00 , &H00 , &H3C , &H00 , &H03 , &HF0 , &H00 , &H1F , &H00 ,
&H7C , &H00 , &H03 , &HE0
Data &H7C , &H00 , &H00 , &HF8 , &H0F , &H00 , &H7C , &H00 , &H03 , &HE1 , &HF0 , &H00 ,
&H01 , &HE0 , &H04 , &H00
Data &H01 , &H00 , &H00 , &H3C , &H00 , &H03 , &HF0 , &H00 , &H3F , &H00 , &H3F , &H00 ,
&H07 , &HC0 , &H7C , &H00
Data &H00 , &HF8 , &H0F , &H00 , &H7C , &H00 , &H07 , &HC3 , &HE0 , &H00 , &H00 , &HF0 ,
&H04 , &H00 , &H01 , &H00
Data &H00 , &H3C , &H00 , &H00 , &HFF , &H03 , &HFE , &H00 , &H3F , &HE0 , &H3F , &HC0 ,
&H7C , &H00 , &H00 , &HF8
Data &H0F , &H00 , &H7F , &HFF , &HFF , &HC7 , &HE0 , &H00 , &H00 , &HF8 , &H04 , &H00 ,
&H01 , &H00 , &H00 , &H3C
Data &H00 , &H00 , &H7F , &HFF , &HFC , &H00 , &H1F , &HFF , &HFF , &H80 , &H7C , &H00 ,
&H00 , &HF8 , &H0F , &H00
Data &H7F , &HFF , &HFF , &H87 , &HC0 , &H00 , &H00 , &HF8 , &H04 , &H00 , &H01 , &H00 ,
&H00 , &H3C , &H00 , &H00
Data &H3F , &HFF , &HF8 , &H00 , &H0F , &HFF , &HFF , &H00 , &H7C , &H00 , &H00 , &HF8 ,
&H0F , &H00 , &H7F , &HFF
Data &HFF , &H07 , &HC0 , &H00 , &H00 , &HF8 , &H04 , &H00 , &H01 , &H00 , &H00 , &H3C ,
&H00 , &H00 , &H0F , &HFF
Data &HE0 , &H00 , &H03 , &HFF , &HFC , &H00 , &H7C , &H00 , &H00 , &HF8 , &H0F , &H00 ,
&H7F , &HFF , &HFC , &H07
Data &HC0 , &H00 , &H00 , &HFC , &H04 , &H00 , &H01 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &HFE , &H00 , &H00
Data &H00 , &H3F , &HE0 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H04 , &H00 , &H01 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H04 , &H00 , &H01 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H04 , &H00
Data &H01 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H04 , &H00 , &H01 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H04 , &H00 ,
&H01 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H04 , &H00 , &H01 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H04 , &H00 , &H01 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H04 , &H00 , &H01 , &HFF , &HFF , &HFF , &HFF , &HFF ,
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
&H00 , &H00 , &H00 , &H00
Data &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 , &H00 ,
Main:
'Reading first the ROM-number
1wreset 'reset the device
1wwrite &H33 'read ROM command
Ar(1) = 1wread(8) 'read 8 bytes
Print "ROM number : ";
For I = 1 To 8
Printhex Ar(i); 'print ROM-number
Next
Wait 1 'wait a moment
'Writing scratchpad
'It is possible to write 64 bytes of data to the scratchpad. In this example
'i only used 16 bytes.You can write/read the other bytes if you change the
'array or start address.
Text = "Scratchpad data."
Ar(1) = &H96 'Write scratch command
Ar(2) = &HC0 'Start address + C0H
Ar(3) = &HFF - Ar(2) 'Complement of Ar(2)
1wreset 'reset the device
1wwrite &HCC 'Skip read ROM command
1wwrite Ar(1) , 3 'Write command to DS1991
Gosub Fill_write_array 'Fill array with "Text"
1wwrite Ar(1) , 16 'Write 16 bytes to DS1991
Wait 1 'wait a moment
'Writing subkey
'It is possible to write 64 bytes of data to the subkey. In this example
'i only used 16 bytes.You can write/read the other bytes if you change the
'array or start address.
Text = "Hello world!!!!!" 'New subkey
Ar(1) = &H99 'Writing subkey command
Ar(2) = &H10 'Subkey(00H) + Start
address (10H)
Ar(3) = &HFF - Ar(2) 'Complement of Ar(2)
1wreset 'reset the device
1wwrite &HCC 'Skip read ROM command
1wwrite Ar(1) , 3 'Write subkey write command
'Reading subkey
Ar(1) = &H66 'Read secured subkey
command
Ar(2) = &H10 'Subkey(00H) + Start adress
(10H)
Ar(3) = &HFF - Ar(2) 'Complement of Ar(2)
1wreset 'reset the device
1wwrite &HCC 'Skip read ROM command
1wwrite Ar(1) , 3 'Write subkey read command
Ar(1) = 1wread(8) 'DS1991 respons with the
Subkey-id
Text = "PASSWORD" 'Must be the same as the
last 8
Gosub Fill_write_array ' bytes of "writing
Fill_write_array:
For I = 1 To 16
Temp2 = Mid(text , I , 1)
Ar(i) = Asc(temp2)
Next I
Return
'-------------------------------------------------------------------------------
' (c) Copyright 2000 Rhombus Greenville SC
'Code can be used by others providing this header is included in the source.
'===============================================================================
'Getting started in Real-Time Control using BASCOM Part 1
'===============================================================================
'INTRO
'For those who have not yet applied themselves to real-time control using 8-bit
' MCU/MPUs, then these notes and segments of code may help you get started.
'Or if you normally avoid Interrupts, then this first part may change that by
' showing how Timer0 can be expanded into as many timers as you wish, and with
' very little code.
'I am experienced with the 51 at assembler level but new to the Bascom compiler.
' What surprises me the most is that despite the convenience of using a
' compiler, it does not appear to be restricting in any way - hence I hope to
' avoid the use of any assembler code.
'
'Whilst most of what will be described was written a good 15-25 years ago, I
' have checked and it does appear to fit in neatly with present day theory for
' simple real-time control and is classified as 'Co-operative Multi-tasking'.
' There is no forced switching of tasks as with a RTOS, and hence all CPU time
' is devoted to 'getting the job done' and no storage is needed to hold all the
' intermediate states of pre-empted tasks.
'All but the most simple machines require multiple tasks operating in parallel.
' Each task consist of a series of states(steps) with clearly defined logic that
' determines the transition from one state to the next.For example a tank drain
' valve is open, the low level float switch indicates empty but it is known that
' an additional 40 secs needs to be timed for a complete drain and to finally
' close the valve. In parallel a safety locking sequence must be performed on
' another part of the machine before a steam valve is opened.
'
'It is that sort of parallel control that is easily handled by PLCs where there
' is a continuous loop scanning all Inputs, then based on pre-defined logic so
' each task progresses from one stage(step) to the next and resulting in a new
' set of Outputs. With fast loops of Inputing, Processing and Outputing the
' tasks are effectively being controlled in parallel.
'
'When I/O is added that cannot be included in that main I/O loop, such as
' operator interfaces and host serial communication, then the software must use
' interrupts to take whatever time is needed from the main I/O loop to service
' the needs of those asynchronous events. It was at this stage of the PLC's
' history that the limitations of relay ladder logic became more than apparent.
' The PLC solutions to handle these new demands were far from elegant,whereas
' for MPU/MPCs the solutions are a natural and can be achieved very simply
' using the Bascom compiler.
'The methods to be described here have handled slow process control as mentioned
' above, and together with both serial & operator interfaces, They have also
' allowed an 11Mhz 8052 to precisely control a knitting machine's 100+ pneumatic
' outputs in perfect sync with needles flying by at 800 per second, and as the
' operator keyed in a new batch and the host collected production data.
'
'The sharing of CPU time is based on a priority ordered list of tasks and with
' all asyncronous or time critical events being interrupt driven. The main I/O
' tasks are prompted by timer set flags (only one in example) which can also
' serve to distribute CPU demands. Analog inputs can also benefit from using a
' timer rate related to the supply frequency. When each task is completed it
' returns execution to the top of the list.
'Interrupt routines are kept at an absolute minimum and any excessive processing
' needs are off-loaded to the main loop. This holds interrupt service latencies
' to a minimum and avoids the need for either hardware or software priority
' handling to satisfy critical timing issues.
'An example of off-loading interrupt servicing time would be to merely flag the
' timer event shown below, avoid all needs for stacking (it would not change the
' value of any registers, nor even the status flags), and carry out the updates
' in the main loop. For simplicity, the timer ISR below does its processing
' during the interrupt and that will normally be OK but for the most demanding
' applications.
'
'Timer0 ISR code is very small and should be self explicit. In order to show it
' working there is a main loop using those new timers as inputs, a little logic
' for reloading them, and their run status is output to a set of pins to allow
' scoping. For a steady trace the timers are running very fast at a resolution
' of 2mS. If using the Timer0 expansion code in your own applications then it is
' only a matter of defining a new constant for loading the timer.
'Part 2 will add transparent keypad input to change the timer values whilst they
' are running & without affecting their operation until the Enter key finalises
' their new value and they snap to the new timing.
'----- INITIALISATION
Config Timer0 = Timer , Gate = Internal , Mode = 1 '16 bit,own code reloads
On Timer0 Timer_0_int
Enable Interrupts 'enable the use of interrupts
Enable Timer0
'Dumb code to show a set of timers (Timer0 expanded) each running with their own
' individual values, and controlling their own output(a port pin) and effectively
' in parallel.
Main_loop:
Do
'--Test for I/O prompt
If Io_flag = 1 Then Goto Io_control 'Dummy machine control
Loop
Io_control:
'The 3 stages of Read_Inputs/Process/Write_Outputs
'All inputs here are Timer values and hence readily available internally.
'The processing here is merely to watch for Timers(1) to count down to zero
' and then reset its associated P3.5. On the next pass (when Io_flag is found
' set in the Main_loop) we reload all 4 timers with their individual values &
' set their individual flags.
'All subsequent passes with Timer(1)<>0 has pins P3.2.3.4 updated to reflect
' when their counts have reached zero.
'That is not a separate Output stage as such, but if we had remote I/O then
' the equivalents of P3.2.. would be internal bits and once all processing
' was complete, we would output them as a distinct & separate operation as in
' a PLC.
Io_flag = 0
If Timers(1) = 0 Then
If P3.5 = 1 Then 'Dont restart till 1 pass later
Reset P3.5
Else
Timers(1) = 20 : Set P3.5
Timers(2) = 15 : Set P3.2
Timers(3) = 10 : Set P3.3
Timers(4) = 5 : Set P3.4
End If
Else
If Timers(2) = 0 Then Reset P3.2 'Could avoid multiple resets
If Timers(3) = 0 Then Reset P3.3 ' but no real savings
If Timers(4) = 0 Then Reset P3.4
End If
Goto Main_loop
'We return at the highest priority level ignoring the tasks below this one
' but know that they will be reached on the next pass, now that the Io_flag
' is reset. *Except* if the interval set between scans is not realistic: eg
' if the Control routines take 50mS and the Io_flag is set every 40mS then
' all other functions will be blocked out. Timing the different functions in
' the Main_loop and allocating realistic intervals is not a difficult task.
Timer_0_int:
Counter0 = 2ms
Start Timer0
Set Io_flag
Inc Tic_cnt0
If Tic_cnt0 => 1 Then '1=2mS for easy scoping
Tic_cnt0 = 0 '5/50 =10mS/100mS more typical
For Isr_temp = 1 To 4
If Timers(isr_temp) <> 0 Then Decr Timers(isr_temp)
Next
'If Timers(0) <> 0 Then Decr Timers(0) 'Alternatively trade code size
'If Timers(1) <> 0 Then Decr Timers(1) ' for speed
'If Timers(2) <> 0 Then Decr Timers(2)
'If Timers(3) <> 0 Then Decr Timers(3)
End If
Return
'-------------------------------------------------------------------------------
' (c) Copyright 2000 Rhombus Greenville SC
'Code can be used by others providing this header is included in the source.
'===============================================================================
'Getting started in Real-Time Control using BASCOM Part 2
'===============================================================================
'To maintain consistent processing of the I/O tasks in Part 1 we cannot leave
' the main loop, start scanning a keypad and wait for user input. Any keypad
' activity needs to be flagged and brought into the main loop by one means or
' another - and at a pre-determined priority level with respect to other tasks.
'A key encoder chip could take care of full-time scanning, separation of
' multiple keys, debouncing, and we would just watch for its data ready flag.
' For example a 74C922 would need 5 port pins from an MCU or a decode of one
' address for an MPU, to handle 16 keys - they are somewhat more expensive than
' 51 micro itself, but would serve our purpose well.
'An alternative is shown below where the existing Timer0 ISR takes on the
' additional task of sinking just one of the 4 row signals connected to a
' keypad matrix. Everytime the Scan-tics reach a specific count (shown for 10x2
' =20mS) then the next row becomes active low, until the columns indicate a key
' is depressed. Then the active row is maintained and the columns must read the
' same after a further 20mS in order for the key to be accepted. That takes 3
' extra port pins for a total of 8, but no additional logic is required. It
' also scans full-time.
'Either solution will allow detection of an active key in the main loop. The
' code shown for key detection below becomes 'If Key_flag Then Goto Key_10' when
' using an MCU with a 74C922. The Row_col was read and saved at the 20mS point,
' the 922 also saved after debounce and the operator will not be sensitive to a
' few hundred mS of other tasks being completed prior to acting on the last key.
' So key detection receives one of the lowest priorities and will be close to
' the bottom of the loop.
'If fixed and pre-determined functions were associated with each key, then
' keypad input would already be complete. Each key would have an associated
' Goto and they would all return to the top of the main loop (standard practice,
' and no stack used). They may only require one or two bits to be set/reset and
' occupy just a few uSecs. But often that one key will be the first step to an
' operator dialog taking seconds or even minutes and each operator response
' may well consist of several keys as a numeric value is entered and edited.
' The common code could be included in the main loop, but it will become lengthy
' if code for key masks,lengths,min/max values etc are added, and that would
' detract from the loop's clarity.
'At this stage we can now enter new data for each of the timers in parallel
' with normal operation of the 'machine'. Using a scope on P3.5 as a reference.
' the only visual influence of keyboard activity will be when the final Enter
' key causes the trace to snap to the new timing.
'----- INITIALISATION
Config Timer0 = Timer , Gate = Internal , Mode = 1 '16 bit, code reloads
Counter0 = 2ms
On Timer0 Timer_0_int
Enable Interrupts 'enable the use of interrupts
Enable Timer0
'For demo purposes the 'machine' consists of 4 timers cycling & driving 4 port
' pins. There are 4 keypad function keys that initiate a dialog sequence with the
' machine operator to setup new timer values in real-time whilst the 'machine'
' continues normally.
'The keypad is scanned continously in the background and collects the user's
' input without interrupting the control of the machine - a scope can be used
' to monitor the machine's cycling pins as the timers are changed.
Main_loop:
Do
'--Test for I/O prompt
If Io_flag = 1 Then Goto Io_control 'Dummy machine control
'-- From here upto Key_return could be included as part of main loop - but see text
'Each new character causes a 'Jump' from main loop to Key_10
Key_10:
Temp1 = Lookup(temp1 , Xlate_col) 'Translate Column to = 0,1,2,3
Temp2 = Row_col And &HF0 ' isolate Row to = 70,B0,D0,EO
Select Case Temp2
Case &H70 : Key_val = Lookup(temp1 , Row0) ' then use that Row's lookup
Case &HB0 : Key_val = Lookup(temp1 , Row1)
Case &HD0 : Key_val = Lookup(temp1 , Row2)
Case &HE0 : Key_val = Lookup(temp1 , Row3)
Case Else :
End Select
Set New_key ' and avoid repeat keys
'Function Key cases
If Temp1 = 0 Then 'Left hand column of 4 keys
If Pad_active = 0 Then 'Reject new Funxs during Dialog
On Key_val Goto Edit_sp1 , Edit_sp2 , Edit_sp3 , Edit_sp4
Else
Goto Main_loop
End If
Else
'Dialog key cases
'Filtering shown for backspace & CR only - also Max_len
Temp1 = Len(key$)
If Key_val <> 13 Then 'CR terminates dialog in example
If Key_val = 08 Then ' and falls thru to Key_return
Temp1 = Temp1 - 1
If Temp1 < 0 Then Temp1 = 0
Key$ = Left(key$ , Temp1)
Elseif Temp1 < Key_len Then
Key$ = Key$ + Chr(key_val)
End If
Print Key$;
'Locate Y , X : Lcd Key$
Goto Main_loop
Elseif Temp1 = 0 Then
Goto Main_loop
End If
End If
'Goto Key_return
'-- End of code that could be included in the main loop - would need immediately
' above line additionally
Key_return:
Print "CR"
Pad_active = 0
Return
End Sub
Edit_sp1:
'LCD prompt for operator 'Enter new Setpoint Routine
Print "SP1"
Key_rtn 'Returns with an Enter Key
Set_pt1 = Val(key$) 'Makes new value effective
' when timer is loaded next
'Wasn't Called so can 'Jump' to highest priority task
Goto Main_loop:
'We return at the highest priority level ignoring the tasks below this one
' but know that they will be reached on the next pass, now that the Io_flag
' is reset. *Except* if the interval set between scans is not realistic: eg
' if the Control routines take 50mS and the Io_flag is set every 40mS then
' all other functions will be blocked out. Timing the different functions in
' the Main_loop and allocating realistic intervals is not a difficult task.
Timer_0_int:
Counter0 = 2ms
Start Timer0
Set Io_flag
Inc Timer_tics
If Timer_tics => 1 Then '01 and fast to allow scoping
Timer_tics = 0 '05/50 = 10mS/100mS more typical
For Isr_temp = 1 To 4
If Timers(isr_temp) <> 0 Then Decr Timers(isr_temp)
Next
Xlate_col:
Data 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 1 , 1 , 2 , 2 , 3
'Col# * * * * * * * 0 * * * 1 * 2 3 Posn15not possible
' '*' due multiple keys
Row0:
Data 00 , 49 , 50 , 51 'F0 1 2 3
Row1:
Data 01 , 52 , 53 , 54 'F1 4 5 6
Row2:
Data 02 , 55 , 56 , 57 'F2 7 8 9
Row3:
Data 03 , 08 , 48 , 13 'F3 BS 0 CR
'Above lookup tables assume following relationship between port bits and keyboard
'Rows/Columns:
'Port Bits: 7 6 5 4 3 2 1 0
'Rows Top to Bottom: 0 1 2 3
'Columns Left to Right 0 1 2(3)
'Code assumes Port.0 bit to be high when 4x3 pad
'Xlate_col shown for leftmost column taking priority when multiple keys on same
' row are depressed.
'Multiple keys on same column are resolved by the scanning - it halts on first
' key scanned, going down.
'Good for 3 or 4 column key pads - extend Xlate_col with 7'0s'to avoid bad data
' with multiple key cases
$romstart = &H4000
$ramstart = &HC000
$lcd = &H2000
$map
Config Lcd = 20 * 2
Config Debounce = 25 ' Set Key Pad Debounce to 40 Ms
Matrix_input:
Gotkey:
Key = Keyread
Return
' everytime TIMER0 overflows it generates an interrupt and jumps to this place
Timer_0_int:
Incr Count ' increment count
Return
' This table holds the number of available characters for each key (0...9)
Keylength:
Data 2 , 7 , 4 , 4 , 4 , 4 , 4 , 5 , 4 , 5
' This table holds the characters available for each key (0...9)
' If you changes this table than you must change the keylength-table too.
Keydata:
Data " " , "0"
'foprogi
'*******************************************************************************************
'compiler opciok
'*******************************************************************************************
'Hardware
'int0 IR sensor
'if there is an infra signal then the pin is 1
'*******************************************************************************************
'konfig
'*******************************************************************************************
Th0 = 0
Set Tcon.0 'edge trig
Set Tcon.2
On Timer0 Timer_0_int
On Int0 Int0_int
'*******************************************************************************************
'valtozo definiciok
'*******************************************************************************************
Triac Alias P1.0
Led Alias P3.3
New_ir_command = 0
Ide:
If New_ir_command = 0 Then Goto Ide
Infra_count_old = 0
Segw1 = Infra_command
Print N ; " Number of received bit "
Print "Received command ";
Printhex Segw1
Goto Ide
'*******************************************************************************************
'* int rutinok
'*******************************************************************************************
'****************************************
'* timer0 rutin
'****************************************
Rem The Interrupt Handler For The Timer0 Interrupt
Rem .27.. ms each irq
Timer_0_int:
If Infra_count < 150 Then
Incr Infra_count
New_ir_command = 0
Else
New_ir_command = 0
If Infra_count_old <> 0 Then
New_ir_command = 1
End If
End If
Timer_0_int_end:
Return
'****************************************
'* int0 rutin
'****************************************
Rem The Interrupt Handler For The INT0 Interrupt
Rem infra falling edge trigger the irq
Int0_int:
If Infra_count = 150 Then
Infra_count = 0 'start receive
(Infra_count=0)
New_ir_command = 0
Infra_count_old = 0
Infra_command = 0
N = 0
End If
Incr N
Int0_int_end:
Return
'------------------------------------------------------------------------------
' Program Name : Lm75Full.bas
' Program Date : October 15,1999
' Program Written By : M. Akers Enterprises
' Michael W. Akers
' 3800 Vineyard Ave. #E
' Pleasanton, California 94566
' Voice: +1 925 484 4750
' Email: mwakers@home.com
' Program Purpose : This program will demonstrate how to interface to,
' and communicate With The National Semiconductor LM75
' Digital Temperature Sensor.
' Target Processor : Atmel 89C52
'------------------------------------------------------------------------------
' Programmer Date Comments
' ------------------- ---------- --------------------------------------------
' Michael Akers 10/15/99 Initial creation of program.
' -----------------------------------------------------------------------------
' Define the processor (the regfile goes here!)
$regfile = "8052.DAT"
' Define all meta-commands that must beinserted before all other commands.
' Define all subroutines
Declare Sub Readlm75(lm75addr As Byte)
Declare Sub Getconfig(lm75addr As Byte)
Declare Sub Setconfig(lm75addr As Byte)
Declare Sub Gettos(lm75addr As Byte)
Declare Sub Settos(lm75addr As Byte)
Declare Sub Gethyst(lm75addr As Byte)
Declare Sub Sethyst(lm75addr As Byte)
Declare Sub Setaddr(lm75addr As Byte , Flagrw As Byte)
Declare Sub Resetpointer()
Declare Sub Val2temp(lm75tmphi As Byte , Lm75tmplo As Byte , Lm75tmpsign As Bit)
' Define all variables and constants
Dim Lm75read As Byte ' Read address base
Dim Lm75write As Byte ' Write address base
Dim Lm75addr As Byte ' Lm75 Address
Dim Lm75high As Byte ' Lm75 Temperature High
Byte
Dim Lm75low As Byte ' Lm75 Temperature Low Byte
Dim Lm75sign As Bit ' Lm75 Temperature Sign Bit
Dim Lm75config As Byte ' Lm75 Configuration
Dim Lm75toshi As Byte ' Lm75 Tos high byte
Dim Lm75toslo As Byte ' Lm75 Tos low byte
Dim Lm75tossign As Bit ' Lm75 Tos sign bit
Dim Lm75thysthi As Byte ' Lm75 Thyst high byte
Dim Lm75thystlo As Byte ' Lm75 Thyst low byte
Dim Lm75thystsign As Bit ' Lm75 Thyst sign bit
Dim Flagrw As Byte ' Read/Write Flag
Dim Lm75tmphi As Byte ' Lm75 Temp hi byte
Dim Lm75tmplo As Byte ' Lm75 Temp lo byte
Dim Lm75tmpsign As Bit ' Lm75 Temp sign bit
' Define all Configurations and Pin assignments
Config Sda = P1.0
Config Scl = P1.1
Config I2cdelay = 1
Config Lcd = 40 * 4
Config Lcdpin , Db4 = P1.4 , Db5 = P1.5 , Db6 = P1.6 , Db7 = P1.7 , E = P1.3 , Rs =
P1.2
' Initialize variables as needed.
Lm75read = &B10010001
Lm75write = &B10010000
Goto Start
'Program end.
' Begin subroutine section.
Sub Readlm75(lm75addr)
Call Setaddr Lm75addr , 0
I2cstart ' Start the I2C process.
I2cwbyte Lm75read ' Send the LM75 address and
read Info.
I2crbyte Lm75high , 8 ' Get 8 bits and get ACK
from LM75
I2crbyte Lm75low , 9 ' Get 8 bits and send NACK
to LM75
I2cstop ' Stop the I2C system
Call Val2temp Lm75high , Lm75low , Lm75sign ' Convert the value (if
needed)
Lm75high = Lm75tmphi
Lm75low = Lm75tmplo
Lm75sign = Lm75tmpsign
Lm75read = &B10010001 ' Reset Lm75read
End Sub
Sub Getconfig(lm75addr)
Call Setaddr Lm75addr , 2
I2cstart
I2cwbyte Lm75write ' Send write address
I2cwbyte &B00000001 , 8 ' Set pointer register to
point at the configuration register.
I2cstart ' Restart I2C
I2cwbyte Lm75read ' Send read address
I2crbyte Lm75config , 9 ' Read the config register
I2cstop
Resetpointer ' Reset the pointer to '00'
End Sub
Sub Setconfig(lm75addr)
Call Setaddr Lm75addr , 1
I2cstart ' Start I2C
I2cwbyte Lm75write ' Send write address
I2cwbyte &B00000001 , 8 ' Set Pointer to point to
Configuration register
I2cwbyte Lm75config ' Send the Lm75config byte
to write
I2cstop
Resetpointer ' Reset the pointer to '00'
End Sub
Sub Gettos(lm75addr)
Call Setaddr Lm75addr , 2
I2cstart
I2cwbyte Lm75write ' Send write address
I2cwbyte &B00000011 , 8 ' Set Pointer to Tos
I2cstart
I2cwbyte Lm75read ' Send read address
I2crbyte Lm75toshi , 8 ' Read Tos high byte
I2crbyte Lm75toslo , 9 ' Read Tos low byte
I2cstop
Resetpointer ' Reset the pointer to '00'
End Sub
Sub Settos(lm75addr)
Incr Lm75tmphi
Lm75tmpsign = 1
End If
Lm75tmplo = Lm75tmplo And &B10000000
If Lm75tmplo = &B10000000 Then
Lm75tmplo = 5
End If
End Sub
Sub Temp2val(lm75tmphi , Lm75tmplo , Lm75tmpsign)
' This routine will convert a temperature value into the prober hi and lo byte values.
If Lm75tmpsign = 1 Then
Lm75tmphi = Not Lm75tmphi
Incr Lm75tmphi
End If
If Lm75tmplo <> 0 Then
Lm75tmplo = &B10000000
End If
End Sub
' 32 Way Scanner and Potsag Pager Interface MKG 08/06/98 Bascom 8052 Compiler for
89S8252
' Reads PCF8574s on I2C Bus. Inputs to these are active LOW via Opto Couplers
' Inputs are latched until cancelled at source. Change Ports and remove Watchdod if other
CPU used
' Should be easy to alter if you require momentery inputs, Scan routine is fast enough.
' Configure for what Serial O/P you need, i.e Moving Message display/Terminal.
StartUp:
Config WatchDog = 2048
Start WatchDog
Config SCL = P2.0 ' I2C Bus Pin Assignments
Config SDA = P2.1
Config Lcd = 16 * 2
WaitMs 50 ' Allow Display time to
Initialise
$Baud = 300 ' Output to paging TX
$Crystal = 12000000
Cursor OFF
Cls
Lcd "B M Electronics" ' Start Up Message, What you
like
Dim Check As Bit ' Check for Call
Dim DelayTime As Byte ' Used in Loop for Time Between
Calls
Dim AlarmBlips As Byte ' Counter for No of Bleeps on
Sounders
Dim I2cAddress As Byte ' Address to Read
Dim I2cRead As Byte ' Data from PCF8574
Dim I2cStage As Byte ' Individual PCF8574 Location
Dim RealNo As Byte ' Actual Position Number
Dim Result As Byte ' Result of Input Present Test
Dim Message As String * 16 ' Displayed Pager/Lcd Message
StatusLed Alias P0.0 ' Inverted 1=OFF 0=ON
Sounder Alias P2.2 ' True 1=ON 0=OFF
BackLight Alias P2.3 ' True
Relay Alias P2.4 ' True
Sounder = 0 ' Sounder OFF
Relay = 0 ' Relay OFF (If Used)
BackLight = 0 ' Back Light OFF
Check = 0 ' Reset Call Check Bit for
Immediate Page
StatusLed = 1 ' OFF
MainLoop:
Gosub ScanInputs
Reset WatchDog
StatusLed = 0 ' Status Led ON
WaitMs 10 ' Led Flash Time (Once Per Scan
of 32 Points)
StatusLed = 1 ' Status Led OFF - Disable if
faster scan needed
WaitMs 150
If Check = 1 Then : Gosub DelayIt : Goto StartUp : End If
Goto MainLoop
End
Display:
Reset WatchDog
BackLight = 1 ' Display Back Light ON
Check = 1 ' Enables Loop Delay Counter
StatusLed = 0 ' Flashes Led
Message = LookupStr(RealNo , List)
Cls
Lcd "Number Calling.." ' Print to Lcd
Lowerline
Lcd Message ' Alarm message to Lcd
Reset WatchDog
Print "L0012000A" ; Message ' TX Message to Pagers Or other
serial device
Gosub Alarm
Return
End
Alarm:
For AlarmBlips = 0 to 5 ' Modify for faster scan
Reset WatchDog
Sounder = 1 ' Alarm ON
Wait 1
Reset WatchDog
Sounder = 0 ' Alarm OFF
Wait 1
Next AlarmBlips
Return
End
Return
End
List:
Data "One " , "Two " , "Three " , "Four " '
Change these for whatever
Data "Five " , "Six " , "Seven " , "Eight "
Data "Nine " , "Ten " , "Eleven " , "Twelve "
Data "Thirteen " , "Fourteen " , "Fifteen " , "Sixteen "
Data "Seventeen " , "Eighteen " , "Nineteen " , "Twenty "
Data "Twenty One " , "Twenty Two " , "Twenty Three " , "Twenty Four "
Data "Twenty Five " , "Twenty Six " , "Twenty Seven " , "Twenty Eight "
Data "Twenty Nine " , "Thirty " , "Thirty One " , "Thirty Two "
End
'************************************************************************
'*** ***
'*** FLM.BAS is the sorce for a Flow Meter using the 80C552 ***
'*** ***
'*** Started on 15-12-1998 by Mike Crean, Email autlec@nw.com.au ***
'*** ***
'*** This code will run on a 80C552 Micro with 64K bit of ROM and ***
'*** 64K bit of non volatile RAM with RTC ***
'*** ***
'*** The sensor for this flow meter was made from a turbine (fan) ***
'*** with 2 * magnets mounted on it seperated by 180deg's. & ***
'*** opposite polarity's out. A Hall sensor that switches on with ***
'*** one polarity and of with the other was mounted on the out side ***
'*** of a pice of polly pipe. This signal is then fed to a 4018 chip ***
'*** running in / by 10 mode, it's out put is sent to P4.0 as the ***
'*** input signal for the 80C552. ***
'*** ***
'*** If this setup was to be used in another application it would be ***
'*** wise to use / switches on the 4018 to allow for more ***
'*** calibration. ***
'*** ***
'*** I have sent this to Mark Alberts C/O MCS Electronics. As the ***
'*** code listed may give some helpfull ideas to some of the many ***
'*** Bascomlt user's out there. This code is far from complete or ***
'*** bug free, so I hope that Mark will place it on his web site. ***
'*** ***
'*** Ps. The flow rate is output on the LCD or to a terminal when ***
'*** requested. Four buttons allow you to scroll menu's on the LCD ***
'*** ***
'************************************************************************
$large
$baud = 9600
$crystal = 11059200
Config Lcd = 16 * 2
Dim W1 As Integer , W2 As Integer , W3 As Integer , W4 As Integer
Dim W5 As Integer , Cal As Integer , Adc As Integer , Ad As Integer
Dim Rtc As Byte , Rx As Byte , Ck As Byte , Ckgfm As Byte , Min As Byte
Dim Gf As Long
Ck = 0 : Ckgfm = 0 : Gf = 0 : Cal = 0 : W1 = 0 : W2 = 0 : W3 = 0
W4 = 0 : W5 = 0 : Adc = 0 : Min = 0 : Ad = 8299
Cursor Off
Out 16376 , 0 : Out 16377 , 0
Cal = Inp(8194)
If Cal = 0 Then
Cal = 1
End If
Print : Print
Print "; = Version"
Print "f = Flow in m3"
Startone:
W4 = Inp(8202)
Gf = W4 * 255
W3 = Inp(8201)
Gf = Gf + W3
Gf = Gf * 255
W2 = Inp(8200)
Gf = Gf + W2
Start:
Cls
Locate 1 , 1 : Lcd " Flow Meter"
Locate 2 , 1 : Lcd " fL m3"
Locate 2 , 5 : Lcd Gf
Do
If Ck = 0 Then
Getgasreading:
Min = Inp(16378)
If Min = 48 Then
Goto Skipone
End If
Skipone:
If Min = 48 Then
If Ck = 0 Then
Gosub Saveit
End If
End If
If Min = 1 Then
If Ck = 0 Then
Gosub Saveit
End If
End If
If P4.0 = 1 Then
If Ckgfm = 0 Then
W1 = W1 + 1
Ckgfm = 1
End If
End If
If P4.0 = 0 Then
If Ckgfm = 1 Then
Ckgfm = 2
End If
End If
If P4.0 = 1 Then
If Ckgfm = 2 Then
Ckgfm = 0
End If
End If
If W2 = 100 Then
If Ck = 0 Then
Locate 2 , 5 : Lcd " "
End If
End If
If Ck = 0 Then
Locate 2 , 5 : Lcd Gf
End If
W1 = 0
W2 = W2 + 1
Gf = Gf + 1
Out 8200 , W2
If Gf = 16581375 Then
W1 = 0 : W2 = 0 : W3 = 0 : W4 = 0 : Gf = 0
Out 8200 , 0 : Out 8201 , 0 : Out 8202 , 0
End If
End If
If W2 = 255 Then
W1 = 0 : W2 = 0
W3 = W3 + 1
Out 8201 , W3
End If
If W3 = 255 Then
W1 = 0 : W2 = 0 : W3 = 0
W4 = W4 + 1
Out 8202 , W4
End If
If W4 = 255 Then
W1 = 0 : W2 = 0 : W3 = 0 : W4 = 0 : Gf = 0
Out 8200 , 0 : Out 8201 , 0 : Out 8202 , 0
End If
End If
Rx = Inkey
If Rx = 102 Then
Print ":" ; Gf ; " fL-m3"
End If
If Rx = 59 Then
Print : Print ":Flow Meter V1.2"
Print ":Copyright (c) Autlec Perth Aust. 1998"
End If
If Rx = 91 Then
Goto Sendtime
End If
If Rx = 92 Then
Cls : Locate 1 , 3 : Lcd "PC Download"
Locate 2 , 3 : Lcd "Add. "
Print ":Start Download" : Print : Print ":";
Adc = 0
If P4.7 = 0 Then
If Ck = 0 Then
Ck = 1
Waitms 250
End If
End If
If P4.7 = 0 Then
If Ck = 100 Then
Ck = 2
Waitms 250
End If
End If
If P4.7 = 0 Then
If Ck = 101 Then
Ck = 3
Waitms 250
End If
End If
If P4.7 = 0 Then
If Ck = 102 Then
Ck = 4
Waitms 250
End If
End If
If P4.7 = 0 Then
If Ck = 103 Then
Ck = 5
Waitms 250
End If
End If
If P4.7 = 0 Then
If Ck = 104 Then
Ck = 6
Waitms 250
End If
End If
If P4.7 = 0 Then
If Ck = 105 Then
Ck = 7
Waitms 250
End If
End If
If P4.7 = 0 Then
If Ck = 107 Then
Ck = 0
Waitms 250
Goto Start
End If
End If
If Ck = 1 Then
Cls : Locate 1 , 1 : Lcd "V1.2 Copyright"
Locate 2 , 1 : Lcd "Autlec Perth1998"
Print : Print ":Flow Meter V1.2"
Print ":Copyright (c) Autlec Perth Aust. 1998"
Ck = 100
Waitms 250
End If
If Ck = 2 Then
Cls : Locate 1 , 1 : Lcd "Dn * 100 +"
Locate 2 , 1 : Lcd "Rb-Up Lb-Dn_Set"
Print ""
Print ":Changing reading m3"
Print ":Right Button Up - Left Button Dn + Down Button * 100"
Ck = 101
Waitms 250
End If
If Ck = 3 Then
Cls : Locate 1 , 1 : Lcd "fL will be ZERO"
Locate 2 , 1 : Lcd "Rb-Do it"
Print : Print ":Flow will be Zeroed"
Print ":Right Button Do it"
Ck = 102
Waitms 250
End If
If Ck = 4 Then
Cls : Locate 1 , 1 : Lcd " Calibrate"
Locate 2 , 1 : Lcd Cal
Print ":Calibrate"
Print ":" ; Cal
Ck = 103
Waitms 250
End If
If Ck = 5 Then
Cls : Locate 1 , 1 : Lcd " Line Pressure"
Locate 2 , 7 : Lcd "kPa"
Print ":Line Pressure"
If Ck = 6 Then
Cls : Locate 1 , 1 : Lcd "Line Temperature"
Locate 2 , 6 : Lcd "C"
Print ":Line Temperature"
Print ":c - display"
Ck = 105
Waitms 250
End If
If Ck = 7 Then
Cls : Locate 1 , 1 : Lcd "Time"
Locate 2 , 1 : Lcd "Date"
Print ":Time and Date"
Print ":t - display"
Ck = 107
Waitms 250
End If
If P4.4 = 0 Then
If Gf < 160000099 Then
If Ck = 101 Then
Cls : Locate 1 , 1 : Lcd "Changing Reading"
Print ":Changing Reading"
Gf = Gf + 1
W2 = W2 + 1
Locate 2 , 1 : Lcd Gf
Print ":" ; Gf
Waitms 80
End If
End If
End If
If P4.5 = 0 Then
If Gf > 0 Then
If Ck = 101 Then
Cls : Locate 1 , 1 : Lcd "Changing Reading"
Print ":Changing Reading"
Gf = Gf - 1
W2 = W2 - 1
Locate 2 , 1 : Lcd Gf
Print ":" ; Gf
Waitms 80
End If
End If
End If
If P4.6 = 0 Then
If P4.4 = 0 Then
If Gf < 160000099 Then
If Ck = 101 Then
Cls : Locate 1 , 1 : Lcd "Changing Reading"
Print ":Changing Reading"
Gf = Gf + 100
W2 = W2 + 100
Locate 2 , 1 : Lcd Gf
Print ":" ; Gf
Waitms 10
End If
End If
End If
End If
If P4.6 = 0 Then
If P4.5 = 0 Then
If Gf > 100 Then
If Ck = 101 Then
Cls : Locate 1 , 1 : Lcd "Changing Reading"
Print ":Changing Reading"
Gf = Gf - 100
W2 = W2 - 100
Locate 2 , 1 : Lcd Gf
Print ":" ; Gf
Waitms 10
End If
End If
End If
End If
If P4.4 = 0 Then
If Ck = 102 Then
Goto Zero
End If
End If
If P4.4 = 0 Then
If Ck = 103 Then
Locate 2 , 1 : Lcd " "
If P4.5 = 0 Then
If Ck = 103 Then
Locate 2 , 1 : Lcd " "
If Ck = 104 Then
Adc = 0
Gosub Adconvert
Adc = Inp(16192)
Adc = Adc - 55
If Rx = 107 Then
Print ":" ; Adc ; "kPa"
End If
End If
Waitms 50
End If
If Ck = 105 Then
Adc = 0
Gosub Adconvert
Adc = Inp(16193)
Locate 2 , 1 : Lcd " "
Locate 2 , 2 : Lcd Adc
If Rx = 99 Then
Print ":" ; Adc ; "C"
End If
Waitms 50
End If
If Ck = 107 Then
Sendtime:
If Rx = 116 Then
Print ":Time " ; Bcd(rtc) ; ":";
End If
If Rx = 116 Then
Print Bcd(rtc) ; ":";
End If
If Rx = 116 Then
Print Bcd(rtc)
End If
If Rx = 116 Then
Print ":Date " ; Bcd(rtc) ; "/";
End If
If Rx = 116 Then
Print Bcd(rtc) ; "/";
End If
If Rx = 116 Then
Print Bcd(rtc)
Rx = 0 : Ck = 0
Goto Start
End If
Waitms 20
End If
If P4.7 = 0 Then
If P4.4 = 0 Then
Ck = 0
Waitms 250
Goto Start
End If
End If
End
Zero:
If Ck = 102 Then
Ck = 0 : Goto Startone
End If
Return
Saveit:
Ck = 200
Ad = Ad + 1 : Out Ad , Min
Min = Inp(16379)
Ad = Ad + 1 : Out Ad , Min
Ad = Ad + 1 : Out Ad , W2
Ad = Ad + 1 : Out Ad , W3
Ad = Ad + 1 : Out Ad , W4
Return
Adconvert:
MOV DPTR,#16192
MOV R1,#0
!Loopit:
MOV A,R1
MOV ADCON,A
ADD A,#40
MOV ADCON,A
!ADC1:
MOV A,ADCON
JNB ACC.4,ADC1
MOV A,ADCH
MOVX @DPTR,A
PUSH ACC
!SWAP A
POP ACC
INC R1
INC DPTR
CJNE R1,#8,Loopit
Return
End
'-----------------------------------------------------------------
' Copyright 1997 MCS Electronics
' this example uses library version 1.08 [14 sept 1997]
'-----------------------------------------------------------------
'Application Note 3 : controlling the X9CMME
'The X9CMME is a digital potentiometer chip From Xicor http://www.xicor.com
'
'Chip pin Function
'1 /INC increment input
'2 U/D up/down input
'3 VH potmeter high terminal
'4 VSS Ground
'5 VW potmeter wiper terminal
'6 VL potmeter low terminal
'7 /CS chip select/enable
'8 VCC power (+5V)
'The chip used is the X9C103
'The chip can be used to dim a lights or a VCO or ....
'-----------------------------------------------------------------
Do
Set Ud 'count up
Set Up 'set flag too
Pulse = 100 '100 pulses maximum
Gosub Sendpulse 'send pulses
'we must use a bit variable to test the direction since we can only examine
'the state of the Px.x flag if it is used as an input and we are using it as an output.
If Up = 0 Then
Lcd "Counting down" 'display direction
Else
Lcd "Counting Up"
End If
$crystal = 11059200
$iramstart = &H60
$ramstart = 0
$ramsize = &H7FFF
$default Xram
$large
Cursor Off
Display On
Cls
Deflcdchar 1 , 224 , 224 , 224 , 240 , 248 , 252 , 254 , 255 ' \ B
Deflcdchar 2 , 224 , 224 , 224 , 255 , 255 , 255 , 255 , 255 ' = O
Deflcdchar 3 , 255 , 255 , 255 , 255 , 254 , 252 , 248 , 240 ' !/ O
Deflcdchar 4 , 255 , 255 , 255 , 255 , 239 , 231 , 227 , 225 ' !\ O
Deflcdchar 5 , 224 , 224 , 224 , 225 , 227 , 231 , 239 , 255 ' / B
Deflcdchar 6 , 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 ' ! N
' SYMBOL ARRAYS : INCLUDE DATA TO DISPLAY BIG DIGITS USING 6 CUSTOM LCD SYMBOLS
' EACH DIGIT EXISTS OF 12 BYTES DISPLAYING EACH ONE OF THE ABOVE DEFLCDCHAR'S
' NUMBER 1
L1(1) = " " + Chr(5) + Chr(2) + " "
L2(1) = " " + " " + Chr(7) + " "
L3(1) = " " + " " + Chr(7) + " "
L4(1) = " " + " " + Chr(7) + " "
' NUMBER 2
L1(2) = Chr(5) + Chr(2) + Chr(1) + " "
L2(2) = Chr(5) + Chr(2) + Chr(3) + " "
L3(2) = Chr(6) + " " + " " + " "
L4(2) = Chr(6) + Chr(2) + Chr(2) + " "
' NUMBER 3
L1(3) = Chr(5) + Chr(2) + Chr(1) + " "
L2(3) = " " + Chr(2) + Chr(6) + " "
L3(3) = Chr(2) + " " + Chr(6) + " "
L4(3) = Chr(4) + Chr(2) + Chr(3) + " "
' NUMBER 4
L1(4) = Chr(5) + " " + Chr(2) + " "
L2(4) = Chr(6) + Chr(2) + Chr(6) + " "
L3(4) = " " + " " + Chr(6) + " "
L4(4) = " " + " " + Chr(6) + " "
' NUMBER 5
L1(5) = Chr(2) + Chr(2) + Chr(2) + " "
Dim E1 As String * 5
Dim E2 As String * 5
Dim E3 As String * 5
Call Rd1820(valu)
E1 = Str(valu)
E2 = Left(e1 , 4)
Print E2
E3 = E2 + ""
Call Showstring(e3)
Loop
' /**** END LOOP ****/
Dim A2 As String * 1
Dim A3 As Byte
Dim A4 As Byte
Dim B1 As String * 20
Dim B2 As String * 20
Dim B3 As String * 20
Dim B4 As String * 20
Select Case A2
Case Is = "1" : A3 = 1
Case Is = "2" : A3 = 2
Case Is = "3" : A3 = 3
Case Is = "4" : A3 = 4
Case Is = "5" : A3 = 5
Case Is = "6" : A3 = 6
Case Is = "7" : A3 = 7
Case Is = "8" : A3 = 8
Case Is = "9" : A3 = 9
Case Is = "0" : A3 = 10
Case Is = "" : A3 = 11
Case Is = "C" : A3 = 12
Case Is = "c" : A3 = 12
Case Is = "F" : A3 = 13
Case Is = "f" : A3 = 13
Case Is = "H" : A3 = 14
Case Is = "h" : A3 = 14
Case Is = "X" : A3 = 15
Case Is = "." : A3 = 16
Case Else : A3 = 17
End Select
B1 = B1 + L1(a3)
B2 = B2 + L2(a3)
B3 = B3 + L3(a3)
B4 = B4 + L4(a3)
Next A1
Upperline
Lcd B1
Lowerline
Lcd B2
Thirdline
Lcd B3
Fourthline
Lcd B4
End Sub
' ----------------------------------------------------------------
'Author : Bojan Ivancic
' Email : nicrodesign@siol.net
' Example measuring temperature using Dallas DS1820,
' with calculated for 0.1 C resolution and with 8-bit CRC!
' ----------------------------------------------------------------
do
Temperature
waitms 250
loop
End
'//////////////////////////////////////////////////////////////////////////////
sub Temperature ' actual measuring
'//////////////////////////////////////////////////////////////////////////////
sub Read1820 ' reads sensor ans calculate
' T for 0.1 C
1wreset ' reset the bus
1wwrite &hcc ' read internal RAM
1wwrite &Hbe ' read 9 data bytest
bd(1) = 1wread(9) ' read bytes in array
1wreset ' reset the bus
'//////////////////////////////////////////////////////////////////////////////
sub CRCit ' calculate 8 bit CRC
' bigger but faster
CRC = 0 ' needs a 256 elements table
for i = 1 to 9
tmp = CRC xor bd(i)
CRC = lookup(tmp , crc8)
next
end sub
'//////////////////////////////////////////////////////////////////////////////
crc8:
data 0 , 94 , 188 , 226 , 97 , 63 , 221 , 131 , 194 , 156
data 126 , 32 , 163 , 253 , 31 , 65 , 157 , 195 , 33 , 127
data 252 , 162 , 64 , 30 , 95 , 1 , 227 , 189 , 62 , 96
data 130 , 220 , 35 , 125 , 159 , 193 , 66 , 28 , 254 , 160
data 225 , 191 , 93 , 3 , 128 , 222 , 60 , 98 , 190 , 224
data 2 , 92 , 223 , 129 , 99 , 61 , 124 , 34 , 192 , 158
data 29 , 67 , 161 , 255 , 70 , 24 , 250 , 164 , 39 , 121
data 155 , 197 , 132 , 218 , 56 , 102 , 229 , 187 , 89 , 7
data 219 , 133 , 103 , 57 , 186 , 228 , 6 , 88 , 25 , 71
data 165 , 251 , 120 , 38 , 196 , 154 , 101 , 59 , 217 , 135
data 4 , 90 , 184 , 230 , 167 , 249 , 27 , 69 , 198 , 152
data 122 , 36 , 248 , 166 , 68 , 26 , 153 , 199 , 37 , 123
data 58 , 100 , 134 , 216 , 91 , 5 , 231 , 185 , 140 , 210
data 48 , 110 , 237 , 179 , 81 , 15 , 78 , 16 , 242 , 172
data 47 , 113 , 147 , 205 , 17 , 79 , 173 , 243 , 112 , 46
data 204 , 146 , 211 , 141 , 111 , 49 , 178 , 236 , 14 , 80
data 175 , 241 , 19 , 77 , 206 , 144 , 114 , 44 , 109 , 51
data 209 , 143 , 12 , 82 , 176 , 238 , 50 , 108 , 142 , 208
data 83 , 13 , 239 , 177 , 240 , 174 , 76 , 18 , 145 , 207
data 45 , 115 , 202 , 148 , 118 , 40 , 171 , 245 , 23 , 73
data 8 , 86 , 180 , 234 , 105 , 55 , 213 , 139 , 87 , 9
data 235 , 181 , 54 , 104 , 138 , 212 , 149 , 203 , 41 , 119
data 244 , 170 , 72 , 22 , 233 , 183 , 85 , 11 , 136 , 214
data 52 , 106 , 43 , 117 , 151 , 201 , 74 , 20 , 246 , 168
data 116 , 42 , 200 , 150 , 21 , 75 , 169 , 247 , 182 , 232
data 10 , 84 , 215 , 137 , 107 , 53