本帖最后由 绿眼孤狼 于 2011-1-29 22:02 编辑
/**********************************************************
说明:
本程序能实现USART1和USART2直接通信,发送和接收的
数据正确.
实验条件:
直接把 USART1 的 TXD,RXD 和 USART2 的 RXD,TXD
对应连接即可.
**********************************************************/
#include "stm32f10x_lib.h"
#include "stm32f10x_conf.h"
#include "ili9320.h"
/******************************/
vu8 TxBuffer1 = 129; //USART1发送的数据
vu8 TxBuffer2 = 56; //USART2发送的数据
vu8 RxBuffer1; //USART1接收数据
vu8 RxBuffer2; //USART2接收数据
/********** 函数声明 *********/
void RCC_Configuration(void);
void NVIC_Configuration(void);
void GPIO_Configuration(void);
void USART_Configuration(void);
void Sys_Init(void);
/********** 系统初始化 ******************/
void Sys_Init(void)
{
#ifdef DEBUG
debug();
#endif
//----- 模块初始化配置 -------
RCC_Configuration();
NVIC_Configuration();
GPIO_Configuration();
USART_Configuration();
}
/******* 主函数 **********/
int main(void)
{
Sys_Init();
while(1);
}
/****** 系统时钟配置函数 ********/
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus;
RCC_DeInit(); //RCC system reset(for debug purpose)
RCC_HSEConfig(RCC_HSE_ON); //Enable HSE
HSEStartUpStatus = RCC_WaitForHSEStartUp(); //Wait till HSE is ready
if(HSEStartUpStatus == SUCCESS)
{
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //Enable Prefetch Buffer
FLASH_SetLatency(FLASH_Latency_2); //Set 2 Latency cycles
RCC_HCLKConfig(RCC_SYSCLK_Div1); //AHB clock = SYSCLK
RCC_PCLK2Config(RCC_HCLK_Div1); //APB2 clock = HCLK
RCC_PCLK1Config(RCC_HCLK_Div2); //APB1 clock = HCLK/2
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); //PLLCLK = 8MHz * 9 = 72 MHz
RCC_PLLCmd(ENABLE); //Enable PLL
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); //Wait till PLL is ready
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //Select PLL as system clock source
while(RCC_GetSYSCLKSource() != 0x08); //Wait till PLL is used as system clock source
//-----------------------------------------------------
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1
| RCC_APB2Periph_AFIO, ENABLE); //GPIOA and USART1 clock ENABLE
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); //USART2 clock ENABLE
}
}
/****** 中断配置函数 ********/
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
//--- 在内存中进行调试 -----
#ifdef VECT_TAB_RAM
NIVC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
//--- 在FLASH中进行调试 ----
#else
NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0);
#endif
//---- 配置USART1 和 USART2中断,并使能 -----
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//------------------------------------
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStructure);
}
/********** 通用I/O配置函数 ***************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure USART1 and USART2 Rx as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//------------------
/* Configure USART1 and USART2 Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/*****************************************************
函数名称:USART_Configuration(void)
功能说明:USART通信模块配置,配置USART工作模式;
输入参数:无
返回值 :无
*****************************************************/
void USART_Configuration(void)
{
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;
/* Configure USART1 */
USART_Init(USART1, &USART_InitStructure);
/* Configure USART2 */
USART_Init(USART2, &USART_InitStructure);
/* Enable USART1 Receive and Transmit interrupts */
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
/* Enable USART2 Receive and Transmit interrupts */
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
/* Enable the USART1 */
USART_Cmd(USART1, ENABLE);
/* Enable the USART2 */
USART_Cmd(USART2, ENABLE);
}
/*************************************************/
#ifdef DEBUG
void assert_failed(u8 *file, u32 line)
{
while(1)
{}
}
#endif
/***************************************************/
/***************************************************/
#include "stm32f10x_it.h"
/***********************************/
extern vu8 TxBuffer1;
extern vu8 TxBuffer2;
extern vu8 RxBuffer1;
extern vu8 RxBuffer2;
extern vu16 USART_IDx;
/*********** USART1 中断服务程序 *************/
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
/* Read one byte from the receive data register */
RxBuffer1 = USART_ReceiveData(USART1);
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
}
if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
{
/* Write one byte to the transmit data register */
USART_SendData(USART1, TxBuffer1);
USART_ClearITPendingBit(USART1, USART_IT_TC);
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
}
}
/*********** USART2 中断服务程序 *************/
void USART2_IRQHandler(void)
{
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
/* Read one byte from the receive data register */
RxBuffer2 = USART_ReceiveData(USART2);
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
}
if(USART_GetITStatus(USART2, USART_IT_TXE) != RESET)
{
/* Write one byte to the transmit data register */
USART_SendData(USART2, TxBuffer2);
USART_ClearITPendingBit(USART2, USART_IT_TC);
USART_ITConfig(USART2, USART_IT_TXE, DISABLE);
}
} |
|