⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main._c

📁 本实例实现GSM模块的发送短消息
💻 _C
字号:
#include "iom32v.h"
#include <macros.h>
#include "uart1.h"
#include "UART.h"
#include "globle_temp.h"
//#include "sim340.h"
//端口初始化
void port_init(void)
{
 PORTA = 0x00;
 DDRA  = 0x00;
 PORTB = 0x00;
 DDRB  = 0x00;
 PORTC = 0x00; 
 DDRC  = 0x00;
 PORTD = 0x00;
 DDRD  = 0x00;
}
//串口初始化
void  uart0_init(void)
{
  //USART初始化
  UCSRB = 0x00;
  UCSRA=0x00;  //设置通信参数:8位数据位,1位停止位,无校验位
  UCSRC=0x86;  //选择寄存器C
  UBRRH =(unsigned char)(baud_set>>8);//设置波特率
  UBRRL =(unsigned char)baud_set;
  UCSRB=0xD8;  //接收、发送数据使能,并允许中断
}
//设备初始化
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 port_init();
 uart0_init();

 MCUCR = 0x00;
 GICR  = 0x00;
 TIMSK = 0x00; //timer interrupt sources
 SEI(); //re-enable interrupts
 //all peripherals are now initialized
}
void  main(void)
{
 int  nLen1;      //发送短消息时的命令编码长度
 int  nLen2;      //发送短消息的PDU编码长度
 char pOut1[40];  //命令代码缓冲区
 char pOut2[200]; //PDU编码缓冲区
 char PhoneNumber[13]={'8','6','1','5','9','6','1','1','5','2','1','3','7'};
 int  nPhone;   //被叫用户的手机号码和长度
 char pBuf[6]={'H','E','L','L','O','!'};//发送数据的内容和长度
 int   data_Len;
 char UART0_RX_Temp[200];	// 串口 0 的接收缓冲区
 int  n_tx0_length;         //要发送的数据长度
 int  i;
 char nSend;                //发送成功标志
 nPhone=13;
 data_Len=6;
 //uart0_init();               //串口初始化
 //DDRB|=0x20;                 //打开B口第五位作为输出
 //PORTB|=(1<<bit5);
 //#asm("sei");  //使能全局中断
 init_devices();
 DDRB|=0x20;                  //打开B口第五位作为输出
 PORTB|=(1<<bit5);
 DDRA|=0x04;                  //打开A口第二位作为输出
 PORTA|=(1<<2);
 DDRA|=0x10;                  //A口第4位输出,为模块上电
 PORTA&=~(1<<4);
 n_tx0_length=readcsca(uart0_tx_buf);  //读取短消息中心号码
 putstring(uart0_tx_buf,n_tx0_length);
 getstring(UART0_RX_Temp);
 sca_len=GetPhone(UART0_RX_Temp,sca);
 sca[sca_len]='F';
 //n_tx0_length = setCmgf(uart0_tx_buf);
 //putstring(uart0_tx_buf,n_tx0_length);
 //delay_nms(500);	   
	for(;;)
    {   
			//发送对方的电话号码和信息,并返回发送的命令串和信息的PDU编码
    	    SendMsm(PhoneNumber,nPhone,pBuf,data_Len,&nLen1,&nLen2,pOut1,pOut2,sca,sca_len);
    	    putstring(pOut1,nLen1);
    	    //等待"> "
    	    for(;;)
    	    {
    	    	if(Uart_RecvFlag == 1)
    	    	{
    	    	    Uart_RecvFlag = 0;
    	    	    for(i = 0;i < nRX0_Len;i++)
	                 {
		               UART0_RX_Temp[i] = rx_buffer[i];
	                 }
    	    	    if(nRX0_Len >= 2)
    	    	    {
    	    	    	if((UART0_RX_Temp[0] == 62)   //如果是"> ",则发送PDU编码
    	    	    	   && (UART0_RX_Temp[1] == 32))
    	    	    	{
						    putstring(pOut2,nLen2);
    	    	    	    nSend = 1;
    	    	    	    break;
    	    	    	}
    	    	    }
    	    	    else 
    	    	    {
    	    	    	nSend = 0;
    	    	    	break;
    	    	    }
    	    	    
    	    	}
    	    	
    	    }
    	    for(i = 0;i < nLen2;i++) 
    	    {
    	    	putchar(pOut2[i]);
    	    }
    	    delay_nms(10000);
    	}
    	
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -