Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

 

‫א‬ ‫א‬

‫ﺍﻟﺘﺠﺎﺭﺏ ﺍﻟﻌﻤﻠﻴﺔ‬

‫א‬ ‫א‬

Programming
Embedded Systems Microcontroller

You Can Practice Microcontroller Programming Easily Now!


Thursday, October 23, 2008

 
  
‫א‬‫א‬   ‫א‬
‫א‬
 

Esssential Insttructions inn Bascom-AAVR Bascom-AVVR ‫א‬‫א‬

 •
W‫א‬‫א‬
 ‫א‬  ‫אא‬
regfile = "m128def.dat"
$r EATmega1288F‫א‬
‫א‬‫א‬
crystal = 1000000
$c ‫א‬‫א‬
‫א‬‫א‬‫א‬
baud = 9600
$b  
‫א‬‫א‬‫אא‬

 •
 W‫א‬‫א‬
 ‫א‬  ‫אא‬
ait value
Wa e EVValue‫א‬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
 KE1F ‫א‬،E0F 

Le
eds Alias
s Portd ELedsF 
‫א‬EDF‫אא‬‫א‬ 
Le
eds Alias
s Portd.5  ELedF 
‫א‬E5F
‫א‬

 ‫א‬‫א‬   


‫א‬‫א‬    ،   ‫א‬   W
‫א‬  
‫א‬
‫א‬‫א‬
‫א‬K‫א‬‫אא‬


   K‫א‬
‫אא‬‫א‬


‫א‬‫א‬
‫א‬ 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   KE­32,7768 to +32,7677F
im Var4 As
Di A Word   KE0 to 65535F
im Var5 As
Di A Long   KE­2147483648  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 ‫א‬EF
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
 

Exp.1: Scrolling LEDs  ‫א‬‫א‬‫א‬W‫א‬‫א‬

 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
 

Exp.2: Tact Switches  ‫א‬‫א‬W‫א‬‫א‬

 W‫א‬‫א‬
 K‫א‬‫א‬‫א‬
 W‫א‬

 
 W
 KJP6, JP8‫א‬
 
  Faculty of Electrical and Electronic Eng.
 
 7   Automatic Control & Automation Dept.
  
‫א‬‫א‬   ‫א‬
‫א‬
 

 W‫א‬‫א‬

 W‫ א‬‫א‬‫א‬ 


  KE
‫א‬
‫א‬‫א‬‫א‬FGND 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.
  
‫א‬‫א‬   ‫א‬
‫א‬
 

Exxp.3: Expannsion Connnectors E


/F ‫א‬‫א‬‫א‬W‫א‬
 
‫א‬

 W‫א‬‫א‬
 K‫א‬/‫א‬‫א‬– ‫א‬‫א‬‫א‬‫א‬
‫א‬
 W‫א‬
 

 
 W
 
 KE–F‫א‬‫א‬‫א‬
JP1~ JP6
‫א‬

 W‫א‬‫א‬

‫א‬‫א‬‫א‬
 ‫א‬‫ א‬



 WESF‫א‬‫א‬‫א‬‫א‬
‫א‬‫א‬‫א‬
  KE‫א‬FVCC
C ‫א‬‫א‬‫א‬ -1
 KE‫א‬FGND
D ‫א‬‫א‬‫א‬ -2

 
 
 


‫א‬‫א‬
‫א‬ 12   
‫א‬‫א‬‫א‬
 
  Practical Class 2   Programming Microcontrollers
 

 :E‫א‬FGND‫א‬‫א‬‫א‬‫א‬‫א‬ •
$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‫א‬FVCC‫א‬‫א‬‫א‬‫א‬‫א‬ •
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
 

Exp.4: Controlling Relay (On/Off)  W‫א‬‫א‬‫א‬

 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

Reseet Relay1  K‫א‬‫א‬


Waitt 2
Reseet Relay2
Next I
nd
En
'-
-----------------------


‫א‬‫א‬
‫א‬ 16   
‫א‬‫א‬‫א‬
 

You might also like