⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 STM32 三个串口同时使用(232/232/485)
💻 C
字号:
/****************************************************************************
* Copyright (C), 2010 安富莱电子 www.armfly.com
*
* 【本例程在安富莱STM32F103ZE-EK开发板上调试通过             】
* 【QQ: 1295744630, 旺旺:armfly, Email: armfly@qq.com       】
*
* 文件名: main.c
* 内容简述: 3串口演示例程。
*	串口1和串口2是3线式RS232接口
*	串口3是RS485接口。
*	使用中断方式操作串口,实现3个UART FIFO,相互不干扰。
*	串口1接收PC机发送的字符串(回车换行结束)后将该字符串发送到串口2和串口3。
*
* 文件历史:
* 版本号  日期       作者    说明
* v0.1    2010-09-05 armfly  首版
*
*/

#include "stm32f10x.h"
#include <stdio.h>
#include "usart_printf.h"
#include "uart_api.h"

#define EXAMPLE_NAME	"UART FIFO Demo(2 RS232 & 1 RS485)"
#define EXAMPLE_DATE	"2010-09-05"

static void GPIO_Configuration(void);

/*******************************************************************************
	函数名:main
	输  入:
	输  出:
	功能说明:用户程序入口
*/
int main(void)
{
	uint8_t ucaRxBuf[1024];
	uint16_t usRxCount;
	uint8_t ucTemp;
	
	/*
		这个函数是ST库中的函数,函数实体在
		Libraries\CMSIS\Core\CM3\system_stm32f10x.c

		配置内部Flash接口,初始化PLL,配置系统频率
		系统时钟缺省配置为72MHz,你如果需要更改,则需要去修改相关的头文件中的宏定义
	 */
	SystemInit();

	/* 配置按键GPIO和LED GPIO */
	GPIO_Configuration();

	/* 初始化所有串口设备, 在uart_api.c文件 */
	comInit();

	/* 通过串口输出例程名和更新日期 */
	PrintfLogo(EXAMPLE_NAME, EXAMPLE_DATE);
	
	/* 简单的通信协议,遇到回车换行符认为1个命令帧 */
	usRxCount = 0;
	while(1)
	{		
		/* 
			接收COM1口的数据,分析并处理 
			可以将此段代码封装为一个函数,在主程序其它流程调用
		*/
		if (comGetChar(COM1, &ucTemp))
		{
			if (usRxCount < sizeof(ucaRxBuf))
			{
				ucaRxBuf[usRxCount++] = ucTemp;
			}
			else
			{
				usRxCount = 0;
			}
			
			/* 遇到换行字符,认为接收到一个命令 */
			if (ucTemp == 0x0A)	/* 换行字符 */
			{		
				/* 在接收到的字符串加1个前缀,以示区别 */
				comSendBuf(COM2, (uint8_t *)"COM2 ", 5);
				comSendBuf(COM2, ucaRxBuf, usRxCount);
				
				/* 在接收到的字符串加1个前缀,以示区别 */
				comSendBuf(COM3, (uint8_t *)"COM3 ", 5);
				comSendBuf(COM3, ucaRxBuf, usRxCount);

				usRxCount = 0;
			}
		}
		
		/* 用户可以在这里添加其他的处理代码,比如检测按键,处理显示等 */
	}
}

/*******************************************************************************
	函数名:GPIO_Configuration
	输  入:
	输  出:
	功能说明:配置7个按键为输入口线,4个LED为输出口线

	按键口线分配:
	USER键     : PG8  (低电平表示按下)
	TAMPEER键  : PC13 (低电平表示按下)
	WKUP键     : PA0  (!!!高电平表示按下)
	摇杆UP键   : PG15 (低电平表示按下)
	摇杆DOWN键 : PD3  (低电平表示按下)
	摇杆LEFT键 : PG14 (低电平表示按下)
	摇杆RIGHT键: PG13 (低电平表示按下)
	摇杆SELECT键: PG7 (低电平表示按下)

	LED口线分配:
	LED1 : PF6  (输出0点亮)
	LED2 : PF7  (输出0点亮)
	LED3 : PF8  (输出0点亮)
	LED4 : PF9  (输出0点亮)

*/
static void GPIO_Configuration(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;

	/* 第1步:打开GPIOA GPIOC GPIOD GPIOF GPIOG的时钟
	   注意:这个地方可以一次性全打开
	*/
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC
			| RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOG,
				ENABLE);

	/* 第2步:配置所有的按键GPIO为浮动输入模式(实际上CPUf复位后就是输入状态) */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);	/* PA0 */

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
	GPIO_Init(GPIOC, &GPIO_InitStructure);	/* PC13 */

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
	GPIO_Init(GPIOD, &GPIO_InitStructure);	/* PD3 */

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_13
					  | GPIO_Pin_14 | GPIO_Pin_15;
	GPIO_Init(GPIOG, &GPIO_InitStructure);	/* PG7,8,13,14,15 */

	/* 第3步:配置所有的LED指示灯GPIO为推挽输出模式 */
	/* 由于将GPIO设置为输出时,GPIO输出寄存器的值缺省是0,因此会驱动LED点亮
		这是我不希望的,因此在改变GPIO为输出前,先修改输出寄存器的值为1 */
	GPIO_SetBits(GPIOF,  GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9);
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOF, &GPIO_InitStructure);

	/* 使能RS485芯片的收发控制IO */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOF, &GPIO_InitStructure);
	GPIO_ResetBits(GPIOF,  GPIO_Pin_10);	/* 禁止发送 */
	GPIO_SetBits(GPIOF,  GPIO_Pin_11);		/* 禁止接收 */
}

#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 2009 STMicroelectronics *****END OF FILE****/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -