- 3
- 0
- 约4.33万字
- 约 37页
- 2019-10-07 发布于天津
- 举报
第十章 串行端口接口控制与实习
10-1.3 SCI实习范例
一. SCI自我传输范例
程序:SCI1.C
/*==============================================*/
/* Filename : SCI1.C */
/*==============================================*/
#include f240regs.h
#include LH092.h
char i;
static const char Table[] /* 发射的数据 */
= {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
void main()
{
InitCPU();
sciinit(); /* SCI启始环境设定 */
while(1)
{
for(i=0;i8;i++) /* 依序发射8个byte数据 */
{
while(SCICTL2 0x80 ) /* TXRDY=1 wait*/
CS0=SCITXBUF=Table[i]; /* 发射1个byte数据,并在CS0显示 */
while(SCIRXST 0x40 ) /* RXRDY=1 wait*/
PBDATDIR=~SCIRXBUF | 0xff00; /* 接收数据,并在IOPB反相显示 */
Delay(180000); /* 延时 */
}
}
}
二. UART人机接口
Dim a(0), c(0) As Byte
Dim b() As Byte
Dim buffer1, buffer2 As Variant
Private Sub Command1_Click()
Dim i, j As Integer
a(0) = Val(Text1(0).Text)
MSComm1.RTSEnable = True
c(0) = a(0)
MSComm1.Output = c 数据从COM1送出
MSComm1.RTSEnable = False
MSComm1.InBufferCount = 0
End Sub
Private Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.Settings = 19200,n,8,1
MSComm1.PortOpen = True
MSComm1.InputMode = comInputModeBinary
MSComm1.RThreshold = 1 COM1接收数据长度
End Sub
Private Sub Form_Unload(Cancel As Integer)
MSComm1.RTSEnable = False
MSComm1.PortOpen = False 关闭通信端口
End Sub
Private Sub MSComm1_OnComm() COM1通信事件
buffer1 = MSComm1.Input
b = buffer1
Text2(0).Text = b(0)
End Sub
Private Sub Text1_Validate(Index As Integer, Cancel As Boolean) 避免数值超过0~255
If Val(Text1(Index).Text) 256 Or Val(Text1(Index).Text) 0 Then Cancel = True
End If
End Sub
提供主程序使用如下:
void sciinit(void) /* SCI起始程序 */
{
SCICCR =0x17; /* 设定为无同位,8个数据位,1个停止位 */
SCICTL1=0x13; /* 响应TX,RX,内部SCICLK禁用 RX ERR,SLEEP ,TXWAKE */
SCICTL2=0x02; /* 响应RX中断,禁用TX中断 */
SCIHBAUD=00;
SCILBAU
原创力文档

文档评论(0)