/ *********************************************** /
# include“ stdio.h”
#include“ sting.h”
#include“ stm32f10x.h”
#define LED_ON ResetInputDataBit(GPIOC、GPIO_Pin_8)
#define LED_OFF SetInputDataBit(GPIOC、GPIO_Pin_8)
#define USARTMY USART2
#define MaxLength 10
u8 RXBuffer [Max ];
u8 TXBuffer [Max];
u8 RXCount = 0;
u8 TXCount = 0;
void GPIO_Config(void)
{ GPIO_InitTypeDef GPIO_InitStrue; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC、ENABLE); // GPIOBクロックを有効にします(LEDはBP5ピンにあります)。 (タイマー3チャネル2はBP5ピンに再マッピングする必要があります)GPIO_InitStrue.GPIO_Pin = GPIO_Pin_8; GPIO_InitStrue.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStrue.GPIO_Speed = GPIO_Speed_50MHz; //最大出力速度を設定
GPIO_Init(GPIOB、&GPIO_InitStrue); // GPIOポート初期化設定
}
void Delay_ms(u16時間)
{ u16 i = 0; while(time –){ i = 12000; while(i–); } } void USARTx_GPIO_Configuration(USART_TypeDef * USARTx){ if(USARTx == USART1){ GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA、ENABLE); // USART1_Txを代替プッシュプルとして構成します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 as input floating
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
if (USARTx == USART2)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART2 | RCC_APB2Periph_GPIOA,ENABLE);
// Configure USART1_Tx as alternate push-pull
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// Configure USART1_Rx as input floating
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
if (USARTx == USART3)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART3 | RCC_APB2Periph_GPIOB,ENABLE);
// Configure USART1_Tx as alternate push-pull
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// Configure USART1_Rx as input floating
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
if
}
void NVIC_Configuration(USART_TypeDef * USARTx)
{ NVIC_InitTypeDef * NVIC_InitStructure;
if (USARTx == USART1)
{
NVIC_InitStructure.NVIC_IRQChannl = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannlPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannlSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannlCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//接收中断使能
}
if (USARTx == USART2)
{
NVIC_InitStructure.NVIC_IRQChannl = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannlPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannlSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannlCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);//接收中断使能
}
if (USARTx == USART3)
{
NVIC_InitStructure.NVIC_IRQChannl = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannlPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannlSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannlCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);//接收中断使能
}
}
//次に、シリアルポートパラメータを設定します
void USARTx_Configuration(USART_TypeDef * USARTx)
{
USART_InitTypeDef USART_InitStructure;
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_Init(USARTx、&USART_InitStructure);
USART_Cmd(USARTx、ENABLE);
}
//キャラクターを送信します
void SendChar(USART_TypeDef * USARTx、u8 data)
{
USART_SendData(USARTx, (u8) data);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
{ } } //文字列を送信する
void SendString(u8 * buf、u8 len)
{ for(u8 i = 0; i <len; i ++){ SendChar(USARTMY、buf ++); } } //文字列の受信u8ReceiveByte(USART_TypeDef * USARTx){ while(USART_GetFlagStatus( USARTx、USART_FLAG_RXNE)== RESET); return(USART_ReceiveData(USARTx)); } //受信フラグに戻る// 1 = ON、2 = OFF、other =無効// USARTx x = 1,2,3 u8 ReceiveFlag( SART_TypeDef * USARTx){ while(1){ RXBuffer [RXCount ++] = ReceiveByte(USARTx); if(strstr((char )RXBuffer、“ ON”)!= NULL)// str1に部分文字列str2が含まれているかどうかを判別{ RXCount = 0; 1を返します。
}
else if(strstr
((char )RXBuffer、“ OFF”)!= NULL){ RXCount = 0; 2を返します。} else { if(RXCount> 3)RXCount = 0; 0を返します。} } void EmptyRXBuffer(u8 Length){ u8 i; for(i = 0; i <Length; i ++)RXBuffer [i] = 0; } void EmptyTXBuffer(u8 Length){ u8 i; for(i = 0; i <Length; i ++)TXBuffer [i] = 0; } / ************************************************ ***** /
int main(void)
{ u8長さ; GPIO_Config();
USARTx_Configuration(USARTMY);
USARTx_GPIO_Configuration(USARTMY);
NVIC_Configuration(USARTMY);
while(1)
{ TXBuffer [Max] =“ ON”; // OFF長さ= strlen(TXBuffer)-1; SendString(TXBuffer、Length); EmptyTXBuffer(Length); 長さ= 0; スイッチ(RxOK){ ケース1:if(GPIO_ReadInputDataBit(GPIOC、GPIO_Pin_8)== RESET){ RxOK = 0; TXBuffer [Max] =(u8)“ ON”; 長さ= strlen(TXBuffer)-1; SendString(TXBuffer、Length); } else LED_ON; break; ケース2:if(GPIO_ReadInputDataBit(GPIOC、GPIO_Pin_8)== SET){
RxOK = 0;
TXBuffer [Max] =(u8)“ OFF”;
長さ= strlen(TXBuffer)-1;
SendString(TXBuffer、Length);
}
else
LED_OFF; break;
ケース0:ブレーク;
}
EmptyRXBuffer(Max);
}
/ stm32f10x.h /
extern u8 RxBuffer [];
extern vu8 RxCount;
extern vu8 RxHeader;
extern vu8 RxOK;
extern vu8 RxLength;
void USART1_IRQHandler(void);
void USART2_IRQHandler(void);
void USART3_IRQHandler(void);
/ stm32f10x.c /
void USART1_IRQHandler(void)
{ if(USART1_GetITStatus(USART1、USART_IT_RXNE)!= RESET){ USART_ClearITPendingBit(USART1、USART_IT_RXNE);
RxBuffer[RxCount++] = USART_ReceiveData(USART1);
RxCount & = 0XFF;
}
if(strstr((char *)RXBuffer , "1") != NULL)
{
RxCount = 0;
RxOK = 1;
}
else if(strstr((char *)RXBuffer , "2") != NULL)
{
RxCount = 0;
RxOK = 2;
}
else
{
RxCount = 0;
RxOK = 0;
}
if (USART_GetFlagStatus (USART1,USART_FLAG_ORE) == SET) //空闲中断
{
USART_ClearFlag (USART1,USART_FLAG_ORE);//先读SD
USART_ReceiveData(USART1); //在读DR
}
}
void USART2_IRQHandler(void)
{ if(USART3_GetITStatus(USART2、USART_IT_RXNE)!= RESET){ USART_ClearITPendingBit(USART2、USART_IT_RXNE);
RxBuffer[RxCount++] = USART_ReceiveData(USART2);
RxCount & = 0XFF;
}
if(strstr((char *)RXBuffer , "1") != NULL)
{
RxCount = 0;
RxOK = 1;
}
else if(strstr((char *)RXBuffer , "2") != NULL)
{
RxCount = 0;
RxOK = 2;
}
else
{
RxCount = 0;
RxOK = 0;
}
if (USART_GetFlagStatus (USART2,USART_FLAG_ORE) == SET) //空闲中断
{
USART_ClearFlag (USART2,USART_FLAG_ORE);//先读SD
USART_ReceiveData(USART2); //在读DR
}
}
void USART3_IRQHandler(void)
{ if(USART3_GetITStatus(USART3、USART_IT_RXNE)!= RESET){ USART_ClearITPendingBit(USART3、USART_IT_RXNE);
RxBuffer[RxCount++] = USART_ReceiveData(USART3);
RxCount & = 0XFF;
}
if(strstr((char *)RXBuffer , "1") != NULL)
{
RxCount = 0;
RxOK = 1;
}
else if(strstr((char *)RXBuffer , "2") != NULL)
{
RxCount = 0;
RxOK = 2;
}
else
{
RxCount = 0;
RxOK = 0;
}
if (USART_GetFlagStatus (USART3,USART_FLAG_ORE) == SET) //空闲中断
{
USART_ClearFlag (USART3,USART_FLAG_ORE);//先读SD
USART_ReceiveData(USART3); //在读DR
}
}