Вы находитесь на странице: 1из 185

..

MicroCode Studio -
BASIC.

PicBasic Pro.

2007

7
8

1. PIC-




/

10
10
12
14
14

2. PicBasicPro
(IDE) - Microcode Studio Plus.

(ICD)
MicroCode Loader
(The Serial Communicator)
Easy HID Wizard
PicBasicPro


ABS
COS
DCD
DIG
DIV32
MAX MIN
NCD
REV
SIN

17

16

18
31
34
34
36
36
36
36
37
37
38
39
40
40
41
42
44
44
45
45
45
46
46
46
47
48
48
48

SQR



49
49
49
50
51

3.
1 (Hello Word)
2.
3.
4. -
5.
6.
7. 7

8.
9.
10. 12
11.

52
52
54
58
60
64
66

4. PicBasicPro
@
ADCIN
ASMENDASM
BRANCH
BRANCHL
BUTTON
CALL
CLEAR
CLEARWDT
COUNT
DATA
DEBUG
DEBUGIN
DISABLE
DISABLE DEBUG
DISABLE INTERRUPT
DTMFOUT
EEPROM
ENABLE
ENABLE DEBUG
ENABLE INTERRUPT
END
4

71
74
76
79
83
86
88
89
89
90
90
91
97
97
97
97
98
99
100
100
101
101
101
102
103
103
103
104

ERASECODE
FORNEXT
FREQOUT
GOSUB
GOTO
HIGH
HPWM
HSERIN
HSEROUT
HSERIN2
HSEROUT2
I2CREAD
I2CWRITE
IFTHEN
INPUT
LET
LCDIN
LCDOUT
LOOKDOWN
LOOKDOWN2
LOOKUP
LOOKUP2
LOW
NAP
ON INTERRUPT
OUTPUT
OWIN
OWOUT
PAUSE
PAUSEUS
PEEK
PEEKCODE
POKE
POKECODE
POT
PULSIN
PULSOUT
PWM
RANDOM
RCTIME
READ
READCODE
REPEATUNTIL

104
104
105
106
107
107
108
109
112
113
114
116
118
121
121
122
122
123
125
126
127
128
128
129
130
131
131
132
133
133
135
135
135
136
136
138
138
139
140
140
141
141
141
5

RESUME
RETURN
REVERSE
SERIN
SELECTCASE
SERIN2
SEROUT
SEROUT2
SHIFTIN
SHIFTOUT
SLEEP
SOUND
STOP
SWAP
TOGGLE
WHILEWEND
USBINIT
USBIN
USBOUT
WRITE
WRITECODE
XIN
XOUT
DEFINE

142
142
142
143
145
145
149
151
155
157
158
159
160
160
160
161
161
161
162
162
163
163
164
166

169

171

1.
Micro-LAN.

172

2. 10

180

INTERNET

185

186


,
,
BASIC. ,
.
.
. ,
, .
.
,
( ).

80 .
.
,
86 ..
Sinkler Z80.
,

- BASIC.
,
. , ,
PIC-.
.
PIC-.
PicBasicPro.

PIC, Basic- .

PicBasicPro.
.

,
.
,
.



, .
, . .,
, .
,
Microchip PIC-
- , BASIC.
, , ,
.
, .
.
. ,
, , ,
. .
,
, .
,
, ,
,
. ,
.
, - . -
( ) , ,
.

,
( ).
, ,
.
, .
, .
, ,
,
,
.
,
.
50
.
.

8

, (, ,
), :
0
= * ;

.
,
,
:
,
, .
, ,
. 2
.
- , ,
, ( )

, .
BASIC -
. , BASIC c
*.bas,
- *.asm.

- *.hex .
.
- ,
.

, , .
,
. - ,
.
.
.
1.

.1.
.hex .
1. PIC-
Microchip
.
PC.
.
,
. : PIC 12C5xx, PIC 16C5xx, PIC
16C50x. ,
.
14 . : PIC
126xx, PIC 1400, PIC 16C55x, PIC 16C6x(x), PIC 16C7x(x), PIC 16C8x, PIC
16F8x(x), PIC 16C9xx.
, 16 .
: PIC 17Cxx PIC 18Xxx


Handbook of Microcontrollers , -
+5,
,
. .
,
.
VSS VDD.
VSS ( );
VDD .
10

PIC-
0,01 0,1 VSS
VDD. , .

.
PIC- 4,0 - 6,0 ,
2,0 .
, -
.
PIC- C
F L. -
, ,
+5 . , ,
,
. 7805
( 1425), 5.1 .
2 .

. 2. .

R = (U U)/(I +I),
R ,
U ,
U ,
I ,
I .
:
P = (U U)*(I +I).
11

.
, MCLR. PIC
(. 3). ,
( 0,05 /).
, (
), 4.
,
,
.
, Microchip,
. R,
100 1 .
MCLR
.

3. MCLR
.

12

.4. MCLR
.
, , ,
,
, ,
. , Microchip,
5. ,
, VZ+0,7 ,

0,7*(1+R2/R1).
.
(RESET) .
:

, .
.
1. ,
(
) .
2. ,

() .
: ADM709, DS1233,
MAX809, 117147.
,
PIC- 4,5
. , ,
, ,

13

5. MCLR

.
.
, ,
PIC. :
;
;
;
;
PIC16F87x.
PIC.
.
( ),
. PIC (TCY) .
PIC-
.

:

;
RC-;
;
;
.


PIC-. ,
.
.
RC-.
6. .
,
N.
Microchip. ,
14

2.2
.
1 ,
.
3 100 .
(=0).
20 .
.

6. PIC

.
, 6..

.
Microchip.
,

.

PIC 2
.
PIC-

2.

RC
4
LP
0 200
XT
200 4
HS
4 20 (
15

)
1 2
( ) ( 3).
3.

LP

XT

HS

32*
100
200
100
200
455
1000
2000
4000
4000
8000
20000

1,

2,

15
15
0-15
15-30
15-30
15-30
15-30
15
15
15
15
15

15
15
0-15
200-300
100-200
15-100
15-30
15
15
15
15
15

XT R1 ,
HS.
, R1,
.
, , 6. PIC . ,

. , OSC1,
OSC2 (
), , .
CLCOUT.

/,
.
/ .

,
/.

TRIS (TRI-state buffer enable). TRIS
16

, . , PORTB
TRISB, PORTC TRISC.
1 TRIS
.
0 . ,
, 1 () I
(Input ), 0 () O (Output ). ,
: TRISC=%01100111 , 0, 1, 2, 5, 6
, 3, 4, 7 .
, TRIS
PIC- ,
PORTA, PORTB PORTC,
PORTD PORTE .
BASIC - INPUT OUTPUT.
4 PORTA (RA4) PIC-
.

( ).
, PORTB
(PORTB weak pull-up), _RPBU
OPTION. .
:
Option_Reg.7=0
PORTB

,
, 50 .

.
.
(PIC12C50X, PIC12C67x, 12CE67x 12F675) .
PORTA GPIO - TRISA.
.
2. PicBasic Pro.
BASIC Beginner's all purpose Symbolic Instruction Code (
) 60
. ,
.
1970 ,
,

17

. ALTAIR
BASIC,
. (Bill Gates),
Microsoft, (Paul Allen).
BASIC IBM PC,
QBasic (Quick Basic BASIC)
MS-DOS.
. ,
MS-DOS QBasic Visual Basic for MSDOS, Windows Visual Basic
( 6- ). Microsoft
Visual Basic for Applications (VBA),
. , Microsoft Word
VBA ,
, -. VBA
, , ,
. ,
BASIC QBasic,
.
PIC- 1992 - 1993 Parallax
BasicStamp. ,
BASIC Parallax, ,
, . , ,
, -,
PIC- .
,
, , .
microEngineering Labs. Inc
PicBasic PicBasicPro. Basic PROTON+
Crownhill.
BasicStamp BS. ,
, BasicStamp,
. , , 1998 microBasic mikroElektronika,
.
(IDE) - Microcode Studio Plus.
.
,
,
. ,
. ,
.
.
18

Microcode Studio Plus.


: www.melabs.com www.mecanique.co.uk
MicroCode Studio, .
- Microcode Studio Plus. -
PICBasic Standard
PICBasic PRO. MicroCode
Studio Microcode Studio Plus ,
31 ( ,
). , , ,
, .
PIC: 16F84 (A), 16F627 (A), 16F628 (A), 16F870, 16F871, 16F872, 16F873
(A), 16F874 (A), 16F876 (A) 16F877 (A),
Microchip.

INCLUDE.
Microcode Studio Plus
PIC-.
,
,
,
bootloader.
,
COM ,
. On Line
Internet.
Windows 98,
98SE, ME, NT 4.0 SP 6, 2000 XP ().
:
233 ( 500 ),
64 ( 128 ),
40 ,
16 .
MCSP_2300,
setup.exe.
,
. Next, ,
, , .
, Yes, , No.
,

C:\Program
Files\Mecanique\MCSP. ,
Next.
19

,
.
- MicroCode Studio Plus. Next.
.
, , .
, Finish.
,
. . 7.

. 7. Microcode Studio Plus


,
5 : File, Edit, View, Project Help.
.
: ,
, , ICD .
.
, .
.
:
File

20

New - .
, ,
.

16F877. ,
'default.ini'. ,
.
.
Open - ,
.
Save - . ,
.
Save As ,

.
Close - .
Close All - , ,
.
Reopen - .
Print Setup - .
Print Preview - ,
.
Print
.
Exit - .
Edit
Undo -
.
Redo - Undo.
Cut -
. ,
.
Copy -
. , .
Paste -
. ,
.
21

Delete - . ,
.
Select All - .
Find - .
Replace - .
Find Next - .
,
. ,
.
View
Code Explorer - .
Serial Communicator
RS-232.
EasyHID USB Wizard
USB.
Loader - MicroCode Loader.
Loader Options -
.
Compile and Program Options
.
Editor Options- .
Toolbars - .
, .
Online Updates- Online.
Project
Compile ,
.
Compile and Program -
,
,

.
Program - .

22

ICD Compile -
,
.
ICD Compile and Program - ,

.
Help
Help Topics .
Loader Help Topics -
Online Form - PICBASIC.
About -
MicroCode Studio PICBasic.
:
Find- . Ctrl+F
Replace- . Ctrl+F
Indent - .
, ,
.
Outdent -
. , ,
.
Block Comment -
.
, .
Block Uncomment -
. ,
, .
Compile Only- .
F9.

23

Compile and Program-


. F10.
Target Processor
ICD Compile
,
.
,
. :
, .
, *.hex
, ICD.
. Ctrl+F9
ICD Compile and Program


. Ctrl+F10
ICD Control -
, , .
, ,
ICD Compile ICD Compile and Program. F5,
F6, F7, F8
ICD Communications Port
COM ICD.
ICD Breakpoint - . ,
,
. ,
, ,
. ,
. ,
.
.
ICD Animate
. ,
24

, ,
. ICD
, , .
PAUSE , .
F2
MicroCode Studio Plus
- ICD (In Circuit Debugger).
.
, .
ICD ,
()
. ,
.
, Pentium 90 8
, 4MHz. ,
10 . ICD .
,
,
.
, , ,
, ,
.
,
.
,
.
,
sort.
,
, .
,
. ,
. , ,
,
.
, , .
View .
Editor Options..
. 4 .
14 . :
Show Line Numbers in Left Gutter -
.
25

Show Right Gutter -


. .
.
Use Smart Tabs - .
Convert Tabs to Spaces - ,
.
Automatically Indent - ,
ENTER
. ,
.
Wrap Document Selection Tabs
. ,
,
..
Automatically Jump to First Error

.
Clear Undo History After Compile

, .
.
Open Last File(s) When Application Starts - ,
, ,
.
Display Full Filename Path in Application Title Bar - , MicroCode
Studio
( , ).
, .
Prompt if File Reload Needed - MicroCode Studio ,
.
, , :
?. ,
.
Auto Change Identifiers - ,
,
. , :
MyIndex VAR BYTE
myindex, MicroCode Studio
myindex MyIndex. ,
, ,
.
,
myindex.
ASM...ENDASM
26

, .
,
, ,
INCLUDE.
Use .PBP as Default Extension - ,
*.pbp,
. , *.bas,
.
Default Source Folder - MicroCode Studio
, Open, Save..as.
.
- Highlighter ( )
, .
(, )
:







, MicroCode Studio
. , ,
,
,
.
- Program Header (
). ,
.
, ,
.
,
, ,
.
View > Compile and Program Options.
.
27

- Compiler, MicroCode Studio


,
Find automatically. .
, PICBasic
PicBasicPro - . ,
(, PBP 2.42, PBP 2.43 ..),
, .. Find manually.
Assembler. ,
MicroCode Studios, PM.EXE. ,
microEngineering Labs PicBasicPro.
.
, MPASM, Microchip.
, MPASM
. , PIC18xxx.
PM.EXE
. MPASM.
MPASM Microchip.
, MicroCode Studio
mpasmwin.exe. , .
:
PICBasicPro.

MPASM

Programmer().
MicroCode Studio
.

. , , MicroCode Studio, .
, Add New Programmer.
, 8.

. 8. .

28

Next.
MicroCode Studio ,
.
. , Compile and
Program ,
, .
, MicroCode Studio
, . ,
, ,
. , Create a
custom programmer entry Next.
, ,
9. . ,
, My New Programmmer.

. 9. .
, Next
.

.10.
.
,
. 10. Next.

29

.11.
11.
Find Automatically,
Find Manually.
, , ,
,
. , , EPICWin

. MicroCode Studio
Compile and Program Program
,
.

.12.
, blink.bas,
16F628, blink.hex,
, .
EPICWin:
-pPIC$target-device$ $hex-filename$
EPICWin ,
.
:
30

-pPIC16F628 blink.hex
(ICD)
,
MicroCode Studio. The MicroCode Studio ICD
. ,
COM ,
, ,
, .
,
,
. 13.
COM MAX232,
.
,
USART.

. 13. MAX232
-
. 14.

31

. 14. COM- .
, MicroCode Studio
16F628.
, MicroCode Studio Plus
: 16F627(A), 16F628(A), 16F73,
16F74, 16F76, 16F77, 16F870, 16F871, 16F873(A), 16F874(A), 16F876(A),
16F877(A), 16F87, 16F88, 18F242, 18F248, 18F252, 18F258, 18F442, 18F448,
18F452, 18F458, 18F1220, 18F1320, 18F2220, 18F2320, 18F4220, 18F4320, 18F6620,
18F6720*, 18F8620 18F8720. , *
64 . ICD
.

. 15. (ICD).
ICD,
.
,
. :\Program
Files\Mecanique\MCSP\MCLoader

:\Program
Files\Mecanique\MCSP\MCLoader\LoaderHEX. ,
32

,
,
. , ,
. ,
,
.
ICD Compile, ICD Compile and Program.
, : Project > ICD Compile
Project > ICD Compile and Program.
, ,
(Please reset the target microcontroller in order to access
the bootloader process..). ,
.
,
. ICD Run.
15.


, ,
, .
, ICD Pause,
.
. ICD Step.
,
.
,
Toggle Breakpoint
.
.
.
. , ,
, . ,
, ,
Properties Breakpoint
( ).
, .
0 , ,
.
- ,
. ,
,
ENTER. ,
, $,
%.

33

MicroCode Loader.
MicroCode Loader
, , ,

. MicroCode Loader
View > Loader.
16.

. 16. MicroCode Loader


, , .
,
. Open;
*.hex . Program;
. Read;
. Verify;
. Erase;
. Run User Code bootloader
. Information

.
.
, - ,
. , ,
, , ,
-.
.
(The Serial Communicator).


34

.
VIEW > SERIAL COMMUNICATOR
F4. 17.

17. .
.
. ,
. .
,

.
(1, 2 3).
(300 115200).
,
() .
(7 8)
. .
CONNECT F9.
, ,
.

, .
, .
,
, .
.

35

Easy HID Wizard


, MicroCode Studio
Plus, Easy HID (Human Interface Device) Wizard.

USB.
, . ,
18F2455, 18F2550, 18F4455
18F4550.
PicBasicPro
PicBasicPro
,
. PBP
, ,
.
-
, ,
,
.
BASIC.
PicBasicPro
, . ,
.
, , ,
.
,
PicBasic
.
.
PICBasic
.
- ,
, .
.
.
, label, LABEL
Label - .
, PICBasic 32 .

36

.
. ,
, .
,
, .
BASIC, .
,
Speed = 60
Speed
60, : 60
/.
,
. ' ( ),
; ( ), REM. ,
,
.
Speed = 60 ' 60 /
:
' -
' REM.
.
, BASIC ,
, , ,
. :
Speed = 60 '
.
.
,
. ,
, . PICBasicPro
BIT, BYTE, WORD. ,

1, 8, 16 .
, . ,
, ,
.

VAR. (BIT), (BYTE)
(WORD).
.
:
37

Label VAR Size,

Label -
, .
. Size (BIT, BYTE WORD).
:
dog VAR byte
cat VAR bit
w0 VAR word

( ), .
BASIC (
VAR), .
.
, ,
.
.
VAR ,
( ) .
.
Label VAR Label1 {.Modifiers},

Label - , .
Label1 .
Modifiers
. 4.
:
fido VAR dog
b0 VAR w0.byte0
b1 VAR w0.byte1
flea VAR dog.0

' fido dog


' b0 - w0
' b1 - w0
' flea dog
4.

BIT0 0

0
38

BIT1 1

BIT2 2

BIT3 3

BIT4 4

BIT5 5

BIT6 6

BIT7 7

BIT8 8

BIT9 9

BIT10 10

10

BIT11 11

11

BIT12 12

12

BIT13 13

13

BIT14 14

14

BIT15 15

15

BYTE0 LOWBYTE
BYTE1 HIGHBYTE
.
, ,
,
,
(, 0,001, ).
,
. ,
,
.
, ,
. , .
,
,
. CON.
porog CON 0.003
39

.
, ,
.
.
PicBasicPro :
, .
%
$. -
.
:
100
%100
$100

'
'
'

PicBasicPro ,
, , ,
. .
:
LCDOUT "Hello"

' Hello


.
.
BasicStamp PicBasicPro SYMBOL.
.
SYMBOL
.
:
SYMBOL lion = cat ' cat
VAR
SYMBOL mouse = 1
' - mouse = 1
.
, ,
,
40

. BASIC, PicBasicPro
. .
. .
LAB:

PRINT "Hello World"


GOTO LAB

32
- , . :
LABEL1:
, :1LABEL:
, - 1.
.
. :THISISALABEL:
:
THIS_IS_A_LABEL:
.
.
(BIT, BYTE WORD),
. ,
- ,
. ,
.
Label VAR Size[Number of elements]

Label - , , .
Size (BIT, BYTE WORD).
Number of elements . :

41

sharks VAR byte[10]


fish VAR bit[8]
- 0. , ,
fish [0] fish [7] 8 .
,
:

5

BIT

256

BYTE

96*

WORD

48*

.
.
PicBasicPro
.
.
, ,
, , .
2.47.
:
16 .
PicBasiPro
6

**

16

*/

16

42

//

()

<<

>>

ABS

COS

DCD

2n

DIG

DIV32

32

MAX

MIN

NCD

REV

SIN

SQR

&

&/

|/

^/

, .
, ,
, :
1. ,
;
2.
.
, (,
2, 2*, 5d, 5*D);
3. ,
.
, ( BASIC
,
43

, . .).
, . . ,
, - ;
4. .
.
.
* 16 32- . , .
** 16 32- .
,
,
32- .
:
W1 = W0 * 1000

' W0 1000 ( 16

) W1
W2 = W0 ** 1000 ' W0 1000 ( 16
, 0) W2
- * / 16 32- .
W3 = W1 */ W0 ' W1 W0 ( 16 32
) W3
.
/ 16- 16 16- .
// 16- . .
:
W1 = W0 / 1000 W0 1000 W1
W2 = W0 // 1000 W0 1000 W2

44

.
<< >>
, .. 0 15.
:
B0 = B0 << 3 ' B0 3
( 8)
W1 = W0 >> 1 ' W0 1
W1 ( 2),
, , . 1000
1111101000, 1000<<3
1111101000000 8000 (1000*8), 1000>>2 11111010
250(1000/4).
ABS.
ABS .
127 ( =1),
ABS = 256 - .
32767
( =1), ABS = 65536 .
:
B0 VAR BYTE
B0 = 200
B1 = ABS B0

50 (255-200)
COS.

COS 8- .
. ,
.
0, ,
(
ABS). PicBasicPro
.
PicBasicPro COS 255 ,
360 .
45

*255/360 - .
+127 -127 (
+1 -1).
:
B1 = COS B0
DCD.
DCD ,
, 0
, .
0 15.
:
B0 = DCD 2 ' B0 %00000100
DIG.
DIG .
(0 - 4 0 )
.
:
B0 = 123
' B0 123
B1 = B0 DIG 1
' B1
B0 (.. 2)
DIV32.
:
DIV32 Number
* PicBasicPro 16 ,
32-
. ,
, 16 ,
. *,
16 , **,
16 . ,
32- .
32-
16- ,
.
46

, DIV32. DIV32
31- (
2147483647) 15- (
32767). 16 , DIV32
,
32- .
Div32, , ,
, 32-
. , ,

DIV32.
,
DIV32.
:
a VAR WORD
b VAR WORD
c VAR WORD
dummy VAR WORD
b = 500
c = 1000
DISABLE
' ,
dummy = b * c ' ** */
a = DIV32 100
ENABLE
'
b 500, c 1000. , 500000.
PicBasicPro
.. - WORD , 16- (65535). ,
, 16 .
Div32 32
. 32-bit
(b*c) 100 Div32. 16 (5000) - a.
MAX MIN.
MAX MIN
, . ,
.
:

47

B1 = B0 MAX 100 ' B1 B0 100 (B1


100 255
B1 = B0 MIN 100 ' B1 B0 100
(B1 100)
NCD.
NCD
1.
, 1.
0, 1.
:
B0 = NCD %01001000 ' B0 7
REV.
REV
. ,
1 16. ,
.
:
B0 = %10101100 REV 4 ' %10101100
B0 %10100011
SIN.
SIN 8- .
. ,
.
0, ,
(
ABS). PicBasicPro
.
PicBasicPro SIN 255 ,
360 .
*255/360 - .
+127 -127 (
+1 -1).
:
B1 = SIN B0

48

SQR.
SQR . PicBasic
, 8-
, .
:
B0 = SQR W1

' B0 W1
.


.
.
,
1 . ,
B0 = B0 & %00000001
B0 = B0 | %00000001
B0 = B0 ^ %00000001
B0

' 0 B0
' B0 0 1
' 0

.
7

&

|

^

~
()
&/

|/

^/

.
IF..THEN,
. PicBasicPro
:

8
49

= ==

<> ! =

<

>

<=

>=

.
.
. 0
. .

IF..THEN. PicBasicPro :

9.

AND &&

OR ||

XOR ^^

NOT AND

NOT OR

NOT XOR

:
IF (A == big) AND (B > mean) THEN run
, PicBasicPro
.

50

.
.
,
.
. .
, , .
,
. ,

,
, .
.
, , .
: " Pin0 1" - ,
, . :
" , " .

, ,
. ,
.
,
, . ,
, ,
,
.
.
, - ,
Pin0 B1.
,
.
:
BattLED var PORTB.0
level
var byte

'
'

If level < 10 Then


' ,
High BattLED '
Endif
. .
,
51

. ,
, , - .
GOTO. GOTO.
GOTO , , ,

. , ,
. GOTO
GOSUB.
3. .

BASIC.
, ,
,
.
.
PIC16F877.
.
, .
MicroCode Studio
.
, Hello Word.
1 (Hello Word).
,
(PORTB.0).
, , ,
- . . 18 .
( ) ,
- , ,
. ,
.
:
Symbol LED = PORTB.0 PORTB.0 LED
TRISB = %11111110

.0 ,


main:


52

LED = 1

Pause 500

0,5

LED = 0

Pause 500

0,5

goto main

( )

,
(PORTB.0), LED.
, LED,
. , ,
. ,
PORTB ( ,
, ), .0 . ,
TRIS
. ,
%. , , ,
, 1- Input (), 0
Output (). .0 ,
OUTPUT ..:
OUTPUT PORTB.0
. ,
.
TRIS
.
main,
. PORTB.0
(1), , .
0.5 ,
. ,
(0).
. , ..
main. , ,
.
.
MicroCode Studio .
*.hex
.

53

. 18. Hello Word.


.
,
.
, :
DEFINE OSC
X
.
4 .

2. .
.
,
. 19.

. 19. .
54

:
Define LOADER_USED 1
'
(ICD)
LEDS

var

PORTB

' LEDS,



TRISB = %00000000

' PORTB

LEDS = 0

PORTB

Main:

LEDS = LEDS + 1

pause 500

0,5

goto main

,
ICD.

.
LEDS,
.
PORTB.
TRISB. , PORTB
.
LEDS. ,

LEDS = %00000000
, PORTB
0. .
Main:.
LEDS . , 0 PORTB
.
,
0.5 .
goto . ,
LEDS
, . , ,
55

, .
LEDS
. ,
255 (%11111111). .
,
.
' - I

I var Byte
Symbol LEDS = PORTB

' PORTB,



Pause 500
TRISB = %00000000

Loop:
LEDS = 1

' PORTB

0.
Pause 500
For I = 0 To 6


LEDS = LEDS << 1

'


Pause 500
Next
GoTo Loop

' 0,5
' 8
' Loop

I.
.
.
. ,

. , ,
TRISB 0, ,
PORTB . Loop,
.
LED = 1.
56

LED = %00000001, ,
, . ,
0 ( ),
(
). 0,5
. , ,
.
FORNEXT.
: , FOR NEXT
, ,
(I = 0),
(7). step.
, 1.
, FORNEXT
(<<),
PORTB (%00000001) .
I = 0 LED = %00000010
I = 1 LED = %00000100
I = 2 LED = %00001000
I = 3 LED = %00010000
I = 4 LED = %00100000
I = 5 LED = %01000000
I = 6 LED = %10000000

.
,
. ,
I 6. ,
FOR NEXT, ,
. , GOTO,
Loop.
.
,
,
PAUSE. (>>)
, LEDS
Loop, .
, , ,
.

57

3. .
,
. PORTA.0
,
.
. 20.
.
PORTA.0 ( ),
. , RA0
, .
D1. D2 .. , ,
? 35.
.

. 20. .
:

ADCON1 = %00000110
' PORTA .
TRISA = %00000001
' RA4-RA1 ,
RA0 .
TRISB = %00000000
PORTB
PORTB = %00000000
' PORTB ;

Main:
' *** ***
IF PORTA.0 = 0 THEN LED
'
LED,
58

GOTO Main

' .

LED:
'*** . ***
PORTB = PORTB + 1
'
.
GOTO Main
' main.
, ,
. , PORTA
( / ),
ADON1 6.
.
PORTB .
IF..THEN. ,
, .
, IF (PORTA.0 = 0), ,
, THEN.
, .
, IF THEN,
PORTA.0. 0, .. ,
THEN.
1, .
, , .

, , .

, ,
. ,
, ,
, . ?
. .
,
. ,
, 10
. ,

, ,
.
.
BUTTON.
(PORTB = PORTB + 1)
200 500 .
BUTTON.
:
59

ADCON1 = 6
TRISA = %00000001
PORTA = %00000001
TRISB = %00000000
PORTB = %00000000
B0 var

byte

Button
0

b0 = 0
Main:

Button PORTA.0,0,255,255,B0,1,LED

'

LED
'

GOTO Main

LED:
PORTB = PORTB + 1
GOTO Main

BUTTON .
.
,
- .
4. - .

, -
().
, .
- ,
, PIC
. - .
, ,
, , ,
.

, (POT),
PORTA.0. ,
60

,

, ,
PORTB. ,
, .
,
; ,
. , .
.
.
8- 10-. PIC 16F877 10- ,
8-.
8- .
, .
. 21 .
. ,
.

. 21. .
, :
'

Define ADC_BITS 8
.

' (3

Define ADC_CLOCK 3
= RC).
Define ADC_SAMPLEUS 50
adval var byte

' .

' adval


61

' ---[ ]------------------------Init:


PortB = $00

' PORTB 0 (

).
TrisB = %00000000

' PORTB

.
TRISA = %11111111

' PORTA .

ADCON1 = %00000010

' PORTA .

' ---[ ]-------------------------main:


ADCIN 0, adval

' 0,

adval.
'---------- -------------------------------------------------------

if adval < 75 then


portb = %00000000
endif
if adval > 100 then
100,
portb = %00000001
endif
if adval > 120 then
120,
portb = %00000011
endif
if adval > 140 then
portb = %00000111
endif

' 75,
' .

'
' D1.

'
' D2.

' 140,
' D3.

62

if adval > 160 then


portb = %00001111
endif
if adval > 180 then
portb = %00011111
endif
if adval > 200 then
portb = %00111111
endif
if adval > 210 then
portb = %01111111
endif
if adval > 220 then

' 160,
' D4.

' 180,
' D5.

' 200,
' D6.

' 210,
' D7.

' 220,

portb = %11111111
endif

' D8.

Pause 100

' 100 .

goto main

' .

ADCIN 0,
adval, .
0
,
DEFINE. (
adval).
,
. , ,
, .
.
, ,
PORTA.

63

,
.

, .
, , .
.
5. .
-
, , .
.
180 ,
,
- 360 . +5 ..
.
: ,
. ,
-
(PWM).
20 . 1
, 2 -
.
. 1,5

.
22

. 22 .

64


. , , ,
.
, Perpetuum Mobile. ,
, .
.
: ,
.
,
( )
.

.
:
Define ADC_BITS 8
Define ADC_CLOCK 3
Define ADC_SAMPLEUS 50
' ---[ ]---------------------------B2 var word

'

.
adval var byte

'


' ---[ ]------------------------TrisB = %00000000

' PORTB .

TRISA = %11111111

' PORTA .

ADCON1 = %00000010

' PORTA

.
' ---[ ]----------------------Main:
PORTB. 2 = 0
ADCIN porta.0, adval

' 0

adval
b2 = adval*10/256

' 2

adval.
65

PULSOUT PORTb.2, 100 + b2 ' .


pause 20
goto main

'

PULSOUT PORTb.2,100
+ b2.
. - PORTb.2,
100 + b2 .
. .
PORTB. 2
0. PORTA.0
adval. ,
, 1 2 ,
0 5
1 . adval
8 (byte) 0 256.
adval/256 .
, PicBasicPro ,
.
adval 10 256.
b2
.
(
4 10 ). ,
20 .
.
. ,
, .
, .
, ,
.
,
, .
, , -
(LCD).
6. .

. 23
.
66

. 23. .
, ( 99%)
Hitachi 44780.
? 14 .
27.
30

1
Vdd

2
Vcc
+
3
Contrast (Vee)

4
R/S
_/

5
R/W
/_
6


7-14
Data
: D0 -7,,D7 14

. ,
. R/W.
2 : 8- ,
, 4- ,
4 , 4 . ,
4-
, . R/S
,
. R/S 1,
ASCII ,
. R/S 0,
R/W 1, ,
. R/W
, .
. ,
4,1 ,
160 .
, ,
R/S 1.
67

PicBasicPro .
.
24.

. 24. .
,
. ,
, -

.
.
.
16 (2 x16).
25.

. 25. .
68

:
' [ ]DEFINE LCD_DREG PORTB
' ,
.
DEFINE LCD_DBIT 4
' ,
,
DEFINE LCD_RSREG PORTB
' ,
RS.
DEFINE LCD_RSBIT 3
' ,
RS.
DEFINE LCD_EREG PORTB
' ,
E.
DEFINE LCD_EBIT 0
' ,
E.
DEFINE LCD_BITS 4
' 4 .
DEFINE LCD_LINES 2
' .
DEFINE LCD_COMMANDUS 2000 '
.
DEFINE LCD_DATAUS 50
'
.
'
DEFINE ADC_BITS 8
DEFINE ADC_CLOCK 3

'
'

DEFINE ADC_SAMPLEUS 50 '


'
Res Var Word

' ,


Volts1 Var Word

'

Volts2 Var Word

'

'

69

Conv1 Con 19

' 5000/256 = 19.53, this is the decimal part

Conv2 Con 53

'

'
TRISA = 1

' RA0 (AN0)

TRISB = 0

' PORTB

PAUSE 500

' 0.5

ADCON1 = 0

' AN0 - AN4


OPTION_REG = %00000000
ADCON0 = %11000001

'


LCDOUT $FE, 1

'

Main:
ADCIN 0, Res

'

Res
Volts1 = Res * Conv1

' 19

Volts2 = Res * Conv2


Volts2 = Volts2 / 100

' 53

Volts1 = Volts1 + Volts2

'

LCDOUT $FE,2,"V = ",DEC4 Volts1


PAUSE 500
GOTO Main
END

'

' 0,5

,
-.
. ,
. .
.
.
.
-

PORTA.0.
Res
70

. ,
5000 ,
Res 255, ,
5000/256 = 19,53 . , PicBasicPro
,
,
(19,53)
. .
.
: LCDOUT $FE,2,"V = ",DEC4 Volts1.
,
- V = .
Volts, 4 .
.

.
.
7. 7 .
7-
.
.
.
.
, , 7 . ,
, .
7- , , ,
. 7-
, ,
, . 26
.

. 26. 7- .

71

PORTB PIC 16F877 ,


.
0 9, ,
.
.
Lookup,
.
. 27. .

. 27. 7- .
:
x var byte
numb1 var byte

TRISB = %00000000
PORTB = %00000000
.

' .
' ,
' 7- .
' PORTB .
' PORTB


Main:
, 0 9
FOR x = 0 TO 9
GOSUB convrt
' .
72

portb = numb1
PAUSE 1000
NEXT
HIGH 0 PORTB.0
.
pause 1000
LOW 0 PORTB.0

' 1
' - 9,
' .

FOR x = 9 TO 0 STEP -1
' 9 0.
GOSUB convrt
PORTB = numb1
' PORTB
7- .
' 1 .
PASE 1000
NEXT
HIGH 0 PORTB.0
.
PAUSE 1000
LOW 0 PORTB.0
GOTO loop

' 0,
' 1 .
' .
' .

' 7- .
convrt:
lookup x[$7E,$0C,$B6,$9E,$CC,$DA,$F8,$0E,$FE,$CE],numb1
Return
, , .
. ,
.
, FOR... TO...NEXT 0 9.
, GOSUB,
7 .
LOOKUP. [$7E,$0C,$B6,$9E,$CC,$DA,$F8,$0E,$FE,$CE] ()
- numb1. , , =
0, , .. $7E, = 5,
- $CC. -
- $. ,
, .
41 .
$7E %1111110.
, , G, ,
, 0.
73

$0C %00001100. ,
0000110, , B C, ..
1. , ,
7- .
RETURN ,
, . .
1 , ,
9. HIGH 0
, (Dp).
1 , LOW 0,

.
.
-. PIC
- EEPROM (
), , 1 .
, 1 ,
-. EEPROM PicBasicPro
- WRITE
READ. .
Phillips - I2C.
PicBasicPro I2CIN I2COUT.
,
EEPROM.
8. .
. , PIC16F877
- 256 .
.
. ,
, .
:
' [ ]DEFINE LCD_DREG PORTB
' ,
.
DEFINE LCD_DBIT 4
' ,
,
DEFINE LCD_RSREG PORTB
' ,
RS.
DEFINE LCD_RSBIT 3
' ,
RS.
74

DEFINE LCD_EREG PORTB


' ,
E.
DEFINE LCD_EBIT 0
' ,
E.
DEFINE LCD_BITS 4
' 4 .
DEFINE LCD_LINES 2
' .
DEFINE LCD_COMMANDUS 2000 '
.
DEFINE LCD_DATAUS 50
'
.
' -[ ]num var byte
.
mem var byte
.
' -[ ]
Main:
FOR num = 0 TO 10

WRITE num, num


0 10.
NEXT
' ***** ******
LCDOUT $fe, 1
' LCD.
LCDOUT $fe, 2
' .
'
, 10
FOR num =10 TO 0 STEP -1
READ num, mem
LCDOUT dec mem
PAUSE 500
' 0.5 .
NEXT
PAUSE 1000
GOTO Main

-.

WRITE num, num ,
- num
(num).
0 10 ,
.
, -.
75

,
- .
- READ num, mem.
num,
mem.
.
. .
.
9. .
PIC, Microchip, Atmel
.
I2C, PicBasicPro
I2CIN I2COUT,
.
I2C. I2C
Inter Integrated Circuit (IIC, I2C).
Philips,

. I2C
400
/.
:
: SDA (Serial Data)
SCL (Serial Clock)
;
;

. , 128
.
;

;


;

. 400 ;

, N, .
76

24LC00,
. 16 .
, ,
PIC, . , ,
EEPROM PIC ,
EEPROM.
16 EEPROM
. 28 .
, ,
.
.

. 28. .
:
' [ ]DEFINE LCD_DREG PORTB
' ,
.
DEFINE LCD_DBIT 4
' PORTB,
- DB4.
DEFINE LCD_RSREG PORTB
' ,
RS.
DEFINE LCD_RSBIT 3
' PORTB,
RS.
DEFINE LCD_EREG PORTB
' ,
E.
77

DEFINE LCD_EBIT 0
' PORTB,
E.
DEFINE LCD_BITS 4
' (4 ).
DEFINE LCD_LINES 2
' .
DEFINE LCD_COMMANDUS 2000
'
.
DEFINE LCD_DATAUS 50
'
.
' -[ ]adr var byte
.
dan var byte
.
Control con %10100000
' -
EEPROM.
DPIN var PORTA.0
' -
2
I C.
CPIN var PORTA.1
' -
2
I C.
Address var byte
' .
' -[ ]
adcon1 = 7
' PORTA
.
TRISA = %00000000
' PORTA .
porta = %00000000
' -[ ]
Main:
FOR adr = 0 TO 10

0 10
I2CWRITE dpin,cpin,control,adr,[adr] '

PAUSE 20

NEXT
'___ ____
LCDOUT $fe, 1
LCDOUT $fe, 2
FOR adr =10 TO 0 STEP-1
'
10 0
I2CREAD dpin,cpin,control,adr,[dan]
78

dan.
LCDOUT dec dan
'
dan.
NEXT
PAUSE 1000

GOTO Main
' .
. ,
I2CWRITE
dpin,cpin,control,adr,[adr].
adr
.
dpin cpin .
0 10 ,
,
.

I2CREAD
dpin,cpin,control,adr,[dan].
,

adr,
dan. ,
10,9,80.

12 ( ) .
10. 12 .
, ,
-,
.
.
. ,
,
.
, ,
.
-,
.
. ?
,
. 29 .

79

. 29. .
()
.

, ,
. ,
, , ,
, ,
. 30 12

.

. 30. 12- .
:
80

DEFINE LCD_DREG PORTD


' ,
.
DEFINE LCD_DBIT 0
' ,
LCD DB4.
DEFINE LCD_RSREG PORTD
' ,
RS .
DEFINE LCD_RSBIT 7
' ,
RS.
DEFINE LCD_EREG PORTD
' ,
E.
DEFINE LCD_EBIT 6
' ,
E.
DEFINE LCD_BITS 4
' 4- .
DEFINE LCD_LINES 2
' .
DEFINE LCD_COMMANDUS 1000 '
.
DEFINE LCD_DATAUS 50
'
.
' -[ ]Flag
Var Bit .
Var Byte
Key
Debounce
Var Bit ' .
D_Flag Var Bit ' Debounce flag used by Inkeys.
' -[ ]
TRISB=%00000111
PortB=%11111111
flag=1
LCDOUT $fe, 1
' .
LCDOUT $fe, 2
' .
' -[ ]
Main:
GOSUB KeyScan
IF FLAG =0 THEN LCDOUT $FE,1:LCDOUT $FE,2
'***** ******
IF Flag=1 THEN LCDOUT key ' ,
. .
PAUSE 100
' 100
.
GOTO Main
' ,
81

.
Keyscan:
' 12-

KEY. ,
128.
" Debounce ", 1,
, 0, .
.
Debounce=1
Key=0
TrisB=%11100000

' Debounce.
' KEY .
' 4 PortB .
' 3 PortB .
'

Option_Reg.7=0
PortB.
PortB=%01111101 ' D 0.
GOSUB Col
' .
IF Flag=1 THEN GOTO Map ' ,
Map.
PortB=%11111011 ' C 0.
GOSUB Col
' .
IF Flag=1 THEN GOTO Map ' ,
Map.
PortB=%11110111 ' B 0.
GOSUB Col
' .
IF Flag=1 THEN GOTO Map ' ,
Map.
PortB=%11101111 ' A 0.
GOSUB Col
' .
IF Flag=1 THEN GOTO Map ' ,
Map.
D_Flag=0
' ( ),
debounce.
Debounce=0
' .
GOTO Exit
Map:
IF D_Flag=1 THEN Exit
' .
D_Flag=1
' Debounce.
Debounce=0
' Debounce.
Exit:
'Lookup Key,[1,2,3,4,5,6,7,8,9,10,0,11,128],Key
82

' Key .
' ** ascii ,
.
,

LOOKUP
' Key ASCII .
KEY,["*","0","#","7","8","9","4","5","6","1","2","3",32],Key

'IF FLAG=1 THEN LCDOUT key


' Hello World
'pause 500
RETURN
' .
' .
' , Flag 1, 0,
.
' , , - KEY,
12.
Col:
Flag=1
' Flag 1.
IF PortB.5=0 THEN S_Exit ' ,
. S_Exit(,)
Key=Key+1
' KEY
.
IF PortB.6=0 THEN S_Exit ' ,
. S_Exit(,)
Key=Key+1
' KEY
.
IF PortB.7=0 THEN S_Exit ' ,
. S_Exit(,)
Key=Key+1
' KEY (
12).
Flag=0
' flag 0.
S_Exit:
.
PAUSEUS 100
RETURN
' .
.
11. .

1 ,
83

8
().

, . ,
,
,
.
31,
:

. 31.

Speaker VAR PORTA.0


Notes VAR Word[9]
Key VAR Byte

' PORTA.0
' .
'

'
Notes[1] = 262 : Notes[2] = 294 : Notes[3] = 330 : Notes[4]
= 349
Notes[5] = 392 : Notes[6] = 440 : Notes[7] = 494 : Notes[8]
= 524
'
TRISB = %11111111

' PORTB

84

'

OPTION_REG.7 = 0
PORTB
TRISA = 0

' PORTA

ADCON1 = %111

' PORTA


Main:
Key = PORTB

' PORTB Key

IF Key <> 255 THEN

'

key = ~key

Key

Key = NCD Key

' Key


FREQOUT Speaker,50,Notes[Key] '
ENDIF
GOTO Main
END

'
'

.
-,
. , Notes 9
. .
, ,
.
. .
PORTB ( )
Key.
, , ,
.
, .. Key 255
IF...THEN.
Key. ,
Key = %11110111.
Key = %00001000.
NCD,
1. , Key = 4. , ,
, Speaker 50
- Notes[Key].
.
( ).
11
,
85

.
SAMPLES
.
PicBasicPro
2.47.
4. PicBasicPro.
PicBasic Pro
2.47:
@
.
ADCIN
.
ASM..ENDASM .
BRANCH
(
ON..GOTO).
BRANCHL
BRANCH (
BRANCH).
BUTTON

CALL
.
CLEAR
.
CLEARWDT
.
COUNT
.
DATA

DEBUG

DEBUGIN
.
DISABLE
.
DISABLE DEBUG
.
DISABLE INTERRUPT .

DTMFOUT

EEPROM
( DATA).
ENABLE
.
ENABLE DEBUG
.
ENABLE INTERRUPT
.
END

FOR..NEXT
(
)
FREQOUT

GOSUB

GOTO
.
86

HIGH
.
HPWM
.
HSERIN
.
HSEROUT
.
I2CREAD
I2C.
I2CWRITE

2
I C.
IF..THEN..ELSE..ENDIF .
INPUT
.
LCDIN
.
LCDOUT
.
{LET}
().
LOOKDOWN

LOOKDOWN2
/.
LOOKUP
.
LOOKUP2
/ .
LOW

.
NAP
.
ON DEBUG
.
ON INTERRUPT
.
OWIN
.
OWOUT
.
OUTPUT
.
PAUSE
( n .).
PAUSEUS
( n .).
PEEK
.
POKE
.
POT
.
PULSIN
.
PULSOUT
.
PWM
.
RANDOM
- .
RCTIME

.
READ
.
READCODE
.
RESUME
.
RETURN

GOSUB.
REVERSE
.
SELECT CASE .
87

SERIN
SERIN2
SEROUT
SEROUT2
SHIFTIN
SHIFTOUT
SLEEP
SOUND
STOP
SWAP
TOGGLE
USBIN
USBINIT
USBOUT
WHILE..WEND
WRITE
WRITECODE
.
XIN
XOUT

.
.
.
).
.
.
.
.
.
.
.
USB.
USB .
USB.
, .
.
WORD
X-10.
X-10..

.
@.
@
- @ , ,
, BASIC
. ,
PicBasicPro.
:
i var byte
rollme var byte
FOR i = 1 TO 4
@ rlf _rollme, F
NEXT i

.
:
@ Include "fp.asm"
88

- @ 0
. ,
- @.
ADCIN.

ADCIN Channel,Var

, ,
. -
, ADCIN

.
, ADCIN,
TRIS , .
ADCON1
,

. .
Microchip
.
DEFINE.
:
DEFINE ADC_BITS 8
'
DEFINE ADC_CLOCK 3
' (rc = 3)
DEFINE ADC_SAMPLEUS 50
'
TRISA = 255
' PORTA
ADCON1 = 2
' PORTA
ADCIN 0, B0
' 0,
B0
ASM...ENDASM.
:
ASM
{assembler code}
ENDASM
ASM ENDASM PicBasicPro, ,

PicBasicPro.

89

,
PicBasicPro.

8 . , ,
.
,
. ASM
0.
:
ASM
bsf PORTA, 0 ; 0 PORTA 1
bcf PORTB, 0 ; 0 PORTB
ENDASM
BRANCH.
:
BRANCH Index,[Label{,Label...}]
BRANCH
, .
. .
, - , ,
, , .
,
,
BRANCH. BRANCH 255 (256 18Cxxx).
, .
, BRANCHL.
:
BRANCH B4,[dog,cat,fish]
:
' dog
' cat
' fish

If B4=0 Then dog


If B4=1 Then cat
If B4=2 Then fish

BRANCHL.
:
BRANCHL Index,[Label{,Label...}]

90

BRANCHL ( BRANCH ) ,
BRANCH. ,
. ,
,
, , BRANCH.
, ,
, BRANCH. ,
, BRANCH
, (
2 ), BRANCH BRANCHL.
.
:
BRANCHL B4,[dog,cat,fish]
:
If B4=0 Then dog
' 4=0 dog
If B4=1 Then cat ' 4=1 cat
If B4=2 Then fish ' 4=2 fish
BUTTON.
:
BUTTON Pin, DownState, Delay, Rate, BVar, Action, Label
, ,
.
- Pin.
. Pin
, 0 15, ,
0 15 (, B0)
(, PORTA.0).
Pin.
0.
. 45.

91

. 32. .
DownState
.
.
Pin ,
.
Delay () , ,
. ,
Delay 0 255.

(debounce). BUTTON.
: 0 255. Delay = 0,
BUTTON
. Delay = 255,
, .
Rate Delay
.
.
,
1 100
, - .
. ,
.
, BUTTON
,
.
. BUTTON ,
. , ,
, ,
. ,
. .
BUTTON .
92

BVar .
0.
Action .
0 1 ,
.
Label - , ,
.
Action
BUTTON.
, Action 0,
BUTTON .
BUTTON, Pin.
DownState
Label .
( ),
, ,
.
BUTTON 50 .
BVar Delay. ,
,
BUTTON BVar.
,
DownState . BUTTON
Label. ,
BVar 1. BVar
1, BUTTON

.
BUTTON BVar 40
4 (8 20 ),
4 40*( Delay-1)[] + 50[] (50
BUTTON ).
BUTTON (
Pin DownState)
BVar Rate.
BUTTON
BVar ( Pin DownState
- ).
, BVar > 1,
Label. BVar 1,
, BVar
Rate. .

93

, 40*(Rate
1)[] 4 .
:' BUTTON. 3
.
' 1 ,
().
Device 16F84A
XTAL = 4
Dim buf1 As Byte
Dim buf2 As Byte
Dim buf3 As Byte
Symbol sw1 = PORTb.0
Symbol sw2 = PORTb.1
Symbol sw3 = PORTb.2
Symbol led1 = PORTb.7
Symbol led2 = PORTb.6
Symbol led3 = PORTb.5
delayms 500
Clear
Low PORTB.5
low PORTB.6
LOW PORTB.7

' 1
' 2
' 3

' PIC-
'
'

chk1:
delayus 25
Button sw1,0,40,5,buf1,0,chk2
' 1 (
2 )
Toggle led1
' 1
chk2:
Button sw2,0,40,5,buf2,0,chk3
' 2 (
3 )
Toggle led2
' 2
chk3:
Button sw3,0,40,5,buf3,0,chk1
' 3 (
1 )
Toggle led3
' 3
GoTo chk1

'
94


BUTTON
Action 1.
BUTTON
BVar 0, Pin
DownState ( ),
BVar .
BUTTON (
Pin DownState),
BVar Delay.

BUTTON, ,
BVar
, . ,
1 , BVar 1.
Rate, .
BUTTON ,
,
, .
,
, ,
, .
, Action 1:
' BUTTON. 3
.
' 1 ,
().
Device 16F84A
XTAL = 4
Dim buf1 As Byte
Dim buf2 As Byte
Dim buf3 As Byte
Symbol sw1 = PORTb.0
Symbol sw2 = PORTb.1
Symbol sw3 = PORTb.2
Symbol led1 = PORTb.5
Symbol led2 = PORTb.6
Symbol led3 = PORTb.7
delayms 500
Clear
Low PORTb.5

' 1
' 2
' 3

' PIC-
'
'
95

low PORTB.6
LOW PORTB.7
Main:
delayms 20
Button sw1,0,4,5,buf1,1,chk1 ' 1 (
1 )
Button sw2,0,40,5,buf2,1,chk2 ' 2 (
2 )
Button sw3,0,40,5,buf3,1,chk3 ' 3 (
1 )
GoTo Main
chk1:

Toggle led1

'

GoTo Main
chk2:

Toggle led2

'

GoTo Main
chk3:

Toggle led3

GoTo Main

'
'


:
MyButton var PortB.0
LED var PortB.7
TRISB = %00000001
Loop:
If MyButton=0 then
Toggle LED
While MyButton=0:Pause 100:Wend
endif
Goto Loop
End
96

CALL.
:
CALL Label
,
Label.
PicBasicPro, GOSUB.
GOSUB CALL , CALL,
Label .
CALL PicBasic.
:
CALL pass

' , _pass
CLEAR.

. CLEAR
.
, .
, PicBasic Pro.
,
,
CLEAR.
:
CLEAR ' 0
CLEARWDT.
.
, ..
0. , .
:
CLEARWDT '
COUNT.
:
COUNT Pin,Period,Var
97

, Pin
- Period, Var. Pin
. Pin

0 15, , 0
15 (, B0) (, PORTA.0).
Period .
, DEFINE OSC.
COUNT Pin
. ,
4 , , 20
. 20 4 . ,
, - 25 4
125 20 . ,
.
:
' Pin1 100
COUNT PORTB.1, 100, W1
' - pin
COUNT PORTA.2, 1000, W1 ' 1
SEROUT PORTB.0, N2400, [W1]
DATA.
:
{label} DATA {@Location,}Constant1{,Constant2...}
(Constant)
EEPROM, .
-, .
Location ,
0 .. Location , ,
.
EEPROM ,
.
Constant ,
. WORD ,
.
ASCII.
.
DATA ,
EEPROM, PIC16F84 PIC16C84. ,
98

,
EEPROM.
1:
DATA @5,10,20,30

' 10, 20 30 5

2:
dlabel DATA word $1234 ' : $34, $12
3:
DATA (4), 0(10)
4 , 10
0.
4:
DATA ab
, .. 0 a
97. b 98.
DEBUG.
:
DEBUG Item {,Item...}

. : 8
, 1
( - 8N1). (/).
,
Define, .
(#), ,
ASCII . DEBUG ,
SEROUT2.
DEBUG
11.

{I}{S}BIN{1..16}

{I}{S}DEC{1..5}


99

{I}{S}HEX{1..4}

REP c\n

c, n

STR ArrayVar{\n}

DEBUG
.
. , ,
.
, ,
.. MicroCode Studio (ICD) .
.

.
SEROUT.
DEBUGIN.
:
DEBUGIN {Timeout,Label,}[Item{,Item...}]

,
8 . 1
(8N1). .
: Timeout - Label, ,
,
. ,
DEBUGIN .
DEBUGIN DEBUG
(MicroCode Studio (ICD)).
, , .
.

: SERIN, SERIN2, SEROUT, SEROUT2.
DISABLE.
DISABLE
.
,
, ENABLE. DISABLE
ENABLE ,
100

, ,
.
:
DISABLE
myint: led = 1
'
RESUME '
ENABLE '
DISABLE DEBUG.
.
, ENABLE ENABLE
DEBUG.
DISABLE INTERRUPT.
DISABLE INTERRUPT
. , ,
, ,
ENABLE ENABLE INTERRUPT.
DTMFOUT.
:
DTMFOUT Pin,{Onms,Offms,}[Tone{,Tone...}]

.
Pin ,
. ,
0 15, ,
0 15 (, B0),
(, PORTA.0).
Onms
. Onms 200
Offms .
Offms 50
Tone
. 0 15.
0 - 9 . 10
* , 11
101

#. 12-15, A-D
.
DTMFOUT FREQOUT.
, - Pin
. Pin
, 19,

.

. 33. .
DTMFOUT FREQOUT
20 .
10 , 4 .
FREQOUT.
:
' 212 Pin1
DTMFOUT PORTB.1,[2,1,2]
EEPROM.
:
EEPROM {Location,}[Constant{,Constant...}]
EEPROM.
- Constant{,Constant...}
EEPROM Location . Location
, - [Constant{,Constant...}]
EEPROM, 0. ,
, .
.
ASCII.
.

102

EEPROM ,
EEPROM .
PIC16F84 PIC16C84.
EEPROM, I2C,
12CE67x 16CE62x.
EEPROM - , ,
, , .
.
DATA.
EEPROM READ.
:
' EEPROM 10, 20 30 5
EEPROM 5,[10,20,30]
ENABLE.
ENABLE
,
DISABLE. DISABLE ENABLE ,
, ,
.
:
DISABLE '
myint: led = 1
'
RESUME '
ENABLE '

ENABLE DEBUG.
ENABLE DEBUG ,
DISABLE
DISABLE DEBUG.
ENABLE INTERRUPT.
ENABLE INTERRUPT
,
DISABLE INTERRUPT DISABLE.
103

END.

END

(Sleep).
( ) .
END , SLEEP .
: END,
STOP, GOTO
.
ERASECODE.
:
ERASECODE Block
PIC-, -, ,
, PIC18Fxxx ,
,
WRITECODE. ,
. 32 (64 )
, .
Microchip.
, , Block.
Block . ,
.
, ,
.
:
ERASECODE $100 ' , $100
FOR...NEXT.
:
FOR Count = Start TO End {STEP {-} Inc}
{Body}
NEXT {Count}
,
.
: - ,
- Start. End
Start -End,
NEXT,
104

, - Inc ( ),
. .
,
- - Start
- End ( , ,
). STEP ,
1. 255,
WORD.
1:
FOR i = 1 TO 10
SEROUT 0,N2400,[#i, ]

NEXT i
SEROUT 0,N2400,[10]

' 1 10
' Pin0
'
'

2:
FOR B2 = 20 TO 10 STEP -2
' 20 10 - 2
SEROUT 0,N2400,[#B2, ]
' Pin0

NEXT B2
'
SEROUT 0,N2400,[10] '
FREQOUT.
:
FREQOUT Pin,Onms,Frequency1{,Frequency2}
Pin - Onms
20.

. 34.
FREQOUT.
RC-,
- Frequency1,
, Frequency1 Frequency2.
0 32767 .
Onms . Pin
. ,
0 15, ,
105

0 15 (, B0) (,
PORTA.0). FREQOUT
- ,
. 19
.
FREQOUT
20 . 10 ,
4 .

, ,
.

20 . ..:
(Tone)N = (Tone)20 * 20/N

N ( ),
(Tone)20 , , 20 ,
(Tone)N , , N.
:
' PORTB.1 1KHz 2
FREQOUT PORTB.1,2000,1000
GOSUB.
:
GOSUB Label
,
, Label,
. GOSUB GOTO,
RETURN
GOSUB.
. ,
. ,
. .
.
:
GOSUB beep
' , beep
...
beep: HIGH 0
' Pin0
106

SOUND 1,[80,10] ' Pin1


LOW 0
'
RETURN
'
GOTO.
:
GOTO Label
,
Label.
:
GOTO send
' send
...
send: SEROUT 0,N2400,["Hi"]
' Hi Pin0

HIGH.
:
HIGH Pin
.
. Pin ,
0 15, ,
0 15 (, B0)
(, PORTA.0)).
:
HIGH 0
' Pin0
(+5 )
HIGH PORTA.0 ' pin 0 PORT,
(+5 )
led VAR PORTB.0
' LED PORTB.0
HIGH led
' LED
(+5 )
, - ,
(
), , :
PORTB.0 = 1

' PORTB.0

107

HPWM.
:
HPWM Channel,Dutycycle,Frequency

Frequency. - Channel
, .
1, 2 3 PWM. - Dutycycle .
0 255, 0
( , - 0), 255 (
, 1). 127 50 %- ().
Dutycycle 2.55, %.
Frequency - PWM.
2 ,
.
. .
4 245 .
32767 .
HPWM
:
HPWM

12.
, 14- 18Cxxx, 17Cxxx,

245

3907

489

7813

10

611

9766

12

733

11719

16

977

15625

20

1221

19531

24

1465

23438

33

2015

32227

40

2442

na

108

, PIC18C452, ,
HPWM.
DEFINE :
DEFINE
DEFINE
DEFINE
DEFINE

CCP1_REG
CCP1_BIT
CCP2_REG
CCP2_BIT

PORTC
' Hpwm
2
' Hpwm
PORTC
' Hpwm
1
' Hpwm

DEFINE , , 1 2,
PWM 2 PWM 3 PIC17C7xx.
DEFINE , - 1,.
DEFINE HPWM2_TIMER 1 ' Hpwm 1
DEFINE HPWM3_TIMER 1 ' Hpwm 1
:
HPWM 1,127,1000
HPWM 1,64,200

' 50 % 1
' 25 % 2
HSERIN.

:
HSERIN {ParityLabel,}{Timeout,Label,}[Item{,...}]

.
,
USART.
.
. ,
, DEFINE.
'
DEFINE HSER_RCSTA 90h
'
DEFINE HSER_TXSTA 20h
'
DEFINE HSER_BAUD 2400

109

' SPBRG (
HSER_BAUD)
DEFINE HSER_SPBRG 25
HSERIN
4 .
,
DEFINE,
. , -Timeout
-Label ,
, ,
, .
.
- 8N1, .. 8
, 1 .
, 7E1 (7 ,
, 1 ) 7O1 (7 ,
, 1 ).
DEFINE:
'
DEFINE HSER_EVEN 1
' ,
DEFINE HSER_ODD 1
HSER_EVEN HSER_ODD 0 -
.
, DEFINE
HSEROUT.
ParityLabel( ).
, ,
. , ,
,
DEFINE.
,
RS-232.
HSERIN . HSERIN
,
SERIN2.
HSERIN
13

110

BIN{1..16}

DEC{1..5}

HEX{1..4}

SKIP n

STR ArrayVar\n{\c}

n ,

c

WAIT ( )

WAITSTR ArrayVar{\n}

( 17Cxxx),
HSERIN .
, .
- HSER_RCSTA, HSER_TXSTA HSER_SPBRG
PIC- RCSTA, TXSTA SPBRG
.
, Microchip
.
BRGH ( 2) TXSTA
.
,
. , HSER_TXSTA
24- 20-. Microchip
.
2-
, ,
. , USART
.
, CREN RCSTA.
DEFINE:
DEFINE HSER_CLROERR
, , ,
, , .
:
RCSTA.4 = 0
111

RCSTA.4 = 1
:
B0 var byte
W1 var word
Main:
HSERIN [B0, dec W1]

Goto Main

, HSERIN
SERIN. ,
USART,
.
HSEROUT.
:
HSEROUT [Item {,Item...}]

.
,
USART.
.
. ,
, DEFINE.
'
DEFINE HSER_RCSTA 90h
'
DEFINE HSER_TXSTA 20h
'
DEFINE HSER_BAUD 2400
' SPBRG (
HSER_BAUD)
DEFINE HSER_SPBRG 25
/ ,
HSEROUT 4 .

112

, , DEFINE
. - 8N1, .. 8
, 1 .
, 7E1 (7 ,
, 1 ) 7O1 (7 ,
, 1 ).
DEFINE:
'
DEFINE HSER_EVEN 1
'
DEFINE HSER_ODD 1
, DEFINE
HSERIN. HSEROUT
, SEROUT2
(. 11).
17Cxxx 2 ,
HSEROUT .
, .
' B0,
USART
HSEROUT [dec B0,10]
HSERIN2.
:
HSERIN2 {ParityLabel,}{Timeout,Label,}[Item{,...}]

USART.
.

.
HSERIN2 -
.

DEFINE:
'
DEFINE HSER2_RCSTA 90h
113

'
DEFINE HSER2_TXSTA 20h
'
DEFINE HSER2_BAUD 2400
' SPBRG ( HSER_BAUD)
DEFINE HSER2_SPBRG 25
HSERIN2
4 .
, -
DEFINE, .
-Timeout Label ,
,
.
.
8N1, 8 , 1
. , 7E1 (7 ,
, 1 ) 7O1 (7 ,
, 1 ) ,
DEFINE:
'
DEFINE HSER2_EVEN 1
'
DEFINE HSER2_ODD 1
HSERIN2
HSEROUT2. - - ParityLabel
. ,
,
, .
HSERIN2 ,
SERIN2 (. 13).
HSEROUT2.
:
HSEROUT2 [Item {,Item...}]

,
.
114

, USART.

.
, DEFINE:
' 2
DEFINE HSER2_RCSTA 90h
' 2
DEFINE HSER2_TXSTA 20h
' - 2
DEFINE HSER2_BAUD 2400
' SPBRG ( HSER_BAUD)
DEFINE HSER2_SPBRG 25
HSEROUT2, ,
4 .
,
DEFINE, .
8N1, 8
, 1 .
, 7E1 (7 ,
, 1 ) 7O1 (7 ,
, 1 ) ,
DEFINE:
' 2 -
DEFINE HSER2_EVEN 1
' 2 -
DEFINE HSER2_ODD 1
, (DEFINE HSER2_)
HSERIN2 HSEROUT2.
HSEROUT2 ,
SEROUT2 (. 11).
' B0,
USART
HSEROUT2 [dec B0,10]

115

I2CREAD.
:
I2CREAD
DataPin,ClockPin,Control,{Address,}[Var{,Var...}]{,Label}
,
I2C. - ClockPin () DataPin () - Control
Addres. , ,
Var. ClockPin DataPin ,
0 15, ,
0 15 (, B0)
(, PORTA.0).
I2CREAD I2CWRITE ,
EEPROM 2-
I2C. 24LC01B .

,
.
master I2C
I2C. , ,
.
12- PIC- I2C,

DEFINE.
I2CREAD,
.
DEFINE I2C_SCL PORTA,1 ' 12-
DEFINE I2C_SDA PORTA,0 ' 12-
7
,
. - ,
.
PicBasic. ,
I2C. , 24LC01B, %1010, ,
, %10100000 $A0.
:
14.

24LC01B

128 %1010xxx0
116

24LC02B

256 %1010xxx0

24LC04B

512 %1010xxb0

24LC08B

1K %1010xbb0

24LC16B

2K %1010bbb0

24LC32B

4K %1010ddd0

24LC65

8K %1010ddd0

bbb = ( )
ddd =
xxx =
( )
. ,
8- . ,
16- . ,
.
,
. ,
2 ,
. ,
, .. , .
STR.
.
STR ,
,
(\) :
a VAR byte[8]
I2CREAD PORTC.4,PORTC.3,$a0,0,[STR a\8]
,
, - .
-Label, ,
I2C.
12CE67x , - GPIO.6, GPIO.7. 16CE62x , - EEINTF.1, EEINTF.2.
Microchip .
I2C (100 )
8 . (400 )
20 .
117

8
:
DEFINE I2C_SLOW 1
, 12-
, DEFINE .
4
MHz. 400 4 .
I2C
.
.
:
DEFINE I2C_HOLD 1
I2C
Vcc 4.7 ,
,
(. 40).
I2C
:
DEFINE I2C_SCLOUT 1
addr VAR byte
cont CON %10100000
addr = 17 ' 17
' 17 B2
I2CREAD PORTA.0,PORTA.1,cont,addr,[B2]

, I2CREAD
I2CWRITE, Non-Volatile Memory Products
Data Book.
I2CWRITE.
:
I2CWRITE
DataPin,ClockPin,Control,{Address,}[Value{,Value...}]{,Labe
l}
I2CWRITE .
DataPin ClockPin
118

. ClockPin DataPin
, 0 15,
, 0 15 (, B0)
(, PORTA.0). ( )
, .
,
, .
.
12- PIC- I2C,

DEFINE.
I2CWRITE,
.
DEFINE I2C_SCL PORTA,1 ' 12-
DEFINE I2C_SDA PORTA,0 ' 12-
EEPROM 10
, .
I2CREAD I2CWRITE ,
( ), .
1 I2CWRITE.
.

.
EEPROM
.
.
,
I2C, ,
.
, 2 . ,
. ,
, , .
STR.
,
EEPROM .
SEEPROM.
SEEPROM. STR,
( ),
(\) :
a VAR byte[8]
I2CWRITE PORTC.4,PORTC.3,$a0,0,[STR a\8]

119

,
. - ,
, ,
.
-Label, ,
I2C.
I2C ,
(100 ) 8 .
(400 ) 20 .
8
, :
DEFINE I2C_SLOW 1
I2C .

( 12- ).
:
DEFINE I2C_HOLD 1
I2C ,
,
:
DEFINE I2C_SCLOUT 1
:
addr VAR byte
cont CON %10100000
addr = 17
' 17
' 6 17
I2CWRITE PORTA.0,PORTA.1,cont,addr,[6]
PAUSE 10 ' 10
addr = 1 ' 1
' B2 1
I2CWRITE PORTA.0,PORTA.1,cont,addr,[B2]
PAUSE 10 ' 10

120

IF...THEN.
:
IF Comp {AND/OR Comp...} THEN
Statements...
ELSE
Statements...
ENDIF
.

. If..Then
. ,
, Then.
, , Then . ,
0, .
. .
,
.
IF..THEN
Then. ELSE ENDIF,
.
IF B0 <> 10 THEN
B0 = B0 + 1
B1 = B1 - 1
ENDIF
IF B0 = 20 THEN
led = 1
ELSE
led = 0
ENDIF
INPUT.
:
INPUT Pin
.
, 0 15, ,
0 15 (, B0)
(, PORTA.0).
:

121

INPUT 0 ' Pin0


INPUT PORTA.0 ' pin 0 PORTA,
,
( ) :
TRISB.0 = 1 ' pin 0 PORTB,
, 1
TRIS:
TRISB = %11111111 ' PORTB

LET.
:
{LET} Var = Value
LET .
, .
, LET .
:
LET B0 = B1 * B2 + B3
B0 = SQR W1
LET .
LCDIN.
:
LCDIN {Address,}[Var{,Var...}]
-
Address - Var.
,
. ,
.
, LCDOUT. LCDIN
.
$40 $7f.
$80.
122


.
/ (R/W)
PIC . , 0
1, ,
(LCDIN) (LCDOUT). DEFINE
:
DEFINE LCD_RWREG PORTE
' R/W
PORTE
DEFINE LCD_RWBIT 2
' R/W 2
LCDOUT.
:
LCDOUT Item {,Item...}
-
. PicBasicPro
Hitachi 44780 .
14 16 - .
(#), ,
ASCII (
12-). LCDOUT
, SEROUT2 (. 11).

. ,
, 0.5 .
15 ,
$FE.
15

$FE, 1

$FE, 2

( )

$FE, $0C
$FE, $0E
$FE, $0F
$FE, $10
$FE, $14
$FE, $C0
123

$FE, $94
$FE, $D4
.
.
.
16x2, $0, $40.
:
LCDOUT $FE, $C0
, .
16x1 8x2
8 .
4 .

.
:
LCDOUT $FE, 1, "Hello"
LCDOUT B0, #B1

' Hello

PIC 4
8- . 8- , 8
. 4-
, 4
. Enable (E) Select Register (SR)
. R/W ,
LCDOUT .
PicBasicPro, ,
. , ,
4- , DB4 - DB7,
PORTA.0 - PORTA.3, Select Register PORTA.4,
Enable PORTB.3. ,
:
'
DEFINE LCD_DREG PORTB
' , (0 4), 4-

DEFINE LCD_DBIT 4
' Register Select
DEFINE LCD_RSREG PORTB
124

' Register Select


DEFINE LCD_RSBIT 1
' Enable
DEFINE LCD_EREG PORTB
' Enable
DEFINE LCD_EBIT 0
' (4 8 )
DEFINE LCD_BITS 4
LCD ( )
DEFINE LCD_LINES 2
'
DEFINE LCD_COMMANDUS 2000
'
DEFINE LCD_DATAUS 50
PicBasicPro, 2
, 4- 4
PORTB, Select Register PORTB.1, Enable
PORTB.0 (. 35).

. 35. - PIC16F84
4- .
LOOKDOWN.
:
LOOKDOWN Search,[Constant{,Constant...}],Var
125

LOOKDOWN 8- - Search.
,
-Var. ,
. ,
1. .
, .
, .
ASCII .
,
LOOKDOWN, ,
. 255
(256 18Cxxx).
:
SERIN 1,N2400,B0 '
Pin1 0
LOOKDOWN B0,["0123456789ABCDEF"],B1 '
B0 B1
SEROUT 0,N2400,[#B1]
' 1
Pin0
LOOKDOWN2.
:
LOOKDOWN2 Search,{Test}[Value{,Value...}],Var
LOOKDOWN2 8- Search. ,
-Var. ,
. ,
1, .
, .
Test,
,
.
< > .
, >
, , Search. ,
, =. 16 .
,
ASCII .
, Search. ,
,
LOOKDOWN2, ,
126

, . 85 (256
18Cxxx) .
LOOKDOWN2 , 3
, , LOOKDOWN.
8- ,
, , LOOKDOWN.
:
LOOKDOWN2 W0,[512,W1,1024],B0
LOOKDOWN2 W0,<[10,100,1000],B0

LOOKUP.
:
LOOKUP Index,[Constant{,Constant...}],Var
LOOKUP ,
8- Index
- Var. Index ,
. Index ,
. . Index
, ,
.
.
, ASCII . ,
LOOKUP,

. 255 (256 18Cxxx)
.
:
FOR B0 = 0 TO 5
' 0 5
LOOKUP B0,["Hello!"],B1 ' Hello!
0 1
SEROUT 0,N2400,[B1]
'
1
NEXT B0
'

127

LOOKUP2.
:
LOOKUP2 Index,[Value{,Value...}],Var
LOOKUP2 ,
8- Index
- Var. Index ,
. Index ,
. . Index
, ,
. 16-
.
ASCII .
,
Index. ,
LOOKUP2. LOOKUP2 85
(256 18Cxxx) .
LOOKUP2 , 3
, , LOOKUP.
8- ,
LOOKUP.
:
LOOKUP2 B0,[256,512,1024],W1

LOW.
:
LOW Pin
LOW
. .
, 0 15,
, 0 15 (, B0)
(, PORTA.0).
:
LOW 0
' Pin0 (0
)
LOW PORTA.0 ' 0 PORTA
(0 )
128

led VAR PORTB.0 ' LED pin0 PORTB


LOW led ' LED (0 )
, ,
( ),
:
PORTB.0 = 0 ' pin 0 PORTB 0
NAP.
:
NAP Period

, - Period.
NAP, .
Period ,
, R/C
.
NAP ,
.
Period
16

(.)

18

36

72

144

288

576

1.152

2.304

:
NAP 7 2.3

129

ON INTERRUPT.
:
ON INTERRUPT GOTO Label
ON INTERRUPT
. 2
PicBasicPro.

, .

. ,
PicBasicPro.
PicBasicPro
RESUME.
,
.
Label
, .
, RESUME
, . PicBasicPro
, DISABLE ENABLE,
.
DISABLE, .
ENABLE, , ( ,
DISABLE) .
, DISABLE,
.
,
, .
, PAUSE
SERIN, ,
.
. , PAUSE 300,
PAUSE 100: PAUSE 100: PAUSE 100.

,
.
:
ON INTERRUPT GOTO myint '
- myint
INTCON = %10010000 ' RB0
. . .
DISABLE
'
130

myint: led = 1 ' LED


RESUME
'
ENABLE
'
(
), INTCON $80:
INTCON = $80
OUTPUT.
:
OUTPUT Pin
.
, 0 15, ,
0 15 (, B0),
(, PORTA.0).
:
OUTPUT 0
OUTPUT PORTA.0

' Pin0
' pin 0 PORTA

-,
( ) :
TRISB.0 = 0 ' pin 0 PORTB

TRIS:
TRISB = %00000000

OWIN.
:
OWIN Pin, Mode,[Item {,Item}]
Pin
,
. ,
0 15, ,
0 15 (, B0), (, PORTA.0).
(Mode) , - /
, , , .
OWIN
131

17

1 =

1 =

0 = - , 1 = -

:
0. , . 1
, . 4 -
, . - Item
, .
- STR, ,
, , SKIP, ,
. SKIP STR
12- PIC- -
.
:
OWIN PORTC. 0,0,[temperature\2, SKIP 4, count_remain,
count_per_c]

PORTC 0 . 2
, 4
2 .
OWOUT.
:
OWOUT Pin, Mode, [Item{, Item...}]
Pin
, .
, 0 15,
, 0 15 (, B0),
(, PORTA.0). ,
/ ,
(. 17).
:
0 . 1
, . 4 - ,
132

. - Item
, .
: STR,
REP ,
. REP STR 12-
- .
:
OWOUT PORTC. 0,1,[$cc,$be]

PORTC.0, $cc $be.
PAUSE.
:
PAUSE Period
Period.
Period . 16-
; 65 535 (
, ). (NAP SLEEP),
PAUSE .
, PAUSE
, .
, . PAUSE
4 .
, PicBasicPro
DEFINE.
:
PAUSE 1000 ' 1

PAUSEUS.
:
PAUSEUS Period

133

PAUSE. -Period
. 16- ;
65 535 . PAUSEUS
, ,
, , ,
, PAUSEUS.
,
ASM..ENDASM.

18
OSC

3 (3.58)

20

24

12

10

12

16

20

25*

32*

33*

40**

* PIC17Cxxx and PIC18Cxxx only.


** PIC18Cxxx only.
PAUSEUS 4 .
,
DEFINE.
:
PAUSEUS 1000 ' 1

134

PEEK.
:
PEEK Address,Var
PEEK Address - Var.
PIC , -
PEEK. , PIC ,
PEEK POKE. PIC- 8 PicBasic Pro,
, .
PEEKCODE.
:
PEEKCODE Address,Var

Address - Var .
PEEKCODE , ,

.
:
PEEKCODE $3ff, OSCCAL
PIC12C671/12CE673
PEEKCODE $7ff, OSCCAL
PIC12C672/12CE674

OSCCAL
OSCCAL

POKE.
:
POKE Address,Value

.
135

PEEK PORTB, B0
PEEK POKE .
:
B0 = PORTB
POKECODE.
:
POKECODE Value{,Value...}
,
.
POKECODE ,
.
- Org ,
. ,
.
,
POKECODE .
STOP END.
:
POKECODE 10, 20, 30
10, 20, 30

Generates:
retlw 10
retlw 20
retlw 30
@
org 7ffh $7ff
POKECODE $94
OSCCAL
PIC12C672/12CE674
Generates:
org 7ffh
retlw
94h

POT.
:
POT Pin,Scale,Var
136

, ,
- Pin
(. . 36). ,
0 15, ,
0 15 (, B0), (, PORTA.0).
,

( 5 50 ). Scale ()

RC-. RC-
(1). RC
(255).
,
Var 255
.

. 36. .
, Scale .
,
.
127 . ,
, , POT
- 254. 255, . 253 ,
.
,
.
B0 Var Byte
scale Var Byte
For scale = 1 To 255
POT 0, scale, B0
If (B0 > 253) Then calibrated
Next scale
Serout 2,0,[" Increase R or C.", 10,13]
Stop
137

calibrated:
Serout 2,0,[" Scale= ",# scale, 10,13]
PULSIN.
:
PULSIN Pin,State,Var
- Pin.
State , ,
1 0 . - State ,
0 1 .
- Var. , , ,
, .
8- , 16 . ,
0 15, , 0
15 (, B0), (, PORTA.0).
PULSIN .
4 , ,
, 10 . 20
, 2 .
OSC PULSIN.
.
:
' Pin4
W3
PULSIN PORTB.4,1,W3
PULSOUT.
:
PULSOUT Pin,Period
- Pin Period.
; .
. ,
0 15, ,
0 15 (, B0), (,
PORTA.0). PULSOUT
. 4 ,
10 .
20 , 2 . OSC
138

PULSOUT.
.
:
' 1 ( 4 ) Pin5
PULSOUT PORTB.5,100
PWM.
:
PWM Pin,Duty,Cycle

- Pin. . PWM
256 . -Duty
, 0 0 % 255 100 %. - Cycle
. - Pin
, 0 15, ,
0 15 (, B0),
(, PORTA.0). PWM
. 4 ,
- 5 . 20 , 1 . OSC PWM.
.
, , .
.
PWM ,
R C . 37 .

. 37.
PWM.
:
PWM PORTB.7,127,100 ' Pin7
PWM 100
139

50%-

RANDOM.
:
RANDOM Var

- Var.
16-.
RANDOM,
.
RANDOM ,
.
65535. .
:
RANDOM W4 ' W4
RCTIME.
:
RCTIME Pin,State,Var
RCTIME , - Pin
(State) - Var .
- PULSIN. ,
0 15, ,
0 15 (, B0), (,
PORTA.0). RCTIME ,
( , )
.
RCTIME . 4 ,
10 .
20 ,
2 . OSC - DEFINE
RCTIME.
.
Pin ,
0.
:
LOW PORTB.3
' PORTB.3
PAUSE 10
' 10
RCTIME PORTB.3,0,W0 ' Pin3
140

READ.
:
READ Address,Var
EEPROM
- Address - Var.
PIC-,
EEPROM, PIC16F84, PIC16C84
PIC16F87x.
EEPROM,
I2C 12CE67x 16CE62x. I2CREAD.
:
READ 5,B2 ' EEPROM 5
B2
READCODE.
:
READCODE Address,Var

- Address - Var.
PIC16Fxxx PIC18Xxxx
.

.
:
READCODE 100,W
' 100
W
REPEAT...UNTIL.
:
REPEAT
Statements...
UNTIL Condition
REPEAT...UNTIL ,
. UNTIL.
,

141

i = 0
REPEAT
PORTB.0[i] = 0
i = i + 1
UNTIL i > 7

RESUME.
:
RESUME {Label}
,
. RESUME RETURN,
PicBasicPro.
- Label,
,
, . ,
.
RETURN.
.
, GOSUB,
.
:
GOSUB sub1
' sub1
...
sub1:
SEROUT 0,N2400,["Lunch"] ' Lunch
Pin0
RETURN

' Gosub
REVERSE.

:
REVERSE Pin
, , .
,
. , , .
Pin , 0
142

15, , 0 15 (,
B0), (, PORTA.0).
:
OUTPUT 4
REVERSE 4

' Pin4
' Pin4
SERIN.

:
SERIN Pin,Mode,{Timeout,Label,}{[Qual...],}{Item...}

Pin
I2C; ( ) : 8
(8N1).
SERIN SERIN BS1 (BasicStamp 1) c
- Timeout. - Pin
. , 0
15, , 0 15 (,
B0), (, PORTA.0). (,
T2400) MODEDEFS.BAS. ,
PicBasicPro :
INCLUDE "modedefs.bas"
.
:
19
No.
T2400

2400

T1200

1200

T9600

9600

T300

300

N2400

2400

N1200

1200

N9600

9600

N300

300

,
Timeout Label. Timeout
143

Label,
Timeout. .
, ,
, . SERIN

. -
, .
,

,
.
. ,
SERIN ,
. #, SERIN
ASCII
.
SERIN 4 .
,
DEFINE.

. ,
, .

. 38. COM
SERIN .
, RS-232
, - PIC

.
:
' , , Pin1
B0
SERIN 1,N2400,["A"],B0
144

SELECT...CASE.
:
SELECT CASE var
CASE expr1 {, expr...}
statements
CASE expr2 {, expr...}
statements
{CASE ELSE statements}
END SELECT
, ,
, IF.. THEN. ,

.
, , SELECT CASE. CASE ,
, CASE . IS
, . CASE ,
CASE ELSE. SELECT
END SELECT CASE.
:
SELECT CASE x
CASE 1
y = 10
CASE 2, 3
y = 20
CASE IS > 5
y = 100
CASE ELSE
y=0
END SELECT
SERIN2.
:
SERIN2 DataPin{\FlowPin},Mode,{ParityLabel,}
{Timeout,Label,}[Item...]

.
145

SERIN2 Serin BS2 (BasicStamp 2). DataPin ,


- FlowPin .
DataPin FlowPin ,
0 15, , 0
15 (, B0), (, PORTA.0).
- FlowPin,
, .
, FlowPin
, .

, Mode (). Mode
,
. 13
. 13 ,
. 14 , , 14
0, , 14 1 ;
15 .
,
RS-232.
1
+ 20. ,
:
(1000000 / baud) 20
:
600 1000000/600 = 1666,6 - 20 = 1646

.
20


0 - 12
300
3313
600
1646
1200
813
2400
396
4800
188
9600
84
19200
32
: = 84 (9600 ,
, - ), = 16780 (2400 ,
, - ), = 27889 (300 ,
146

, ). ParityLabel ,
,
. , ,
( 13 1).
-Timeout -Label ,
,
.
. DEFINE
.
4 8 (
-8, DEFINE).
( ),
DEFINE:
DEFINE
DEFINE
DEFINE
DEFINE
DEFINE

SER2_BITS
SER2_BITS
SER2_BITS
SER2_BITS
SER2_BITS

4
5
6
7
8

' 4
' 5
' 6
' 7
' 4 ( )

:
DEFINE
DEFINE
DEFINE
DEFINE
DEFINE

SER2_BITS
SER2_BITS
SER2_BITS
SER2_BITS
SER2_BITS

5
6
7
8
9

' 4
' 5
' 6
' 7 ( )
' 8

SERIN2

, SERIN2 ,
.
21

BIN{1..16}

DEC{1..5}

HEX{1..4}

SKIP n

STR ArrayVar\n{\c}

n ,
c
147

WAIT ( )

WAITSTR ArrayVar{\n}

1) , BIN,
ASCII . , BIN,
B0 A1000, B0 - 8.
2) , DEC,
ASCII . , DEC,
B0 A123, 0 - 123.
3) , HEX,
ASCII . ,
HEX, B0 AFE, B0
- 254.
4) SKIP,
. , SKIP 4 4 .
5) STR,
,
, .

n ,
.
6) ,
.
- WAIT.
SERIN2 ,
. -
,
( -
). ,
.
.
7) WAITSTR ,
WAIT, , SERIN2
.
WAIT WAITSTR ,
SERIN2 ,
. , ASCII
.
BIN, DEC HEX, SERIN2
, ASCII
. ,
. ,
, .
148

BIN, DEC HEX . ,


, .
, SERIN2
,
. SERIN2 4
.
, , DEFINE
. ,
RS-232 ,
- PIC,
.
SERIN2 12- PIC-
- .
:
A Pin1
B0
SERIN2 1,16780,[WAIT(A),B0]
2 4
SERIN2 PORTA.1,84,[SKIP 2,DEC4 B0]
SEROUT.
:
SEROUT Pin,Mode,[Item {,Item...}]
,
, 8 ,
(8N1). SEROUT Serout
BS1. Pin .
, 0 15, ,
0 15 (, B0),
(, PORTA.0). (, T2400),
MODEDEFS.BAS. ,
:
INCLUDE "modedefs.bas"
BS1DEFS.BAS BS2DEFS.BAS MODEDEFS.BAS.
, .
.
22.

149

T2400

2400

T1200

1200

T9600

9600

T300

300

N2400

2400

N1200

1200

N9600

9600

N300

300

OT2400

2400

OT1200

1200

OT9600

10

9600

OT300

11

300

ON2400

12

2400

ON1200

13

1200

ON9600

14

9600

ON300

15

300

* 12
SEROUT
SEROUT.
1) , .
2) ( ),
ASCII . 13 - , 10 -
.
3) , (#), ASCII
. , W0 = 123, # W0 ( # 123) '1',
'2', '3'.
SEROUT 4 .
,

DEFINE,
.

. DEFINE
.
. DEFINE
1 - 65 535 (0.001 65.535 )
150

. , 1
, :
DEFINE CHAR_PACING 1000

. 39. COM
SEROUT .
, RS-232
,
- PIC
.
:
' ASCII B0 Pin0

SEROUT 0,N2400,[#B0,10]
SEROUT2.
:
SEROUT2 DataPin{\FlowPin},Mode,{Pace,}
{Timeout,Label,}[Item...]

. SEROUT2 Serout
BS2. DataPin ,
FlowPin . DataPin FlowPin
, 0 15,
, 0 15 (, B0),
(, PORTA.0).
FlowPin.
.
,
, FlowPin .
151

,
. -Timeout -Label

, FlowPin
.
.
,
. DEFINE
.
, . DEFINE
1 - 65 535 (0.001 65.535
) . -Mode
,
. 13
. 13 ,
. 14 14 0
, 14 1 .
15 , .
1
- 20. ,
:
(1000000 / baud) 20
:
600 1000000/600 = 1666,6 - 20 = 1646
{ }
.
23
0 - 12
300

3313

600

1646

1200

813

2400

396

4800

188

9600

84

19200

32

13 , ( 13 =
1) ( 13 = 0). ,
152

- 8N1 (8 ,
1 ). ,
7E1 (7 ,
1 ). 14
. 14 = 0,
RS-232. 14 = 1,
. ,
, - RS-232.
: = 84 (9600 ,
, , ). = 16780 (2400
, , , ).
= 60657 (300 , , , ).
DEFINE

4 8 ( 8,
DEFINE).
( ) DEFINE:
DEFINE
DEFINE
DEFINE
DEFINE
DEFINE

SER2_BITS
SER2_BITS
SER2_BITS
SER2_BITS
SER2_BITS

4
5
6
7
8

' 4
' 5
' 6
' 7
' 4 ( )

:
DEFINE
DEFINE
DEFINE
DEFINE
DEFINE

SER2_BITS
SER2_BITS
SER2_BITS
SER2_BITS
SER2_BITS

5
6
7
8
9

' 4
' 5
' 6
' 7 ( )
' 8

SEROUT2 ,

SERIN2, .
24

{I}{S}BIN{1..16}

{I}{S}DEC{1..5}

{I}{S}HEX{1..4}

REP c\n

STR ArrayVar{\n}

n ,
153

c
1) .
2) ( )
ASCII . , 13 - , 10 - .
3) , BIN,
ASCII . , B0 = 8, BIN
B0 ( BIN 8) "1000".
4) , DEC,
ASCII . , B0 = 123,
DEC B0 ( DEC 123) "123".
5) , HEX,
ASCII . , B0 =
254, HEX B0 ( HEX 254) " ".
6) REP,
. , REP A0" \4 "0000".
7) STR,
.
.
BIN, DEC HEX
.
I ( ),
- "%", "#" "$", ,
, .
S ( - ), ,
.
.
, PicBasicPro . ,
. , B0 = 9
- 10. DEC B0 "255". SDEC B0 "-1".
BIN, DEC HEX
. , ,
.
. , ,
SEROUT2 ,
. , BIN6 8 "001000", BIN2 8
"00". .
, ISDEC4 B0. SEROUT2
4 .

DEFINE,
.
154

:
' 2400 Pin0 ASCII
B0,
SEROUT2 0,16780,[dec B0,10]
' 9600 PORTA.1 B0 =
B0
SEROUT2 PORTA.1,84,["B0=", ihex4 B0]
SHIFTIN.
:
SHIFTIN DataPin,ClockPin,Mode,[Var{\Bits}...]

SPI
.
, -DataPin,
ClockPin Var.
ClockPin DataPin ,
0 15, , 0
15 (, B0), (, PORTA.0). \Bits , .
DEFINE,
8 .
(, MSBPRE), MODEDEFS.BAS.
, :
INCLUDE "modedefs.bas"
.
. 0-3 ,
. .
ClocPin
. 4-7,
, 0
.
25

MSBPRE

,
.

.
ClocPin - .

LSBPRE

,
155

.

.
ClocPin - .

MSBPOST

LSBPOST

,
.

.
ClocPin
.

,
.

.
ClocPin .

,
.

.
ClocPin - .

,
.

.
ClocPin - .

,
.

.
ClocPin - .

,
.

.
ClocPin - .

50 .
2 .
DEFINE 65.535
, .

156

PAUSEUS. ,
100 :

DEFINE SHIFT_PAUSEUS 100

. 40. SPI.
:
SHIFTIN 0,1,MSBPRE,[B0,B1\4]
SHIFTOUT.
:
SHIFTOUT DataPin,ClockPin,Mode,[Var{\Bits}...]
SHIFTIN
SPI
.
, Var,
DataPin ClockPin.
ClockPin DataPin ,
0 15, , 0
15 (, B0), (, PORTA.0). \Bits , .
DEFINE,
8 .
(. MSBPRE), MODEDEFS.BAS.
, PicBasic Pro :
INCLUDE "modedefs.bas".
.
. 0-1
ClocPin .
.
4-5, , ,
157


.
26

LSBFIRST

MSBFIRST

,
.
Clc - .

,
.
Clc - .

,
.
Clc - .

,
.
Clc - .

50
. 2
. DEFINE

65 535 65.535 . ,
100
:
DEFINE SHIFT_PAUSEUS 100
:
SHIFTOUT 0,1,MSBFIRST,[B0,B1]
SHIFTOUT PORTA.1, PORTA.2, 1,[wordvar\4]
SLEEP.
:
SLEEP Period

- Period ( ). - 16 .
, 65 535 (.. , 18 ).
SLEEP ,
.
2.3
.
158

:
SLEEP 60 ' 1
SOUND.
:
SOUND Pin,[Note,Duration{,Note,Duration...}]
- Pin.
.
, 0 15, ,
0 15 (, B0)
(, PORTA.0). Note
. Note = 0 . Note 1 127
, 128 255 - .
( 1, 128 - , 127 255
). Note = 1 78.74 ,
Note = 127 - 10,000 . - Duration
0 255 12 .
Note Duration .
SOUND

-.

- PIC ,
(. . 55).

, . Piezo-,
.

.41. .
:
SOUND PORTB.7,[100,10,50,10] ' Pin7
2

159

STOP.
.
.
:
STOP '
SWAP.
:
SWAP Variable,Variable
. , -
- . SWAP,
, ,
. SWAP bit, byte
word.
SWAP,
.
:
temp = B0
B0 = B1
B1 = temp
SWAP B0, B1

'
'
TOGGLE.

:
TOGGLE Pin

.
. ,
0 15, ,
0 15 (, B0), (,
PORTA.0).
:
LOW 0
TOGGLE 0

' Pin0
' Pin0

160

WHILE...WEND.
:
WHILE Condition
Statements...
WEND
WHILE, .
,
, WEND.
.
:
i = 1
WHILE i <= 10
SEROUT 0,N2400,["No:",#i,13,10]
i = i + 1
WEND
USBINIT.
,
USB .
, USB ,
USB . , PIC16C745 765.
PicBasicPro Microchip
USB. USB- PIC
, USB .
USB ,
( USB). USB
, (SHIFTIN SHIFTOUT)
(SERIN, SEROUT) . , USB,
Microchip.
USBIN.
:
USBIN Endpoint,Buffer,CountVar,Label
USBIN
USB .
, .
Countvar , . -
, Label.
USB USB Microchip,
. USB ,
161

( USB).
USB , (SHIFTIN SHIFTOUT)
(SERIN, SEROUT) .
USB, Microchip.
USBOUT.
:
USBOUT Endpoint,Buffer,Count,Label
USBOut Buffer -Count
- Endpoint USB .

- Label . USB
USB Microchip, .
USB ,
( USB). USB
(SHIFTIN SHIFTOUT) (SERIN,
SEROUT) . USB,
Microchip.
WRITE.
:
WRITE Address,Value
EEPROM
. PIC ,
EEPROM, PIC16F84, PIC16C84
PIC16F87x. EEPROM ,
EEPROM DATA. WRITE
10 .
,
() WRITE, (
) , . ,
WRITE,
. , WRITE
EEPROM,
I2C, 12CE67x 16CE62x .
I2CWRITE.
:
WRITE 5,B0 ' B0 EEPROM 5

162

WRITECODE
:
WRITECODE Address,Value
-Value
- Address. PIC16F87x
.
.
. ,
() DISABLE
WRITECODE, ( ) ,
. ,
WRITECODE, .
:
WRITECODE 100,W ' W 100
XIN.
:
XIN DataPin,ZeroPin,{Timeout,Label,}[Var{,...}]
X-10.
- House Code Key Code. ,
,
.
.
( TW-523) X-10
XIN.
, .
X-10 ,
.
, X-10, DataPin
. -ZeroPin
.
.
X-10.
+5
4.7 . ZeroPin DataPin

, 0 15, ,
0 15 (, B0),
(, PORTA.0). - Timeout (
) - Label () ,
, X-10
.
163

( 60 , 8.33 ). XIN
, 0 ZeroPin,
, .
, XIN .
Key Code X-10 ,
.
, X-10.
, .
, , , .
, , , .
Key Code ( 0 15) (1-16).
:
housekey VAR WORD
' X-10
loop: XIN PORTA.2,PORTA.0,[housekey]
' , X-10
LCDOUT $fe,1,"House=",#housekey.byte1,
"Key=",#housekey.byte0
GOTO loop '
' X-1. 1 ,
nodata
XIN PORTA.2,PORTA.0,1,nodata,[housekey]
XOUT.
:
XOUT DataPin,ZeroPin, [HouseCode\KeyCode{\Repeat}{,...}]
XOUT X-10 HouseCode KeyCode,
. -Repeat ()
, ( ) 2 .
- Bright ()
Dim (). XOUT ,
X-10.
.

. PL-513,
, - TW-523,
. X-10 ,
.
XOUT DataPin
, X-10.
ZeroPin
164

0,
.
+5 4.7 .
ZeroPin DataPin ,
0 15, ,
0 15 (, B0), (,
PORTA.0)
XOUT 0
ZeroPin. ,
XOUT .
KeyCode X-10 ,
. ,
, X-10, ,
.
, . , , ,
. KeyCode ( 0 15)
1-16. KeyCode (, unitOn)
MODEDEFS.BAS. , PicBasic
Pro :
"modedefs.bas"
PicBasic Pro. BS1DEFS.BAS BS2DEFS.BAS
MODEDEFS.BAS. ,
. KeyCode
.
27
KeyCode

KeyCode No.

unitOn

%10010

unitOff

%11010

unitsOff

%11100

lightsOn

%10100

lightsOff

%10000

bright

%10110

dim

%11110

X-10 4 .
X-10
,
4,7 . :
PL 513
165

28

Zero

Zero

X-10

X-10

TW-523
29

Zero

X-10

X-10

:
house VAR BYTE
unit VAR BYTE
INCLUDE "modedefs.bas"
house = 0 ' house 0
unit = 8 ' unit 8
' (unit) 8 (house) 0
XOUT PORTA.1,PORTA.0,[house\unit,house\unitOn]
' (house) 0
XOUT PORTA.1,PORTA.0,[house\lightsOff]
' 10
XOUT PORTA.1,PORTA.0,[house\0]
loop: XOUT PORTA.1,PORTA.0,[house\unitOn]
PAUSE 10000
' 10
XOUT PORTA.1,PORTA.0,[house\unitOff]
PAUSE 10000
' 10
GOTO loop
DEFINE.
PicBasicPro, , ,
,
166

PicBasicPro. DEFINE
PicBasicPRO
. .
PicBasicPro:
DEFINE ADC_BITS 8 '
DEFINE ADC_CLOCK 3 ' (rc = 3)
DEFINE ADC_SAMPLEUS 50 '
DEFINE BUTTON_PAUSE 10 '
c
DEFINE CCP1_REG PORTC ' Hpwm
DEFINE CCP1_BIT 2 ' Hpwm
DEFINE CCP2_REG PORTC' Hpwmt
DEFINE CCP2_BIT 1 ' Hpwmt
DEFINE CHAR_PACING 1000 '
DEFINE DEBUG_REG PORTB ' Debug
DEFINE DEBUG_BIT 0 ' Debug
DEFINE DEBUG_BAUD 2400 '
Debug
DEFINE DEBUG_MODE 1 ' Debug: 0 = True, 1 = Inverted
DEFINE DEBUG_PACING 1000 '
Debug
DEFINE DEBUGIN_REG PORTB '
Debugin
DEFINE DEBUGIN_BIT 0 '
Debugin
DEFINE DEBUGIN_MODE 1 ' Debugin:
0 = , 1 =
DEFINE HPWM2_TMR 1 ' Hpwm 2 1
DEFINE HPWM3_TMR 1 ' Hpwm 3 1
DEFINE HSER_BAUD 2400 ' Hser
DEFINE HSER_CLROERR 1 '
Hser
DEFINE HSER_SPBRG 25 ' SPBRG
( HSER_BAUD)
DEFINE HSER_RCSTA 90h '

DEFINE HSER_TXSTA 20h '

DEFINE HSER_EVEN 1 '
DEFINE HSER_ODD 1 '
DEFINE HSER2_BAUD 2400 ' 2
DEFINE HSER2_CLROERR 1 '
167

2
DEFINE HSER2_SPBRG 25 ' SPBRG 2
( HSER_BAUD)
DEFINE HSER2_RCSTA 90h ' 2

DEFINE HSER2_TXSTA 20h ' 2

DEFINE HSER2_EVEN 1 ' 2
DEFINE HSER2_ODD 1 ' 2
DEFINE I2C_HOLD 1 ' I2C

DEFINE I2C_INTERNAL 1 ' EEPROM
16CExxx 12CExxx
DEFINE I2C_SCLOUT 1 '

DEFINE I2C_SLOW 1 ' >8

DEFINE I2C_SDA PORTA,0 ' ( 12 )
DEFINE I2C_SCL PORTA,1 ' (
12- )
DEFINE LCD_DREG PORTA '

DEFINE LCD_DBIT 0 '
( 0 4)
DEFINE LCD_RSREG PORTA '
RS
DEFINE LCD_RSBIT 4 ' RS

DEFINE LCD_EREG PORTB '
ENABLE
DEFINE LCD_EBIT 3 '
ENABLE
DEFINE LCD_RWREG PORTE '
read/write
DEFINE LCD_RWBIT 2 '
read/write
DEFINE LCD_BITS 4 ' 4 8

DEFINE LCD_LINES 2 '
DEFINE LCD_COMMANDUS 2000 '
DEFINE LCD_DATAUS 50 '
DEFINE LOADER_USED 1 ' Bootloader
168

DEFINE NO_CLRWDT 1 ' CLRWDT


DEFINE PULSIN_MAX 1000 '
pulsin
DEFINE OSC 4 ' : 3(3.58), 4, 8, 10, 12,
16, 20, 24, 25, 32, 33, 40
DEFINE OSCCAL_1K 1 '
PIC12C671/CE673
DEFINE OSCCAL_2K 1
PIC12C672/CE674
DEFINE SER2_BITS 8 ' Serin2
Serout2
DEFINE SER2_ODD 1 '
DEFINE SHIFT_PAUSEUS 50 '
Shiftin Shiftout
.
PicBasic Pro
.
:
PORTB.1 = 1 ' PORTB 1
,

VAR
:
led VAR PORTA.0
HIGH led
(+5 )

' PORTA.0 led


' led (PORTA.0)

BASIC Stamp, ,
PicBasicPro ,
, .. 0 - 15.
,
.
31

PIC-

0-7

8 - 15

8-

GPIO*

GPIO*

169

18-

PORTB

PORTA*

28- ( 14C000)

PORTB

PORTC

28- (14C000)

PORTC

PORTD

40-

PORTB

PORTC

*GPIO PORTA 8 - .
8 (, PORTA),

, 8 - 12.
13, 14 15
. PIC-, 0 6, 21 33,
PORTB.0 ( GPIO.0 8 ,
PORTC.0 PIC14C000). , PicBasicPro
(. Pin0 Pin10). ,
bsdefs.bas
. ,
(. PORTA.1).
.
, , .
VAR:
led VAR PORTB.3
PicBasicPro
BASIC Stamp, .
"bs1defs.bas" "bs2defs.bas".
PicBasic Pro, ,
BASIC Stamp. BS1DEFS.BAS B0-B13, W0W6 .. BS2DEFS.BAS Ins, Outs, B0-B25,
W0-W12 ..
PIC- ,
.
( ),
TRIS. 0 TRIS
. , 1
TRIS
. :
PORTA .
TRISA = %00000000
' TRISA = 0
PORTB .
170

TRISB = %11111111 ' TRISB = 255


PORTC ,
.
TRIS = %10101010 ' TRIS = 170
.
12-
, 12-
, PicBasic Pro
.
,
:
32



ADCIN

DATA


DTMFOUT

EEPROM


FREQOUT

HPWM

HSERIN

HSEROUT

ON INTERRUPT

READ


READCODE

RESUME

SERIN2

SEROUT2
USB
USBIN
USB
USBINIT
USB
USBOUT

WRITE


WRITECODE

XIN

XOUT
12-
.
171

1.

Micro-LAN.
,
Button, Dallas Semiconductor 1991 .
Touch Memory, ,
.
, , , (
),

. , ,
1-Wire, . . ,
Micro-LAN (Miniature Local Area Network).
: 1)
(,
, ..); 2) ()
( ); 3)
,
; 4) ; 5)

.
Micro-LAN

. ,

( , ..). Micro-LAN
(. . 42).
, .
.

,
, .

172

42. Micro-LAN.

(. . 43). ,
.

.

43. .

.
. ,

.
. ()
.
DS1820 DS18S20.
- :
173



.

( ).

Reset () Presence ( ).
Reset
480-960 .
, 100 ( 60
240 ). ,
.
, ,
,
. .
.
.


.

.
.
.

.
. ,

. . 44
.

44.
.
(
450 960 ). ,
174

( 15
60 ) (
60 240 ).
.
.

.
60 .
.
1 .
.
,
,
. ,
, .
.
.
. ,
, ,
. ,
. 5
: . 1, . 0,
.
. 0.
. 0 50.
60 , 120 .
. 1.
. 1 45.
115 . ,
.

45. . 0 . 1 .
175

.

. ,
.
1 .
15 . ,
1
.
46.
115 .
, . 0. . 1,
.
15 .
, , , .
1.
. 1 .

46. . 0 . 1.
.
8 .
. 1-Wire ,
64- ().

. : 8 , 48
8 CRC-, 56 .
, 64-. .
. 2
.
33

READ ROM
( )
SKIP ROM


33H
()
CCH (
176

( )
MATCH ROM
( 55H
)
SEARCH ROM
( )

)
(
)


(
F0H

)

.
64
. 8
, 48 8
().
.
,
.
.
,
.
64
.
.


.
64- . , ,
,
.
.
,
.
, . ,
.
. ,
.

. ,

177

. 1,
10b. , 1 0,
01b. ,
.
,
.
, . 0, . 1 ,
00. ,
, . 0 1 . ,
,
.

64 .
64- .
,
.
,
. ,
, , . ,

.
/.
/ ,
.

. ,
.
.
(CRC).
,
Dallas Semiconductor
.
64 . ,
,
. 6
.

(cyclic redundancy check CRC).
.
( ) 8

178

, , .
, .

.
(. . 47).
/.
/ ,
.
.
,
. .
.

47. 8-
, X8+X5+X4+1.

.
,
. ( )
( )
. 16 . Dallas Semiconductor 64
8- .
, . 47
(, , )

. , ,
:
64-
.
( ) 64 .
8- , .. ,
1 8 .
, 8
.
179


(XOR).
8- .
.
,
. ,
, ,
.
, ( )
.
, ,
.
2.
10.
10 ( -10)
. 10
70- PICO Electronics, . PICO ,
,
.
( 10)
.
10
X10 USA (X10 Inc).
, 10- : X10 Inc.,
Leviton, Marmitek, IBM, SmartLinc, PowerHouse .
10?
X10 - .
X10 -
(, , , ..)
,
.
X10 256
.
10.
10 - ,
( ).
,
.
10 1
180

120 5 . .

. 48. ,
10.

200 .

10 , ,
2.778 60 .
50 , 3,33 .
10
.
,
( )
( 1 16) (, ..), .. .

. 49. .
( + + )
(. 49)
.
/,

.
- , 1110
, ..
.
,

181

. ,
, ( ).
50.

. 50. .
34.
10

A
B
C
D
E
H
F
G
I
J
K
L
M
N
O
P



H1 H2 H4 H8
D1 D2 D4 D8 D16
0
1
1
0
1
0
1
1
0
0
1
1
1
0
2
1
1
1
0
0
0
0
1
0
3
0
0
1
0
0
1
0
1
0
4
1
0
1
0
0
0
0
0
1
5
0
0
0
1
0
1
1
0
1
6
1
1
0
1
0
1
0
0
1
7
1
0
0
1
0
0
1
0
1
8
0
1
0
1
0
0
1
1
1
9
0
1
1
1
0
1
1
1
1 10 1
1
1
1
0
0
0
1
1 11 0
0
1
1
0
1
0
1
1 12 1
0
1
1
0
0
0
0
0 13 0
0
0
0
0
1
0
0
0 14 1
0
0
0
0
0
1
0
0 15 0
1
0
0
0
1
1
0
0 16 1
1
0
0
0
0
0
0
0
1
0
0
0
1
1
0
0
1
0
1
0
0
1
1
1
0
1
0
0
1
0
1
0
1
1
182



[1]
HALT

HALT
[2]

[3]

=
=

0
0
1

1
1
0

1
1
0

0
1
0

1
1
1

1
1

0
0

0
1

1
1

1
1
1

1
1
1

0
0
1

0
1
1

1
1
1

1) - HAIL (-)
.
Hail Acknowledge.
2) - Pre-Set Dim, D8
5 {D8H8H4H2H1},
.
3) - Extended Data ( )
(8 ) ,
- .
, .
.
,
.
Extended Code Extended Data:
( ), .
256 .
16 ,
(, , , )
. D16 "
", 1, , .
. 5- " ",
:
1110010110100101011001111001011010010101100100000011100101101001011001
101110010110100101100110.
94 , 47
0,94 ( !). , ,

183

. " " "


" , .. .
10 .
.

, , "", 94 , 47
0,94 . ( !).
"" , , ..
.

X10 ,
"" . (, , ..)
( 10).

120 X10.
.
,
.
, , , 10
.
"" 10 ,
.
( ),
.
.
X10 (),
.
, ,
"" , .
,
.
X10 .
10
. extended codes
( ). ,
,
, .
X10
.
,
, 10, , ,
, 10
184

.
X10
.
X10

( ).
X10 80-
, X10
, .

X10 -
(, ,
);
X10.

185

.
1. Chuck Hellebuyck Programming PIC Microkontrollers With PICBASIC - Elsevier
Science, 2003
2. . PIC-. . : ,
2002.
3. . PIC-. .: , 2002.
4. . .
. : , 2004.
5. . . PIC16X7XX. .-:
, 2002.
6. By Les Johnson. Experimenting with the PICBASIC PRO COMPILER. - Rosetta
Technologies. 2000.

186