22/09/2016
UARTLibrary
UARTLibrary
mikroCPROforPICLibraries>HardwareLibraries>
UARTLibrary
[Link]
providescomfortableworkwiththeAsynchronous(fullduplex)mode.
YoucaneasilycommunicatewithotherdevicesviaRS232protocol(forexamplewithPC,seethefigureattheendofthe
topicRS232HWconnection).YouneedaPICMCUwithhardwareintegratedUART,[Link],simply
usethefunctionslistedbelow.
Important:
[Link],
simplychangetheletterxintheroutineprototypeforanumberfrom1to2.
SwitchingbetweentheUARTmodulesintheUARTlibraryisdonebytheUART_Set_Activefunction(UART
moduleshavetobepreviouslyinitialized).
[Link],readtheappropriatedatasheetbefore
utilizingthislibrary.
LibraryRoutines
UARTx_Init
UARTx_Data_Ready
UARTx_Tx_Idle
UARTx_Read
UARTx_Read_Text
UARTx_Write
UARTx_Write_Text
UART_Set_Active
GenericRoutines
UART_Data_Ready
UART_Tx_Idle
UART_Read
UART_Read_Text
UART_Write
UART_Write_Text
UARTx_Init
Prototype
voidUARTx_Init(constunsignedlongbaud_rate)
Returns
Nothing.
Description
[Link]
[Link],compilerwill
reportanerror.
Requires
YouneedPICMCUwithhardwareUART.
UARTx_InitneedstobecalledbeforeusingotherfunctionsfromUARTLibrary.
[Link]
1/9
22/09/2016
UARTLibrary
Parameters:
baud_rate:requestedbaudrate
RefertothedevicedatasheetforbaudratesallowedforspecificFosc.
Note:CalculationoftheUARTbaudratevalueiscarriedoutbythecompiler,asitwould
producearelativelylargecodeifperformedonthelibrarylevel.
Therefore,[Link]
thisparameterneedstobeaconstant,andnotavariable.
Example
//InitializehardwareUART1andestablishcommunicationat9600bps
UART1_Init(9600)
UARTx_Data_Ready
Prototype
charUARTx_Data_Ready()
Returns
1ifdataisreadyforreading
0ifthereisnodatainthereceiveregister
Description
Usethefunctiontotestifdatainreceivebufferisreadyforreading.
Requires
[Link]
UARTx_Init.
Example
//Ifdataisready,readit:
if(UART1_Data_Ready()==1){
receive=UART1_Read()
}
UARTx_Tx_Idle
Prototype
Returns
charUARTx_Tx_Idle()
1ifthedatahasbeentransmitted
0otherwise
Description
Usethefunctiontotestifthetransmitshiftregisterisemptyornot.
Requires
[Link]
UARTx_Init.
Example
//Ifthepreviousdatahasbeenshiftedout,sendnextdata:
if(UART1_Tx_Idle()==1){
UART1_Write(_data)
}
[Link]
2/9
22/09/2016
UARTLibrary
UARTx_Read
Prototype
charUARTx_Read()
Returns
Returnsthereceivedbyte.
Description
FunctionreceivesabyteviaUART.UsethefunctionUARTx_Data_Readytotestifdataisreadyfirst.
Requires
[Link]
UARTx_Init.
Example
//Ifdataisready,readit:
if(UART1_Data_Ready()==1){
receive=UART1_Read()
}
UARTx_Read_Text
Prototype
voidUARTx_Read_Text(char*Output,char*Delimiter,charAttempts)
Returns
Nothing.
Description
[Link]
storedintheparameteroutputdelimitersequenceisstoredintheparameterdelimiter.
Thisisablockingcall:thedelimitersequenceisexpected,otherwisetheprocedureexits(ifthe
delimiterisnotfound).
Parameters:
Output:receivedtext
Delimiter:sequenceofcharactersthatidentifiestheendofareceivedstring
Attempts:definesnumberofreceivedcharactersinwhichDelimitersequenceisexpected.
IfAttemptsissetto255,thisroutinewillcontinuouslytrytodetecttheDelimitersequence.
Requires
[Link]
UARTx_Init.
Example
ReadtextuntilthesequenceOKisreceived,andsendbackwhatsbeenreceived:
UART1_Init(4800)//initializeUART1module
Delay_ms(100)
while(1){
if(UART1_Data_Ready()==1){//ifdataisreceived
UART1_Read_Text(output,"OK",10)//readstextuntil'OK'isfound
UART1_Write_Text(output)//sendsbacktext
}
}
UARTx_Write
[Link]
3/9
22/09/2016
UARTLibrary
Prototype
voidUARTx_Write(chardata_)
Returns
Nothing.
Description
ThefunctiontransmitsabyteviatheUARTmodule.
Parameters:
_data:datatobesent
Requires
[Link]
UARTx_Init.
Example
unsignedchar_data=0x1E
...
UART1_Write(_data)
UARTx_Write_Text
Prototype
voidUARTx_Write_Text(char*UART_text)
Returns
Nothing.
Description
[Link].
Parameters:
UART_text:texttobesent
Requires
[Link]
UARTx_Init.
Example
ReadtextuntilthesequenceOKisreceived,andsendbackwhatsbeenreceived:
UART1_Init(4800)//initializeUART1module
Delay_ms(100)
while(1){
if(UART1_Data_Ready()==1){//ifdataisreceived
UART1_Read_Text(output,"OK",10)//readstextuntil'OK'isfound
UART1_Write_Text(output)//sendsbacktext
}
}
UART_Set_Active
Prototype
voidUART_Set_Active(char(*read_ptr)(),void(*write_ptr)(unsignedchardata_),char
(*ready_ptr)(),char(*tx_idle_ptr)())
Returns
Nothing.
[Link]
4/9
22/09/2016
Description
UARTLibrary
SetsactiveUARTmodulewhichwillbeusedbytheUARTlibraryroutines.
Parameters:
read_ptr:UARTx_Readhandler
write_ptr:UARTx_Writehandler
ready_ptr:UARTx_Data_Readyhandler
tx_idle_ptr:UARTx_Tx_Idlehandler
Requires
RoutineisavailableonlyforMCUswithtwoUARTmodules.
UsedUARTmodulemustbeinitializedbeforeusingthisroutine.SeeUARTx_Initroutine
Example
UART1_Init(9600)//initializeUART1module
UART2_Init(9600)//initializeUART2module
RS485Master_Init()//initializeMCUasMaster
UART_Set_Active(&UART1_Read,&UART1_Write,&UART1_Data_Ready,&UART1_Tx_Idle)//setUART1active
RS485Master_Send(dat,1,160)//sendmessagethroughUART1
UART_Set_Active(&UART2_Read,&UART2_Write,&UART2_Data_Ready,&UART2_Tx_Idle)//setUART2active
RS485Master_Send(dat,1,160)//sendthroughUART2
UART_Data_Ready
Prototype
Returns
Description
charUART_Data_Ready()
1ifdataisreadyforreading
0ifthereisnodatainthereceiveregister
Usethefunctiontotestifdatainreceivebufferisreadyforreading.
ThisisagenericroutinewhichusestheactiveUARTmodulepreviouslyactivatedbythe
UART_Set_Activeroutine.
Requires
[Link]
UARTx_Init.
Example
//Ifdataisready,readit:
if(UART_Data_Ready()==1){
receive=UART_Read()
}
UART_Tx_Idle
Prototype
charUART_Tx_Idle()
Returns
1ifthedatahasbeentransmitted
0otherwise
[Link]
5/9
22/09/2016
Description
UARTLibrary
Usethefunctiontotestifthetransmitshiftregisterisemptyornot.
ThisisagenericroutinewhichusestheactiveUARTmodulepreviouslyactivatedbythe
UART_Set_Activeroutine.
Requires
[Link]
UARTx_Init.
Example
//Ifthepreviousdatahasbeenshiftedout,sendnextdata:
if(UART_Tx_Idle()==1){
UART_Write(_data)
}
UART_Read
Prototype
charUART_Read()
Returns
Returnsthereceivedbyte.
Description
FunctionreceivesabyteviaUART.UsethefunctionUART_Data_Readytotestifdataisreadyfirst.
ThisisagenericroutinewhichusestheactiveUARTmodulepreviouslyactivatedbythe
UART_Set_Activeroutine.
Requires
[Link]
UARTx_Init.
Example
//Ifdataisready,readit:
if(UART_Data_Ready()==1){
receive=UART_Read()
}
UART_Read_Text
Prototype
voidUART_Read_Text(char*Output,char*Delimiter,charAttempts)
Returns
Nothing.
Description
[Link]
storedintheparameteroutputdelimitersequenceisstoredintheparameterdelimiter.
Thisisablockingcall:thedelimitersequenceisexpected,otherwisetheprocedureexits(ifthe
delimiterisnotfound).
ThisisagenericroutinewhichusestheactiveUARTmodulepreviouslyactivatedbythe
UART_Set_Activeroutine.
Parameters:
Output:receivedtext
Delimiter:sequenceofcharactersthatidentifiestheendofareceivedstring
Attempts:definesnumberofreceivedcharactersinwhichDelimitersequenceisexpected.
IfAttemptsissetto255,thisroutinewillcontinuouslytrytodetecttheDelimitersequence.
[Link]
6/9
22/09/2016
UARTLibrary
Requires
[Link]
UARTx_Init.
Example
ReadtextuntilthesequenceOKisreceived,andsendbackwhatsbeenreceived:
UART1_Init(4800)//initializeUART1module
Delay_ms(100)
while(1){
if(UART_Data_Ready()==1){//ifdataisreceived
UART_Read_Text(output,"OK",10)//readstextuntil'OK'isfound
UART_Write_Text(output)//sendsbacktext
}
}
UART_Write
Prototype
voidUART_Write(chardata_)
Returns
Nothing.
Description
ThefunctiontransmitsabyteviatheUARTmodule.
ThisisagenericroutinewhichusestheactiveUARTmodulepreviouslyactivatedbythe
UART_Set_Activeroutine.
Parameters:
_data:datatobesent
Requires
[Link]
UARTx_Init.
Example
unsignedchar_data=0x1E
...
UART_Write(_data)
UART_Write_Text
Prototype
voidUART_Write_Text(char*UART_text)
Returns
Nothing.
Description
[Link].
ThisisagenericroutinewhichusestheactiveUARTmodulepreviouslyactivatedbythe
UART_Set_Activeroutine.
Parameters:
UART_text:texttobesent
[Link]
7/9
22/09/2016
UARTLibrary
Requires
[Link]
UARTx_Init.
Example
ReadtextuntilthesequenceOKisreceived,andsendbackwhatsbeenreceived:
UART1_Init(4800)//initializeUART1module
Delay_ms(100)
while(1){
if(UART_Data_Ready()==1){//ifdataisreceived
UART_Read_Text(output,"OK",10)//readstextuntil'OK'isfound
UART_Write_Text(output)//sendsbacktext
}
}
LibraryExample
[Link],itimmediatelysendsitback.
IfPICisconnectedtothePC(seethefigurebelow),youcantesttheexamplefromthemikroCPROforPICterminalfor
RS232communication,menuchoiceToolsTerminal.
CopyCodeToClipboard
charuart_rd
voidmain(){
ANSEL=0//ConfigureANpinsasdigital
ANSELH=0
UART1_Init(9600)//InitializeUARTmoduleat9600bps
Delay_ms(100)//WaitforUARTmoduletostabilize
UART1_Write_Text("Start")
UART1_Write(10)
UART1_Write(13)
while(1){//Endlessloop
if(UART1_Data_Ready()){//Ifdataisreceived,
uart_rd=UART1_Read()//readthereceiveddata,
UART1_Write(uart_rd)//andsenddataviaUART
}
}
}
HWConnection
[Link]
8/9
22/09/2016
UARTLibrary
RS232HWconnection
Copyright(c)[Link].
Whatdoyouthinkaboutthistopic?Sendusfeedback!
[Link]
Wantmoreexamplesandlibraries?
Findthemon
9/9