元旦放假,急于赶一个小项目,做了一个M0516LAN驱动SIM900A的测试。第一次用SIM900A,其中遇到几个小问题,调了好久,也问过群里用过SIM900A的几位朋友才调通,在此感谢。最弱智的问题是我把NRESET接到了PWM3上,然后又启动了PWM3,虽然后面把P43置成IO输出却不成功,然后大半晚上就那么悲剧掉了。新唐的库是置成了复用功能再想置回IO,需要先DrvGPIO_InitFunction(E_FUNC_GPIO);才能配置IO功能的。我后来还是不启动PWM就通过了开机过程。只要SIM900A开机了,NETLIGHT的LED就会闪烁,不管有没有SIM卡。另外,所有SIM900A的AT命令都是以“\r”结束的。
“sim900a.c ”文件源码如下:
#include <stdio.h>
#include <M051Series.h>
#include "Driver\DrvUART.h"
#include "Driver\DrvGPIO.h"
#include "Driver\DrvSYS.h"
#define PWRKEY E_PORT2, E_PIN1
#define NRESET E_PORT4, E_PIN3
#define STATUS E_PORT2, E_PIN3
#define RI E_PORT3, E_PIN2
#define CTS E_PORT0, E_PIN0
/*
_________________________________________________
| M0516LAN SIM900A |
| |
| P21 PWRKEY ----> PWRKEY |
| P43 NRESET ----> NRESET |
| P23 STATUS <---- STATUS |
| P32 RI <---- RI |
| P00 CTS1 <---- CTS |
| P13 TXD1 ----> RXD |
| P12 RXD1 <---- TXD |
|_______________________________________________|
*/
int UART1_init(void)
{
STR_UART_T sParam;
/* Set UART1 Pin */
DrvGPIO_InitFunction(E_FUNC_UART1);
// outpw(&SYS->P1_MFP, (inpw(&SYS->P1_MFP) | (0x3<<10)) & ~(0x3<<2));
// outpw(&SYS->P0_MFP, (inpw(&SYS->P0_MFP) | (0x3<<8)) & ~(0x3<<0));
/* Select UART1 Clock Source From 12Mhz*/
DrvSYS_SelectIPClockSource(E_SYS_UART_CLKSRC,0);
/* UART1 Setting */
sParam.u32BaudRate = 9600;
sParam.u8cDataBits = DRVUART_DATABITS_8;
sParam.u8cStopBits = DRVUART_STOPBITS_1;
sParam.u8cParity = DRVUART_PARITY_NONE;
sParam.u8cRxTriggerLevel= DRVUART_FIFO_1BYTES;
if(DrvUART_Open(UART_PORT1,&sParam) != E_SUCCESS)
{
printf("UART1 open failed\n");
return FALSE;
}
/* Enable Interrupt and install the call back function */
// DrvUART_EnableInt(UART_PORT1, (DRVUART_RLSINT | DRVUART_RDAINT),UART1_INT_HANDLE);
/* Disable Interrupt */
// DrvUART_DisableInt(UART_PORT1,DRVUART_RLSINT | DRVUART_THREINT | DRVUART_RDAINT);
return TRUE;
}
int SIM900A_Open(void)
{
int dly;
DrvGPIO_ClrBit(PWRKEY);
for(dly=0;dly<2000;dly++) DrvSYS_Delay(1000); //delay 1S
DrvGPIO_SetBit(PWRKEY);
for(dly=0;dly<3000;dly++) DrvSYS_Delay(1000); //delay 3S
if(DrvGPIO_GetBit(STATUS))
{
printf("SIM900A Open Success!!!\n\r");
return 1;
}
else
{
printf("SIM900A Open Failure!!!\n\r");
return 0;
}
}
int SIM900A_Close(void)
{
int dly;
DrvGPIO_ClrBit(PWRKEY);
for(dly=0;dly<6000;dly++) DrvSYS_Delay(1000); //delay 1S-5S
DrvGPIO_SetBit(PWRKEY);
for(dly=0;dly<4000;dly++) DrvSYS_Delay(1000); //delay 2S
if(DrvGPIO_GetBit(STATUS))
{
printf("SIM900A Close Failure!!!\n\r");
return 1;
}
else
{
printf("SIM900A Close Success!!!\n\r");
return 0;
}
}
int SIM900A_Reset(void)
{
int dly;
DrvGPIO_ClrBit(NRESET);
DrvSYS_Delay(100); //delay 20uS-50uS
DrvGPIO_SetBit(NRESET);
for(dly=0;dly<2000;dly++) DrvSYS_Delay(1000); //delay 2S
if(DrvGPIO_GetBit(STATUS)==1)
return 1;
else
return 0;
}
int SIM900A_init(void)
{
int ret =0;
DrvGPIO_Open(STATUS, E_IO_INPUT);
DrvGPIO_Open(RI, E_IO_INPUT);
DrvGPIO_Open(CTS, E_IO_INPUT);
DrvGPIO_Open(PWRKEY, E_IO_OUTPUT);
DrvGPIO_Open(NRESET, E_IO_OUTPUT);
DrvGPIO_SetBit(PWRKEY);
DrvGPIO_SetBit(NRESET);
ret = SIM900A_Open();
UART1_init();
return ret;
}
int SIM900A_gets(char *buf)
{
int ret=0;
int dly=3000000; // delay 3S
while(dly-->1)
{
if(UART1->FSR.RX_EMPTY == 0 )
{
*buf++ =UART1->DATA;//printf("%c",*(buf-1));
ret++;dly=10000; //delay 10ms
}
DrvSYS_Delay(1); //delay 1us
}
return ret;
}
void SIM900A_puts(const char *buf)
{
while(*buf)
{
while(UART1->FSR.TX_FULL == 1);
UART1->DATA =(*buf++)&0xff;//printf("%c",*(buf-1));
}
}
// AT+CPOWD=1 //Power down
// AT+CFUN=x //Power Saving
// AT+CBC //monitor the supply voltage
// AT+IPR=x //fixed baud rate ,“RDY” is received
// AT+CSCLK=1 //enable the SLEEP mode
// AT+CMIC //adjust the input gain level of microphone
// AT+SIDET //set the side-tone level
// AT+CLVL //adjust the output gain level
// AT+CADC //read the voltage value on ADC
void SIM900A_test(void)
{
char buffer[128];
printf("\n\r\n\r");
printf("*** 9G-M0516 V1.00 Build by yuanxihua@21cn.com on ("__DATE__ " - " __TIME__ ")\n\r");
printf("*** 9G-M0516 V1.00 Rebooting ...\n\r\n\r");
SIM900A_init();
printf("SIM900A_test Send AT COMMAND @ 9600 bps !!!\n\r");
// SIM900A_puts("ATI\r");
SIM900A_puts("AT+GMM\r");
if(SIM900A_gets(buffer)>0)
{printf("\n\r%s\n\r",buffer);}
}
//源码完毕。--缥缈九哥调试通过。
半主机的调试信息如下:
*** 9G-M0516 V1.00 Build by yuanxihua@21cn.com on (Jan 3 2012 - 13:16:32)
*** 9G-M0516 V1.00 Rebooting ...
SIM900A Open Success!!!
SIM900A_test Send AT COMMAND @ 9600 bps !!!
AT+GMM
SIMCOM_SIM900A
OK
--缥缈九哥验证
读SIM卡电话本。
SIM900A_puts("AT+CPBS=\"SM\"\r");
读第一条电话本记录
//get SIM no.1 phoneid
SIM900A_puts("AT+CPBR=1,1\r");
memset(buffer,'\0',sizeof(buffer));
if(SIM900A_gets(buffer)>0)
{printf("\n\r%s\n\r",buffer);}
for(i=0;i<11;i++)
{
//AT+CPBR=1,1
//+CPBR: 1,"13925101136",129,"yuanxihua"
// printf("\n\r%c",buffer[24+i]);
callphoneid[i]= buffer[24+i];
}
//
发中文短信。
SIM900A_puts("AT+CMGF=1\r"); //TEXT Mode
memset(buffer,'\0',sizeof(buffer));
if(SIM900A_gets(buffer)>0)
{printf("\n\r%s\n\r",buffer);}
SIM900A_puts("AT+CSCS=\"UCS2\"\r"); //for CHINESE
memset(buffer,'\0',sizeof(buffer));
if(SIM900A_gets(buffer)>0)
{printf("\n\r%s\n\r",buffer);}
//不用配置短信服务中心号码
/*
SIM900A_puts("AT+CSCA=\"002B0038003600310033003800300030003200300030003500300030\",145\r"); //+8613800200500 短信中心号码
memset(buffer,'\0',sizeof(buffer));
if(SIM900A_gets(buffer)>0)
{printf("\n\r%s\n\r",buffer);}
*/
SIM900A_puts("AT+CSMP=17,167,0,25\r");
memset(buffer,'\0',sizeof(buffer));
if(SIM900A_gets(buffer)>0)
{printf("\n\r%s\n\r",buffer);}
// SIM900A_puts("AT+CMGS=\"00310033003900320035003100300031003100330036\"\r"); //13925101136 被叫号码,不固定,从SIM卡读。
SIM900A_puts("AT+CMGS=\"");
for(i=0;i<11;i++)
{
SIM900A_puts("003");
SIM900A_putchar(callphoneid[i]);
}
SIM900A_puts("\"\r");
memset(buffer,'\0',sizeof(buffer));
if(SIM900A_gets(buffer)>0)
{printf("\n\r%s\n\r",buffer);}
SIM900A_puts("4F60597D ");
//你好 短信内容,UNICODE格式;
SIM900A_putchar(26); //<Ctrl+Z>
memset(buffer,'\0',sizeof(buffer));
if(SIM900A_gets(buffer)>0);
{printf("\n\r%s\n\r",buffer);}
打电话
printf("SIM900A Phone a call by AT-commond @ 9600 bps !!!\n\r");
// Call a phone
SIM900A_puts("ATD");
for(i=0;i<11;i++)
{
SIM900A_putchar(callphoneid[i]);
}
SIM900A_puts(";\r");
memset(buffer,'\0',sizeof(buffer));
if(SIM900A_gets(buffer)>0)
{printf("\n\r%s\n\r",buffer);}
发英文短信
printf("SIM900A Send a English SMS by AT-commond @ 9600 bps !!!\n\r");
//init SIM900A for SMS
SIM900A_puts("AT+CMGF=1\r"); //TEXT Mode
memset(buffer,'\0',sizeof(buffer));
if(SIM900A_gets(buffer)>0)
{printf("\n\r%s\n\r",buffer);}
SIM900A_puts("AT+CSCS=\"GSM\"\r");
memset(buffer,'\0',sizeof(buffer));
if(SIM900A_gets(buffer)>0)
{printf("\n\r%s\n\r",buffer);}
//prepore config SIM900A for sendding a SMS
SIM900A_puts("AT+CMGS=\"+86");
for(i=0;i<11;i++)
{
SIM900A_putchar(callphoneid[i]);
}
SIM900A_puts("\"\r");
memset(buffer,'\0',sizeof(buffer));
if(SIM900A_gets(buffer)>0)
{printf("\n\r%s\n\r",buffer);}
//send a SMS to SIM900A
SIM900A_puts("This a report from yuanxihua@21cn.com!!!\x1a");// \x1a = <Ctrl+Z>
memset(buffer,'\0',sizeof(buffer));
if(SIM900A_gets(buffer)>0)
{printf("\n\r%s\n\r",buffer);}
最近编辑记录 缥缈九哥 (2020-05-09 20:01:43)
离线