/**
****************************************************************************** * @file USART/Interrupt/main.c
* @author MCD Application Team
* @version V3.3.0
* @date 04/16/2010
* @brief Main program body
****************************************************************************** * @copy
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONL Y AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMA TION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SA VE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
*
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "platform_config.h"
#include "stm32_eval.h"
#include "stdio.h"
/** @addtogroup STM32F10x_StdPeriph_Examples
* @{
*/
/** @addtogroup USART_Interrupt
* @{
*/
GPIO_InitTypeDef GPIO_InitStructure;
/* Private typedef -----------------------------------------------------------*/
typedef enum { FAILED = 0, PASSED = !FAILED} TestStatus;
/* Private define ------------------------------------------------------------*/
#define TxBufferSize (countof(TxBuffer) - 1)
#define RxBufferSize TxBufferSize
/*RS485工作模式*/
#define IDLE 0
#define RX_MODE 1
#define TX_MODE 2
/* Private macro -------------------------------------------------------------*/
#define countof(a) (sizeof(a) / sizeof(*(a)))
/* Private variables ---------------------------------------------------------*/
USART_InitTypeDef USART_InitStructure;
uint8_t TxBuffer[] = "https://www.doczj.com/doc/035683595.html, RS485总线收发实验";
uint8_t RxBuffer[RxBufferSize];
__IO uint8_t TxCounter = 0x00;
__IO uint8_t RxCounter = 0x00;
uint8_t NbrOfDataToTransfer = TxBufferSize;
uint8_t NbrOfDataToRead = RxBufferSize;
__IO TestStatus TransferStatus = FAILED;
u8 RS485_Mode = IDLE;
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
void RX_Mode(void);
void TX_Mode(void);
void GPIO_KEY_Config(void);
u8 ReadKeyDown(void);
void Delay_ARMJISHU(__IO uint32_t nCount);
void RS485_MODE_SET(void);
void Led_Ctrl(uint16_t LEDNum, uint8_t LEDOnOff);
/* Private functions ---------------------------------------------------------*/
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
/**
* @brief Main program
* @param None
* @retval None
*/
int main(void)
{
/*!< At this stage the microcontroller clock setting is already configured,
this is done through SystemInit() function which is called from startup
file (startup_stm32f10x_xx.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32f10x.c file
*/
u8 i = 0;
/* System Clocks Configuration */
RCC_Configuration();
/* NVIC configuration */
NVIC_Configuration();
/* Configure the GPIO ports */
GPIO_Configuration();
GPIO_KEY_Config(); //按键使用的GPIO初始化
GPIO_SetBits(GPIO_LED,DS1_PIN|DS2_PIN|DS3_PIN|DS4_PIN);/*关闭所有的LED指示灯*/
/* USART1 and USART2 configured as follow:
- BaudRate = 9600 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
USART_https://www.doczj.com/doc/035683595.html,ART_BaudRate = 115200; /*设置波特率为115200*/
USART_https://www.doczj.com/doc/035683595.html,ART_WordLength = USART_WordLength_8b;/*设置数据位为8*/ USART_https://www.doczj.com/doc/035683595.html,ART_StopBits = USART_StopBits_1; /*设置停止位为1位*/ USART_https://www.doczj.com/doc/035683595.html,ART_Parity = USART_Parity_No; /*无奇偶校验*/ USART_https://www.doczj.com/doc/035683595.html,ART_HardwareFlowControl =
USART_HardwareFlowControl_None;/*无硬件流控*/
USART_https://www.doczj.com/doc/035683595.html,ART_Mode = USART_Mode_Rx | USART_Mode_Tx; /*发送和接收*/
/*配置串口1 */
USART_Init(USART1, &USART_InitStructure);
USART_https://www.doczj.com/doc/035683595.html,ART_BaudRate = 9600; /*设置波特率为115200*/ USART_https://www.doczj.com/doc/035683595.html,ART_WordLength = USART_WordLength_8b;/*设置数据位为8*/ USART_https://www.doczj.com/doc/035683595.html,ART_StopBits = USART_StopBits_1; /*设置停止位为1位*/ USART_https://www.doczj.com/doc/035683595.html,ART_Parity = USART_Parity_No; /*无奇偶校验*/ USART_https://www.doczj.com/doc/035683595.html,ART_HardwareFlowControl = USART_HardwareFlowControl_None;/*无硬件流控*/
USART_https://www.doczj.com/doc/035683595.html,ART_Mode = USART_Mode_Rx | USART_Mode_Tx; /*发送和接收*/
/*配置串口2*/
USART_Init(USART2, &USART_InitStructure);
/* 使能串口1 */
USART_Cmd(USART1, ENABLE);
Led_Ctrl(DS1_PIN,ON); //点亮所有LED灯
Delay_ARMJISHU(6000000);
Led_Ctrl(DS1_PIN,OFF); //熄灭所有LED灯
Delay_ARMJISHU(6000000);
Led_Ctrl(DS1_PIN,ON); //点亮所有LED灯
printf("\n\r---------------------------------------------\n ");
printf("\n\r神舟III号485总线发送实验\n");
printf("\n\r--按USER2按键设置神舟III号为RS485发送端\n");
printf("\n\r--按USER1按键设置神舟III号为RS485接收端\n");
printf("\n\r---------------------------------------------\n ");
while(RS485_Mode == IDLE)
{
RS485_MODE_SET();
}
/* Check the received data with the send ones */
//TransferStatus2 = Buffercmp(TxBuffer1, RxBuffer2, RxBufferSize2);
/* TransferStatus2 = PASSED, if the data transmitted from USART1 and
received by USART2 are the same */
/* TransferStatus2 = FAILED, if the data transmitted from USART1 and
received by USART2 are different */
while(1)
{
if(RS485_Mode == RX_MODE)//RX模式
{
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
while(RxCounter < RxBufferSize)
{
;
}
printf("\n\r接收到的数据:");
printf("[%s]",RxBuffer);
RxCounter = 0;
}
else if(RS485_Mode == TX_MODE) //TX模式
{
/*使能串口2的发送*/
USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
/* Wait until end of transmission from USART1 to USART2 */
while(TxCounter < TxBufferSize)
{
;
}
printf("\n\r正在发送数据:");
for(i = 0 ; i < NbrOfDataToTransfer; i++ )
{
printf("%c",TxBuffer[i]);
if( i == NbrOfDataToTransfer )
{
printf("\n\r");
}
}
TxCounter = 0;
Delay_ARMJISHU(6000000);
}
else
{
RS485_MODE_SET();
}
}
}
void RX_Mode(void)
{
/*设置为接收模式*/
GPIO_ResetBits(GPIOF,GPIO_Pin_11);
}
void TX_Mode(void)
{
/*设置为接收模式*/
GPIO_SetBits(GPIOF,GPIO_Pin_11);
}
/**
* @brief Configures the different system clocks.
* @param None
* @retval None
*/
void RCC_Configuration(void)
{
/*使能串口1和串口2使用的GPIO时钟*/
RCC_APB2PeriphClockCmd(USART1_GPIO_CLK |USART2_GPIO_CLK, ENABLE); /* Enable USART1 Clock */
/*使能串口1时钟*/
RCC_APB2PeriphClockCmd(USART1_CLK, ENABLE);
/*使能串口2时钟*/
RCC_APB1PeriphClockCmd(USART2_CLK, ENABLE);
/*使能LED灯使用的GPIO时钟*/
RCC_APB2PeriphClockCmd(RCC_GPIO_LED, ENABLE);
}
/**
* @brief Configures the different GPIO ports.
* @param None
* @retval None
*/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*串口1 RX管脚配置*/
/* Configure USART1 Rx as input floating */
GPIO_InitStructure.GPIO_Pin = USART1_RxPin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(USART1_GPIO, &GPIO_InitStructure);
/*串口2 RX管脚配置*/
/* Configure USART2 Rx as input floating */
GPIO_InitStructure.GPIO_Pin = USART2_RxPin;
GPIO_Init(USART2_GPIO, &GPIO_InitStructure);
/*串口1 TX管脚配置*/
/* Configure USART1 Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = USART1_TxPin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(USART1_GPIO, &GPIO_InitStructure);
/*串口2 TX管脚配置*/
/* Configure USART2 Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = USART2_TxPin;
GPIO_Init(USART2_GPIO, &GPIO_InitStructure);
/* 配置神舟III号LED灯使用的GPIO管脚模式*/
GPIO_InitStructure.GPIO_Pin = DS1_PIN|DS2_PIN|DS3_PIN|DS4_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIO_LED, &GPIO_InitStructure);
/* 配置神舟III号RS485方向控制管脚*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOF, &GPIO_InitStructure);
}
/**
* @brief Configures the nested vectored interrupt controller.
* @param None
* @retval None
*/
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
/* Enable the USART2 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/**
* @brief Compares two buffers.
* @param pBuffer1, pBuffer2: buffers to be compared.
* @param BufferLength: buffer's length
* @retval PASSED: pBuffer1 identical to pBuffer2
* FAILED: pBuffer1 differs from pBuffer2
*/
TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
{
while(BufferLength--)
{
if(*pBuffer1 != *pBuffer2)
{
return FAILED;
}
pBuffer1++;
pBuffer2++;
}
return PASSED;
}
/****************************************************************************** *
* Function Name : GPIO_KEY_Config.
* Description : Configures the Extension Button.
* Input : None.
* Output : None.
* Return : None.
******************************************************************************* /
void GPIO_KEY_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure KEY1 Button */
RCC_APB2PeriphClockCmd(RCC_KEY1, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_KEY1;
GPIO_Init(GPIO_KEY1_PORT, &GPIO_InitStructure);
/* Configure KEY2 Button */
RCC_APB2PeriphClockCmd(RCC_KEY2, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_KEY2;
GPIO_Init(GPIO_KEY2_PORT, &GPIO_InitStructure);
/* Configure KEY3 Button */
RCC_APB2PeriphClockCmd(RCC_KEY3, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_KEY3;
GPIO_Init(GPIO_KEY3_PORT, &GPIO_InitStructure);
/* Configure KEY4 Button */
RCC_APB2PeriphClockCmd(RCC_KEY4, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_KEY4;
GPIO_Init(GPIO_KEY4_PORT, &GPIO_InitStructure);
}
/****************************************************************************** *
* Function Name : ReadKey
* Description : Reads key from demoboard.
* Input : None
* Output : None
* Return : Return RIGHT, LEFT, SEL, UP, DOWN or NOKEY
******************************************************************************* /
u8 ReadKeyDown(void)
{
/* 1 key is pressed */
if(!GPIO_ReadInputDataBit(GPIO_KEY1_PORT, GPIO_KEY1))
{
return KEY1;
}
/* 2 key is pressed */
if(!GPIO_ReadInputDataBit(GPIO_KEY2_PORT, GPIO_KEY2)) {
return KEY2;
}
/* 3 key is pressed */
if(!GPIO_ReadInputDataBit(GPIO_KEY3_PORT, GPIO_KEY3)) {
return KEY3;
}
/* 4 key is pressed */
if(!GPIO_ReadInputDataBit(GPIO_KEY4_PORT, GPIO_KEY4)) {
return KEY4;
}
/* No key is pressed */
else
{
return NOKEY;
}
}
void Led_Ctrl(uint16_t LEDNum, uint8_t LEDOnOff)
{
if(LEDOnOff == 1)
{
/* Turn On */
GPIO_ResetBits(GPIO_LED, LEDNum);
}
else if(LEDOnOff == 0)
{
/* Turn Off */
GPIO_SetBits(GPIO_LED, LEDNum);
}
}
static void Delay_ARMJISHU(__IO uint32_t nCount)
{
for (; nCount != 0; nCount--);
}
void RS485_MODE_SET(void)
{
/* 使能串口2 */
USART_Cmd(USART2, ENABLE);
if(ReadKeyDown() == KEY1)
{
RS485_Mode = TX_MODE;
printf("\n\rRS485 发送模式设置成功");
TX_Mode();
Led_Ctrl(DS2_PIN,ON);
}
else if(ReadKeyDown() == KEY2)
{
RS485_Mode = RX_MODE;
printf("\n\rRS485 接收模式设置成功");
printf("\n\r等待接收数据");
Led_Ctrl(DS3_PIN,ON);
RX_Mode();
}
else if(ReadKeyDown() == KEY3)
{
RS485_Mode = IDLE;
printf("\n\r 退出发送接收,请重新设置工作模式");
printf("\n\r USER1按键:设置RS485为接收模式");
printf("\n\r USER2按键:设置RS485为发送模式");
}
}
/**
* @brief Retargets the C library printf function to the USART.
* @param None
* @retval None
*/
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(EV AL_COM1, (uint8_t) ch); /*发送一个字符函数*/
/* Loop until the end of transmission */
while (USART_GetFlagStatus(EV AL_COM1, USART_FLAG_TC) == RESET)/*等待发送完成*/
{
}
return ch;
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/**
* @}
*/
/**
* @}
*/
/******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/