You are on page 1of 35

‫א‬ ‫א‬

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

‫א‬ ‫א‬

Programming
Embedded Systems Microcontroller

You Can Practice Microcontroller Programming Easily Now!


Monday, April 27, 2009

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

Exxp.26:Seriall Communiications RSS232   ‫א‬


‫א‬W‫א‬
‫ א‬
‫א‬

 W
‫א‬‫א‬
  K‫א‬‫א‬RS2332 ‫א‬‫א‬
‫א‬

 W‫א‬
W
‫א‬
‫• א‬
 K
‫• א‬
  K

‫א‬،‫א‬ ‫א‬ ‫א‬‫א‬


‫א‬ ‫א‬
 ‫א‬ ‫א‬ 
 ‫א‬  ‫א‬ ‫א‬ ‫א‬
 ‫א‬ 
  ‫א‬  
 K 

‫א‬‫א‬،‫א‬‫א‬


‫א‬
‫א‬
‫א‬
 
‫א‬‫א‬
 K‫א‬‫א‬
‫א‬ ،
 Serrial Comm
municatio
ons Paralllel 
Asynchronous Synchronous Co
ommuniccations
• Morse codde telegraphyy o I2C ƒ LPTT
• RS-232 (CCOM Port) o SPI ƒ ISA
A
• RS-423 o PS2 ƒ EIS
SA
• RS-485 ƒ VESA
ƒ ATAA
• USB)
Universal Serial Bus (U
ƒ SCS
SI
• FireWire
ƒ PCII
• Ethernet ƒ PCMMCIA
• Fiber Channnel1 ƒ IEE
EE-1284
• InfiniBandd2 ƒ IEE
EE-488
• MIDI3
• DMX5124
• Serial ATAA5
• SpaceWiree6
• PCI Expreess
• SONET anda SDH7
• T-1, E-18
                                                            
1
Hiigh-speed, for connnecting computeers to mass storagge devices
2
Veery high speed, broadly comparable in scope to PC CI
3
Coontrol of electronnic musical instrum
ments
4
Coontrol of theatricaal lighting
5
Neew replacement forf parallel IDE
6
Sppacecraft communnication network
7
Hiigh speed telecom mmunication overr optical fibers
8
Hiigh speed telecom mmunication overr copper pairs

‫א‬‫א‬
‫א‬ 2   
‫א‬‫א‬‫א‬
 
  Pra
actical Class 9
 9   Prrogramming M
 Microcontrolllers
 

 WRS232 
‫א‬
‫ א‬

DTTE9‫א‬
،‫א‬ ‫א‬
 ‫א‬ 

 KDCE100‫א‬


 
 W‫א‬
‫א‬ 
 

 
 W
TTL‫א‬‫א‬‫א‬‫א‬‫א‬

 K+3V ~ +25V
+ ‫א‬"Spacee"‫א‬‫א‬
‫א‬W“0” ¾
 .-3V ~ -25V ‫א‬"Maark" 
‫א‬‫א‬
‫א‬W“1” ¾
 .-3V ~ +3V ‫א‬
 

W“x” ¾

‫אא‬‫א‬،“GN
ND” ‫אא‬
±25V 
 ‫א‬‫א‬‫א‬W
 K500mA 
‫א‬

 
                                                            
9
Daata Terminal Equuipment (computeer)
10
D Circuit-termiinating Equipmennt (modem)
Data
  Facculty of Electrrical and Electtronic Eng.
 
3    Automatiic Control & A
Automation Dept.
  
‫א‬‫א‬  ‫א‬
‫א‬
 

 WRS2332‫א‬
‫א‬‫א‬‫א‬
 EAdvantaggesF
‫א‬ gesF‫א‬
 EDisadvantag
D
   ‫א‬‫א‬  ‫א‬  9 System-to-Systeem  ‫א‬   
  ×
 K‫א‬‫א‬‫א‬  Chip-2-Chhip 

 50‫א‬
 ‫א‬
 9 .Chip--2-Sensor 

‫א‬ 
‫א‬  
 ،
  
‫א‬
‫א‬ 
  ×
 K


  K
E±225F‫א‬‫א‬

‫א‬
 9 KRS2232<>TTL ‫א‬
 ‫א‬
 ×
 KE“0” , “1”F‫א‬  KSingle
S Masteer/Single Sllave ×
K
 
‫א‬‫א‬
 9 K ×

 

 W
‫א‬RSS232 
‫א‬‫א‬‫א‬

‫א‬‫א‬،Serrial Port “C
COM” RS232 ‫א‬ 
‫א‬
‫א‬

 K‫א‬
P
Port Adddress
CO
OM1 0x33F8
CO
OM2 0x22F8
CO
OM3 0x33E8
CO
OM4 0x22E8

 W ‫א‬CO
‫א‬DB-9Piin‫א‬‫א‬ OMx 
‫א‬‫א‬


 

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

 
 W‫א‬‫א‬E1, 2, 3, …., 9F‫א‬

Pin Name Direction Function Description


1 CD In Control Carrier Detect
2 RXD In Data Receive Data
3 TXD Out Data Transmit Data
4 DTR Out Control Data Terminal Ready
5 GND --- Ground System Ground
6 DSR In Control Data Set Ready
7 RTS Out Control Request to Send
8 CTS In Control Clear to Send
9 RI In Control Ring Indicator
 :CD – Carrier Detect (Control sent from DCE to DTE)
 K‫א‬‫א‬‫א‬‫א‬‫א‬

:RxD – Receive Data (Data sent from DCE to DTE)


 KEDTEF‫א‬‫א‬EDCEF‫א‬‫א‬‫א‬‫א‬‫א‬
EIdle State, “1  or Negative”F‫א‬،‫א‬‫א‬ (Mark state, “0 or Positive”F
 K‫א‬‫א‬‫א‬

:TxD – Transmit Data (Data sent from DTE to DCE)


 KEDCEF‫א‬‫א‬EDTEF‫א‬‫א‬‫א‬‫א‬
EIdle  State, “1 or Negative”F‫א‬،‫א‬EMark state, “0 or Positive”F
 K‫א‬‫א‬

  Faculty of Electrical and Electronic Eng.
 
5    Automatic Control & Automation Dept.
  
‫א‬‫א‬  ‫א‬
‫א‬
 

:DTR – Data Teerrminal Ready (Control sent from DTE to DCE)


‫א‬‫א‬‫א‬ ،‫א‬ ‫א‬ 
  EDTEEF ‫א‬   
 K‫א‬‫א‬‫א‬‫א‬
EDC
CEF
:DSR – Data Set Ready (Control sent from DCE to DTE)
 KEDTEF‫א‬‫א‬‫א‬EDC
CEF ‫א‬
 
 K‫א‬‫א‬E“1”F 
‫א‬،
‫א‬E“0”F 

:RTS – Request To Send (Control sent from DTE to DCE)


،EDTTEF‫א‬‫א‬ 
 ‫א‬ EDC
CEF ‫א‬ 

 K
 ‫א‬‫א‬ ‫א‬‫א‬‫א‬

‫א‬E“1”F ‫א‬
،‫א‬
‫א‬‫א‬E“0”F 

 K‫א‬


:CTS – Clear To Send (Control sent from DCE to DTE)



‫א‬‫אא‬
‫א‬ ‫א‬ EDTE
D F ‫א‬ ‫אא‬

‫א‬‫א‬‫א‬،EDCEF‫ א‬‫א‬
‫א‬‫א‬
K‫א‬‫א‬
‫א‬  E“1”F  ‫א‬ 
   ،‫א‬   ‫א‬ ‫א‬   E“0”F 

  K‫א‬
 ‫א‬

:RI – Ring Indicator (Control sent from DCE to DTE)



  ،‫א‬  EDTE
D F ‫א‬‫א‬  
 K


‫א‬‫א‬‫א‬‫א‬

“TxD
T ” ‫א‬  
 
     ‫א‬
 
    ،
 W‫א‬‫א‬

‫א‬‫א‬‫א‬“RxxD” ‫א‬

 

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

‫א‬     ‫א‬  EHardware  handshakingF     
 W‫א‬‫א‬،ETxD, RxDF‫א‬‫א‬ERTS, CTSF‫א‬

‫א‬‫א‬ CTS‫א‬‫א‬‫א‬‫א‬ ¾
  K
،‫א‬‫א‬ RTS‫א‬‫א‬‫א‬‫א‬‫א‬ ¾
  KE‫א‬FRTS‫א‬
  W‫א‬‫א‬‫א‬‫א‬ ¾
  K‫א‬‫א‬‫א‬‫א‬ •
،  ‫א‬ ‫א‬  ‫א‬  RTS ‫א‬  ‫א‬ ‫א‬    •
  K‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬

 
 WRS232‫א‬‫א‬‫א‬
SPECIFICATIONS  RS232  RS423 
Mode of Operation  SINGLE‐ENDED  SINGLE‐ENDED 
Total Number of Drivers and Receivers on One Line  1DRIVER/1RECVR  1DRIVER/10RECVR 
Maximum Cable Length  50 FT  4000 FT 
Maximum Data Rate  20kb/s  100kb/s 
Maximum Driver Output Voltage  ±25V  ±6V 
Driver Output Signal Level (Loaded Min.)  Loaded  ±5V to ±15V  ±3.6V 
Driver Output Signal Level (Unloaded Max)  Unloaded  ±25V  ±6V 
Driver Load Impedance (Ohms)  3k to 7k  >=450 
Max. Driver Current in High Z State  Power On  N/A  N/A 
Max. Driver Current in High Z State  Power Off  ±6mA @ ±2v  ±100uA 
Slew Rate (Max.)  30V/µS  Adjustable 
Receiver Input Voltage Range  ±15V  ±12V 
Receiver Input Sensitivity  ±3V  ±200mV 
Receiver Input Resistance (Ohms)  3k to 7k  4k min 

‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬
،‫א‬‫א‬Shield24AWG
 K100Ω16PF/FT

  Faculty of Electrical and Electronic Eng.
 
7    Automatic Control & Automation Dept.
  
‫א‬‫א‬  ‫א‬
‫א‬
 

 WEAsynchroonizeF‫א‬‫א‬ESyynchronizeF‫א‬‫ א‬‫א‬
‫ א‬

‫א‬

‫א‬
WE‫א‬‫א‬F‫א‬‫א‬
‫א‬W 
 ‫א‬‫א‬‫א‬،Eclock  or  strobe
s F ‫א‬‫א‬
 KEF ‫א‬‫ א‬  ‫א‬


 
‫א‬
‫א‬‫א‬
‫\א‬‫א‬‫א‬
‫א‬
‫א‬W
 K‫א‬



‫א‬‫א‬
WE‫א‬F ‫א‬‫א‬W 

‫א‬‫אא‬،‫א‬‫אא‬‫א‬‫א‬EStarrt  BitF ‫א‬
‫א‬
‫א‬‫א‬EStop  Bit
B F ‫א‬‫א‬
  K
‫א‬‫א‬‫א‬‫א‬ 
 ‫א‬
‫א‬

 

 

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

،‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬W
  K‫א‬

 W‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬

  KEFull-DuplexF‫א‬EHalf-DuplexF‫א‬W‫א‬ 9
  K6, 7 or 8bit W‫א‬ 9
  KEBaud RateF‫א‬ 9
 ‫א‬‫א‬   ،EParity  BitF ‫א‬   ‫א‬‫א‬   ‫א‬‫א‬  9
  KEEven or OddF‫א‬
 KE1, 1.5 or 2F‫א‬ 9

‫א‬  ‫א‬    WEHalf-DuplexF ‫א‬  ‫א‬
       ،‫א‬ ‫א‬    ‫א‬ 
 K‫א‬
‫א‬‫א‬WEFull-DuplexF‫א‬‫א‬
 K‫א‬‫א‬‫א‬

،‫א‬‫א‬‫א‬WEParity BitF‫א‬
 K‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬

 
‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬“0”‫א‬‫א‬ “Even”‫א‬
  K‫א‬‫א‬K“1”
10110010 > Parity Bit = 0 | 10110110 > Parity Bit = 1
‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬ “0”‫א‬‫א‬ “Odd”‫א‬
  K‫א‬‫ א‬.“1”
10110010 > Parity Bit = 1 | 10110110 > Parity Bit = 0
  Faculty of Electrical and Electronic Eng.
 
9    Automatic Control & Automation Dept.
  
‫א‬‫א‬  ‫א‬
‫א‬
 

 ،

‫א‬‫א‬ ‫א‬ ‫א‬WENF
‫א‬ 

 KASC
CII=127‫א‬ N=7bit 
W
 ‫א‬
،5, 6, 7 or 8bit 


 

  K1, 1.5 orr 2 K‫אא‬‫א‬‫א‬


 WEStop BitF
‫א‬

 

،
 ‫א‬ 
  
‫א‬ 
 ‫א‬ ‫א‬
   WEBaudd  RateF ‫ א‬
 
 W‫א‬

300, 600,
6 1200, 24400, 4800, 9600,
9 192000, 38400, 576600, 1152000, etc…

 W‫א‬‫אא‬
‫א‬‫א‬

 
 

 W‫א‬‫א‬‫א‬‫א‬‫א‬

 
/  
 

 

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

 W‫א‬RS232‫א‬‫א‬

 
  Faculty of Electrical and Electronic Eng.
 
11    Automatic Control & Automation Dept.
  
‫א‬‫א‬  ‫א‬
‫א‬
 

‫א‬RS232 ‫א‬
 W ‫א‬‫ א‬
‫א‬

8250-UART
8 ‫א‬‫א‬‫א‬‫אא‬‫א‬
‫א‬
 K‫א‬‫א‬
‫א‬
‫א‬

‫א‬
‫א‬
  WEIO Output ReggisterF‫א‬/‫ א‬
OR, Input/O 
CO
OM1: 0x3F8
8   |   COM2
2: 0x2F8
 KTxD
D‫א‬‫א‬ RxD ‫א‬
‫א‬  ‫א‬‫א‬‫א‬‫א‬ 

 
  WELCR
R, Line Conntrol RegisteerF‫א‬

CO
OM1: 0x3FB
B   |   COM2
2: 0x2FB
K‫א‬E‫א‬‫אא‬F‫א‬‫א‬

 
RegisterF
  WELSR, Linne Status R ‫א‬ 

CO
OM1: 0x3FD
D   |   COM2
2: 0x2FD
 K‫א‬
‫אא‬‫א‬
‫א‬
‫א‬
‫א‬
‫א‬ 

 
  WEMCR, Modem
M Conntrol RegistterF‫ א‬

CO
OM1: 0x3FC
C   |   COM2
2: 0x2FC
 K8250
‫א‬‫א‬‫א‬
‫א‬
‫ א‬
E‫א‬‫אא‬F‫א‬‫א‬

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

  WEMSR, Modem Status RegisterF‫א‬


COM1: 0x3FE   |   COM2: 0x2FE
‫א‬‫א‬ “dxxx=1” ‫א‬ ‫א‬
 K‫א‬

 
 
 WEIER, Interrupt Enable RegisterF‫א‬
COM1: 0x3F9   |   COM2: 0x2F9
    ‫א‬   E“1” ActiveF ‫א‬   COM ‫א‬ ‫א‬ 
 KCOM2IRQ3‫א‬COM1IRQ4‫א‬‫א‬‫א‬،‫א‬

 

  WEIIR, Interrupt Identification RegisterF‫א‬‫א‬


COM1: 0x3FA   |   COM2: 0x2FA 
K‫א‬‫א‬

 

 
  Faculty of Electrical and Electronic Eng.
 
13    Automatic Control & Automation Dept.
  
‫א‬‫א‬  ‫א‬
‫א‬
 

 ‫אא‬، COM3 ‫א‬


  ‫א‬
 W
 K‫א‬
 ‫א‬‫א‬‫א‬‫אא‬

 

 W‫א‬‫א‬
‫א‬ 8250
‫א‬
‫א‬ 
  ‫א‬‫ א‬‫א‬ 
K"Divisor"،1..8432MHZ 
1.8432
2  10
   
16 

9600bppsW
 ‫؟‬Diviisor  

1.8432  10 1.8432  10
    12

16  16  96600

UART82250 ‫א‬
 W ‫א‬D=12
D ‫אא‬
 KELCRF  Bit7=11 •
‫א‬
0 F ‫א‬‫א‬‫א‬ELSBF‫א‬‫א‬ •
 KE0x3F8
‫א‬‫א‬ •
 KE0xx3F9F‫א‬‫א‬‫א‬EMSBF
‫א‬Bit7=0 •
KELCRF 

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

 WVB, MVS2008.netCOM‫א‬‫א‬
‫א‬‫א‬‫א‬‫א‬،‫א‬‫א‬‫א‬‫א‬ 
  ‫א‬   ‫א‬ ‫א‬‫א‬  ‫א‬  EActiveX & OCX ComponentsF
 ،Bios‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬
 K‫א‬‫א‬‫א‬‫א‬‫א‬

 K‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬

‫א‬EMSCOMM32.ocxF“OCX”“MSComm”‫א‬ VB6
 KCOM‫א‬‫א‬‫א‬

،‫א‬‫א‬ System32‫א‬‫א‬‫א‬W
 K‫א‬‫א‬VB6SP6‫א‬

‫א‬‫א‬ “Standard  EXE”‫א‬‫א‬


‫א‬،‫א‬‫א‬‫א‬ VB6‫א‬‫א‬
 K‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬“Enterprice Edition”
 

  Faculty of Electrical and Electronic Eng.
 
15    Automatic Control & Automation Dept.
  
‫א‬‫א‬  ‫א‬
‫א‬
 


‫א‬‫א‬  VB6 “Microsofft Visual Stuudio 2008” 
 
‫א‬
   ?SerialPort
S ? 
 ‫א‬‫א‬
‫א‬
 .(VB.net, C#.net
C or C+++.netF‫א‬‫א‬
‫א‬‫א‬
‫א‬‫א‬‫א‬.n
net

 
E‫א‬
 
  ‫א‬  SerialPoort ‫א‬ ‫א‬
  F     ‫א‬  ‫א‬  
 KVBB6
‫א‬

WVB6
‫א‬W 
‫א‬ ‫א‬    ‫א‬    
  ‫א‬   ‫א‬  
K‫א‬‫א‬ (COMF
‫א‬
    
‫א‬‫א‬  ‫א‬
 KETest11/ProjRS2322.vbpF 
‫א‬
‫א‬‫א‬

‫א‬
?OutGoinng?‫ א‬


?Send  Data
D ? ‫א‬
 ‫א‬ 
‫א‬‫א‬ ‫א‬ 
  ‫אא‬ ‫א‬ 

K
?InComiing? ‫א‬


‫א‬‫א‬
‫א‬ 16   
‫א‬‫א‬‫א‬
 
  Practical Class 9   Programming Microcontrollers
 

 W‫א‬
Private Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.Settings = "9600,N,8,1"
MSComm1.RThreshold = 1
MSComm1.InputLen = 0
MSComm1.InBufferCount = 0
End Sub

Private Sub cmdClear_Click()


txtOutput.Text = ""
txtInput.Text = ""
End Sub

Private Sub optCOM1_Click()


MSComm1.CommPort = 1
End Sub

Private Sub optCOM2_Click()


MSComm1.CommPort = 2
End Sub

Private Sub optOpen_Click()


MSComm1.PortOpen = True
End Sub

Private Sub optClose_Click()


MSComm1.PortOpen = Flase
End Sub

Private Sub optTXT_Click()


MSComm1.InputMode = comInputModeText
End Sub

Private Sub optBIN_Click()


MSComm1.InputMode = comInputModeBinary
End Sub

Private Sub cmdSendData_Click()


MSComm1.Output = txtOutput.Text & Chr(13)
End Sub

Private Sub cmdExit_Click()


If MSComm1.PortOpen = True Then MSComm1.PortOpen = Flase
End
End Sub

Private Sub MSComm1_OnComm()


Static sBuff As String
If MSComm1.CommEvent = comEvReceive Then
If optBIN.Value = True Then
sBuff = sBuff & StrConv(MSComm1.Input, vbUnicode)
txtInput.Text = sBuff
Else
txtInput.Text = txtInput.Text & MSComm1.Input
End If
End If
End Sub

  Faculty of Electrical and Electronic Eng.
 
17    Automatic Control & Automation Dept.
  
‫א‬‫א‬  ‫א‬
‫א‬
 

 :“MSSComm” ‫א‬
‫א‬‫א‬‫א‬
 
MS
SComm1.Co
ommPort = N
 K‫א‬‫א‬?N? 
‫א‬‫א‬‫ א‬

MS
SComm1.Se
ettings = "Baud,Pa
arity,Bit
ts,Stop"
KE‫א‬،‫א‬،‫א‬،‫א‬F‫א‬‫א‬‫א‬‫א‬ 

MS
SComm1.RT
Threshold = n
،E‫א‬
 F?comEvR  
Receive?‫א‬
 ‫ א‬‫א‬‫א‬‫א‬
 K‫א‬ n=0 
MS
SComm1.In
nputLen = n
‫א‬ n=0
،
‫ א‬‫א‬
‫א‬‫ א‬
 K‫א‬‫א‬
MS
SComm1.In
nBufferSize = n
KE1~1024
1 F ‫א‬
MS utBufferSize = n
SComm1.Ou
KE1~1024F‫א‬
MS nBufferCount = n
SComm1.In
K‫א‬‫א‬
‫א‬
MS utBufferCount = n
SComm1.Ou
K‫א‬‫א‬
‫א‬
MS ortOpen = True | Flase
SComm1.Po F
K‫א‬‫א‬‫א‬| 
MS
SComm1.In nputMode = comInpu utModeTex xt | comI InputMode eBinary
K‫א‬‫א‬ ‫א‬E|F‫א‬
‫א‬‫א‬?Input? ‫א‬‫א‬‫א‬ 
va
ar = MSCo
omm1.InPut
K‫א‬‫א‬
MS utPut = var
SComm1.Ou
K‫אא‬
‫א‬

MS ommEvent = Value
SComm1.Co
 K‫א‬‫א‬
 

Va
alue  
‫א‬
co
omEvCD CD ‫א‬

co
omEvCTS  CTS ‫א‬

co
omEvDSR  DSR ‫א‬

co
omEvRing R ‫א‬
RI ‫א‬

co
omEvRecei
ive  K‫ א‬
RThresho
old ‫א‬
‫א‬‫א‬‫א‬

co
omEvSend  K‫א‬
SThresh
hold ‫א‬‫א‬‫א‬‫א‬

co
omEvEOF  KEvbCrLfF‫ א‬



MSSComm1.DT TREnable = True | Flase


 “0”،‫א‬ “1”
‫א‬ ETrueF KDTR 
‫א‬ ‫א‬ |  | 
K‫א‬
“0” ‫א‬
EFlas
seFK‫א‬
 
MS
SComm1.Ha
andshaking = comNo
one|comRT
TS|comXOn
nXoff|com
mRTSXOnXO
Off
 K‫א‬
 ‫א‬

‫א‬‫א‬
‫א‬ 18   
‫א‬‫א‬‫א‬
 
  Practical Class 9   Programming Microcontrollers
 
MSComm1.RTSEnable = True | Flase
“1”‫א‬ETrueFKHardwareRTS‫א‬‫א‬||
 K‫א‬“0”‫א‬EFlaseFK‫א‬“0”،‫א‬
 
 W‫א‬‫א‬
 W‫א‬‫א‬‫א‬‫א‬
‫א‬ ‫א‬ WEPoling the PortF ‫א‬‫ א‬9
     ‫א‬  ‫א‬     ‫א‬  
K‫א‬
‫א‬‫א‬‫א‬‫א‬‫א‬ 
KEFooter ByteF‫א‬EHeader ByteF‫א‬‫א‬
‫א‬ ‫א‬ ‫א‬ ‫א‬   WEOnComm() eventF ‫א‬‫א‬  ‫א‬ 9
‫א‬‫א‬‫א‬ ‫א‬ OnComm‫א‬
K‫א‬
‫א‬،‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬
K‫א‬‫א‬‫א‬

 

  Faculty of Electrical and Electronic Eng.
 
19    Automatic Control & Automation Dept.
  
‫א‬‫א‬  ‫א‬
‫א‬
 

 WM
Matlab
‫א‬W 

   ،‫א‬ ‫א‬  ‫א‬
  ‫א‬
 
  
  Maatlab ‫א‬   

 ‫א‬
 
‫א‬
 W‫א‬‫א‬
ob
bj = seri
ial('Port','PropertyName',
,Property
yValue,..
..)
Se
er = seri
ial('COM1','BaudRa
ate',9600
0,'DataBi
its',8, 'Parity',
' 'non');
 KE
‫א‬،‫א‬،‫א‬F 
‫א‬‫א‬
‫א‬‫א‬
fo
open(obj)
fo
open(Ser);
 K‫א‬‫א‬
fc
close(obj)
fc
close(Ser
r);
 K‫א‬‫א‬

de
elete(obj)
de
elete(Ser
r);
 K
‫א‬‫א‬‫א‬
‫א‬‫א‬
fp
printf(fi
id, format, A, ...)
fp er,'This is Test');
printf(Se
K‫א‬ ETXTF‫א‬

fw
write(fid
d, format, A, ...)
fw
write(Ser
r,4);
K‫א‬
EBINF ‫א‬

A = fscanf
f(fid, format)
A = fscanf
f(Ser);
K
‫א‬ETXTF ‫א‬‫א‬
A = fread(fid)
A = fread(Ser);
K
‫א‬EBINF
‫א‬‫א‬
 W
‫א‬
se
er = serial('COM
M1','BaudRate',9
9600,'Da
ataBits',
,8);
fo
open(ser)

printf(ser,'This
fp s is Test')
A = fscanf(ser);
fp
printf(ser,A)

fo
or i=1:5
);
fwrite(ser,i)
en
nd
A = fread(ser);
fw
write(ser,A);

close(ser)
fc
elete(ser)
de
cl
lear ser

‫א‬‫א‬
‫א‬ 20   
‫א‬‫א‬‫א‬
 
  Pra
actical Class 9
 9   Prrogramming M
 Microcontrolllers
 

 WTTTL <> RS2332‫א‬


‫א‬‫א‬
 
 RS232 
 ‫א‬ ‫א‬ 
‫א‬ ‫א‬‫א‬
 ‫א‬ 
 
‫א‬ 
‫א‬
  ،  TTL ‫א‬  ‫א‬ 
‫א‬
 K
‫א‬
‫א‬EEAdapterF‫א‬

  ‫א‬


 Max232 ‫א‬ ‫א‬‫א‬
 
 KTTL<>RS2232

 

 

 

 

ERSS232F‫א‬
‫א‬ TTL<>RS2232  ‫א‬
  ‫א‬ 
‫א‬
 KMax2322 ‫א‬‫א‬‫א‬‫א‬EUART
U F 
 

 
 
 
 

  Facculty of Electrrical and Electtronic Eng.
 
21    Automatiic Control & A
Automation Dept.
  
‫א‬‫א‬  ‫א‬
‫א‬
 

ERSS232F‫א‬
‫א‬ TTL<>RS2232 ‫א‬  ‫א‬
‫א‬
 K‫א‬‫א‬ EUART
U F 

 

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

Exp.27: UART Interface   UART‫א‬‫א‬W‫א‬‫א‬‫א‬

 W‫א‬‫א‬
  KUART‫א‬‫א‬‫א‬
 W‫א‬

 

  W‫א‬
 KTxD, RxD‫א‬S312,4‫א‬
 W‫א‬‫א‬
K RS232‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬
.UART‫א‬‫א‬‫א‬‫א‬

  Faculty of Electrical and Electronic Eng.
 
23    Automatic Control & Automation Dept.
  
‫א‬‫א‬  ‫א‬
‫א‬
 

 WEUniiversal Asynnchronous Receiver


R andd Transmittter InterfaceeF UART‫א‬‫א‬
   ‫א‬ ‫א‬   ‫א‬
‫א‬ 
‫א‬ ‫א‬ ‫א‬ 
  ‫א‬  

،TTL‫א‬‫א‬‫א‬
 RS232 
‫א‬‫א‬

 KUARTT‫א‬‫א‬ RS232 
‫א‬‫א‬
‫א‬‫א‬‫אא‬‫א‬

 ،EMCU-M
MCUF 
  
  ‫א‬ ‫א‬   ‫א‬
‫א‬   
 KEMCU-PC
M F
‫א‬

 
 W
 
 A ‫א‬‫א‬‫א‬
‫א‬ AVR 

  KTXD, RXD
D‫א‬‫א‬‫א‬‫א‬WUAART 9
 TXD,
T RXD ‫א‬‫א‬‫א‬‫א‬WUSAART 9
  K‫א‬
  XCK
‫א‬

ART Frame FormatF‫ א‬


 WEUA
RSS232 ‫א‬    
  UA
ART  ‫א‬  ‫א‬ 
‫א‬ 
  
 K‫א‬‫א‬
‫א‬


 
St: Start bit, always
a low.
Daata bits: (0 to
t 8).
P: Parity bit (Can be oddd or even)
Spp: Stop bit, always
a high.
IDDLE: No trannsfers on thhe communiication line (RxD or TxxD), IDLE line is high.

‫א‬‫א‬
‫א‬ 24   
‫א‬‫א‬‫א‬
 
  Practical Class 9   Programming Microcontrollers
 

  WEBaud Rate RegisterF‫א‬

 K0 – 4095‫א‬ UBRRH and UBRR‫א‬ UBRRW 

 9600bps 1Mhz‫א‬ UBRR‫א‬W


 K‫א‬
1000000
,       1  5.510416  6 
  16  9600

،9600‫א‬‫א‬‫א‬
      ‫א‬      ‫א‬ ‫א‬  ‫א‬ 
 K‫א‬‫א‬‫א‬‫א‬‫א‬

‫א‬‫א‬‫א‬‫א‬‫א‬
‫؛‬‫א‬ 0.5%‫א‬،‫א‬
 K5%‫א‬

 W‫א‬‫א‬‫א‬

%      % 

 W‫א‬‫א‬،‫א‬‫א‬W

9600
%    1   100%  7.52% 
8928.571

   ‫א‬ ‫א‬‫א‬    ‫א‬       W
 K‫א‬

  Faculty of Electrical and Electronic Eng.
 
25    Automatic Control & Automation Dept.
  
‫א‬‫א‬  ‫א‬
‫א‬
 

 W‫א‬‫א‬
 ‫א‬  ‫א‬
 
baud = Va
$b ar   .UART0
U (Hardwware) ‫א‬‫א‬‫א‬
Ba
aud = Va
ar K‫א‬ UART0 (Hardware)‫א‬‫א‬

BAUD1 = var
$B v   .UART1
U (Hardwware) ‫א‬‫א‬‫א‬
Ba
aud1 = Var
V K‫א‬ UART1 (Hardware)‫א‬‫א‬

Ba
aud #x , BaudRate   x‫א‬
  UART0 (Sooftware) 


aud1 #x , BaudRate
Ba   x‫א‬
  UART1 (Sooftware) 


rint Var ; "const"


Pr .UART0 
‫א‬‫א‬
‫א‬
rint [#ch
Pr hannel,] Var; "const" .channel ‫א‬ UARTx 
‫א‬‫א‬ ‫א‬
.UARTx ‫א‬‫א‬‫א‬
rintbin Var
Pr V [; Va
arn]
KEF ‫א‬‫א‬
 ‫א‬W[; Var
rn]

.channel ‫א‬ UARTx 


‫א‬‫א‬
‫א‬
rintbin #channel,
Pr # Var [;Varn]
KEF ‫א‬‫א‬
 ‫א‬ W[; Var
rn]

 KUART00 ‫א‬‫א‬ RS2322‫א‬‫א‬‫א‬‫ א‬‫א‬


K‫א‬
‫א‬
W["prompt
t"]
nput ["pr
In rompt"] , Var [,Varn]
 KE،
 F
‫א‬‫א‬WVar
V
KEn=1,2…F ‫א‬
W[,Var
rn]

nput #ch, Var [,V


In Varn ] .chh UART0 ‫א‬ RS2322‫א‬‫א‬‫א‬‫א‬‫א‬
K
UART0 ‫א‬ RS2322‫א‬‫א‬‫א‬‫א‬‫א‬
nputbin Var1
In V [,Va
ar2]
 KEF ‫א‬‫א‬‫א‬W[,Var
r2]

nputbin #channel,
In # Var1[,Var2] .chh UARTx ‫א‬ RS2322‫א‬‫א‬‫א‬‫א‬‫א‬

nputhex ["prompt"
In [ ],Var[,Varn] KHEX UART0 ‫א‬ RS2322‫א‬‫א‬‫א‬‫א‬‫א‬

va
ar = INKE
EY() .UART0 
‫א‬‫ א‬buffer
b 
Ascii
A ‫א‬
UAR
RTx ‫א‬ ‫ א‬buffeer   
  Ascii
A  ‫א‬  
Va
ar = Inke nel)
ey(#chann
  .“0”  ‫א‬‫אא‬ .channel 
‫א‬
 UART0 ‫א‬ ‫ א‬buffer 
     

va
ar = WAIT
TKEY()
 K Ascii ‫א‬
  UARTx ‫א‬‫ א‬buffer 



Va
ar = Wait annel)
tkey(#cha
 K Ascii‫א‬channel ‫א‬
‫א‬ “1”
“   UART0 ‫א‬
 ‫א‬  bufffer  

Va
ar = Isch
harwaitin
ng() ‫א‬‫א‬،“0“ ،

 >‫א‬
‫א‬


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

‫א‬ “1”   UARTx ‫א‬ ‫א‬  buffer  
Var = Ischarwaiting(#channel)
 .“0”
‫א‬ ‫א‬‫א‬  UART1,2 ‫א‬   ‫א‬  
$Timeout = value ‫ و‬Waitkey()  Input ‫א‬   ‫א‬ ‫א‬ ‫א‬  ،Input
K‫א‬‫ א‬buffer،‫א‬

Echo On|off  KInput‫א‬‫א‬‫א‬‫א‬‫א‬|


‫א‬‫א‬‫א‬EcompilerF‫א‬
Config Input = Term,
‫א‬‫א‬‫א‬ECR, LF, CRLF or LFCRF 
Echo = Echo | Noecho
 KInput‫א‬‫א‬‫א‬
‫א‬‫א‬‫א‬‫א‬‫א‬ 
$serialinput2lcd
 .Terminal ‫א‬‫א‬

Config Serialin|Serialin1|Serialin2|Serialin3 = Buffered, Size = Size


[, Bytematch = All|byte|none]
[, Cts = Pin, Rts = Pin, Threshold_full = Num, Threshold_empty = Num]

W  SRAM ‫א‬ Serialin ‫א‬‫א‬‫א‬EInput BufferF‫א‬


SizeMAX= 255
ƒ SERIALIN : 1st UART Hardware Interface > UART0
ƒ SERIALIN1 : 2nd UART Hardware Interface > UART1
ƒ SERIALIN2 : 3rd UART Hardware Interface > UART2
ƒ SERIALIN3 : 4th UART Hardware Interface > UART3
K‫א‬‫א‬‫א‬‫א‬ “ASCII” “Bytematch=byte”
 W‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬
ƒ Serial0CharMatch (for SERIALIN or the 1st UART/UART0)
ƒ Serial1CharMatch (for SERIALIN1 or the 2nd UART/UART1)
ƒ Serial2CharMatch (for SERIALIN2 or the 3rd UART/UART2)
ƒ Serial3CharMatch (for SERIALIN3 or the 4th UART/UART3)
 K‫א‬‫א‬EF‫א‬“Bytematch=all”
ƒ Serial0ByteReceived (for SERIALIN or the 1st UART/UART0)
ƒ Serial1ByteReceived (for SERIALIN1 or the 2nd UART/UART1)
ƒ Serial2ByteReceived (for SERIALIN2 or the 3rd UART/UART2)
ƒ Serial3ByteReceived (for SERIALIN3 or the 4th UART/UART3)
 K‫א‬“Bytematch=none”
 K‫א‬“CTS”‫א‬‫א‬‫א‬WCts = Pin
K‫א‬“RTS”‫א‬‫א‬‫א‬WRts = Pin
‫א‬   ‫א‬    “RTS=1” ‫א‬   ‫א‬ ‫א‬   WThreshold_full = Num
K‫א‬‫א‬‫א‬
‫א‬     ‫א‬       ‫א‬ ‫א‬   WThreshold_empty = Num
 K‫א‬‫א‬“CTS=0”
K“CTS-RTS”‫א‬‫א‬‫א‬‫א‬‫א‬‫א‬

  Faculty of Electrical and Electronic Eng.
 
27    Automatic Control & Automation Dept.
  
‫א‬‫א‬  ‫א‬
‫א‬
 

onfig Ser
Co rialout|Serialout1|Serialo
out2|Seri
ialout3 = Buffere
ed, Size = Size
 W  SRA
AM ‫א‬ Seriaalout  ‫אא‬‫ א‬‫א‬EOutput BBufferF‫אא‬
SizzeMAX= 255
SEERIALOUT : 1st UART Haardware Interfface > UART00
SEERIALOUT1 : 2nd UART Hardware
H Interrface > UART11
SEERIALOUT2 : 3rd UART Hardware Interfface > UART22
SEERIALOUT3 : 4th UART Haardware Interfface > UART33

onfig Com
Co mx = Baud
d , Synchrone = 0|1 , Pari
ity = Non
ne|disabl
led|even|odd ,
St
topbits = 1|2 , Databits
D = 4|6|7|8|9 , Clo
ockpol = 0|1

UART
 K‫א‬“x”  ‫א‬‫א‬‫א‬‫א‬
‫א‬‫א‬‫א‬
.$Bauud‫א‬‫א‬
‫א‬‫א‬
'dumm
my'
،‫א‬
 WBa
aud
 K?‫א‬|‫א‬? ‫א‬
WSynch
hrone = 0|1
0
 KE||
|F‫א‬
‫א‬WParit
ty = None
e|disable
ed|even|o
odd
 K‫א‬WStop
pbits = 1|2
1
 K‫א‬WDa
atabits = 4|6|7|8
8|9
K‫א‬‫א‬
‫א‬
‫א‬‫א‬WCloc
ckpol = 0|1
0
erin Var
Se r , Bts , Port , Pin , Bau
ud , Pari
ity , Dbi
its , Sbi
its
erout Var
Se r , Bts , Port , Pin , Bau
ud , Pari
ity , Dbi
its , Sbi
its
‫א‬
‫א‬ ‫א‬‫א‬
 ‫א‬  | ‫א‬
‫א‬ 
‫א‬‫א‬ ‫א‬
‫א‬ ?Se
erout? ‫א‬
‫א‬
،‫א‬


 K
‫א‬‫א‬‫א‬‫א‬‫א‬
،?Serin
n?‫א‬
‫א‬

 K| ‫אא‬‫א‬‫א‬WVar
V
 K| ‫א‬
‫א‬‫א‬WBts
B
 K‫א‬‫א‬‫א‬WPo
ort
 K
‫א‬‫א‬‫א‬‫א‬
‫א‬WPin
P
 K‫א‬
 WBa
aud
 KENO N=1, ODD=2F ‫א‬‫א‬WPariity
ONE=0, EVEN
KE7,8F‫אא‬WDbi
its
 KE1,2F
‫א‬WSbi
its

 >‫א‬‫א‬
‫א‬ 
‫א‬ 
‫א‬
‫א‬‫א‬W


 K‫א‬‫א‬‫א‬‫א‬
‫א‬
‫א‬‫א‬W



‫א‬‫א‬
‫א‬ ‫ א‬‫א‬ ‫א‬
‫א‬‫א‬‫א‬W

 KEData Bus
B F
ts=0 ‫א‬،
.Bt ‫א‬W

 Wn ‫א‬‫א‬‫א‬
‫א‬
Va
ar = Bufs
space(n) n=0 : output buffeer 1st UART | n=1 : input bbuffer 1st UARRT
n=2 : output buffeer 2nd UART | n=3 : input buffer 2nd UAART

‫א‬‫א‬
‫א‬ 28   
‫א‬‫א‬‫א‬
 
  Practical Class 9   Programming Microcontrollers
 
Clear Serialin  KE‫א‬|‫א‬F‫א‬‫א‬‫א‬
Clear Serialout
 WEUARTF‫א‬
 W"device"
 KE"COM1:"F Comx ‫א‬‫א‬W"Hardware"‫א‬‫א‬
 W‫א‬‫א‬W"Software"‫א‬‫א‬
Open "device" For Mode As #channel "COMpin:Speed,N,Parity,Stopbits[,Inverted]" 
 W
Examples:  KE|F‫א‬‫א‬‫א‬‫א‬WCOMpin
'open hardware UART  KEBaudF‫א‬WSpeed
Open "com1:" For Binary As #1
 KE6,7,8 or 9F‫א‬‫א‬‫א‬WN
'open a Transmit channel for output
(software UART)
Open "comd.1:9600,8,n,1" For Output As #2
 K(||) N|O|E <Parity
'open a Receive channel for input  KE1|2F‫א‬WStopbits
(software UART)
Open "comd.0:9600,8,n,1" For Input As #3  K‫א‬‫א‬W[,Inverted]
 WWMode
 KCom1, Com2  Binary or RandomW"Hardware"‫א‬‫א‬
.‫א‬‫א‬ OutputInputW"Software"‫א‬‫א‬
 K‫א‬W#channel
Close #channel  Esoftware UARTF‫א‬‫א‬‫א‬
Get #channel, Var  K‫א‬‫א‬EHW or SWF‫א‬
Put #channel , Var  K‫א‬‫א‬EHW or SWF
Enable | Disable Urxc .UART‫א‬‫א‬‫א‬‫א‬|
On Urxc Rx_isr   K‫א‬‫א‬‫א‬‫א‬
Enable | Disable Utxc .UART‫א‬‫א‬‫א‬|
On Utxc Tx_isr  K‫א‬‫א‬‫א‬‫א‬
Enable | Disable Udre .UART‫א‬‫א‬‫א‬|
On Udre Empty_isr  K‫א‬‫א‬‫א‬‫א‬
Enable | Disable Serial K‫א‬UART‫א‬‫א‬|
$dbg  K‫א‬‫א‬‫א‬EDebuggingF‫א‬
Dbg

 K‫א‬(;)‫א‬‫א‬‫א‬EPrintFW

 .Print Chr (var); Printbin ‫א‬W

  ‫؛‬        Printbin  ‫א‬ ‫א‬‫א‬  W
 KArrEF‫א‬‫א‬‫א‬
Printbin Arr(1) ; 10 

‫؛‬ Inputbin‫א‬‫א‬‫א‬ W


 KArr‫א‬‫א‬‫א‬‫א‬
Inputbin Arr(1) , 10 
  Faculty of Electrical and Electronic Eng.
 
29    Automatic Control & Automation Dept.
  
‫א‬‫א‬  ‫א‬
‫א‬
 

 >
‫א‬‫א‬

Inputtbin‫א‬W

‫א‬‫א‬EPrint #channelF ‫א‬‫א‬‫א‬  W


‫א‬‫א‬ OP
PEN  &  CLOSE ‫א‬‫א‬
‫א‬EInput #channe
elF

  K‫א‬

‫א‬

    ‫א‬‫א‬ ‫א‬  COM


M1  EOpeen ice"F
"devi  ‫א‬‫א‬  W
 KOPE
EN & CLOSE ‫א‬‫א‬



 ، ‫א‬
  ‫א‬  valuee ‫א‬  $Timeout
  lue ‫א‬  W
= val

 W‫א‬‫א‬‫א‬


eout = value
$Time v
  fOSC Value = 100 V
Value = 1000
0 Va
alue = 10000
0 Vallue = 100000
0

1MHZ 1.6ms 16ms 160ms 1600ms


2MHZ 0.8ms 8ms 80ms 800ms
4MHZ   0.4ms 4ms 40ms 400ms
8MHZ   0.2ms 2ms 20ms 200ms
16MHZ   0.1ms 1ms 10ms 100ms
 K‫א‬ASC
CII‫א‬
‫א‬

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

 WInput‫א‬‫א‬‫א‬
$regfile = "m8def.dat"
$crystal = 4000000
$baud = 9600
'----------------------
Dim Num1 As Integer
Dim Num2 As Integer
Dim Sum As Integer
'----------------------
Do
Num1 = 0 : Num2 = 0
Input "Enter 1st Number: " , Num1
Input "Enter 2nd Number: " , Num2
Sum = Num1 + Num2
Print "Sum: " ; Sum
Loop 
End 

 WInputHex‫א‬‫א‬‫א‬
$regfile = "m8def.dat"
$crystal = 4000000
$baud = 9600
'----------------------
Dim Num1 As Byte, Num2 As Byte, Sum As Word
'----------------------
Do
Num1 = 0 : Num2 = 0
Inputhex "Enter 1st Number as two-character hex-code: " , Num1
Inputhex "Enter 2nd Number as two-character hex-code: " , Num2
Sum = Num1 + Num2
Print "Sum Dec:" ; Sum
Print "Sum Hex:" ; Hex(sum)
Print "-------------"
Loop
End 

 WWaitkey()‫א‬‫א‬‫א‬
$regfile = "m8def.dat"
$crystal = 4000000
$baud = 9600
'----------------------
Dim Inchar As Byte
'----------------------
Do
Inchar = Waitkey()
Print Inchar
Loop Until Inchar = " "
Print "END..." Pressed keys is:
End 
123456ABCE

 

  Faculty of Electrical and Electronic Eng.
 
31    Automatic Control & Automation Dept.
  
‫א‬‫א‬  ‫א‬
‫א‬
 

 WInput, Inputhex ‫א‬‫א‬


  
‫א‬

regfile = "m8def.da
$r at"
crystal = 4000000
$c
baud = 960
$b 00
'----------------------
Dim Num1 As
s Byte, Nu
um2 As Byt
te
Dim Sum As Integer, Arr(2) As
s Byte
'----------------------
Do
o
Num1 = 0 : Num2 = 0
Enter DEC: " , Num1
Input "E 1 , Num2
Sum = Nu
um1 + Num2
2
Print "S
Sum: " ; Sum
S
Print "-----------"

x "Enter HEX:
Inputhex H ", Nu
um1, Num2
Sum = Nu
um1 + Num2
2
Print "S
Sum: " ; Sum
S
Print "-----------"
; 
Lo
oop
nd 
En

 WUART0
U 
‫א‬
regfile = "m8def.da
$r at"
crystal = 4000000
$c
baud = 19200
$b
'----------------------
onfig Seri
Co ialin = Bu
uffered , Size = 10
0
onfig Seri
Co ialout = Buffered
B , Size =10
0
En
nable Inte
errupts
'----------------------
Dim Arr(10) As Byte
'----------------------
aud = 9600
Ba 0
Do
o
arwaiting() = 1 The
If Ischa en
tbin Arr(1
Input 1) , 10
tbin Arr(1
Print 1) , 10
ms 10
Waitm

r Serialin
Clear n 
r Serialou
Clear ut
End If
Lo
oop
nd 
En

 W‫א‬‫א‬‫א‬
 
regfile = "m8def.da
$r at"
crystal = 4000000
$c
baud = 960
$b 00
'----------------------
nable Urxc
En c
n Urxc Get
On tchar
En
nable Inte
errupts
'----------------------
Dim Inchar As String
g * 1
'----------------------
Do
o
nop
oop Until Inchar = " "
Lo
'----------------------


Ge
etchar:
Inchar = Inkey() : Print Inchar
I
eturn 
Re

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

 WUrxc, Utxc, Udre‫א‬‫א‬


$regfile = "m8def.dat"
$crystal = 4000000
$baud = 9600
'----------------------
Enable Urxc
Disable Utxc
Disable Udre
On Urxc Getchar
On Utxc Finish
On Udre Empty
Enable Interrupts
'----------------------
Dim Inchar(10) As Byte
Dim Flag As Bit
'----------------------
Do
nop
Loop Until Flag = 1
End
'----------------------
Getchar:
Disable Urxc
Enable Udre
Enable Utxc

Inputbin Inchar(1) , 10
Printbin Inchar(1) ; 10
Return
'----------------------
Finish:
Disable Utxc : Set Flag
Print "Serial TX complete interrupt!"
Return
'----------------------
Empty:
Disable Udre : Print ""
Print "UART Register is Empty!"
Return 

 

 

 

 

 

 

  Faculty of Electrical and Electronic Eng.
 
33    Automatic Control & Automation Dept.
  
‫א‬‫א‬  ‫א‬
‫א‬
 

 W‫א‬‫א‬‫א‬‫א‬UAR
RT0, UARTT1‫א‬‫א‬

regfile = "m128def.dat"
$r
crystal = 4000000
$c
baud = 960
$b 00
baud1 = 9600
$b
'-------------------------
onfig Com1
Co 1=Dummy, Synchrone
S =0, Parit
ty = None, Stopbits
s =1, Data
abits =8, Clockpol=
=0
onfig Com2=Dummy, Synchrone
Co S =0, Parit
ty = None, Stopbits
s =1, Data
abits =8, Clockpol=
=0

pen "com1:" For Bin


Op nary As #1
1
pen "com2:" For Bin
Op nary As #2
2

onfig Seri
Co uffered , Size = 20
ialin = Bu 0 , Bytema
atch = 27
onfig Seri
Co ialin1 = Buffered
B , Size = 20
2 , Bytemmatch = Al
ll

onfig Seri
Co ialout = Buffered
B , Size = 20
2
onfig Seri
Co ialout1 = Buffered , Size = 20
En
nable Inte
errupts
'-------------------------
Dim Msg As String * 10
'------------------------
Do
o
arwaiting() = 1 The
If Ischa en
t Msg : Pr
Input rint Msg
End If

arwaiting(#2) = 1 Then
If Ischa T
t #2 , Msg
Input g : Print #2 , Msg
End If
Lo
oop
En
nd
'------------------------
Se
erial0char
rmatch:
Print "E
Esc Char!"
Re
eturn
'------------------------
Se
erial1byte
ereceived:
Print #2 , " "
Print #2 , "We go
ot a Char!"
Re
eturn
'------------------------
Close #1
Close #2 

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

 W‫א‬‫א‬‫א‬
$regfile = "m8def.dat"
$crystal = 4000000
'------------------------
Ucsrb = 0 : Wait 1
Dim Value As Byte
'------------------------
Open "comb.0:9600,8,n,1" For Output As #1
Open "comb.1:9600,8,n,1" For Input As #2

Open "comc.0:9600,8,n,1" For Output As #3


Open "comc.1:9600,8,n,1" For Input As #4
Open "comd.6:9600,8,n,1" For Output As #5
Open "comd.7:9600,8,n,1" For Input As #6
'------------------------
Print #1 , "SW UART1, " ; "Enter a value"
Input #2 , Value
Print #1 , "value is: " ; Value
Print #3 , "SW UART2, " ; "Enter a value"
Input #4 , Value
Print #3 , "value is: " ; Value
Print #5 , "SW UART3, " ; "Enter a value"
Input #6 , Value
Print #5 , "value is: " ; Value
'------------------------
Get # 2 , A : Put # 1 , A
Do
Value = Inkey(#2)
If Value > 0 Then Print #1 , "SW UART1:" ; Chr(value)

Value = Inkey(#4)
If Value > 0 Then Print #3 , "SW UART2:" ; Chr(value)
Value = Inkey(#6)
If Value > 0 Then Print #5 , "SW UART3:" ; Chr(value)
Loop Until Inkey(#2) = 1 Or Inkey(#4) = 1 Or Inkey(#6) = 1
'------------------------
Close #6 : Close #5 : Close #4
Close #3 : Close #2 : Close #1
End 

 
 

  Faculty of Electrical and Electronic Eng.
 
35    Automatic Control & Automation Dept.

You might also like