本人学生,在做一个课程设计,用的是PIC24HJ64GP502单片机,我这个程序的功能是单片机和GSM模块相连,手机给GSM模块发短信,单片机控制GSM模块收短信,然后识别短信内容,在不同的引脚输出不同的高低电平。我这个程序现在出现的问题就是那个中断接收这个地方,我定义了一个叫SyBuF的数组,用来接收存储从GSM模块上传递过来的内容(包括短信内容),用的是中断接收方式,但是现在在中断接收函数后面的程序中出现SyBuF的地方就显示有错误(就是那个GSM_INIT函数里面出现错误了),好像是说什么中断函数不能和其他程序共用资源(这里就是这个SybuF数组),但是我这个程序必须共用这个数组,求大神看看有什么解决俄办法。#include "p24HJ64GP502.h"
#include<stdio.h>
_FOSCSEL(FNOSC_FRC);
_FOSC(FCKSM_CSECMD & OSCIOFNC_OFF & POSCMD_NONE);
_FWDT(FWDTEN_OFF)
#define FCY 40000000
#define BAUDRATE 9600
#define RxIn 90
#define BRGVAL ((FCY/BAUDRATE)/16)-1
#define uchar unsigned char
#define uint unsigned int
unsigned char SyBuF[90];
uchar Index=0;
unsigned char AT_CMGR[]="AT+CMGR=";//??????
uchar AT_Read[12]; //????????????
uchar numberbuf[3]; //????????
uchar CommandBuf[6];
int receiveready=0;
void Delay_ms(uint i);
void uart_init(void);
void UART2_write(unsigned char dat);
void UART2_puts(char *s);
void __attribute__((__interrupt__)) __U2RXInterrupt(void);
void GSM_INIT(void);
void receive_ready(void);
void read_message(void);
void readcommend(void);
void uart_init(void)//???UART?????
{
TRISBbits.TRISB7=0;
TRISBbits.TRISB6=1;
PORTBbits.RB7=1;
RPINR19bits.U2RXR=6; //RP10,11 ??TX,RX??? RP6 RX
RPOR3bits.RP7R=3; // RP7 TX
U2MODEbits.STSEL = 0; // 1-stop bit
U2MODEbits.PDSEL = 0; // No Parity, 8-data bits
U2MODEbits.ABAUD = 0; // Auto-Baud Disabled
U2MODEbits.BRGH = 0; // Low Speed mode
U2BRG =BRGVAL; // BAUD Rate Setting for 9600
U2STAbits.UTXISEL0 = 0; // Interrupt after one TX character is transmitted
U2STAbits.UTXISEL1 = 0;
U2STAbits.URXISEL = 0; // Interrupt after one RX character is transmitted
SRbits.IPL=3;//CPU????3
IPC7bits.U2TXIP=4;//????????4
IPC7bits.U2RXIP=4;//????????4
IFS1bits.U2TXIF=0;//????????
IEC1bits.U2TXIE = 1; // Enable UART TX interrupt
IEC1bits.U2RXIE = 1;//Enable UART RX interrupt
U2MODEbits.UARTEN = 1; // Enable UART
U2STAbits.UTXEN = 1; // Enable UART TX
TRISBbits.TRISB10=0;
TRISBbits.TRISB11=0;
TRISBbits.TRISB12=0;
TRISBbits.TRISB13=0;
unsigned int i;
for(i = 0; i < 4160; i++) // /* wait at least 104 ?s (1/9600) before sending first char */
{ Nop();
}
}
void Delay_ms(uint ms)//?????
{
uchar i;
while(ms--)
{
for(i=0;i<120;i++);
}
}
void UART2_write(unsigned char dat)//????????
{ U2TXREG = dat;
while(!U2STAbits.TRMT);
}
void UART2_puts(char *s)//?????????
{
while(*s)
{
UART2_write(*s);
s++;
}
UART2_write(0x0D);
UART2_write(0x0A);
}
//void _interrupt_((_interrupt_,no_auto_psv))_U2RXInterrupt(void)//????????
void __attribute__((__interrupt__)) __U2RXInterrupt(void)
{ char n ;
n=U2RXREG;
IFS1bits.U2RXIF = 0;
int z=0;
if(z>=90) z=0;
SyBuF[z]=n;z++;
}
void GSM_INIT(void)//??GSM?????
{ kk:Delay_ms(1000);
UART2_puts("AT");//??
Delay_ms(1000);
UART2_puts("ATE");//????
Delay_ms(1000);
UART2_puts("AT+CNMI=2,1");//???????????????????????
UART2_puts("AT+CSCA="+8613800220500""//????????
Delay_ms(1000)
for(Index=0;Index<RxIn;Index++)
{ SyBuF[Index]=0x00;}
Index=0;
UART2_puts("AT+CMGF=1");//???????Text
Delay_ms(1000);
if ((SyBuF[2]=='O')&&(SyBuF[3]=='K'))
{
for(Index=0;Index<RxIn;Index++)
SyBuF[Index]=0x00;
Index=0;
}
else
{ for(Index=0;Index<RxIn;Index++)
{SyBuF[Index]=0x00;}
Index=0;
goto kk
}
}
void receive_ready(void);//??????
{
uchar a;
if((SyBuF[5]==0x54)&& SyBuF[6]==0x49)
{
receiveready=1;
}
else {
for (a=0;a<Index;a++)
{SyBuF[Index]=0x00;}
Index=0;
}
}
void read_message(void)//?????
{
uchar i;
Delay_ms(1000);
for(i=0;i<3;i++)
{
numberbuf[i]=SyBuF[14+i];
}
for(i=0;i<8;i++)
{
AT_Read[i]=AT_CMGR[i];
}
for(i=8;i<11;i++)
{
AT_Read[i]=numberbuf[i-8];
}
for(Index=0;Index<RxIn;Index++)
{
SyBuF[Index]=0x00;
}
Index=0;
UART2_puts(AT_Read);
}
void readcommend(void)//????:??????,??????????
{
uchar i;
for(i=0;i<5;i++)
{ CommandBuf[i]=SyBuF[64+i];}
if((CommandBuf[0]=='o')&&(CommandBuf[1]=='p')&&(CommandBuf[2]=='e')&&(CommandBuf[3]=='n'))//?????????????
{
switch(CommandBuf[4])
{case 0x31:LATBbits.LATB10=1;//???1??
break;
case 0x32: LATBbits.LATB11=0; //???2??
break;
case 0x33: LATBbits.LATB12=0; //??? 3??
break;
case 0x34: LATBbits.LATB13=0;//??? 4??
break;}
}
else
{
if((CommandBuf[0]=='s')&&(CommandBuf[1]=='h')&&(CommandBuf[2]=='u')&&(CommandBuf[3]=='t'))
{
switch(CommandBuf[4])
{
case 0x31: LATBbits.LATB10=1; //?????1
break;
case 0x32: LATBbits.LATB11=1; //?????2
break;
case 0x33: LATBbits.LATB12=1; //?????3
break;
case 0x34: LATBbits.LATB13=1;//?????4
break;
}
}
}
}
int main()//???
{
uart_init();
GSM_INIT(void);
while (1)
{ receive_ready();
if (receiveready==1);
{
read_message();
receiveready=0;
}
Delay_ms(2000);
readcommend();
for (Index=0;Index<RxIn;Index++)
{
SyBuF[Index]=0x00;
}
Index=0;
}
}
|