?? main.c
字號:
/****************************************************************************
* Copyright (C), 2010 安富萊電子 www.armfly.com
*
* 【本例程在安富萊STM32F103ZE-EK開發(fā)板上調(diào)試通過 】
* 【QQ: 1295744630, 旺旺:armfly, Email: armfly@qq.com 】
*
* 文件名: main.c
* 內(nèi)容簡述: 3串口演示例程。
* 串口1和串口2是3線式RS232接口
* 串口3是RS485接口。
* 使用中斷方式操作串口,實現(xiàn)3個UART FIFO,相互不干擾。
* 串口1接收PC機(jī)發(fā)送的字符串(回車換行結(jié)束)后將該字符串發(fā)送到串口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);
/*******************************************************************************
函數(shù)名:main
輸 入:
輸 出:
功能說明:用戶程序入口
*/
int main(void)
{
uint8_t ucaRxBuf[1024];
uint16_t usRxCount;
uint8_t ucTemp;
/*
這個函數(shù)是ST庫中的函數(shù),函數(shù)實體在
Libraries\CMSIS\Core\CM3\system_stm32f10x.c
配置內(nèi)部Flash接口,初始化PLL,配置系統(tǒng)頻率
系統(tǒng)時鐘缺省配置為72MHz,你如果需要更改,則需要去修改相關(guān)的頭文件中的宏定義
*/
SystemInit();
/* 配置按鍵GPIO和LED GPIO */
GPIO_Configuration();
/* 初始化所有串口設(shè)備, 在uart_api.c文件 */
comInit();
/* 通過串口輸出例程名和更新日期 */
PrintfLogo(EXAMPLE_NAME, EXAMPLE_DATE);
/* 簡單的通信協(xié)議,遇到回車換行符認(rèn)為1個命令幀 */
usRxCount = 0;
while(1)
{
/*
接收COM1口的數(shù)據(jù),分析并處理
可以將此段代碼封裝為一個函數(shù),在主程序其它流程調(diào)用
*/
if (comGetChar(COM1, &ucTemp))
{
if (usRxCount < sizeof(ucaRxBuf))
{
ucaRxBuf[usRxCount++] = ucTemp;
}
else
{
usRxCount = 0;
}
/* 遇到換行字符,認(rèn)為接收到一個命令 */
if (ucTemp == 0x0A) /* 換行字符 */
{
/* 在接收到的字符串加1個前綴,以示區(qū)別 */
comSendBuf(COM2, (uint8_t *)"COM2 ", 5);
comSendBuf(COM2, ucaRxBuf, usRxCount);
/* 在接收到的字符串加1個前綴,以示區(qū)別 */
comSendBuf(COM3, (uint8_t *)"COM3 ", 5);
comSendBuf(COM3, ucaRxBuf, usRxCount);
usRxCount = 0;
}
}
/* 用戶可以在這里添加其他的處理代碼,比如檢測按鍵,處理顯示等 */
}
}
/*******************************************************************************
函數(shù)名: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復(fù)位后就是輸入狀態(tài)) */
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設(shè)置為輸出時,GPIO輸出寄存器的值缺省是0,因此會驅(qū)動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芯片的收發(fā)控制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); /* 禁止發(fā)送 */
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****/
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -