AT+CPMS="SM","SM","ME"
txdx2005 said:#include "c8051f020.h" // SFR declarations
//#include "gb2unicode.h"
#include "gb2unicode_char.h"
#include "math.h"
#include "sms.h"
////////////////////////////////////////////////////////////////////////////
UCHAR code ini_display[]={0xc4,0xa3,0xbf,0xe9,0xd2,0xd1,0xd5,0xfd,0xb3,0xa3,0xc6,0xf4,0xb6,0xaf,
0xa3,0xac,0xbf,0xaa,0xca,0xbc,0xb9,0xa4,0xd7,0xf7};
//**************main函数开始************
void main (void)
{
UCHAR i,j;
unsigned char xdata sms_rec[200]={0};
unsigned char Byte_Len=0;
unsigned char String_Len=0;
unsigned char xdata Gb_Len=0;
unsigned char ch[]="4F60597D000A003C676867AB003E";
unsigned char xdata rec_buffer[200];
unsigned char sms_index_high,sms_index_low;
Mcu_ini();
led1_close();
led2_close();
led3_close();
Inter_Enable();
Baudrate_Init(); // init the serial port
Serial1_Inter_Close();
//sound module test
Serial1_Inter_Open();
Serial0_Inter_Close();
//test wireless module
sms_receive_flag=0;
sms_index_len=0;
for(i=0;i<SMS_MAXLENGTH;i++)
for(j=0;j<2;j++)
sms_index[j]='0';
sound_ini();
start_GSM();
//删除卡内所有短信
for(i=1;i<10;i++)
AT_cmgd('0','0'+i);
for(i=0;i<10;i++)
AT_cmgd('1','0'+i);
//设置收到短信时发送CMTI
AT_cnmi();
Serial0_Inter_Open();
led1_open();
send(0xf4);
disp_chn(ini_display,12);
sound_send(ini_display,24);
while(1)
{
if(sms_index_len>0)
{
led3_open();
Serial0_Inter_Close();
sms_index_high=sms_index[--sms_index_len][0];
sms_index_low=sms_index[sms_index_len][1];
Serial0_Inter_Open();
Delay_1S();
Delay_1S();
String_Len=AT_cmgr(sms_index_high,sms_index_low,SMS_Buffer);
Byte_Len=String2Bytes(SMS_Buffer, sms_rec,String_Len);
Gb_Len=DecodeUcs2(sms_rec, rec_buffer,Byte_Len);
send(0xf4);
disp_chn(rec_buffer,Gb_Len);
sound_send(rec_buffer,Gb_Len*2);
AT_cmgd(sms_index_high,sms_index_low);
led3_close();
}
}
}
/******************************************
//MCU Initialization
Port Configuration and Oscillator set
==========================================*/
void Mcu_ini(void)
{
unsigned char idata delay;
OSCXCN = 0x67; // Enable external crystal
WDTCN = 0xDE; // disable watchdog timer
WDTCN = 0xAD;
XBR0 = 0x04; // Set UART0
// XBR0 = 0x00;
XBR1=0x00; //disable /INT0,/INT1
XBR2 = 0x44; // Set UART1,Enable XBAR
P0MDOUT = 0x05; // Output configuration for P0 P0.0->TX0,P0.2->TX1 Push-Pull, others Open-drain
// P0MDOUT = 0x07;
P1MDOUT = 0x00; // Output configuration for P1 all Open-Drain
P2MDOUT = 0x00; // Output configuration for P2 all Open-Drain
P3MDOUT = 0x3b; // Output configuration for P3 P3.2(SOUND_READY),P3.6(LCD_BUSY),P3.7(LCD_REQ) Open-Drain,others Push-Pull
P0 |= 0x0a; // Enable RX0,RX1
P1 |= 0xff;
P3 |= 0x44;
delay=256; // Delay >1 ms before polling XTLVLD.
while(delay--);
while (!(OSCXCN & 0x80)); // Wait until external crystal has // started.
OSCICN = 0x0C; // Switch to external oscillator
OSCICN = 0x88; // Disable internal oscillator; enable
// missing clock detector.
while (!(OSCXCN & 0x80)); // Wait until external crystal has // started.
OSCICN = 0x08; // Switch to external oscillator
}
/*-------------------------------------------------------------------------
Uart0 and Uart1 Initialization
Uart0 Baudrate:9600bps
Uart1 Baudrate:9600bps
=================================*/
void Baudrate_Init(void)
{
//UART0 initialization use timer2 as baudrate generator
T2CON = 0x30; //Set Timer2 as Baudrate Generator of UART0
RCAP2H = 0xff; //Set Reload High Byte
RCAP2L = 0xc4; //Set Reload Low Byte,baudrate=9600bps
SCON0 = 0x50; //Set UART0 Mode 1 (1 Start Bit ,8 Data Bit ,1 Stop Bit) and Enable Receive
//UART1 initialization use timer4 as baudrate generator
T4CON = 0x30; //Set Timer4 Mode 2 (Auto Reload Byte)
RCAP4H = 0xfe; //Set Reload High Byte
RCAP4L = 0x20; //Set Reload Low Byte=1200bps
SCON1 = 0x50; //Set UART1 Mode 1 (1 Start Bit ,8 Data Bit ,1 Stop Bit) and Enable Receive
//**********************
ET2 = 0; //Disable Timer2 Interrupt
EIE2 &= 0xfb; //Disable Timer4 Interrupt
TR2 = 1; //Start Timer2
T4CON |= 0x04; //Start Timer4
}
//=================================================
//***********Interrupts Open and Close*************
//=================================================
void Serial0_Inter_Close()
{ ES0=0; }
void Serial0_Inter_Open()
{ ES0=1; }
void Serial1_Inter_Close(void)
{ EIE2 &= 0xbf; }
void Serial1_Inter_Open(void)
{ EIE2 |= 0x4f; }
//========================
//open global interrupts
void Inter_Enable()
{
EA=1;
}
void Inter_Disable()
{
EA=0;
}
//*********************************
/*************************************************************************
GSM Program Module
***************************************************************************/
//*********************************sms函数****************************************
void start_GSM(void)
{
UINT idata i;
led2_open();
// for(i=0;i<5;i++) Delay_1S();
power_down();
start_tc35();
for(i=0;i<11;i++) Delay_1S();
AT_SEND();
Delay_1S();
Delay_1S();
// Signal_test();
// Delay_1S();
AT_cmgf();
Delay_1S();
led2_close();//start complete
}
//cut off power
void power_down(void)
{
PD=1; //power off
Delay_1S();
//Delay_1S();
//Delay_1S();
//Delay_1S();
Delay_1S();
PD=0;
Delay_1S();//wait for module initialization
Delay_1S();
Delay_1S();
Delay_1S();
//Delay_1S();
//Delay_1S();
//Delay_1S();
}
//turn on GSM engine using Ignition line IGT
void start_tc35(void)
{
IGT=0;
Delay_1S();
Delay_1S();
IGT=1;
}
void uart0_send(unsigned char data_buffer[],unsigned char data_len)
{
unsigned char i;
SCON1 &= 0xFD; //clear send flag
for(i=0;i<data_len;i++)
{
SBUF0 = data_buffer;
while(TI0==0);
TI0=0;
}
}
void AT_SEND(void)
{
UCHAR idata i,error_counter;
error_counter=0;
while(1)
{
TI0=0;
RI0=0;
for(i=0;i<5;i++)
rec=0x00;
start_timer0();
uart0_send(Command_At,3);
for(i=0;i<9;i++)
{
while(!RI0);
rec=SBUF0;
RI0=0;
}
close_timer0();
if(rec[5]=='O'&&rec[6]=='K') break;
else if(error_counter++<10) Delay_1S();
else deal_with_error();
}
}
/***********Signal_test函数开始**********
Signal quality test
****************************/
void Signal_test(void)
{
UCHAR idata i,error_counter;
error_counter=0;
while(1) // AT+CSQ
{
TI0=0;
RI0=0;
start_timer0();
uart0_send(Command_Csq,7);
for(i=0;i<28;i++) //only receive part data (10)
{
while(!RI0);
rec = SBUF0;
RI0 = 0;
}
close_timer0();
if(rec[15]!= '9'&& rec[16]!= '9') break;
else if(error_counter++<10) Delay_1S();
else deal_with_error();
}
Delay_1S();
RI0=0;
}
/***********AT+cmgf*************
set preferred message format:text mode or PDU mode
In PDU mode, a complete SMS Message including all header information is passed
as a binary string (in hexadecimal format, so only this set of characters is allowed :
{‘0’,’1’,’2’,’3’,’4’,’5’,’6’,’7’,’8’,’9’, ‘A’, ‘B’,’C’,’D’,’E’,’F’} ). Each pair or characters is converted to
a byte (ex : ‘41’ is converted to the ASCII character ‘A’, whoes ASCII code is 0x41 or 65).
In Text mode, every commands and responses are in ASCII characters.
********************************/
void AT_cmgf(void)
{
UCHAR idata i;
while(1) // AT+CSQ
{
TI0=0;
RI0=0;
/*
for(i=0;i<10;i++)
{
SBUF0 = Command_Cmgf;
while(!TI0); TI0 = 0;
}
*/
uart0_send(Command_Cmgf,10);
break;
Delay_1S();
}
Delay_1S();
RI0=0;
TI0=0;
}
/***********AT+cmgf*************
set preferred message format:text mode or PDU mode
In PDU mode, a complete SMS Message including all header information is passed
as a binary string (in hexadecimal format, so only this set of characters is allowed :
{‘0’,’1’,’2’,’3’,’4’,’5’,’6’,’7’,’8’,’9’, ‘A’, ‘B’,’C’,’D’,’E’,’F’} ). Each pair or characters is converted to
a byte (ex : ‘41’ is converted to the ASCII character ‘A’, whoes ASCII code is 0x41 or 65).
In Text mode, every commands and responses are in ASCII characters.
********************************/
/*
void AT_cmgf(void)
{
UCHAR idata i,error_counter;
error_counter=0;
while(1) // AT+CSQ
{
TI0=0;
RI0=0;
start_timer0();
for(i=0;i<10;i++)
{
SBUF0 = Command_Cmgf;
while(!TI0); TI0 = 0;
}
for(i=0;i<16;i++) //only receive part data (10)
{
while(!RI0);
rec = SBUF0;
RI0 = 0;
}
close_timer0();
if(rec[12]!= 'O'&& rec[13]!= 'K')break;
else if(error_counter++<10) Delay_1S();
else deal_with_error();
}
Delay_1S();
RI0=0;
TI0=0;
}
*/
/***********AT_cmgs****************************************
Send message function
After sending "CMGS" command,there will be 16 bytes received,
among which the 15th and 16th must be "0x3E"and "0x20"
***********AT+cmgs函数开始********************************/
void AT_cmgs(void)
{
UCHAR idata i;
while(1) // AT+cmgs
{
TI0=0;
RI0=0;
/*
for(i=0;i<12;i++)
{
SBUF0 = Command_Cmgs;
while(!TI0); TI0 = 0;
}
*/
uart0_send(Command_Cmgs,12);
start_timer0();
for(i=0;i<16;i++)
{
while(!RI0);
rec = SBUF0;
RI0 = 0;
}
close_timer0();
if(rec[14]== 0x3E&& rec[15]== 0x20)
break;
// Delay_1S();
}
// Delay_1S();
for(i=0;i<50;i++); //延时一段时间
RI0=0;
TI0=0;
}
//***********AT+cmgs函数结束*************
//========sms有关的函数结束===================
//发送GSMhead[],Phone_number,GSM_set[],GSM_sms[],0x1A;
//***********SEND_sms函数开始*************
void SEND_sms(void)
{
UCHAR idata i;
// while(1)
{
TI0=0;
RI0=0;
//send GSMhead(12 bytes)
for(i=0;i<12;i++)
{
SBUF0 = GSMhead;
while(!TI0); TI0 = 0;
}
//send phone number(12 bytes)
for(i=0;i<12;i++)
{
SBUF0 = Center_number;
while(!TI0);
TI0 = 0;
}
//send GSM_set(6 bytes)
for(i=0;i<6;i++)
{
SBUF0 = GSM_set;
while(!TI0); TI0 = 0;
}
//send sms contents(26 bytes)
for(i=0;i<26;i++)
{
SBUF0 = GSM_sms;
while(!TI0); TI0 = 0;
}
//send end bytes:0x1A
SBUF0=0x1A;
while(!TI0); TI0 = 0; //发送信息结束
/*
for(i=0;i<75;i++)
{
while(!RI0);
rec = SBUF0;
RI0 = 0;
}
if(rec[72]== 'O'&& rec[73]== 'K')break;
*/
Delay_1S();
}
// Delay_1S();
for(i=0;i<255;i++); //延时一段时间
RI0=0;
TI0=0;
}
//***********SEND_sms函数结束*************
void AT_cnmi(void)
{
UCHAR idata i,error_counter;
error_counter=0;
Serial0_Inter_Close();
while(1)
{
TI0=0;
RI0=0;
uart0_send(Command_Cnmi,18);
start_timer0();
for(i=0;i<24;i++) //only receive part data (10)
{
while(!RI0);
rec = SBUF0;
RI0 = 0;
}
close_timer0();
if(rec[20]== 'O'&& rec[21]== 'K') break;
else if(error_counter++<10) Delay_1S();
else deal_with_error();
}
Serial0_Inter_Open();
Delay_1S();
RI0=0;
}
//+CMGR: 3,,20
unsigned char AT_cmgr(unsigned char index_high,unsigned char index_low,unsigned char buffer[])
{
UCHAR idata i;
UCHAR byte_length=0;
UCHAR length;
UCHAR temp;
TI0=0;
RI0=0;
if(index_high=='0')
byte_length=82;
else
byte_length=83;
Serial0_Inter_Close();
uart0_send(Command_Cmgr,8);
if(index_high!='0')
{
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);
temp = SBUF0;
RI0 = 0;
}
//接收两个字节的短信长度字符
//短信长度高位
while(!RI0);
temp = SBUF0;
RI0 = 0;
if((temp>='0')&&(temp<='9'))
length=(temp-'0')<<4;
else
length=(temp-'A'+10)<<4;
//短信长度低位
while(!RI0);
temp = SBUF0;
RI0 = 0;
if((temp>='0')&&(temp<='9'))
length|=(temp-'0');
else
length|=(temp-'A'+10);
//length*2个短信内容字符
for(i=0;i<length*2;i++)
{
while(!RI0);
buffer=SBUF0;
RI0=0;
}
for(i=0;i<8;i++)
{
while(!RI0);
temp=SBUF0;
RI0=0;
}
close_timer0();
Serial0_Inter_Open();
Delay_1S();
RI0=0;
TI0=0;
return length*2;
}
void AT_cmgd(unsigned char index_high,unsigned char index_low)
{
UCHAR idata i,error_counter;
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=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&&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;
}
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;
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;
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;
}
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?