有图真相——焊接液晶以及调试GSM结果
帖点代码出来:就是UART2的,与GSM通信的串口通信,波特率位9600
#include "config.h"
u8 RxBuffer2[RxBuffer2_SIZE];
u8 TxBuffer2[TxBuffer2_SIZE];
u16 USART2_RX_HeadIndex;
u16 USART2_RX_TailIndex;
u16 USART2_TX_HeadIndex;
u16 USART2_TX_TailIndex;
u16 UASRT2_Input_Index;
/******************************************************************************
******************************************************************************/
void USART2_Initialise( void )
{
//------------------------------------------------------------------
GPIO_InitTypeDef GPIO_InitStructure;//I/O口初始化结构体
NVIC_InitTypeDef NVIC_InitStructure;//中断初始化结构体
USART_InitTypeDef USART_InitStructure;//串口初始化结构体
USART_ClockInitTypeDef USART_ClockInitS;
//------------------------------------------------------------------
// Flush the buffers by setting the indexes to 0
USART2_RX_HeadIndex = 0;
USART2_RX_TailIndex = 0;
USART2_TX_HeadIndex = 0;
USART2_TX_TailIndex = 0;
UASRT2_Input_Index = 0;
//------------------------------------------------------------------
/* Enable the USART2 Pins Software Remapping */
GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
//------------------------------------------------------------------
/* Configure USART2 Rx (PD.06) as input floating */
//GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;//zhai 10-26
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;//zhai 10-26
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);//zhai 10-26
//GPIO_Init(GPIOD, &GPIO_InitStructure);//zhai 10-26
/* Configure USART2 Tx (PD.05) as alternate function push-pull */
//GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;//zhai 10-26
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;//zhai 10-26
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
//GPIO_Init(GPIOD, &GPIO_InitStructure);//zhai 10-26
GPIO_Init(GPIOA, &GPIO_InitStructure);//zhai 10-26
//------------------------------------------------------------------
/* Enable the USART2 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//-----------------------------------------------------------------
// 串口相关配置
//USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 位宽
USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位
USART_InitStructure.USART_Parity = USART_Parity_No; //校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//硬件控制流
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //串口模式
USART_ClockInitS.USART_Clock = USART_Clock_Disable; //SCLK时钟使能(同步模式下)
USART_ClockInitS.USART_CPOL = USART_CPOL_Low; //时钟极性(同步模式下)
USART_ClockInitS.USART_CPHA = USART_CPHA_2Edge; //时钟相位(同步模式下)
USART_ClockInitS.USART_LastBit = USART_LastBit_Disable; //最后一位时钟脉冲(同步模式下)
USART_Init(USART2, &USART_InitStructure); /* Configure USART2 配置 串口发送方式寄存器*/
USART_ClockInit(USART2, &USART_ClockInitS);
//USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //USART中断配置
USART_Cmd(USART2, ENABLE); // Enable USART2
}
bool USART2_TX_Char(char c)
{
bool success =TRUE;
//OS_CPU_SR cpu_sr;
//OS_ENTER_CRITICAL(); // 进入 临界段
if(USART2_TX_Count() >(TxBuffer2_SIZE-1))
{ success =FALSE; }
else
{
TxBuffer2[USART2_TX_HeadIndex++] =c;
if(USART2_TX_HeadIndex >=TxBuffer2_SIZE)
{ USART2_TX_HeadIndex =0; }
USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
}
// OS_EXIT_CRITICAL(); // 退出临界段
return success;
}
s16 USART2_RX_Char(void)
{
u8 ch;
// OS_CPU_SR cpu_sr;
// OS_ENTER_CRITICAL(); // 进入 临界段
if(USART2_RX_Count() ==0)
{
// OS_EXIT_CRITICAL(); // 退出临界段
return -1;
}
ch =RxBuffer2[USART2_RX_TailIndex++];
if (USART2_RX_TailIndex >=RxBuffer2_SIZE)
{ USART2_RX_TailIndex =0;}
// OS_EXIT_CRITICAL(); // 退出临界段
return ch;
}
u16 USART2_TX_Count(void)
{
s16 num;
num =USART2_TX_HeadIndex -USART2_TX_TailIndex;
if (num <0)
{ num +=TxBuffer2_SIZE; }
return num;
}
u16 USART2_RX_Count(void)
{
s16 num;
num =USART2_RX_HeadIndex -USART2_RX_TailIndex;
if (num <0)
{ num +=RxBuffer2_SIZE; }
return num;
}
bool USART2_PutChar(u8 *s,u16 length)
{
u16 i;
bool success =TRUE;
for(i=0; i<length; i++)
{
if(!USART2_TX_Char(*s++))
{
success =FALSE;
break;
}
}
return success;
}
bool USART2_PutString(char *s)
{
bool success =TRUE;
while (*s)
{
if(!USART2_TX_Char(*s++))
{
success =FALSE;
break;
}
}
return success;
}
u16 UART2Read(char *s,u16 count)
{
u16 i=0;
while(USART2_RX_Count() >0 && i <= count)
{
s[i]=RxBuffer2[USART2_RX_TailIndex];
USART2_RX_TailIndex++;
i++;
}
return i;
}
/*******************************************************************************
* Function Name : USART1_IRQHandler
* Description : This function handles USART1 global interrupt request.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
/*---------------interrupt sub---------------------------------*/
void USART2_IRQHandler(void)
{
char ch;
//OS_CPU_SR cpu_sr;
//OS_ENTER_CRITICAL(); // 进入 临界段
//OSIntNesting++;
//OS_EXIT_CRITICAL(); // 退出临界段
if(USART_GetITStatus(USART2, USART_IT_RXNE) !=RESET)
{
ch =USART_ReceiveData(USART2);
if(USART2_RX_Count() <(RxBuffer2_SIZE -1))
{
RxBuffer2[USART2_RX_HeadIndex++] =ch;
if(USART2_RX_HeadIndex >=RxBuffer2_SIZE)
{
USART2_RX_HeadIndex =0;
}
}
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
}
if(USART_GetITStatus(USART2,USART_IT_TXE) !=RESET)
{
if(USART2_TX_Count() >0)
{
USART_SendData(USART2, TxBuffer2[USART2_TX_TailIndex++]);
USART_ClearITPendingBit(USART2, USART_IT_TXE);
if(USART2_TX_TailIndex >=TxBuffer2_SIZE)
{ USART2_TX_TailIndex =0; }
}
else
{ /* Disable the USART1 Transmit interrupt */
USART_ITConfig(USART2, USART_IT_TXE, DISABLE);
}
}
// OSIntExit();
}
串口1:为监听程序的运行
#include "config.h"
#define TxBuffer1_SIZE 200
#define RxBuffer1_SIZE 200
u8 RxBuffer1[RxBuffer1_SIZE];
u8 TxBuffer1[TxBuffer1_SIZE];
u16 USART1_RX_HeadIndex;
u16 USART1_RX_TailIndex;
u16 USART1_TX_HeadIndex;
u16 USART1_TX_TailIndex;
u16 UASRT1_Input_Index;
/******************************************************************************
******************************************************************************/
void USART1_Initialise( void )
{
GPIO_InitTypeDef GPIO_InitStructure;//I/O口初始化结构体
NVIC_InitTypeDef NVIC_InitStructure;//中断初始化结构体
USART_InitTypeDef USART_InitStructure;//串口初始化结构体
USART_ClockInitTypeDef USART_ClockInitS;
// Flush the buffers by setting the indexes to 0
USART1_RX_HeadIndex = 0;
USART1_RX_TailIndex = 0;
USART1_TX_HeadIndex = 0;
USART1_TX_TailIndex = 0;
UASRT1_Input_Index = 0;
// Configure USART1 Tx (PA.09) as alternate function push-pull
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// Configure USART1 Rx (PA.10) as input floating
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// Enable the USART1 Interrupt
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// 串口相关配置
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 位宽
USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位
USART_InitStructure.USART_Parity = USART_Parity_No; //校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//硬件控制流
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //串口模式
USART_ClockInitS.USART_Clock = USART_Clock_Disable; //SCLK时钟使能(同步模式下)
USART_ClockInitS.USART_CPOL = USART_CPOL_Low; //时钟极性(同步模式下)
USART_ClockInitS.USART_CPHA = USART_CPHA_2Edge; //时钟相位(同步模式下)
USART_ClockInitS.USART_LastBit = USART_LastBit_Disable; //最后一位时钟脉冲(同步模式下)
USART_Init(USART1, &USART_InitStructure); /* Configure USART1 配置 串口发送方式寄存器*/
USART_ClockInit(USART1, &USART_ClockInitS);
// USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //接收终端使能
USART_Cmd(USART1, ENABLE); // Enable USART1
}
bool USART1_TX_Char(char c)
{
bool success =TRUE;
//OS_CPU_SR cpu_sr;
//OS_ENTER_CRITICAL(); // 进入 临界段
if(USART1_TX_Count() >(TxBuffer1_SIZE-1))
{ success =FALSE; }
else
{
TxBuffer1[USART1_TX_HeadIndex++] =c;
if(USART1_TX_HeadIndex >=TxBuffer1_SIZE)
{ USART1_TX_HeadIndex =0; }
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
}
//OS_EXIT_CRITICAL(); // 退出临界段
return success;
}
s16 USART1_RX_Char(void)
{
u8 ch;
//OS_CPU_SR cpu_sr;
//OS_ENTER_CRITICAL(); // 进入 临界段
if(USART1_RX_Count() ==0)
{
// OS_EXIT_CRITICAL(); // 退出临界段
return -1;
}
ch =RxBuffer1[USART1_RX_TailIndex++];
if (USART1_RX_TailIndex >=RxBuffer1_SIZE)
{ USART1_RX_TailIndex =0;}
//OS_EXIT_CRITICAL(); // 退出临界段
return ch;
}
u16 USART1_TX_Count(void)
{ s16 num;
//OS_CPU_SR cpu_sr;
//OS_ENTER_CRITICAL(); // 进入 临界段
num =USART1_TX_HeadIndex -USART1_TX_TailIndex;
if (num <0)
{ num +=TxBuffer1_SIZE; }
//OS_EXIT_CRITICAL(); // 退出临界段
return num;
}
u16 USART1_RX_Count(void)
{ s16 num;
//OS_CPU_SR cpu_sr;
//OS_ENTER_CRITICAL(); // 进入 临界段
num =USART1_RX_HeadIndex -USART1_RX_TailIndex;
if (num <0)
{ num +=RxBuffer1_SIZE; }
//OS_EXIT_CRITICAL(); // 退出临界段
return num;
}
bool USART1_PutChar(u8 *s,u16 length)
{
u16 i;
//OS_CPU_SR cpu_sr;
bool success =TRUE;
//OS_ENTER_CRITICAL(); // 进入 临界段
for(i=0; i<length; i++)
{
if(!USART1_TX_Char(*s++))
{
success =FALSE;
break;
}
}
//OS_EXIT_CRITICAL(); // 退出临界段
return success;
}
bool USART1_PutString(char *s)
{
bool success =TRUE;
//OS_CPU_SR cpu_sr;
//OS_ENTER_CRITICAL(); // 进入 临界段
while (*s)
{
if(!USART1_TX_Char(*s++))
{
success =FALSE;
break;
}
}
//OS_EXIT_CRITICAL(); // 退出临界段
return success;
}
bool USART1_PutHex(u8 *s,u16 length)
{
u16 i;
u8 ch;
bool success =TRUE;
for(i=0; i<length; i++)
{
ch =s[i] >>4;
if( ch<=9 )
{ USART1_TX_Char(ch+'0'); }
else
{ USART1_TX_Char(ch+0x37);}
ch =s[i] &0x0f;
if( ch<=9 )
{ USART1_TX_Char(ch+'0'); }
else
{ USART1_TX_Char(ch+0x37);}
}
return success;
}
u16 UART1Read(char *s,u16 count)
{
u16 i=0;
while(USART1_RX_Count() > 0 && i <= count )
{
s[i]=RxBuffer1[USART1_RX_TailIndex];
RxBuffer1[USART1_RX_TailIndex]=0;
USART1_RX_TailIndex++;
if(USART1_RX_TailIndex==RxBuffer1_SIZE) USART1_RX_TailIndex=0;
i++;
}
return i;
}
/*******************************************************************************
* Function Name : USART1_IRQHandler
* Description : This function handles USART1 global interrupt request.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
/*---------------interrupt sub---------------------------------*/
void USART1_IRQHandler(void)
{
char ch;
//OS_CPU_SR cpu_sr;
//OS_ENTER_CRITICAL(); // 进入 临界段
//OSIntNesting++;
//OS_EXIT_CRITICAL(); // 退出临界段
if(USART_GetITStatus(USART1, USART_IT_RXNE) !=RESET)
{
ch =USART_ReceiveData(USART1);
if(USART1_RX_Count() <(RxBuffer1_SIZE -1))
{
RxBuffer1[USART1_RX_HeadIndex++] =ch;
if(USART1_RX_HeadIndex >=RxBuffer1_SIZE)
{
USART1_RX_HeadIndex =0;
}
}
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
if(USART_GetITStatus(USART1,USART_IT_TXE) !=RESET)
{
if(USART1_TX_Count() >0)
{
USART_SendData(USART1, TxBuffer1[USART1_TX_TailIndex++]);
USART_ClearITPendingBit(USART1, USART_IT_TXE);
if(USART1_TX_TailIndex >=TxBuffer1_SIZE)
{ USART1_TX_TailIndex =0; }
}
else
{ /* Disable the USART1 Transmit interrupt */
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
}
}
// OSIntExit();
}
回复
有奖活动 | |
---|---|
【有奖活动】分享技术经验,兑换京东卡 | |
话不多说,快进群! | |
请大声喊出:我要开发板! | |
【有奖活动】EEPW网站征稿正在进行时,欢迎踊跃投稿啦 | |
奖!发布技术笔记,技术评测贴换取您心仪的礼品 | |
打赏了!打赏了!打赏了! |
打赏帖 | |
---|---|
与电子爱好者谈读图二被打赏50分 | |
【FRDM-MCXN947评测】Core1适配运行FreeRtos被打赏50分 | |
【FRDM-MCXN947评测】双核调试被打赏50分 | |
【CPKCORRA8D1B评测】---移植CoreMark被打赏50分 | |
【CPKCORRA8D1B评测】---打开硬件定时器被打赏50分 | |
【FRDM-MCXA156评测】4、CAN loopback模式测试被打赏50分 | |
【CPKcorRA8D1评测】--搭建初始环境被打赏50分 | |
【FRDM-MCXA156评测】3、使用FlexIO模拟UART被打赏50分 | |
【FRDM-MCXA156评测】2、rt-thread MCXA156 BSP制作被打赏50分 | |
【FRDM-MCXN947评测】核间通信MUTEX被打赏50分 |