?? xsa3.c
字號:
/****************************************************************
文件名: xsa3.c
功 能:這是在PC機或工控機上運行的程序,用PC機的COM1(0x3f8)口,
和單片機通訊,波特率9600。通訊步驟:先向單片機發送一字節
地址碼(第九位為1),再發送15個字節數據(第九位為0),然
后等待接收單片機發來的43個字節數據,并將接收來的內容在
電腦屏幕上顯示出來.
版 本:在Turbo C2.0下編譯通過
****************************************************************/
#include "dos.h"
void l_table(void);
void comm1();
void transd();
void view1();
int initserial (long int baud_rate,char parity);
union p{
char v[240];
};
union p c;
int i,n,j,t;
long unsigned int r;
unsigned char tbut[20];
main()
{
comm1();
initserial (9600,'o');
while(1){
delay(15);
outp(0x3f8+3,0x3b); /*按51系列單片機多機通訊要求,發地址時第九位須為1 */
while(!((inp(0x3f8+5))&0x20));
j=1; /* 地址碼 */
outp(0x3f8,j);
outp(0x3f8+3,0x2b); /*按51系列單片機多機通訊要求,發數據時第九位須為0 */
for(n=0;n<15;n++) { /*發送15個字節數據 */
while(!((inp(0x3f8+5))&0x20));
outp(0x3f8,tbut[n]);
}
com1();
l_table();
clearv2400();
}
}
com1()
{
n=0;
transd(); /*接收43個字節數據 */
}
void transd()
{
r=0;
i=0;
while((i<43)&&(r++<380000)){
if((inp(0x3f8+5))&0x01) c.v[i++]=inp(0x3f8);
}
}
void l_table(void)
{
clrscr();
l_data_1();
sleep(10);
}
l_data_1()
{
i=0;gotoxy(9,1);view1();
i=6;gotoxy(9,3);view1();
i=12;gotoxy(9,5);view1();
i=18;gotoxy(9,7);view1();
i=24;gotoxy(9,9);view1();
i=30;gotoxy(9,11);view1();
i=36;gotoxy(9,13);view1();
}
void view1()
{
printf("%4x%4x%4x%4x%4x%4x",c.v[i],c.v[i+1],c.v[i+2],c.v[i+3],
c.v[i+4],c.v[i+5]);
}
int initserial (long int baud_rate,char parity)
{
union {
struct {
unsigned char lobyte;
unsigned char hibyte;
}byte;
struct {
unsigned baud_factor;
}whole;
}bytes;
unsigned char x;
outp(0x3f8+3,0x80);
bytes.whole.baud_factor=1843200L/((long)baud_rate<<4);
outp(0x3f8+0,bytes.byte.lobyte);
outp(0x3f8+1,bytes.byte.hibyte);
outp(0x3f8+4,0x03);
outp(0x3f8+1,0x00);
switch(parity) {
case 'e':
case 'E': x=0x2b; break;
case 'o':
case 'O': x=0x3b; break;
case 'n':
case 'N': x=0x07; break;
default: return(-1);
}
outp(0x3f8+3,x);
}
clearv2400()
{
for (t=0;t<91;t++){
c.v[t]=0;
}
}
void comm1()
{
for (t=0;t<15;t++){
tbut[t]=0x00;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -