📄 sms.c
字号:
bit high_flag=(index_high!='0');
UCHAR byte_length=0;
error_counter=0;
if(high_flag)
byte_length=17;
else
byte_length=16;
while(1)
{
TI0=0;
RI0=0;
Serial0_Inter_Close();
uart0_send(Command_Cmgd,8);
if(high_flag)
{
SBUF0 =index_high;
while(!TI0); TI0 = 0;
}
SBUF0 =index_low;
while(!TI0); TI0 = 0;
SBUF0 ='\r';
while(!TI0); TI0 = 0;
start_timer0();
for(i=0;i<byte_length;i++)
{
while(!RI0);
rec[i]=SBUF0;
RI0=0;
}
close_timer0();
if((!high_flag&&rec[12]== 'O'&& rec[13]== 'K')||(high_flag&&rec[13]== 'O'&& rec[14]== 'K')) break;
else if(error_counter++<10) Delay_1S();
else deal_with_error();
}
RI0=0;
TI0=0;
Serial0_Inter_Open();
Delay10ms();
}
void Serial0_Service(void) interrupt 4
{
static unsigned char i=0;
static bit cmti_flag=0;
unsigned char j;
unsigned char xdata buffer[16];
unsigned char temp;
if(RI0)
{
temp=SBUF0;
RI0=0; //清接收标志
if(temp=='+')
{
buffer[0]='+';
for(j=1;j<16;j++)
buffer[j]=0;
i=1;
}
else
{
if(i<16)
buffer[i++]=temp;
if(i==5)
{
if(buffer[1]=='C'&&buffer[2]=='M'&&buffer[3]=='T'&&buffer[4]=='I')
cmti_flag=1;
else
cmti_flag=0;
}
else
if(cmti_flag&&i==15)
{
cmti_flag=0;
if(buffer[13]==0x0D&&buffer[14]==0x0A)
{
sms_index[sms_index_len][0]='0';
sms_index[sms_index_len++][1]=buffer[12];
}
else
if(buffer[14]==0x0D)
{
sms_index[sms_index_len][0]=buffer[12];
sms_index[sms_index_len++][1]=buffer[13];
}
else
deal_with_error();
}
}
}
}
unsigned int String2Bytes(const char* pSrc, unsigned char* pDst,const int nSrcLength)
{
unsigned int i;
for ( i = 0; i < nSrcLength; i += 2)
{
// 输出高4位
if ((*pSrc >= '0') && (*pSrc <= '9'))
{
*pDst = (*pSrc - '0') << 4;
}
else
{
*pDst = (*pSrc - 'A' + 10) << 4;
}
pSrc++;
// 输出低4位
if ((*pSrc>='0') && (*pSrc<='9'))
{
*pDst |= *pSrc - '0';
}
else
{
*pDst |= *pSrc - 'A' + 10;
}
pSrc++;
pDst++;
}
// 返回目标数据长度
return (nSrcLength/2);
}
unsigned char DecodeUcs2(const unsigned char* pSrc, char* pDst,const int nSrcLength)
{
unsigned short nDstLength; // UNICODE宽字符数目
unsigned short i,j;
unsigned char temp_h,temp_l;
nDstLength=nSrcLength>>1;
for(j=0;j<nDstLength;j++)
{
//得到两个字节的数据,先取高位,后取低位
temp_h=*pSrc;
temp_l=*(pSrc+1);
pSrc+=2;
if(temp_h==0x00)//如果高位字节为0x00,表明是Ascii表中数据,从0x20-0x7e,将其处理成语音芯片要求的GB2312格式
{
if(temp_l==0x0a||temp_l==0x0d)
{
*pDst++=0xa3;
*pDst++=0xa0;//将0x0a,0x0d当作空格处理
}
else
if(temp_l>=0x20&&temp_l<0x7f)
{
*pDst++=0xa3;
*pDst++=temp_l+0x80;
}
}
else
{
for(i=0;i<13536;i=i+2)
if(temp_h==gb2ucs_lookup[i]&&temp_l==gb2ucs_lookup[i+1])
break;
if(i==13536)//查找表中没有找到,则返回&
{
*pDst++=0xa3;
*pDst++=0xa6;
}
else
{
*pDst++=(i>>1)/94+0xB0;
*pDst++=(i>>1)%94+0xA1;
}
}//else
}//for
return nDstLength;
}
//========sms有关的函数结束===================
/****************************
open and close leds
****************************/
void led1_open()
{ led1=1; }
void led1_close()
{ led1=0; }
void led2_open()
{ led2=1; }
void led2_close()
{ led2=0; }
void led3_open()
{
led3=1;
}
void led3_close()
{
led3=0;
}
//===================================
//***************delay 1s***********
void Delay_1S(void)
{
UCHAR idata i;
for(i=0;i<100;i++) Delay10ms();
}
//=================================
//********延时子程序*********
//*********************************
void Delay10ms()
{
UINT idata i;
for(i=0;i<10000;i++);
}
void start_timer0(void)
{
timer0_counter=0;
CKCON &= 0xF7; //设置定时器0为系统时钟12分频T0M=0
TH0=0x00;
TL0=0x00;
TMOD = 0x01; //设置定时器0为模式1(定时器1为模式0)
TCON &= 0xDF; //清定时器0溢出中断标志TF0
ET0=1; //允许定时器0溢出中断
TCON |=0x10; //开启定时器0
}
void close_timer0(void)
{
timer0_counter=0;
TCON &=0xEF;
ET0=0;
}
//deal with overtime error
void timer0(void) interrupt 1
{
if(timer0_counter++>250)//error times over 250
deal_with_error();
}
//led1,2,3 all open when error occures
void deal_with_error(void)
{
led1_open();
led2_open();
led3_open();
send(0xf4);
disp_chn(error_display,16);
while(1);
}
/*************************LCD module ***************************************
****************************************************************************/
void send (UCHAR dat)
{
LCD_REQ=0;
while(LCD_BUSY==1);//wait until LCD is free
LCD_DATA=dat; //put data on the bus
LCD_REQ=1; //use LCD_REQ to latch the data into OCMJ
while(LCD_BUSY==0);// wait until LCD deals with the request
LCD_REQ=0; //
}
/*write data or command */
void wr_zb (UCHAR comm,UCHAR addr_x,UCHAR addr_y)
{
send(comm);
send(addr_x);
send(addr_y);
}
/*--------------display dot array------------------*/
void disp_lat (UCHAR data1,UCHAR data2)
{
UCHAR i,j,k;
for(k=0;k<32;k=k+4)
{
for(j=0;j<2;j++)
{
for(i=0;i<16;i++)
{
wr_zb(0xf3,i,k+j);
send(data1);
}
}
for(j=2;j<4;j++)
{
for(i=0;i<16;i++)
{
wr_zb(0xf3,i,k+j);
send(data2);
}
}
}
}
/*-----------------display image-----------------*/
void disp_img (UCHAR code *img)
{
UCHAR i,j;
for(j=0;j<32;j++)
{
for(i=0;i<16;i++)
{
wr_zb(0xf3,i,j);
send(img[j*16+i]);
}
}
}
/*
//--------------display Chinese------------------
void disp_chn (UCHAR code *chn)
{
UCHAR i,j;
for(j=0;j<2;j++)
{
for(i=0;i<8;i++)
{
wr_zb(0xf0,i,j);
send(chn[j*16+i*2]-0xa0);
send(chn[j*16+i*2+1]-0xa0);
}
}
}
*/
void disp_chn (UCHAR *chn,UCHAR gb_length)
{
UCHAR i,j;
if(gb_length<=8)
{
for(i=0;i<gb_length;i++)
{
if(chn[i*2]==0xa3)
{
wr_zb(0xf9,i*2,0);
send(chn[i*2+1]-0x80);
}
else
{
wr_zb(0xf0,i,0);
send(chn[i*2]-0xa0);
send(chn[i*2+1]-0xa0);
}
}
}
else
if(gb_length<=16)
{
for(i=0;i<8;i++)
{
if(chn[i*2]==0xa3)
{
wr_zb(0xf9,i*2,0);
send(chn[i*2+1]-0x80);
}
else
{
wr_zb(0xf0,i,0);
send(chn[i*2]-0xa0);
send(chn[i*2+1]-0xa0);
}
}
for(i=0;i<(gb_length-8);i++)
{
if(chn[i*2+16]==0xa3)
{
wr_zb(0xf9,i*2,16);
send(chn[i*2+17]-0x80);
}
else
{
wr_zb(0xf0,i,1);
send(chn[i*2+16]-0xa0);
send(chn[i*2+17]-0xa0);
}
}
}
else
for(j=0;j<2;j++)
{
for(i=0;i<8;i++)
{
if(chn[j*16+i*2]==0xa3)
{
wr_zb(0xf9,i*2,j*16);
send(chn[j*16+i*2+1]-0x80);
}
else
{
wr_zb(0xf0,i,j);
send(chn[j*16+i*2]-0xa0);
send(chn[j*16+i*2+1]-0xa0);
}
}
}
}
/*--------------display English------------------*/
void disp_eng (UCHAR code *eng)
{
UCHAR i,j;
for(j=0;j<4;j++)
{
for(i=0;i<16;i++)
{
wr_zb(0xf1,i,j*8);
send(eng[j*16+i]);
}
}
}
void test_lcd(void)
{
UCHAR i;
LCD_REQ=0; //as output
LCD_BUSY=1;//as input
while(1)
{
send(0xf4);//clear screen
disp_lat(0xf0,0xf0);
for(i=0;i<2;i++)
Delay_1S();
send(0xf4);
disp_chn(tab2);
for(i=0;i<2;i++)
Delay_1S();
send(0xf4);
disp_lat(0xff,0x00);
for(i=0;i<2;i++)
Delay_1S();
send(0xf4);
disp_eng(tab4);
for(i=0;i<2;i++)
Delay_1S();
send(0xf4);
disp_lat(0xcc,0xcc);
for(i=0;i<2;i++)
Delay_1S();
send(0xf4);
disp_img(tab6);
for(i=0;i<2;i++)
Delay_1S();
}
}
/************************************************************************************
end of LCD module
***********************************************************************************/
/****************************sound module******************************************/
void sound_ini(void)
{
sound_par1=Par1_Default;
sound_par2=Par2_Default;
sound_par3=Par3_Default;
sound_buffer[0]=Frame_Head;//01
sound_buffer[1]=0x80;//0x80
sound_buffer[2]=0x80;//0x80
sound_buffer[3]=0xc5;//0xc0
sound_buffer[4]=Frame_End; //0x04
sound_buffer[5]=check_sum(sound_buffer,5);
buffer_len=0x06; //close Timer4
// while(SOUND_READY);
uart1_send(sound_buffer,buffer_len);
//wait for several hundred ms
Delay_1S();
Delay_1S();
Delay_1S();
/*
//uart1 baudrate reset
T4CON &= 0xfb;
T4CON = 0x30; //Set Timer4 Mode 2 (Auto Reload Byte)
RCAP4H = 0xff; //Set Reload High Byte
RCAP4L = 0xc4; //Set Reload Low Byte=9600bps
SCON1 = 0x50; //Set UART1 Mode 1 (1 Start Bit ,8 Data Bit ,1 Stop Bit) and Enable Receive
EIE2 &= 0xfb; //Disable Timer4 Interrupt
T4CON |= 0x04; //Start Timer4
*/
}
unsigned char check_sum(unsigned char data_buffer[],unsigned char data_len)
{
unsigned char i;
unsigned char sum=0;
for(i=0;i<data_len;i++)
{
sum=sum^data_buffer[i];
}
return sum;
}
void uart1_send(unsigned char data_buffer[],unsigned char data_len)
{
unsigned char i;
SCON1 &= 0xFD; //clear send flag
for(i=0;i<data_len;i++)
{
SBUF1 = data_buffer[i];
while(TI1==0);
SCON1 &= 0xFD;
}
}
void sound_test(void)
{
chn_data[0]=0x60;
chn_data[1]=0xA0;
sound_send(chn_data,0x02);
}
void sound_send(unsigned char data_buffer[],unsigned char data_len)
{
unsigned char i;
unsigned char RxBuf1[3];
sound_buffer[0]=Frame_Head;//01
sound_buffer[1]=sound_par1;//0x80
sound_buffer[2]=sound_par2;//0x80
sound_buffer[3]=sound_par3;//0xC8
for(i=0;i<data_len;i++)
sound_buffer[i+4]=data_buffer[i];
sound_buffer[data_len+4]=Frame_End;
sound_buffer[data_len+5]=check_sum(sound_buffer,data_len+5);
buffer_len=data_len+6;
// while(SOUND_READY);
uart1_send(sound_buffer,buffer_len);
sound_flag=0;
while(!sound_flag);
sound_flag=0;
com_flag=0;
while(!com_flag);
com_flag=0;
}
void Serial1_Service(void) interrupt 20
{
unsigned char temp;
if(RI1)
{
temp=SBUF1;
SCON1 &= 0xFE; //清接收标志
if(temp==0x11)
sound_flag=1;
if(temp==0x12)
com_flag=1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -