Software UART 程序设计指引.docx
- 文档编号:10461735
- 上传时间:2023-02-13
- 格式:DOCX
- 页数:26
- 大小:32.65KB
Software UART 程序设计指引.docx
《Software UART 程序设计指引.docx》由会员分享,可在线阅读,更多相关《Software UART 程序设计指引.docx(26页珍藏版)》请在冰豆网上搜索。
SoftwareUART程序设计指引
概述:
SoftUART需要一个标准的输出口和一个标准的外部中断输入口。
数据的传输需要一个8位定时器产生对应波特率的1个bit和1/2个bit时间。
传输和接收数据的格式包含一个起始位、数据位、停止位和校验位。
SoftwareUART需要以下的资源:
ØOutputportforTx
ØInputportforRx(下降沿触发的中断输入口)
Ø8-bittimercontrolregisters
波特率的设置:
软件UART使用一个8位定时器设置波特率,每一个BIT的时间计算为1,000,000/波特率,例如对应于9600的波特率,每一个BIT的时间为1,000,000/9600=104.1667us
范例程序:
下面的程序示例了一个SOFTUART的功能。
程序起始时调用了一个初始化程序来设置输入输出口和8位定时器
流程图:
;****************************************
;MiDeaAirConditionerUART
;AssembledbyRA78K0Slanguage
;Filename:
SoftwareUART.asm
;Date:
;****************************************
$PROCESSOR(9166)
$INCLUDE(Header.inc)
;========================================
;public&externalvariabledefinesegment
;========================================
publicP_UartInit;
;publicP_UartReceive;UARTcommunicationreceivesubroutine
;publicP_UartSend;UARTcommunicationtransmitsubroutine
publicINTP2;
publicINTTM82
publicT_TranTim;
publicV_RxData
publicV_RecAddress
publicV_TraAddress
publicV_BitNum
publicCommTime
publicF_TransFG;
publicF_ReceiFG
extrnV_ModeRun;
extrnV_ModeRunB
extrnV_ModeSet;
extrnV_FanSet
extrnV_KeyData
extrnV_PowerLevel;
extrnV_TempD;
extrnT_05HZ;
extbitF_Commerror;
extbitF_MachineA;
extbitF_TranTim
extbitF_TAAlarm
extbitF_TEAlarm
extbitF_TEHighPro
extbitF_TEHighPro1
extbitF_TELowPro
extbitF_RatingTest;
extbitF_RateTest;
extbitF_CompMin3
extbitF_Defrost;
extbitF_Comp;
extbitF_OutAlarm;
extbitF_ForceCool;
extbitF_Auto
extbitF_BackOil;
extbitF_ModeError;
extbitF_Single;
extbitF_Buzzer;
extbitF_SystemOn;
extbitF_PowerUp
;extbitF_NoForce
;========================================
;Equates
;Bittimeofsomebaudratesbasedonfx=4.19MHz
;Counterresolution
;========================================
BitTime_BaudRate400equ82;
HBtTime_BaudRate400equ41;
TxDataPDirequPM3.1;TXdatadir
RxDataPDirequPM3.2;RXdatadir
;========================================
;bitsegmentDEFINE
;========================================
BITSBSEG;
F_ReceiFGdbit;
F_TransFGdbit;
F_BitStaFgdbit;
;========================================
;datasegmentDEFINE
;========================================
DATA3DSEGSADDRP
V_RecAddress:
ds2
V_TraAddress:
ds2
;----------------------------------------
DATS0DSEGSADDRP
RecBuf0:
ds1;UARTreceivedatabuffer
RecBuf1:
ds1
RecBuf2:
ds1
RecBuf3:
ds1
RecBuf4:
ds1
RecBuf5:
ds1
RecBuf6:
ds1
RecBuf7:
ds1
RecBuf8:
ds1
RecBuf9:
ds1
RecBuf10:
ds1
TraBuf0:
ds1;UARTtransmitdatabuffer
TraBuf1:
ds1
TraBuf2:
ds1
TraBuf3:
ds1
TraBuf4:
ds1
TraBuf5:
ds1
TraBuf6:
ds1
TraBuf7:
ds1
TraBuf8:
ds1
TraBuf9:
ds1
TraBuf10:
ds1
V_RxData:
ds1
V_BitNum:
ds1
T_TranTim:
ds1;UARTcommunicationtimervariable
CommTime:
ds1;transmittimecounter
OutParAdr:
ds1;outdoorparametertableaddress
V_Verify:
ds1
;----------------------------------------
UARTPROCSEG
;========================================
;Modulename:
P_UartInit
;
;DESCRIPTION:
;.ThismoduleistoinitializeUARTcontrolandmode
;registers.
;
;OPERATION:
;.SetdualportstoinputforRxandoutputforTx
;andlatchtheTxporttohigh.
;.Dataformat:
;.1startbit,8-bitdata,2stopbits,noparitybit.
;.Setbaudrateto400.
;.EnableRxandTxoperation.
;.Clearallvariablesandflagsforserialdatatransfer.
;========================================
P_UartInit:
set1RxDataPDir;Inputdirection
clr1TxDataPDir;Outputdirection
set1Pt_TxD20;Settohigh
;\\
set1pmk2;
movintm0,#00100000b;settheINTP2fallingedge
clr1pif2;enableINTP2interrupt
clr1pmk2;
;\\
clr1tce82;
clr1tmmk82;
clr1F_BitStaFg;
movwax,#RecBuf0;
movwV_RecAddress,ax;
ret;
;========================================
;
;========================================
INTP2:
btF_BitStaFg,$INTP2Rt;
clr1tce82;
movtmc82,#00000010b;32.7KHZ
movCR82,#HBtTime_BaudRate400;waitforhalfabittime
set1tce82;enabletmc82operation
INTP2Rt:
reti;
;========================================
INTTM82:
di;
pushpsw;
pushax;
pushbc;
pushde;
pushhl;
bfF_TransFG,$ReceivePro;
brTransPro;
;----------------------------------------
ReceivePro:
btF_BitStaFg,$Rxlp;
bfPt_RxD20,$BitSta;
clr1tce82;disabletmc82operation
brINTTM82Rt;
;
BitSta:
set1F_BitStaFg;
set1pmk2;
movV_BitNum,#0;
movV_RxData,#0;
movCR82,#BitTime_BaudRate400;
brINTTM82Rt;
;----------------------------------------
Rxlp:
incV_BitNum;
cmpV_BitNum,#9;
bnc$rxVerifyBit
clr1cy;
bfPt_RxD20,$rx10;
set1cy;
rx10:
mova,V_RxData;
rorca,1;
movV_RxData,a;
brINTTM82Rt;
;----------------------------------------
rxVerifyBit:
cmpV_BitNum,#10;
bnc$rxStopBit;
brINTTM82Rt;
;----------------------------------------
rxStopBit:
cmpV_BitNum,#11;
bnc$rxStopBit2;
rxStopBit1:
bfPt_RxD20,$RecError;
brINTTM82Rt;
rxStopBit2:
btPt_RxD20,$SRecProc;
RecError:
clr1F_BitStaFg;
clr1pif2;
clr1pmk2;
movRecBuf0,#0;
movwax,#RecBuf0;
movwV_RecAddress,ax;
brINTTM82Rt;
;----------------------------------------
SRecProc:
clr1F_BitStaFg;
clr1pif2;
clr1pmk2;
movwax,V_RecAddress;
movwhl,ax;
mova,V_RxData;
mov[hl],a;
incwhl;
movwax,hl;
movwV_RecAddress,ax;
cmpwax,#RecBuf1;
bnz$SRecEnd;
cmpRecBuf0,#0f0h;
bz$SRecEnd;
brRecError
SRecEnd:
movwax,V_RecAddress;
cmpwax,#RecBuf10;
bnc$SRecEnd1;
brINTTM82Rt;
SRecEnd1:
movwax,#RecBuf0;
movwV_RecAddress,ax;
set1F_ReceiFG;
brINTTM82Rt;
;========================================
TransPro:
incV_BitNum;
cmpV_BitNum,#12;
bc$TxBit;
;----------------------------------------
TxByte:
movV_BitNum,#0;
movwax,V_TraAddress;
movwhl,ax;
mova,[hl];
movV_RxData,a;
incwhl;
movwax,hl;
movwV_TraAddress,ax;
cmpwax,#TraBuf10;
bc$TraStart;
bz$TraStart;
movwax,#0;
movwV_TraAddress,ax;
clr1F_TransFG;
clr1tce82;
clr1F_BitStaFg;
clr1pif2;
clr1pmk2;
brINTTM82Rt;
TraStart:
clr1Pt_TxD20;
movV_Verify,#0;
movCR82,#BitTime_BaudRate400;
brINTTM82Rt;
;----------------------------------------
TxBit:
cmpV_BitNum,#9;
bnc$TxVerify;
clr1cy;
mova,V_RxData;
rorca,1;
movV_RxData,a;
bc$TxBit1;
clr1Pt_TxD20;
brTxBit2;
TxBit1:
set1Pt_TxD20;
TxBit2:
addcV_Verify,#0;
brINTTM82Rt;
;----------------------------------------
TxVerify:
cmpV_BitNum,#10;
bnc$TxStop;
clr1cy;
mova,V_Verify;
rorca,1;
bc$TxVerify1;
set1Pt_TxD20;
brINTTM82Rt;
TxVerify1:
clr1Pt_TxD20;
brINTTM82Rt;
;----------------------------------------
TxStop:
cmpV_BitNum,#11;
bnc$TxStop1;
set1Pt_TxD20;
brINTTM82Rt;
TxStop1:
set1Pt_TxD20;
INTTM82Rt:
pophl;
popde;
popbc;
popax;
poppsw;
ei;
reti;
;========================================
;
;========================================
P_UartReceive:
btF_ReceiFG,$CommCheck;
cmpCommTime,#120;2min通讯保护
bnc$CommError;
ret;
CommError:
set1F_Commerror;
movCommTime,#0;
movRecBuf0,#0;
movRecBuf1,#0;
movRecBuf2,#0;
movRecBuf3,#0;
movRecBuf4,#0;
movRecBuf5,#0;
movRecBuf6,#0;
movRecBuf7,#0;
movRecBuf8,#0;
movRecBuf9,#0;
movTraBuf0,#0;
movTraBuf1,#0;
movTraBuf2,#0;
movTraBuf3,#0;
movTraBuf4,#0;
movTraBuf5,#0;
movTraBuf6,#0;
movTraBuf7,#0;
movTraBuf8,#0;
movTraBuf9,#0;
call!
P_UartInit;
ret;
CommCheck:
clr1F_ReceiFG;
;----------------------------------------
cmpRecBuf0,#0f0h;
bz$ByteTwo;
ret;
ByteTwo:
cmpRecBuf1,#00h;
bz$CheckSum;
ret;
CheckSum:
movb,#9;
movwhl,#RecBuf0;
CALL!
P_CalParaSum1;
cmpa,RecBuf9;
bz$ReceiveOk;
ret;
;----------------------------------------
ReceiveOk:
clr1tce82;
set1pmk2;
clr1F_Commerror;
movCommTime,#0;
set1F_TranTim;
movT_TranTim,#0;
;----------------------------------------\\接收解码
cmpRecBuf2,#0;
bnz$HeatMod;
movV_ModeRunB,#C_ModeOff;
brTempDCheck;
HeatMod:
cmpRecBuf2,#1;
BNZ$CoolMod;
movV_ModeRunB,#C_ModeHeat;
brTempDCheck;
CoolMod:
cmpRecBuf2,#2;
bnz$DryMod;
movV_ModeRunB,#C_ModeCool;
brTempDCheck;
DryMod:
cmpRecBuf2,#3;
bnz$FanMod;
movV_ModeRunB,#C_ModeDry;
brTempDCheck;
FanMod:
movV_ModeRunB,#C_ModeFan;
brTempDCheck;
;----------------------------------------
TempDCheck:
mova,RecBuf3;
cmpa,#10;
bnc$TempDCheck10;
adda,#10;
TempDCheck10:
suba,#10;
movV_TempD,a;
;----------------------------------------
mova,RecBuf4;
bta.0,$Symbol01;
clr1F_MachineA;副机
brSymbol10;
Symbol01:
set1F_MachineA;主机
Symbol10:
bta.1,$Symbol11;
clr1F_Defrost;
brSymbol20;
Symbol11:
set1F_Defrost;
Symbol20:
bta.2,$Symbol21;
clr1F_Comp;
brSymbol30;
Symbol21:
set1F_Comp;
Symbol30:
bta.3,$Symbol31;
clr1F_RateTest;
brSymbol40;
Symbol31:
set1F_RateTest;
Symbol40:
bta.5,$Symbol41;
;clr1F_NoForce;
btF_MachineA,$Symbol50;
bfF_ForceCool,$Symbol50;
clr1F_ForceCool;
movV_ModeSet,#C_ModeOff;
movV_FanSet,#C_FanOff;
set1F_Buzzer;
clr1pmk0;
clr1F_SystemOn;
brSymbol50;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Software UART 程序设计指引 程序设计 指引
![提示](https://static.bdocx.com/images/bang_tan.gif)