中级会员
 
- 积分
- 234
- 金钱
- 234
- 注册时间
- 2017-7-15
- 在线时间
- 16 小时
|
main程式如下所示:#include <stm32f10x.h>
#include <string.h>
#include <stdio.h>
#define USARTmy USART2
#define D2_ON GPIO_ResetBits(GPIOC,GPIO_Pin_8)
#define D2_OFF GPIO_SetBits(GPIOC,GPIO_Pin_8)
#define Max 100
u8 RxBuffer[Max];
u8 TxBuffer[Max];
u8 RxCount=0;
u8 TxCount=0;
typedef enum {ERR=0,OK=!ERR}TXRXstat;
void GPIO_Led_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_SetBits(GPIOC,GPIO_Pin_8);
}
void GPIO_USARTX_Config(USART_TypeDef* USARTx)
{
GPIO_InitTypeDef GPIO_InitStructure;
if(USARTx==USART1)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
if(USARTx==USART2)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
if(USARTx==USART3)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
}
void USART_Configuration(USART_TypeDef* USARTx)
{
USART_InitTypeDef USART_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_Init(USARTx,&USART_InitStructure);
USART_Cmd(USARTx,ENABLE);
}
void delay_nms(u16 time)
{
u16 i=0;
while(time--)
{
i=12000;
while(i--);
}
}
TXRXstat Send1Byte(USART_TypeDef*USARTx,u8 dat)
{
vu32 cnt=0;
USART_SendData(USARTx,dat);
while(USART_GetFlagStatus(USARTx,USART_FLAG_TXE)==RESET)
{
cnt++;
if(cnt>100000)
return ERR;
}
return OK;
}
u8 ReceivelByte(USART_TypeDef* USARTx)
{
while(USART_GetFlagStatus(USARTx,USART_FLAG_RXNE)==RESET)
{}
return (USART_ReceiveData(USARTx));
}
u8 ReceiveOk(USART_TypeDef* USARTx)
{
vu32 cnt=0;
while(1)
{
RxBuffer[RxCount++]=Receive1Byte(USARTx);
if(strstr((char *)RxBuffer,"ON")!=NULL)
{
RxCount=0;
return 1;
}
else
if(strstr((char*)RxBuffer,"OFF")!=NULL)
{
RxCount=0;
return 2;
}
else
if(RxCount>3)
RxCount=0;
cnt++;
if(cnt>100000)
return 0;
}
}
void SendString(USART_TypeDef*USARTx,u8*Message)
{
while(*Message!='\0')
Send1Byte(USARTx,*Message++);
}
void EmptyRxBuffer(u8 len)
{
u8 i;
for(i=0;i<len;i++)
RxBuffer[i]=0;
}
int main(void)
{
// u8 kcnt=0;
SystemInit();
GPIO_Led_Config();
GPIO_USARTX_Config(USARTmy);
USART_Configuration(USARTmy);
while(1)
{
SendString(USARTmy,(u8 *)("Welcome to mycontroller V1.0!\n"));
switch(ReceiveOk(USARTmy))
{
case 1:
if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_8)==RESET)
SendString(USARTmy,(u8*)("D2 in STM32 has been on!\n"));
else
{
D2_ON;
SendString(USARTmy,(u8*)("D2 in STM32 has been turn on!\n"));
}
break;
case 2:
if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_8)!=RESET)
SendString(USARTmy,(u8*)("D2 in STM32 has been off!\n"));
else
{
D2_OFF;
SendString(USARTmy,(u8*)("D2 in STM32 has been turn off\n"));
}
break;
case 0:
SendString(USARTmy,(u8*)("Command is error!\n"));
break;
}
EmptyRxBuffer(Max);
}
}
报错代码如下:
..\Output\Template.axf: Error: L6218E: Undefined symbol Receive1Byte (referred from main.o).
..\Output\Template.axf: Error: L6218E: Undefined symbol cnt1 (referred from stm32f10x_it.o).
|
|