怎么用C语言中断方式实现串口的读写?

怎么用C语言中断方式实现串口的读写?,第1张

#include <stdio.h>

#include <dos.h>

#include <time.h>

#define LSB 0

#define MSB 1

#define RXR 0

#define TXR 0

#define IER 1

#define IIR 2

#define LCR 3

#define MCR 4

#define LSR 5

#define MSR 6

#define Com1_base 0x3f8

#define uchar unsigned char

volatile uchar inputData

volatile uchar onInput = 0x00

volatile uchar onOutput = 0x00

volatile uchar inputdata[256] /*设设置接收缓冲区大小*/

volatile unsigned int count /*中断接收的数据数目*/

int datacount/*当前已发送数据条数*/

void interrupt (*OldVect)()/*函数是用来获取中断处理程序的入口地址的*/

void interrupt SerialISR()

void InitCom()

{

uchar inttemp

/*设置波特率什么的*/

outportb(Com1_base+LCR,0x80) /*使LCR的高位为1,以便读取其它寄存器*/

outportb(Com1_base+LSB,0x0c) /*除数锁存器(低8位)DLL*/

outportb(Com1_base+MSB,0x00) /*除数锁存器(高8位)DLH 产生2400波特率*/

outportb(Com1_base+LCR,0x03) /*8位数据,1位停止位,无校验*/

outportb(Com1_base+IER,0x01) /*接收采用中断方式*/

/*设置中断向量*/

OldVect = getvect(0x0c)/*函数是用来获取中断处理程序的入口地址的*/

disable()

inttemp = inportb(0x21)&0xef

outportb(0x21,inttemp)

setvect(0x0c,SerialISR)/*设置SerialISR的中断入口地址为0X0C*/

enable()

}

void CloseCom()

{

disable()

outportb(Com1_base+IER,0x00) *禁止中断*/

outportb(Com1_base,0x00)

outportb(0x21,inportb(0x21)|~(0xef))

setvect(0x0c,OldVect)

}

void interrupt SerialISR()

{

/*串口中断服务代码*/

inputData = inportb(Com1_base+RXR)

onInput = 0x01

inputdata[count]=inputData

count++

/*服务代码结束*/

outportb(0x20,0x20) /*中断结束的代码*/

}

void SendChar(uchar key)

{

while( ((inportb(Com1_base + LSR)) &0x40) == 0)

outportb(Com1_base + TXR,key)

}

/*...........延时函数......................*/

void delay(unsigned int n)

{

unsigned int i,k

for(k=0k<nk++)

{

for(i=1i<1142i++)

}

}

/*........CDMA命令发送函数.................*/

void CDMA_CommandSend(char *p,unsigned int n)

{

int i,k,m,l

int flag=0

int selse_flag=0 int selse_flag_1=0

int selse_flag1=0int selse_flag1_1=0

time_t start,end

double dif=0

char a

uchar bExit_flag = 0x00

count=0

while(!bExit_flag)

{

count=0

for(i=0i<ni++)

{SendChar(*p)delay(9000)p++}

/*..........等待2S..................*/

time (&start)

dif=0

while(dif<2)

{

delay(9000)

time (&end)

dif = difftime (end,start)

}

if(count>14)

{

time (&start)

dif=0

while(dif<3)

{

delay(9000)

time (&end)

dif = difftime (end,start)

}

}

/*..........判断发送命令是否成功..................*/

for(m=0m<countm++)

{

if(count>10)

{

for(l=0l<countl++)

{

if(inputdata[l-1]=='N'&&inputdata[l-2]=='E'&&inputdata[l-3]=='P'&&inputdata[l-4]=='O'&&inputdata[l-5]=='P'&&inputdata[l-6]=='P'&&inputdata[l-7]=='P'&&inputdata[l-8]=='V') { selse_flag_1=1}

if(inputdata[l-1]=='N'&&inputdata[l-2]=='E'&&inputdata[l-3]=='P'&&inputdata[l-4]=='O'&&inputdata[l-5]=='P'&&inputdata[l-6]=='C'&&inputdata[l-7]=='T'&&inputdata[l-8]=='V') { selse_flag1_1=1}

}

if(selse_flag_1==1){if(inputdata[m-1]=='3'&&inputdata[m-2]=='3'&&inputdata[m-3]==':'&&inputdata[m-4]=='T') selse_flag=1}

if(selse_flag1_1==1){if(inputdata[m-1]=='0'&&inputdata[m-2]==','&&inputdata[m-3]=='1'&&inputdata[m-4]==':'&&inputdata[m-5]=='N') selse_flag1=1}

if(selse_flag_1==1||selse_flag1_1==1)

{

if(selse_flag==1) {if(inputdata[m-1]=='0'&&inputdata[m-2]==':'&&inputdata[m-3]=='N'&&inputdata[m-4]=='E') flag=1}

if(selse_flag1==1) {if(inputdata[m-1]=='0'&&inputdata[m-2]==','&&inputdata[m-3]=='1'&&inputdata[m-4]==':'&&inputdata[m-5]=='S') flag=1}

}

else {if(inputdata[m-1]=='O'&&inputdata[m]=='K') {flag=1}}

}

else {if(inputdata[m-1]=='O'&&inputdata[m]=='K') flag=1 }

}

if(flag==1){bExit_flag = 0x01}

else {p=p-n}

for(i=0i<counti++) printf("%c",inputdata[i])

/*........按空格停止测试.............................*/

if(kbhit()){a=getch()if(a==' '){CloseCom() exit(1)}}

}

}

/*........数据测试发送任务.....................*/

void Data_Send(char *pcomd,int n,char *pdata,int m)

{

int i,k,ltime_t start,end

double dif=0

count=0

for(i=0i<ni++)

{

SendChar(*pcomd)delay(9000)pcomd++

}

time (&start)

dif=0

while(dif<0.2)

{

delay(9000)

time (&end)

dif = difftime (end,start)

}

for(l=0l<ml++)

{

SendChar(*pdata)pdata++

}

printf("The data num:%d",datacount)

datacount++

}

void main()

{int ichar a

/*........模块测试命令..................................*/

uchar CDMA_COMMAND_1[]={"at\r"}

uchar CDMA_COMMAND_2[]={"atz\r"}

uchar CDMA_COMMAND_3[]={"ate1v1\r"}

uchar CDMA_COMMAND_4[]={"AT+CRM=1+CPS=33+CMUX=1+CTA=0\r"}

uchar CDMA_COMMAND_5[]={"AT+VPPPOPEN\r"}

uchar CDMA_COMMAND_6[]={"AT+VTCPOPEN=1,\"60.63.42.129\",1234\r"}

uchar CDMA_COMMAND_7[]={"AT+VTCPSEND=1,20\r"}

uchar CDMA_DATA[]={"ABCDEFGHIJKLMNOPQRST"}

uchar CDMA_COMMAND_9[]={"AT+VPPPCLOSE\r"}

uchar CDMA_COMMAND_8[]={"AT+VTCPCLOSE=1\r"}

uchar bExit_flag1 = 0x00

time_t start,end

double dif=0

InitCom()/*初始化端口1*/

inputData = inportb(Com1_base+RXR)

while(!bExit_flag1)

{

/*.................................................................*/

if(kbhit()) /*按任意键开始测试CDMA*/

{

/*.............................................................. ...*/

/*.........发送TCP断开命令.....................................*/

CDMA_CommandSend(CDMA_COMMAND_8,strlen(CDMA_COMMAND_8))

/*.........发送PPP断开命令......................................*/

CDMA_CommandSend(CDMA_COMMAND_9,strlen(CDMA_COMMAND_9))

/*.........发送AT命令..........................................*/

CDMA_CommandSend(CDMA_COMMAND_1,strlen(CDMA_COMMAND_1))

/*.........发送ATZ命令.........................................*/

CDMA_CommandSend(CDMA_COMMAND_2,strlen(CDMA_COMMAND_2))

/*.........发送ATE1V1命令......................................*/

CDMA_CommandSend(CDMA_COMMAND_3,strlen(CDMA_COMMAND_3))

/*.........发送AT初始化命令....................................*/

CDMA_CommandSend(CDMA_COMMAND_4,strlen(CDMA_COMMAND_4))

/*.........发送PPP拔号命令.....................................*/

CDMA_CommandSend(CDMA_COMMAND_5,strlen(CDMA_COMMAND_5))

/*.........发送网络连接命令....................................*/

CDMA_CommandSend(CDMA_COMMAND_6,strlen(CDMA_COMMAND_6))

/*.........发送数据............................................*/

datacount=0

while(1)

{

Data_Send(CDMA_COMMAND_7,strlen(CDMA_COMMAND_7),CDMA_DATA,strlen(CDMA_DATA))

time (&start)

dif=0

while(dif<0.2)

{

delay(9000)

time (&end)

dif = difftime (end,start)

}

if(kbhit()){a=getch()if(a==' '){bExit_flag1 = 0x01 break}}

}

/*.........发送TCP断开命令.....................................*/

for(i=0i<3i++)

CDMA_CommandSend(CDMA_COMMAND_8,strlen(CDMA_COMMAND_8))

/*.........发送PPP断开命令......................................*/

for(i=0i<3i++)

CDMA_CommandSend(CDMA_COMMAND_9,strlen(CDMA_COMMAND_9))

bExit_flag1 = 0x01

}

/*.................................................................*/

}

CloseCom()/*关闭端口1*/

printf("Bye~.\n")

}

可以用MATLAB产生一个由两个正弦波混叠的信号,然后将其输出地数据存储在一个文本文件中,再导入滤波器的测试文件中就可以了:

N =256

n = 1:256

x = fix(128 + (2^7 - 10) * sin(2*pi*n/N))

plot(x)

fid = fopen('e:/sin.txt','wt')

fprintf(fid,'%x\n',x)

fclose(fid)

这里只有一个正弦信号,你再叠加一个就可以了。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11238132.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-14
下一篇 2023-05-14

发表评论

登录后才能评论

评论列表(0条)

保存