Uart
Uart
***
***
***
***
***
***
***
***
***
***
***
***
***
*****
****
***
****
***
***
***
***
***
***
***
***
***
***
***
***
***
***
**
Expl
oreEmbedded
*
* *
*****
*********
* **
***
******
*****
***
****
******
***
****
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
*
*Fil
e: uar t
.c
*Versi
on: 15.0
*Author:ExploreEmbedded
*Website:http:
//www. expl
oreembedded.com/wiki
*Descri
ption:Filecontai
nstheLibr
aryrouti
nesforUART
Thel i
brari
eshavebeent est
edonExpl or
eEmbeddeddev el
opmentboar ds.West ronglybeli
eve
thatthe
l
ibrary wor ks on any of dev el
opment boar ds for r especti
ve cont r
oll
ers. Howev er
,
ExploreEmbedded
discl
aimsanykindofhardwarefai
l
ureresul
ti
ngoutofusageofl ibr
ari
es,dir
ectl
yorindirect
ly.
Fil
esmaybesubj ectt
ochangewit
houtpri
ornot
ice.Ther
ev i
sionhistorycontai
nstheinformati
on
relat
edt oupdat
es.
GNUGENERALPUBLI CLICENSE:
Thisprogram i
sfreesoftware:youcanredist
ri
buteitand/
ormodi f
y
i
tundert heter
msoft heGNUGener alPubli
cLicenseaspubli
shedby
theFreeSoftwareFoundation,ei
therv
ersion3oft heLi
cense,or
(atyouropti
on)anylaterversi
on.
Thi
spr
ogram i
sdist
ri
butedinthehopethatitwil
lbeuseful
,
butWI
THOUTANYWARRANTY; wit
houtevent hei
mpliedwarr
ant
yof
MERCHANTABILI
TYorFI TNESSFORAPARTI CULARPURPOSE. Seethe
GNUGeneral
Publ
icLicenseformoredetai
ls.
Youshoul
dhav er
ecei
vedacopyoftheGNUGener
alPubli
cLicense
al
ongwiththi
sprogr
am. Ifnot
,see<ht
tp:
//www.
gnu.
org/l
icenses/
>.
Er
ror
sandomissi
onsshoul
dbereport
edtocodel
ibrari
es@expl
oreembedded.
com
*
***
***
***
***
****
***
***
****
***
***
***
*****
***
***
***
* *
* *
****
***
***
*****
***
****
***
***
***
***
***
***
***
*/
/
***
***
***
***
***
***
***
***
***
***
***
***
****
***
***
* *
****
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
*
Revi
si
onHistory
***
* *
****
****
*******
*****
*****
***
***
*****
*****
***
*****
***
****
***
****
*****
***
***
***
****
***
***
***
***
**
15.0:I
nit
ial
version
15.1:Updat
edt heUART_ TxNumberfuncti
ontodispl
ayBin/
Dec/Hexnumbers.
Removedt hefuncti
onsUART_TxHexNumberandUART_ TxBinar
yNumber.
**
***
***
****
******
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
/
#i
ncl
ude<stdarg.
h>
#i
ncl
ude"uart.
h"
#i
ncl
ude"gpio.
h"
#def
ineC_
MaxUart
Channel
s_U84u
#def
ineC_
Uar
tOne_U8 1u
constuar
tChannel
Conf
ig_
stUar
tConf
ig[
C_MaxUar
tChannel
s_U8]
=
{/ *TxPi
nRxPi nUART_PinFun PCONBi tAssoci
atedUARTSt r
uctur
e *
/
{P0_ 2,P0_3,PINSEL_FUNC_ 1, 3 ,(
LPC_UART_TypeDef* )
LPC_UART0_BASE}
,/*
Confi
gur
eP0_ 2,
P0_3forUART0f uncti
on*/
{P2_ 0,P2_1,PINSEL_FUNC_ 2, 4 ,(
LPC_UART_TypeDef* )
LPC_UART1_BASE}
,/*
Confi
gur
eP2_ 0,
P2_1forUART1f uncti
on*/
{P0_ 10,
P0_11,
PINSEL_FUNC_ 1, 24 ,(
LPC_UART_TypeDef* )
LPC_UART2_BASE}
,/*
Confi
gur
eP0_ 10,
P0_11forUART2f uncti
on*/
{P0_0,P0_ 1,PINSEL_FUNC_2, 25 ,
(LPC_UART_TypeDef*)LPC_UART3_BASE} /*
Confi
gur
eP0_ 0,
P0_1forUART3f uncti
on*/
};
/
***
***
***
***
***
***
***
***
***
***
***
****
*****
***
****
***
***
****
*****
***
*****
***
***
*****
****
***
***
***
***
voi
dUART_Ini
t(
uint
8_tv_
uart
Channel_
u8,ui
nt32_tv
_baudRate_
u32)
*
* *
*****
*****
***
****
** *
***
****
***
****
***
***
***
***
****
* *
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
*I/PArguments:
uint32_t:
Baudrat
etobeconfi
gured.
*Returnval
ue :none
*descr
ipt
ion :
Thi
sfuncti
onisusedt oinit
ial
i
zetheUARTatspeci
fi
edbaudr
ate.
I
ftherequestedbaudr at
eisnotwi
thi
nthesuppor
tedr
angethen
t
hedefaultbaudrateof9600isset.
Ref
eruar
t.
hfi
l
eforSuppor
ted(
range)baudr
ates.
*
****
****
***
** *
****
***
******
*****
*****
* **
****
*******
******
****
* *
*****
***
***
***
***
***
***
***
***
***
***
/
v
oidUART_Init
(ui
nt8_
tv _
uartChannel
_ u8,ui
nt32_tv_baudRate_u32)
{
if
(v_
uartChannel
_u8<C_ MaxUartChannels_U8)
{
GPI
O_Pi
nFunct
ion(
Uar
tConf
ig[
v_uar
tChannel
_u8]
.TxPi
n,
Uar
tConf
ig[
v_uar
tChannel
_u8]
.Pi
nFunSel
);
GPI
O_Pi
nFunct
ion(
Uart
Confi
g[v
_uar
tChannel
_u8]
.RxPi
n,
UartConfi
g[v
_uar
tChannel
_u8]
.Pi
nFunSel
);
ut
il
_Bi
tSet
(LPC_SC-
>PCONP,Uart
Confi
g[v_
uar
tChannel_
u8].
pconBi
t);
/*EnableFIFOandresetRx/TxFI
FObuff
ers*
/
UartConfi
g[v_
uar
tChannel_
u8].
UARTx-
>FCR = (
1<<SBI
T_FI
FO) |(
1<<SBI
T_RxFI
FO) |
(
1<<SBI
T_TxFIFO);
/*8bi
tdata,
1Stopbi
t,Nopari
ty*/
Uart
Confi
g[v_
uart
Channel
_u8]
.UARTx-
>LCR = (
0x03<<SBI
T_Wor
dLenght
) |
(
1<<SBI
T_DLAB);
UART_
Set
BaudRat
e(v
_uar
tChannel
_u8,
v_baudRat
e_u32)
;
}
}
/
* *
*****
*****
****
*****
****
*****
****
****
******
*****
*****
*********
****
****
****
******
******
****
* *
*****
*
v
oidUART_ Set
BaudRate(uint
8_tv_uartChannel
_u8,ui
nt32_tv_baudRate_u32)
***
*****
*****
****
*****
*****
****
****
****
******
******
*****
* *
******
****
****
*****
*****
******
******
*****
*I/PArguments:ui
nt32_t:v_
baudRate_u32tobeconf igured.
*Returnval
ue :none
*descr
ipt
ion :
Thi
sfuncti
onisusedt oSet
/Changet
hebaudr
ateonthefl
y.
I
ftherequestedbaudrat
eisnotwi
thi
nthesuppor
tedr
anget
hen
t
hedefaultbaudrat
eof9600isset
.
Ref
eruar
t.
hfi
l
eforSuppor
tedr
angeofbaudr
ates.
*
****
*****
*****
* *
*****
****
****
****
*****
****
*****
***
* *
*****
***
****
***
***
*****
***
***
***
***
***
***
***
**/
v
oidUART_ SetBaudRate(
uint
8_tv_uar
tChannel
_u8,uint
32_tv_
baudRate_
u32)
{
uint
32_tv_UartPcl
k_u32,
v_Pcl
k_u32,
v_RegValue_u32;
i
f(
v_uartChannel_u8<C_ MaxUar t
Channels_
U8)
{
/**BaudRat eCal cul
ati
on:
PCLKSELxr egist
erscontai
nst hePCLKinfoforal
l t
heclockdependentper
ipher
als.
Bit6,
Bit7containstheUartClock(i
e.UART_PCLK)infor
mat i
on.
TheUART_ PCLKandt heactualPeri
pheral
Clock(
PCLK)i scal
cul
atedasbelow.
(Referdatasheetf ormoreinfo)
v
_Uar
tPcl
k_u32=(
LPC_
SC-
>PCLKSEL0>>6)&0x03;
swit
ch(v_Uart
Pcl
k_u32)
{
case0x00:
v_
Pclk_
u32=Sy st
emCor
eCl
ock/
4;
br
eak;
case0x01:
v_
Pclk_
u32=Sy st
emCor
eCl
ock;
br
eak;
case0x02:
v_
Pclk_
u32=Sy st
emCor
eCl
ock/
2;
br
eak;
case0x03:
v_
Pclk_
u32=Sy st
emCor
eCl
ock/
8;
br
eak;
}
v
_RegVal
ue_
u32=(v
_Pcl
k_u32/(
16*v
_baudRat
e_u32)
);
Uar
tConf
ig[
v_uar
tChannel
_u8]
.UARTx-
>DLL=uti
l
_Extr
actByt
e0to8(v_
RegValue_u32);
Uar
tConf
ig[
v_uar
tChannel
_u8]
.UARTx-
>DLM =ut
il
_Ext
ractBy
te8to16(
v_RegValue_u32)
;
ut
il
_Bi
tCl
ear(
Uar
tConf
ig[
v_uar
tChannel
_u8]
.UARTx-
>LCR,SBI
T_DLAB)
;//Cl
earDLAB
af
terset
ti
ngDLL,DLM
}
}
/
* *
*****
*****
****
******
* *
****
******
*****
*****
********
* *
********
*****
****
****
****
****
****
****
****
****
charUART_ RxChar(
uint8_tv_uartChannel_
u8)
***
*****
*****
****
********
****
*******
****
******
* *
*******
* **
*****
*****
****
****
****
****
****
****
****
***
*I/PArguments:none.
*Returnval
ue :char:Ascii
valueofthechar act
err eceiv
ed
*descr
ipt
ion:
Thi
sfunct
ionisusedtorecei
veacharfr
om UARTmodule.
I
twaitsti
l
lacharisreceiv
edandret
urnsi
taft
err
ecepti
on.
***
******
*****
****
******
***
***
***
****
***
***
****
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
***
*/
charUART_ RxChar(
uint
8_tv_
uart
Channel
_u8)
{
charch=0;
i
f(
v_uar
tChannel_u8<C_ MaxUart
Channel
s_U8)
{
/
*Wai tti
llthedataisrecei
ved*/
whil
e(ut
il
_IsBitCl
eared(Uart
Conf
ig[
v_uar
tChannel
_u8]
.UARTx-
>LSR,SBI
T_RDR)
);
ch=Uar t
Conf i
g[v_uar
tChannel
_u8].
UARTx->RBR;
//Copytherecei
veddata
}
r
etur
nch;
}
/
***
***
***
***
***
***
***
***
***
****
***
***
***
***
****
****
***
*****
****
****
***
***
***
***
***
***
***
***
***
***
**
voi
dUART_TxChar(
charv_uar
tDat
a_u8)
*
* *
*****
*****
***
****
** *
****
****
****
***
***
****
****
* *
*****
***
***
*****
***
****
***
***
***
***
***
***
***
***
**
*I/PArguments:
char -
->Asci
ival
ueofthecharactert
obetransmit
ted.
*Returnval
ue :none.
*descr
ipt
ion :
Thi
sfunct
ioni
susedt
otr
ansmi
tachart
hroughUARTmodul
e.
*
****
****
***
* ***
************
*******
****
***
*******
****
****
****
*****
***
******
*****
***
***
***
***
***
***
*/
v
oidUART_TxChar (uint8_tv_uart
Channel_
u8,charv_uart
Data_u8)
{
if
(v_
uartChannel_ u8<C_ MaxUar t
Channels_U8)
{
whi
le(uti
l_
IsBitCleared(Uart
Confi
g[v_
uartChannel
_u8].
UARTx->LSR,SBI
T_THRE)
);// Wait
f
orPrevi
oust r
ansmi ssion
Uart
Conf
ig[
v_uar
tChannel
_u8]
.UARTx-
>THR=v
_uar
tDat
a_u8; /
/Loadt
hedat
ato be
t
ransmi
tt
ed
}
}
/
***
***
***
***
***
***
***
***
***
****
***
***
***
***
******
* *
****
****
* *
******
****
****
***
***
***
***
***
***
***
***
voi
dUART_TxStr
ing(char*pt
r_str
ingPoint
er_
u8)
*
* *
*****
*****
***
****
** *
***
***
*****
****
****
******
****
***
****
***
*****
***
***
***
***
***
***
***
***
***
***
***
*I/PArguments:
String(
Addressofthestr
ing)tobetr
ansmit
ted.
*Returnval
ue :none
*descr
ipt
ion:
Thisfuncti
onisusedt otr
ansmitaNULLter
mi nat
edstr
ingt
hroughUART.
1.
Thept r
_str
ingPoint
er_u8poi
ntst
othefi
rstcharoft
hestri
ng
andtr
aversesti
llt
heend(NULLCHAR)andt r
ansmit
sachareacht
ime
*****
****
******
** *
*****
******
*****
******
******
*****
*****
*****
******
*****
****
***
****
* *
***
***
***
****
*/
#if((
Enable_UART_ TxStr
ing==1)|
|(Enable_
UART_ Pri
ntf==1))
voidUART_ TxStri
ng(ui
nt8_tv_uart
Channel_u8,char*ptr
_str
ingPoint
er_u8)
{
whil
e(*ptr
_stri
ngPointer
_u8)
UART_ TxChar(v
_uartChannel_
u8,* ptr
_str
ingPoint
er_u8++);
//Loop through t
he st
ri
ng
andt r
ansmitcharbychar
}
#endif
/
***
***
***
***
***
***
***
***
***
***
****
*****
***
*****
*****
****
****
*****
***
*****
****
*****
***
***
***
** *
***
**
ui
nt8_tUART_RxStr
ing(
uint
8_tv_uar
tChannel
_u8,char*
ptr
_st
ring)
****
****
*****
* *
****
****
***
****
***
*****
***
***
***
*****
*****
****
***
***
****
****
***
***
***
****
****
***
***
**
*I/PAr guments:char*: Addressofthestri
ng/buff
erwheretherecei
veddataneedstobe
stor
ed
*Returnvalue : ui
nt8_
t:Numberofchar
srecei
ved.
*descripti
on :
1.Thi
sf unct
ion i
s used tor ecei
ve a ASCIIstr
ing through UART til
lthe
carr
iage_ret
urn/
New_ l
ine
2.Thestream ofdataiscopied tothebufferti
l
lcarriage_retur
n/New_
li
neis
encountered.
3.Once t he Car
riage_
ret
urn/
New_ Li
ne i
sr ecei
ved the string wil
lbe nul
l
ter
mi nat
ed.
*
* *
**NOTE**
****
*:
1.
Therecei
vedchari
sECHOEDback,
ifnotr
equi
redt
hencommentUART_
TxChar
(ch)i
nthecode.
2.
BackSl
ashi
snottakencar
e.
**
*******
* ****
*****
*****
********
*******
****
****
* *
******
*****
***
****
***
*****
***
****
***
***
***
***
***
**/
#i
f(Enabl e_UART_RxStri
ng==1)
ui
nt8_tUART_ RxStr
ing(
uint8_tv_uart
Channel_u8,char*ptr
_st
ri
ng)
{
charch;
ui
nt8_ tl
en=0;
whil
e( 1)
{
ch=UART_ RxChar (
v_uartChannel
_u8); / /Recei
veachar
UART_ TxChar(v_uart
Channel_u8,
ch); /
/Echobacktherecei
vedchar
i
f((
ch==' \r
')||
(ch=='\n'
))//
readti
ll
enterkeyispressed
{
i
f(l
en!=0) /*Waitt
il
latl
east1charisrecei
ved* /
{
ptr_stri
ng[l
en]=0; //onceenterkeyispressednul
lter
minatethe
st
ringandbreakt heloop
break;
}
}
el
sei f
((ch=='\b'
)&&( l
en!
=0))
{
l
en- -
; / /I
fbackspaceispressedthendecrementtheindextor
emov et
heoldchar
}
el
se
{
ptr
_ stri
ng[l
en]=ch;//
copythecharintostri
ngandincrementthei
ndex
l
en++;
}
}
r
eturnl
en;
}
#endif
/***
****
*****
*****
*****
*******
* *
****
******
****
* *
****
******
****
*****
*****
********
****
******
*****
* *
***
void UART_ TxNumber (
uint8_t v _numericSyst
em_ u8, ui nt
32_t v _number_u32, ui nt8_t
v_numOfDigit
sToTransmit_u8)
****
****
*****
*****
*****
*********
****
******
******
****
******
****
******
****
* *
******
****
******
********
*
*I/PArguments:
ui
nt8_t: speci f
iestypeofnumberC_ BINARY_ U8(2),
C_DECIMAL_ U8(10),
C_HEX_U8(16)
ui
nt32_t:Numbert obetransmitt
edonUART.
ui
nt8_t:Numberofdi git
st obetransmit
ted
*Ret
urnv
alue :
none
*descr
ipt
ion :
Thi
sfunct
ioni
susedt
otr
ansmi
tamaxof10di
gitdeci
mal
number
.
2nd par
amet
erspeci
fi
es t
he numberofdi
git
sfr
om t
he r
ightsi
de t
o be
t
ransmi
tt
ed
Theout
putf
ort
hei
nputcombi
nat
ionsi
sasbel
ow
Bi
nar
y: 1.(
10,4)then4-LSBwil
lbetr
ansmit
tedie.1010
2.(
10,8)then8-LSBwil
lbetr
ansmit
tedie.00001010
3.(
10,2)then2-LSBwil
lbetr
ansmit
tedie.10
Deci
mal
4.
(12345,
4)then4-
digi
tsie.2345wil
lbetr
ansmi t
ted
5.
(12345,
6)then6-
digi
tsie.012345wil
lbetransmit
ted
6.
(12345,
C_Defaul
tDi
git
sToTransmit
_U8)then12345wi l
lbet
ransmi
tt
ed.
Hex:
7.
(0x12AB,
3)then3-
digi
tsie.2ABwil
lbetransmitt
ed
8.
(0x12AB,
6)then6-
digi
tsie.0012ABwil
lbetransmit
ted
9.
(0x12AB,
C_Defaul
tDi
git
sToTransmit
_U8)then12ABwi l
lbet
ransmi
tt
ed.
****
******
********
****
* *
*****
****
****
*****
******
***
* *
*****
*****
***
***
*****
****
******
*** *
***
***
****
*/
#if((Enabl
e_ UART_TxNumber ==1)||(Enable_
UART_ TxFloat
Number==1)||(Enable_UART_Pri
ntf
==1))
void UART_ TxNumber (ui
nt8_t v_uart
Channel_u8, uint8_t v_
numer i
cSystem_ u8, ui
nt32_t
v_number _
u32, ui
nt8_tv_numOfDigi
tsToTransmit
_u8)
{
uint
8_ti=0,a[
10];
i
f(
C_BINARY_ U8==v _numer icSystem_ u8)
{
while(v_
numOf DigitsToTr ansmi t
_ u8!=0)
{
/*StartExt ract i
ngt hebi tsfrom t hespeci f
iedbi tposit
ions.
GettheAcsi ivaluesoft hebi tsandt r
ansmi t*/
i=ut i
l
_Get BitStatus(v _
number _
u32, (v_numOf Digit
sToTransmit_u8-
1));
UART_ TxChar (
v _uartChannel _u8,util_
Dec2Asci i
(i));
v_numOf DigitsToTr ansmi t
_u8- -
;
}
}
el
seif(
v_number _ u32==0)
{
/*Ifthenumberi szer ot henTr ansmi tSpeci fi
ednumberofzer os*/
/*TODO: tr
snami tsi nglezer oormul t
iple,Cur r
ent l
ysi ngl
ezeroist r
ansmitt
ed*/
//for(i
=0;(
(i
<v _ numOf DigitsToTransmi t_u8)&&( i<C_MaxDi git
sToTransmit_
U8));
i++)
UART_ TxChar (v_uartChannel _u8,'0')
;
}
el
se
{
for(
i=0;i
<v_numOf DigitsToTr ansmi t_u8;i++)
{
/*Cont inueext racti
ngt hedi gitsf r
om r ightside
ti
lltheSpeci f
iedv _numOf DigitsToTr ansmi t_u8* /
if(
v_number _u32! =0)
{
/*Ext r
actt hedi gitsfrom t henumbert illi
tbecomeszer o.
Firstgett her emai nderanddi v i
det henumberby10eacht ime.
exampl
eforDeci
mal
number:
Ifv_
number_
u32=123thenext
ract
edr
emai
nderwi
l
lbe3andnumberwi
l
lbe
12.
Theprocessconti
nuesti
ll
itbecomeszer oormaxdi git
sreached*
/
a[
i]=ut
il
_GetMod32(
v_number_u32,
v_numericSyst
em_ u8);
v_number_u32=v
_number_u32/v_
numericSystem_u8;
}
el
seif
(( v
_numOfDigi
tsToTr
ansmi t_
u8==C_ Default
Digi
tsToTransmi t
_U8)||
(v_
numOf Di
git
sToTransmit_u8>C_MaxDi gi
tsToTransmit_U8))
{
/*Stoptheit
erat
ionift
heMaxnumberofdi gitsarereachedor
theuserexpect
sexact(Defaul
t)digi
tsi
nthenumbert obetransmitted*/
break;
}
el
se
{
/*Incaseuserexpectsmor edigit
stobet ransmitt
edt hantheact ualdi
git
sin
number
,
t
henupdat
et her
emai
ningdi
git
swi
thzer
o.
Ex:v_
number_u32is123anduserwantsfi
vedi
git
sthen00123hast
obe
t
ransmi
tt
ed*
/
a[
i]
=0;
}
}
whi
l
e(i)
{
/*Final
lygett
heasci
ival
uesofthedigi
tsandt
ransmit*
/
UART_ TxChar
(v_
uart
Channel
_u8,
uti
l
_Hex2Asci
i
(a[i
-1]
))
;
i
--;
}
}
}
#endi
f
/
* *
****
*****
*****
*****
******
****
*******
****
*******
******
****
****
******
****
****
****
****
****
****
****
**
voi
d UART_ TxFl
oatNumber (f
loatv_fl
oatNumber_f32)
***
****
*****
*****
*****
******
*****
******
****
*******
******
****
****
******
****
****
****
****
****
****
****
*
*Functi
onname: UART_ TxFloatNumber()
*I/PArguments:fl
oat:f
loatNumbert obet r
ansmi t
tedonUART.
*Ret
urnv
alue :
none
*descr
ipt
ion :
Thi
sfunct
ioni
susedt
otr
ansmi
taf
loat
ingpoi
ntnumber
*Not
e :
Itsupports6di
git
sofpreci
si
on.
Fl
oatwi
llbedisabl
edbydefaul
tasitt
akeshugecont
roll
erresour
ces
I
tcanbeenabledbychangingval
ueofEnabl
e_UART_TxFl
oatNumberto1i
nuar
t.
h
******
******* *
*** *
*****
***************
*****
*****
****
*****
****
*****
*******
**************
******
***
***
/
#if(Enable_ UART_ TxFloatNumber ==1)
voidUART_ TxFl oatNumber (uint8_tv_uar
tChannel
_u8,fl
oatv_fl
oatNumber _ f
32)
{
uint
32_ tv_tempNumber _u32;
/*Dirtyhackt osuppor tthef loat
ingpointbyextr
acti
ngtheintegerandf ractionalpart
.
1.Ty pecastt henumbert ointtogettheint
egerpart.
2.tr
ansmi ttheextractedi ntegerpartfol
l
owedbyadeci mal point
(.).
3.Lat ertheintegerpar tismadezer obysubt r
acti
ngwi t
htheext r
actedi ntegervalue.
4.Finallythef r
acti
onal par tismulti
pli
edby100000t osupport6-digitprecisi
on* /
v
_tempNumber
_u32=(
uint
32_
t)v
_fl
oat
Number
_f32;
UART_TxNumber
(v_
uar
tChannel
_u8,
C_DECI
MAL_
U8,
v_t
empNumber
_u32,
C_Def
aul
tDi
git
sToTr
ans
mit
_U8);
UART_
TxChar
(v_
uar
tChannel
_u8,
'
.'
);
v
_fl
oatNumber
_f32=v_
floatNumber_
f32-v_t
empNumber
_u32;
v
_tempNumber_u32=v
_floatNumber
_f32*1000000;
UART_TxNumber
(v_
uar
tChannel
_u8,
C_DECI
MAL_
U8,
v_t
empNumber
_u32,
C_Def
aul
tDi
git
sToTr
ans
mit_
U8);
}
#endif
/
* *
****
*****
*****
*****
****
*****
** *
******
****
******
****
****
****
****
****
****
****
****
****
****
****
****
**
voi
dUART_ Print
f(constchar*argLi
st,.
..
)
***
****
*****
*****
*****
****
********
******
****
******
****
****
****
****
****
****
****
****
****
****
****
****
*
*Functi
onname: UART_ Pri
ntf()
*I/PArguments:var
iabl
elengthar gumentssimil
artopri
ntf
*Ret
urnv
alue :
none
*descr
ipt
ion :
Thi
sfunct i
oni ssi mil
artoprint
ffuncti
oni
nC.
I
ttakest hear gument swithspecif
iedf
ormatandpr
int
saccor
dingl
y
Thesuppor tedformatspecifi
ersareasbel
ow.
1.%c:char acter
2.%d:signed16- bitnumber
3.%D: si
gned32- bitnumber
4.%u:unsi gned16- bitnumber
5.%U: unsigned32- bi
tnumber
6.%b:16- bitbinarynumber
7.%B:32- bitbinarynumber
8.%f:Floatnumber
9.%x:16-
bithexadeci
malnumber
10.%X:32-
bithexadeci
malnumber
11.%s:St
ri
ng
Not
e:Bydefaul
tallt
hefuncti
onswil
lbedisabl
ed.Ther
equiredfuncti
onscanbeenabl
edby
set
ti
ngtherespect
ivecompil
erswit
chto1inuart
.hfil
e.
Ex: set
ti
ngEnable_UART_
TxDecimalNumberto1wi l
lenable%d
sett
ingEnabl
e_UART_TxHexNumberto1willenabl
e%x
Ext
raf
eat
urei
sav ai
labl
etospecif
yt henumberofdigit
stobetr
ansmit
tedusi
ngpr
int
f.
ex:
%4d:wi
lltr
ansmitthelowerfourdigit
softhedecimal
number.
%12b:
willtr
ansmitthe12-LSBoft henumber
%d:Wil
ltr
ansmittheexactdigit
soft henumber
#####:I ncaseofpr i
nti
ngt hev ari
ables(8-
bit)it
srecommendedt otypecastandpromot
ethem
touint16_ t
.
uint8_ tv _Num_ u8;
UART_ Printf(
"num1: %u",
(ui
nt16_ t
)v_Num_ u8);
*****
***** *
* ***** *******
********
* *
*****
* *
********
*****
*****
****
****
****
*****
****
****
***
***
***
***
***
/
#if(Enabl e_ UART_ Pri
ntf ==1)
voidUART_ Printf(uint8_tv_uart
Channel _u8,constchar*argLi
st,
..
.)
{
constchar* ptr;
doubl ev _float Num_ f32;
va_listar gp;
sint
16_ tv _num_ s16;
sint
32_ tv _num_ s32;
uint16_ tv _num_ u16;
uint32_ tv _num_ u32;
char* st r;
char ch;
uint8_tv _numOf Digit
sToTr ansmit_u8;
v
a_st
art
(ar
gp,
argLi
st)
;
/
*Loopthr
oughtheli
stt
oextr
actal
lthei
nputar
gument
s*/
f
or(
ptr=ar
gLi
st;
*ptr!
='\
0';
ptr
++)
{
ch=* ptr;
i
f(ch==' %') /
* Checkfor'%'astherewi
llbef
ormatspecif
ieraf
teri
t*/
{
ptr++;
ch=* ptr;
i
f (
(ch>=0x30)&&( ch<=0x39))
{
v_ numOf Di
gitsToTransmit_u8=0;
whi le(
(ch>=0x30)&&( ch<=0x39) )
{
v_numOf Digi
tsToTransmit
_ u8=(v
_numOfDigi
tsToTransmit
_u8*10)+(
ch-
0x30)
;
pt
r++;
ch=*ptr
;
}
}
el
se
{
v_
numOf
Digi
tsToTr
ansmi
t_u8=C_
MaxDi
git
sToTr
ansmi
tUsi
ngPr
int
f_U8;
}
swi
tch(
ch) /
*Decodet
het
ypeoft
hear
gument*
/
{
case'C':
case'c': /
*Ar gumenttypeisofchar
,hencer
eadchardat
afr
om t
hear
gp*
/
ch=v a_arg(
argp,int
);
UART_ TxChar(v_
uartChannel
_u8,
ch)
;
break;
case'
d'
: /
*Ar
gumentt
ypei
sofsi
gnedi
nteger
,hencer
ead16bi
tdat
afr
om t
he
ar
gp*
/
v_num_ s16=v a_ar
g(argp,int
);
#i
f(Enabl
e_UART_
TxNumber==1)
i
f(v_num_ s16<0)
{/*Ifthenumberi s-vethendispl
aythe2'
scompl
emental
ongwi
th'
-
'si
gn*
/
v_num_ s16=-v_num_ s16;
UART_ TxChar(
v_uartChannel
_u8,
'-
'
);
}
UART_TxNumber
(v_
uart
Channel
_u8,
C_DECI
MAL_
U8,
v_num_
s16,
v_numOf
Digi
tsToTr
ansmi
t_u8)
;
#endi
f
br
eak;
UART_TxNumber
(v_
uart
Channel
_u8,
C_DECI
MAL_
U8,
v_num_
s32,
v_numOf
Digi
tsToTr
ansmi
t_u8)
;
#endi
f
br
eak;
case'
u'
: /
*Ar
gumentt
ypei
sofunsi
gnedi
nteger
,hencer
ead16bi
tunsi
gned
dat
a*/
v_num_
u16=va_ar
g(ar
gp,
int
);
#i
f(Enabl
e_UART_
TxNumber==1)
UART_TxNumber
(v_
uar
tChannel
_u8,
C_DECI
MAL_
U8,
v_num_
u16,
v_numOf
Digi
tsToTr
ansmi
t_u8)
;
#endi
f
br
eak;
case'U': / *Ar
gumenttypei
sofint
eger,
hencer
ead32bi
tunsi
genddat
a*/
v
_ num_
u32=v a_
arg(
argp,
uint
32_t
);
#i
f(Enabl
e_UART_TxNumber==1)
UART_TxNumber
(v_
uart
Channel
_u8,
C_DECI
MAL_
U8,
v_num_
u32,
v_numOf
Digi
tsToTr
ansmi
t_u8)
;
#endi
f
br
eak;
case'X':/ *Argumenttypeisofhex,hencehexadeci
mal
dat
afr
om t
hear
gp*
/
v_num_ u32=va_ar
g(argp,ui
nt32_t
);
#i
f( Enable_
UART_ TxNumber==1)
UART_ TxNumber(v
_uartChannel
_u8,C_
HEX_ U8,
v_num_ u32,
v_numOf Di
git
sToTransmit
_u8);
#endif
break;
case'F':
case'f'
:/*Argumenttypeisoffl
oat,hencereaddoubledat
af r
om t
hear
gp*
/
v_fl
oatNum_f32=v a_
arg(ar
gp,double)
;
#i
f(Enabl
e_UART_TxFloatNumber==1)
UART_ TxFl
oatNumber(v
_uartChannel
_u8,v
_fl
oatNum_f32);
#endi
f
break;
case'S':
case's':/
*Ar gumenttypei
sofstri
ng,hencegett
hepoi
ntert
ost
ingpassed*
/
str=v a_
arg(ar
gp,char*)
;
UART_ TxStr
ing(
v_uart
Channel
_u8,st
r);
break;
case'%':
UART_TxChar
(v_
uar
tChannel
_u8,
'
%')
;
break;
}
}
el
se
{
/*As'
%'i
snotdetect
edtransmitthecharpassed*
/
UART_
TxChar
(v_uart
Channel_
u8,ch);
}
}
v
a_end(
argp)
;
}
#endif