Professional Documents
Culture Documents
You Can Practice Microcontroller Programming Easily Now!: Thursday, October 23, 2008
You Can Practice Microcontroller Programming Easily Now!: Thursday, October 23, 2008
א א
ﺍﻟﺘﺠﺎﺭﺏ ﺍﻟﻌﻤﻠﻴﺔ
א א
Programming
Embedded Systems Microcontroller
אא א
א
•
Wאא
א אא
regfile = "m128def.dat"
$r EATmega1288Fא
אא
crystal = 1000000
$c אא
אאא
baud = 9600
$b
אאאא
•
Wאא
א אא
ait value
Wa e EVValueאF
aitms value
Wa E
V
ValueאF
aitus value
Wa E
V
ValueאF
•
WאאאE/F א
א א
onfig Por
Co rtc = Output א
C אא
onfig Por
Co rtc.5 = Output
Cאא5 א
onfig Por
Co rtc = Input א
C אא
onfig Pin
Co nc.5 = Input
Cאא5 א
Po
ortc = 255 Cאאאא
Pi
inc.5 = 1 Cא א
5 אאא
Pi
inc.5 = 0 C אא5
אאא
Po
ortc = &B
B11110000 C אאאא
/אא
אאאאא
onfig Por
Co rtc = &B11110000
KE1F א،E0F
Le
eds Alias
s Portd ELedsF
אEDFאאא
Le
eds Alias
s Portd.5 ELedF
אE5F
א
אא
א 2
אאא
Practical Class 2 Programming Microcontrollers
WESet/ResetFאא •
א א
Set bit אEא/אF
Reset bit Eא/אF
Toggle bit אאEא/אF
Wא• א
א א
If Expression1 Then
Statements1 א
...
Elseif Expression2 Then Kא
Statements2
...
1א1אא
Else
Statements3
2א2אא
... 3א
End If
SELECT CASE var א
Case Test1 : Statements1 Kאא
Case Test2 : Statements2 1א var = Test1א
Case Else : Statements3 2א var = Test2א
END SELECT 3א
Wא •
א א
Do
א א א א
Statements
Kאאאאא
Loop [until Expression]
While Condition
Statements Kאא
Wend
For Var = Start To End [step Value] א אא א א
Statements אKEndא Start
Next Var .step
Exit For Forא
Exit Do Doא
Exit While Whileא
WSRAMאאא •
א א
Faculty of Electrical and Electronic Eng.
3 Automatic Control & Automation Dept.
אא א
א
im Var1 As
Di A Bit KE0 or 11F
im Var2 As
Di A Byte KE0 to 255F
im Var3 As
Di A Integer KE32,7768 to +32,7677F
im Var4 As
Di A Word KE0 to 65535F
im Var5 As
Di A Long KE2147483648 to 21474836647F
im Var6 As
Di A Single KE1.5 x 10^–455 to 3.4 x 10^338F
im Var7 As
Di A Double K
im Var8 As
Di A String * 1 KE* mF
chr_num א
im Array(8) As Byte
Di K
onst Symb
Co
Ex
bol = Num
mconst
x. Const Pi = 3.14159265358979 K
onst Symb
Co
Ex
bol = Stringconst
x. Const S = "TEST" K
onst Symb
Co
Ex
bol = Expression
x. Const E =(b1 * 3) + 2 K
ocal Var As Type
Lo K
•
Wאאא
א אא
Px.y
א
א א
ebounce Px.y , stat
De
Ex
x. Deboun
te , labe
el , Sub
nce Key1 , 0 , Sw1 , Sub ،state אא
Kאlabeelאאא
Deboounce אEF
Co bounce = time
onfig Deb
K
אא
itwait x , Set/reset
Bi
א א
Ex
x. Bitwai
it Pinb.7 , reset KאאEאF א
אא
א 4
אאא
Practical Class 2 Programming Microcontrollers
Wאא
Kאאאאאא
Wא
W
KJP9, JP10א
Wאא
Wא
.LEDs7 -1
Kא -2
KאאKKKאא -3
Faculty of Electrical and Electronic Eng.
5 Automatic Control & Automation Dept.
אא א
א
K
אא -4
Wאא
regfile = "m128def.dat"
$r
crystal = 1000000
$c K
א
-----------------------
'-
onfig Por
Co rtd.0 = Output
onfig Por
Co rtd.1 = Output
onfig Por
Co rtd.2 = Output
onfig Por
Co rtd.3 = Output
onfig Por
Co rte.4 = Output Kאא
onfig Por
Co rte.5 = Output
onfig Por
Co rte.6 = Output
onfig Por
Co rte.7 = Output
-----------------------
'-
Le
eds1 Aliaas Portd
Le
eds2 Aliaas Porte
א אא
אא
-----------------------
'-
im I As Byte
Di B , J As Byte K
-----------------------
'-
Do
o
For I = 0 To 3 Kא
J = I + 4
Set Leds1.i
Set Leds2.j
Waitt 1
אאאא
Next I
K
For I = 3 To 0 Step -1
J = I + 4
Reseet Leds1.i
Reseet Leds2.j
Waitt 1
Next I
oop
Lo
אאא
En
nd
K
א
אא
א 6
אאא
Practical Class 2 Programming Microcontrollers
Wאא
Kאאא
Wא
W
KJP6, JP8א
Faculty of Electrical and Electronic Eng.
7 Automatic Control & Automation Dept.
אא א
א
Wאא
W אאא
KE
א
אאאFGND JJP6, JP8
א
-1
KVCC
V
JP6, GND
G
JP8
א-2
WGND
D JP6, JP8 אא
regfile = "m128def.dat"
$r
crystal = 1000000
$c
baud = 4800
$b
K
א
-----------------------
'-
Co bounce = 150
onfig Deb
אאאא
א
onfig
Co Pin
nd.0 = Input
Co
onfig Pin
nd.1 = Input
Co
onfig Pin
nd.2 = Input
Co
onfig Pin
nd.3 = Input
onfig
Co
Co
onfig
Pin
ne.4
Pin
ne.5
=
=
Input
Input
Kאא
Co
onfig Pin
ne.6 = Input
Co
onfig Pin
ne.7 = Input
ortd.0 = 1 : Portd.1 = 1
Po
ortd.2 = 1 : Portd.3 = 1
Po
orte.4 = 1 : Porte.5 = 1
Po
orte.6 = 1 : Porte.7 = 1
Po
-----------------------
'- אאא
Ke
ey1 Alias
s Pind.0
Ke
ey2 Alias
s Pind.1
Ke
ey3 Alias
s Pind.2
Ke
ey4 Alias
s Pind.3
Ke
ey5 Alias
s Pine.4
Ke
ey6 Alias
s Pine.5
Ke
ey7 Alias
s Pine.6
א אא
אא
Ke
ey8 Alias
s Pine.7
-----------------------
'- K
Do
o
ce
Debounc Key1 , 0 , Sw1 , Sub
Debounc
ce Key2 , 0 , Sw2 , Sub
Debounc
ce Key3 , 0 , Sw3 , Sub
Debounc
ce Key4 , 0 , Sw4 , Sub
Debounc
ce Key5 , 0 , Sw5 , Sub
Debounc
ce Key6 , 0 , Sw6 , Sub
Debounc
ce Key7 , 0 , Sw7 , Sub
Debounc
ce Key8 , 0 , Sw8 , Sub
אא
aitms 200
Wa
אאא
oop
Lo
En
nd
'-
-----------------------
Sw
w1:
Print "Switch(1) is pressed!"
Re
eturn
'/
/------------
Sw
w2:
Print "Switch(2) is pressed!"
אא
א 8
אאא
Practical Class 2 Programming Microcontrollers
Return
'/------------
אאא
Sw3: אאאאא
Print "Switch(3) is pressed!"
Return אאא
'/------------
Sw4:
Print "Switch(4) is pressed!"
Return
'/------------
Sw5:
Print "Switch(5) is pressed!"
Return
'/------------
Sw6:
Print "Switch(6) is pressed!"
Return
'/------------
Sw7:
Print "Switch(7) is pressed!"
Return
'/------------
Sw8:
Print "Switch(8) is pressed!"
Return
Faculty of Electrical and Electronic Eng.
9 Automatic Control & Automation Dept.
אא א
א
WVCC
V
JP8 وGND
G
JP6, אא
PoortDא אאאא
א
KS6
אאאא
regfile = "m128def.dat"
$r
crystal = 1000000
$c
baud = 4800
$b
K
א
-----------------------
'-
Co bounce = 150
onfig Deb
אאאא
א
onfig
Co Pin
nd.0 = Input
Co
onfig Pin
nd.1 = Input
Co
onfig Pin
nd.2 = Input
Co
onfig Pin
nd.3 = Input
onfig Pin
Co ne.4
onfig Pin
Co ne.5
= Input
= Input
Kאא
onfig Pin
Co ne.6 = Input
onfig Pin
Co ne.7 = Input
Po
ortd.0 = 0 : Portd.1 = 0
ortd.2 = 0 :
Po Portd.3 = 0
אא
א 10
אאא
Practical Class 2 Programming Microcontrollers
Porte.4 = 1 : Porte.5 = 1
Porte.6 = 1 : Porte.7 = 1 Portdאאאא
'-----------------------
Key1 Alias Pind.0
Key2 Alias Pind.1 PortEאאאא
Key3 Alias Pind.2
Key4 Alias Pind.3
Key5 Alias Pine.4
Key6 Alias Pine.5
Key7 Alias Pine.6 אאאאא
Key8 Alias Pine.7
'----------------------- K
Do
Debounce Key1 , 0 , Sw1 , Sub
Debounce Key2 , 0 , Sw2 , Sub
Debounce Key3 , 0 , Sw3 , Sub
Debounce Key4 , 0 , Sw4 , Sub
Debounce Key5 , 0 , Sw5 , Sub
Debounce Key6 , 0 , Sw6 , Sub
Debounce Key7 , 0 , Sw7 , Sub
Debounce Key8 , 0 , Sw8 , Sub
Waitms 200 אא
Loop
End אאא
'-----------------------
Sw1:
Print "Switch(1) is pressed!"
Return
'/------------
Sw2:
Print "Switch(2) is pressed!"
Return
'/------------
Sw3:
Print "Switch(3) is pressed!"
Return
'/------------
Sw4:
Print "Switch(4) is pressed!"
Return
'/------------
Sw5:
Print "Switch(5) is pressed!"
Return אאא
'/------------
Sw6: אאאאא
Print "Switch(6) is pressed!"
Return
אאא
'/------------
Sw7:
Print "Switch(7) is pressed!"
Return
'/------------
Sw8:
Print "Switch(8) is pressed!"
Return
Faculty of Electrical and Electronic Eng.
11 Automatic Control & Automation Dept.
אא א
א
Wאא
Kא/אא– אאאא
א
Wא
W
KE–Fאאא
JP1~ JP6
א
Wאא
אאא
א א
WESFאאאא
אאא
KEאFVCC
C אאא -1
KEאFGND
D אאא -2
אא
א 12
אאא
Practical Class 2 Programming Microcontrollers
:EאFGNDאאאאא •
$regfile = "m128def.dat"
$crystal = 1000000
$baud = 4800
Kא
'-----------------------
Config Porte = Input
Config Portb = Input
Config Portd = Input
Config Portc = Input Kא
Config Porta = Input
'-----------------------
Porta = 255
Portb = 255
Portc = 255
Portd = 255
Porte = 255 אאאא
'-----------------------
Pa_value Alias Pina
Pb_value Alias Pinb
Pc_value Alias Pinc
Pd_value Alias Pind
Pe_value Alias Pine
אאאאא
'----------------------- K
Do
Print "PA= " ; Pa_value
Print "PB= " ; Pb_value
Print "PC= " ; Pc_value
Print "PD= " ; Pd_value
Print "PE= " ; Pe_value
Print "------------"
Wait 1 אא
Loop
End Kאאאא
'-----------------------
Faculty of Electrical and Electronic Eng.
13 Automatic Control & Automation Dept.
אא א
א
:EאFVCCאאאאא •
regfile = "m128def.dat"
$r
crystal = 1000000
$c
baud1 = 4800
$b
K
א
-----------------------
'-
onfig Por
Co rte = Input
onfig Por
Co rtb = Input
onfig Por
Co rtd = Input
onfig Por
Co rtc = Input K
א
onfig Por
Co rta = Input
-----------------------
'-
Po
orta = 0
Po
ortb = 0
Po
ortc = 0
Po
ortd = 0
Po
orte = 0 א
אאא
'-
-----------------------
Pa
a value Alias
A Pina
Pb
b value Alias
A Pinb
Pc
c value Alias
A Pinc
Pd
d value Alias
Pe
A
e value Alias
A
Pind
Pine
א אא
א
א
-----------------------
'- K
pen "com2:" For Binary As #1
Op
o
Do
Print #1
# , "PA= " ; Pa value
v
Prinnt #1 , "PB= " ; Pb value
P
Print #1 , "PC= " ; Pc vallue
Print #1 , "PD= " ; Pd value
Print #1 , "PE= " ; Pe valuee אא
Print #1 , "------------"
Wait 1 Kאאאא
oop
Lo
lose #1
Cl
En
nd
'-
-----------------------
אא
א 14
אאא
Practical Class 2 Programming Microcontrollers
Wאא
Kאאאאא
Wא
W
KJP22~ JP21א
Faculty of Electrical and Electronic Eng.
15 Automatic Control & Automation Dept.
אא א
א
Wאא
א Portd.7 אא Portd.5 א א
Kאא
K2
אK1 א
Wאא
regfile = "m128def.dat"
$r
crystal = 1000000
$c K
א
-----------------------
'-
onfig Por
Co rtd.5 = Output
onfig Por
Co rtd.7 = Output K
א
-----------------------
'-
Re
elay1 Ali
ias Portd.5 א אא
א
א
Re
elay2 Ali
ias Portd.7
'-
----------------------- K
im I As Byte
Di B
-----------------------
'- א
For I = 0 To 9
Set Relay1
t 2
Wait
Set Relay2
t 2
Wait
אא
א 16
אאא