2# yewuyi
恩,我写了一个中断的程序,但是只能第一次收发正确,我接受用的中断,发送没有用中断。这是程序:
#include <pic.h>
__CONFIG(0x3531);
unsigned char receive232[8]; // 接收数据数组
unsigned char send232[8]={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08}; // 发送数据数组
unsigned char receive_count=0;// 接收数据个数计数
unsigned char send_count=0; // 发送数据个数计数
unsigned char flag,flag1; // =1,接收到8个数据
unsigned char a0,a1,a2,a3,a4,a5,a6,a7;
//=========================延时1MS函数=======================================
void delay(unsigned int n)
{
unsigned int i,j;
for(i=0;i<n;i++)
for(j=0;j<110;j++);
}
/* ****************************************************************
** 函 数 名: sciinitial()
** 功能描述: 232串行通讯初始化子程序,设置低优先级中断接收,使能232收
*************************************************************** */
void init()
{
TRISB = 0xc0;
TRISC = 0x80;
TXSTA = 0x04; // 选择异步高速方式传输8位数据
RCSTA = 0x80; // 允许串行口工作使能,
SPBRG = 0x19; // 4M晶振时波特率为25
}
/* ****************************************************************
** 函 数 名: interrupt low_priority LOW_ISR()
** 功能描述: 低优先级中断子程序:RS232中断接收、中断发送
*************************************************************** */
void interrupt ISR(void)
{
GIE=0;
if(RCIF==1)
{
receive232[receive_count]=RCREG; //接收数据并存储
receive_count++; // 接收计数器加1
if(receive_count>7) // 如果已经接收到8个数据
{
receive_count=0; // 接收计数器清0
flag=1; //置接收到数据标志
}
}
GIE=1;
}
void main()
{
init(); // 串行通讯初始化子程序
TXIE = 0; //开发送中断
RCIE = 1; //开接收中断
PEIE = 1; //开外设中断
GIE = 1; //开总中断
TXEN = 0; //发送使能
CREN = 1; //接收使能
while(1)
{
if(flag==1) // 是否接收到通信数据
{
CREN=0;
flag=0;
a0=receive232[0];
a1=receive232[1];
a2=receive232[2];
a3=receive232[3];
a4=receive232[4];
a5=receive232[5];
a6=receive232[6];
a7=receive232[7];
flag1=1;
}
if(flag1==1)
{
TXEN=1;
TXREG=receive232[send_count];
send_count++; // 发送计数器加1
delay(10);
while(1)
{
if(TXIF==1) break;
}
if(send_count>7)
{
TXEN=0;
flag1=0;
send_count=0;
CREN=1; //允许接收数据
GIE=1;
}
}
}
}
|