操作SIM300实现短信,语音,gprs功能#include "stm32f10x.h" //STM32开发板包含文件#include "LCD.h" / /触摸屏包含文件#include "SysTickDelay.h"#include "UART_INTERFACE.h"#include "touch.h"#include "gsm.h" GSM模块包含文件#include <stdio.h 系统函数包含文件/要写入到M25P16的字符串数组const u8 TEXT_Buffer[]={"STM32 SPI TEST"}; 的宏定义#define SIZE sizeof(TEXT_Buffer)extern unsigned char gImage_sms2[56640];#define LED2_ON GPIO_ResetBits(GPIOC, GPIO_Pin_2)#define LED2_OFF GPIO_SetBits(GPIOC, GPIO_Pin_2)#define LED1_ON GPIO_ResetBits(GPIOC, GPIO_Pin_1)#define LED1_OFF GPIO_SetBits(GPIOC, GPIO_Pin_1)#define LED0_ON GPIO_ResetBits(GPIOC, GPIO_Pin_0)#define LED0_OFF GPIO_SetBits(GPIOC, GPIO_Pin_0)#define KEY3_ON GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_2)==1#define KEY3_OFF GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_2)!=1#define KEY2_ON GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_3)==1#define KEY2_OFF GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_3)!=1#define KEY1_ON GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_4)==1#define KEY1_OFF GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_4)!=1//系统中断管理void NVIC_Configuration(void){NVIC_InitTypeDef NVIC_InitStructure; //定义数据结构体NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);//将中断矢量放到Flash的0地址NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//设置优先级配置的模式,//使能串口中断,并设置优先级NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(&NVIC_InitStructure); //将结构体丢到配置函数,即写入到对应寄存器中}//配置系统时钟,使能各外设时钟void RCC_Configuration(void){SystemInit();}//配置所有外设void Init_All_Periph(void){RCC_Configuration();NVIC_Configuration();GPIO_Configuration();//USART1_Configuration();USART_Configuration(9600);USART_RX_IntteruptEnable(USED_COM_NUMBER); //接收中断使能LCD_Init(); //TFTLCD液晶初始化}void Delay(vu32 nCount){for(; nCount != 0; nCount--);}void Load_Drow_Dialog(void){LCD_Clear(WHITE);//清屏POINT_COLOR=BLUE;//设置字体为蓝色LCD_ShowString(216,0,"RES");//显示清屏区域POINT_COLOR=RED;//设置画笔蓝色/选字母键if(POSCXY(0,186))//1{if(pre_key == 'a'){Entry('a');}else if(pre_key == 'd'){Entry('d');}else if(pre_key == 'g'){Entry('g');}else if(pre_key == 'j'){Entry('j');}else if(pre_key == 'm'){Entry('m');}else if(pre_key == 'p'){Entry('p');}else if(pre_key == 't'){Entry('t');}else if(pre_key == 'w'){Entry('w');}}功能键if(POSBXY(0,207))//中文{printf(GprsText);//向服务器发送数据STM_EV AL_SendChar(USED_COM_NUMBER,0x1a);//以0x1a结束}else if(POSBXY(0,243))//Abc{Entry('.');}else if(POSBXY(0,286))//abc{Entry('0');// CHOSEA('0');}输入位置if(TOUCHARE(0,0,240,50))//选中发送号码输入{num_sta = 1;text_sta = 0;}else if(TOUCHARE(0,50,240,150)){num_sta = 0;text_sta = 1;}if(num_sta == 1)//正在输入号码{POINT_COLOR = 0xFFFF;BACK_COLOR = 0x0000;LCD_ShowString(0,0,"phone number:");POINT_COLOR = 0x0000;BACK_COLOR = 0xFFFF;LCD_ShowString(0,50,"sms text:");}else if(text_sta == 1)//正在输入内容{POINT_COLOR = 0xFFFF;BACK_COLOR = 0x0000;LCD_ShowString(0,50,"sms text:");POINT_COLOR = 0x0000;BACK_COLOR = 0xFFFF;LCD_ShowString(0,0,"phone number:");}////////////////////////////////////////////////////转换结果//根据触摸屏的校准参数来决定转换后的结果,保存在X0,Y0中void Convert_Pos(void){if(Read_ADS2(&Pen_Point.X,&Pen_Point.Y)){Pen_Point.X0=Pen_Point.xfac*Pen_Point.X+Pen_Point.xoff;Pen_Point.Y0=Pen_Point.yfac*Pen_Point.Y+Pen_Point.yoff;}}void Pen_Int_Set(u8 en){if(en)EXTI->IMR |= EXTI_Line12; //开启line1上的中断else EXTI->IMR &=~EXTI_Line12; //关闭line1上的中断}////////////////////////////////////////////////////////////////////////////此部分涉及到使用外部EEPROM,如果没有外部EEPROM,屏蔽此部分即可#ifdef ADJ_SA VE_ENABLE//保存在EEPROM里面的地址区间基址,占用13个字节(RANGE:SA VE_ADDR_BASE~SA VE_ADDR_BASE+12)#define SA VE_ADDR_BASE 40//保存校准参数//void Save_Adjdata(void)//{// s32 temp;// //保存校正结果!// temp=Pen_Point.xfac*100000000;//保存x校正因素// AT24CXX_WriteLenByte(SAVE_ADDR_BASE,temp,4);// temp=Pen_Point.yfac*100000000;//保存y校正因素// AT24CXX_WriteLenByte(SAVE_ADDR_BASE+4,temp,4);// //保存x偏移量// AT24CXX_WriteLenByte(SAVE_ADDR_BASE+8,Pen_Point.xoff,2);// //保存y偏移量// AT24CXX_WriteLenByte(SA VE_ADDR_BASE+10,Pen_Point.yoff,2);//// temp=AT24CXX_ReadOneByte(SAVE_ADDR_BASE+12);// temp&=0XF0;// temp|=0X0A;//标记校准过了// AT24CXX_WriteOneByte(SA VE_ADDR_BASE+12,temp);//}//得到保存在EEPROM里面的校准值//返回值:1,成功获取数据// 0,获取失败,要重新校准//u8 Get_Adjdata(void)//{// s32 tempfac;// tempfac=AT24CXX_ReadOneByte(52);//第五十二字节的第四位用来标记是否校准过!// if((tempfac&0X0F)==0X0A)//触摸屏已经校准过了// {// tempfac=AT24CXX_ReadLenByte(40,4);// Pen_Point.xfac=(float)tempfac/100000000;//得到x校准参数// tempfac=AT24CXX_ReadLenByte(44,4);// Pen_Point.yfac=(float)tempfac/100000000;//得到y校准参数// //得到x偏移量// tempfac=AT24CXX_ReadLenByte(48,2);// Pen_Point.xoff=tempfac;// //得到y偏移量// tempfac=AT24CXX_ReadLenByte(50,2);// Pen_Point.yoff=tempfac;// return 1;// }// return 0;//}#endif//触摸屏校准代码//得到四个校准参数void Touch_Adjust(void){u16 pos_temp[4][2];//坐标缓存值u8 cnt=0;u16 d1,d2;u32 tem1,tem2;float fac;cnt=0;POINT_COLOR=BLUE;BACK_COLOR =WHITE;LCD_Clear(WHITE);//清屏POINT_COLOR=RED;//红色LCD_Clear(WHITE);//清屏Drow_Touch_Point(20,20);//画点1Pen_Point.Key_Sta=Key_Up;//消除触发信号Pen_Point.xfac=0;//xfac用来标记是否校准过,所以校准之前必须清掉!以免错误while(1){if(Pen_Point.Key_Sta==Key_Down)//按键按下了{if(Read_TP_Once())//得到单次按键值{pos_temp[cnt][0]=Pen_Point.X;pos_temp[cnt][1]=Pen_Point.Y;cnt++;}switch(cnt){case 1:LCD_Clear(WHITE);//清屏Drow_Touch_Point(220,20);//画点2break;case 2:LCD_Clear(WHITE);//清屏Drow_Touch_Point(20,300);//画点3break;case 3:LCD_Clear(WHITE);//清屏Drow_Touch_Point(220,300);//画点4break;case 4: //全部四个点已经得到//对边相等tem1=abs(pos_temp[0][0]-pos_temp[1][0]);//x1-x2tem2=abs(pos_temp[0][1]-pos_temp[1][1]);//y1-y2tem1*=tem1;tem2*=tem2;d1=sqrt(tem1+tem2);//得到1,2的距离tem1=abs(pos_temp[2][0]-pos_temp[3][0]);//x3-x4tem2=abs(pos_temp[2][1]-pos_temp[3][1]);//y3-y4tem1*=tem1;tem2*=tem2;d2=sqrt(tem1+tem2);//得到3,4的距离fac=(float)d1/d2;if(fac<0.95||fac>1.05||d1==0||d2==0)//不合格{cnt=0;LCD_Clear(WHITE);//清屏Drow_Touch_Point(20,20);continue;}tem1=abs(pos_temp[0][0]-pos_temp[2][0]);//x1-x3tem2=abs(pos_temp[0][1]-pos_temp[2][1]);//y1-y3tem1*=tem1;tem2*=tem2;d1=sqrt(tem1+tem2);//得到1,3的距离tem1=abs(pos_temp[1][0]-pos_temp[3][0]);//x2-x4tem2=abs(pos_temp[1][1]-pos_temp[3][1]);//y2-y4tem1*=tem1;tem2*=tem2;d2=sqrt(tem1+tem2);//得到2,4的距离fac=(float)d1/d2;if(fac<0.95||fac>1.05)//不合格{cnt=0;LCD_Clear(WHITE);//清屏Drow_Touch_Point(20,20);continue;}//正确了//对角线相等tem1=abs(pos_temp[1][0]-pos_temp[2][0]);//x1-x3tem2=abs(pos_temp[1][1]-pos_temp[2][1]);//y1-y3tem1*=tem1;tem2*=tem2;d1=sqrt(tem1+tem2);//得到1,4的距离tem1=abs(pos_temp[0][0]-pos_temp[3][0]);//x2-x4tem2=abs(pos_temp[0][1]-pos_temp[3][1]);//y2-y4tem1*=tem1;tem2*=tem2;d2=sqrt(tem1+tem2);//得到2,3的距离fac=(float)d1/d2;if(fac<0.95||fac>1.05)//不合格{cnt=0;LCD_Clear(WHITE);//清屏Drow_Touch_Point(20,20);continue;}//正确了计算结果Pen_Point.xfac=(float)200/(pos_temp[1][0]-pos_temp[0][0]);//得到xfacPen_Point.xoff=(240-Pen_Point.xfac*(pos_temp[1][0]+pos_temp[0][0]))/2;//得到xoffPen_Point.yfac=(float)280/(pos_temp[2][1]-pos_temp[0][1]);//得到yfacPen_Point.yoff=(320-Pen_Point.yfac*(pos_temp[2][1]+pos_temp[0][1]))/2;//得到yoffPOINT_COLOR=BLUE;LCD_Clear(WHITE);//清屏LCD_ShowString(35,110,"Touch Screen Adjust OK!");//校正完成delay_ms(1000);LCD_Clear(WHITE);//清屏return;//校正完成}}}//外部中断初始化函数void Touch_Init(void){EXTI_InitTypeDef EXTI_InitStructure;NVIC_InitTypeDef NVIC_InitStructure;GPIO_InitTypeDef GPIO_InitStructure;SPIv_Init();//配置片选管脚GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;GPIO_Init(GPIOE, &GPIO_InitStructure);//不选NotSelect_Touch();//配置中断脚GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;GPIO_Init(GPIOD, &GPIO_InitStructure);GPIO_SetBits(GPIOD,GPIO_Pin_12);//配置int脚GPIO_EXTILineConfig(GPIO_PortSourceGPIOD, GPIO_PinSource12);EXTI_InitStructure.EXTI_Line = EXTI_Line12;EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;//下降沿触发EXTI_InitStructure.EXTI_LineCmd = ENABLE;EXTI_Init(&EXTI_InitStructure);//配置中断NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(&NVIC_InitStructure);}我利用了STM32中的PA9,PA10(分别为通用发送接和通用接受借口)和SIM300中的GTX ,GRX(用于调试和通信的串口),GND(数字接地端)。