?? main.c
字號:
/********************************************************************
單片研習
http://group.ednchina.com/2415
*********************************************************************
例程名稱:USART1串口通信程序(中斷方式)
作 者:阿勇(jshmlly)
實驗硬件:DP-miniSTM32
硬件連接:使用通用232串行線(3線制,使用DB9接口的2、3和5號端腳)將系統
板的232口(J2)與PC機的232口相連。
功能描述:打開串口調試軟件(網上很多可以自己下一個),系統板下載完程序后,
按電源開關重起,使用串口調試軟件向學習板發送一個數據,看調試
軟件會不會接收到同一數據,如果是表明串口中斷接收正常。
********************************************************************/
#include <stm32f10x_lib.h>
/**********************************************************************
* 名 稱:Delay()
* 功 能:延時
* 入口參數:
* 出口參數:
-----------------------------------------------------------------------
* 說明:
***********************************************************************/
void Delay(vu16 cnt) {
uint16 i,j;
for (i=0;i<cnt;i++)
{ for (j=0;j<1000;j++)
{ }
}
}
/**********************************************************************
* 名 稱:RCC_Configuration()
* 功 能:時鐘配置
* 入口參數:
* 出口參數:
-----------------------------------------------------------------------
* 說明:
***********************************************************************/
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus;
//使能外部晶振
RCC_HSEConfig(RCC_HSE_ON);
//等待外部晶振穩定
HSEStartUpStatus = RCC_WaitForHSEStartUp();
//如果外部晶振啟動成功,則進行下一步操作
if(HSEStartUpStatus==SUCCESS)
{
//設置HCLK(AHB時鐘)=SYSCLK
RCC_HCLKConfig(RCC_SYSCLK_Div1);
//PCLK1(APB1) = HCLK/2
RCC_PCLK1Config(RCC_HCLK_Div2);
//PCLK2(APB2) = HCLK
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_4);
//啟動PLL
RCC_PLLCmd(ENABLE);
//等待PLL穩定
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
//系統時鐘SYSCLK來自PLL輸出
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
//切換時鐘后等待系統時鐘穩定
while(RCC_GetSYSCLKSource()!=0x08);
}
/* RCC system reset(for debug purpose) */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //配置GPIOA時鐘
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE); //配置USART1時鐘
}
/**********************************************************************
* 名 稱:GPIO_Configuration()
* 功 能:IO配置
* 入口參數:
* 出口參數:
-----------------------------------------------------------------------
* 說明:為串口1配置引腳
***********************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //管腳9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復用推挽輸出
GPIO_Init(GPIOA, &GPIO_InitStructure); //TX初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //管腳10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空輸入
GPIO_Init(GPIOA, &GPIO_InitStructure); //RX初始化
}
/**********************************************************************
* 名 稱:USART_Configuration()
* 功 能:串口配置
* 入口參數:
* 出口參數:
-----------------------------------------------------------------------
* 說明:
***********************************************************************/
void USART_Configuration(void) //串口初始化函數
{
//串口參數初始化
USART_InitTypeDef USART_InitStructure; //串口設置恢復默認參數
//初始化參數設置
USART_InitStructure.USART_BaudRate = 9600; //波特率9600
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字長8位
USART_InitStructure.USART_StopBits = USART_StopBits_1; //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;//打開Rx接收和Tx發送功能
USART_InitStructure.USART_Clock = USART_Clock_Disable;
USART_InitStructure.USART_CPOL = USART_CPOL_Low;
USART_InitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_InitStructure.USART_LastBit = USART_LastBit_Disable;
USART_Init(USART1, &USART_InitStructure); //初始化
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //接收中斷允許
USART_Cmd(USART1, ENABLE); //啟動串口
}
/**********************************************************************
* 名 稱:NVIC_Configuration()
* 功 能:中斷配置
* 入口參數:
* 出口參數:
-----------------------------------------------------------------------
* 說明:
***********************************************************************/
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel; //通道設置為串口1中斷
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //中斷占先等級0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //中斷響應優先級0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //打開中斷
NVIC_Init(&NVIC_InitStructure); //初始化
}
/**********************************************************************
* 名 稱:main()
* 功 能:主函數
* 入口參數:
* 出口參數:
-----------------------------------------------------------------------
* 說明:
***********************************************************************/
int main (void)
{
RCC_Configuration(); //時鐘配置
GPIO_Configuration(); //IO配置
NVIC_Configuration(); //中斷配置
USART_Configuration(); //USART1配置
while(1)
{
}
}
/**********************************************************************
* 名 稱:USART1_IRQHandler()
* 功 能:USART1中斷
* 入口參數:
* 出口參數:
* 全局變量:
-----------------------------------------------------------------------
* 說明:
***********************************************************************/
void USART1_IRQHandler(void)
{
uint8 dat;
/*中斷接收*/
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)//判斷是不是接收中斷
{
dat = USART_ReceiveData(USART1);
USART_SendData(USART1,dat); //將接收到的數據發送到上位機
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET){} ; // 等待數據移送到移位寄存器
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -