中级会员
 
- 积分
- 218
- 金钱
- 218
- 注册时间
- 2014-8-20
- 在线时间
- 0 小时
|

楼主 |
发表于 2014-9-2 17:02:16
|
显示全部楼层
回复【8楼】溫柔一刀:
---------------------------------
NVIC配置:
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/*使能串口4中断,0级先占优先级,0级次占优先级*/
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void UART4_IRQHandler(void)
{
if(USART_GetITStatus(UART4, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(UART4, USART_IT_RXNE);
USART_SendData(UART5,USART_ReceiveData(UART4)); //缓存COM4中断接收的数据
}
if(USART_GetITStatus(UART4, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(UART4, USART_IT_TC);
}
}
/*******************************************************************************
* Function Name: UART5_IRQHandler
* Description : This function handles UART5 global interrupt request.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void UART5_IRQHandler(void)
{
if(USART_GetITStatus(UART5, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(UART5, USART_IT_RXNE);
USART_SendData(UART4,USART_ReceiveData(UART5)); //缓存COM4中断接收的数据
}
if(USART_GetITStatus(UART5, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(UART5, USART_IT_TC);
}
}
这是中断配置
基础配置
我觉得基础配置应该没有错误。。
void GpioInitialisation(void)
{
/* 定义GPIO初始化结构体 GPIO_InitStructure*/
GPIO_InitTypeDef GPIO_InitStructure;
/* 设置PA10,最大翻转频率为50MHz*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA , &GPIO_InitStructure);
/* 设置USART3的Tx脚(PB.10)为第二功能推挽输出模式 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB , &GPIO_InitStructure);
/* 设置USART3的Rx脚(PB.11)为浮空输入脚 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB , &GPIO_InitStructure);
/* 设置USART2的Tx脚(PA.2)为第二功能推挽输出模式 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA , &GPIO_InitStructure);
/* 设置USART2的Rx脚(PA.3)为浮空输入脚 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* 设置UART5的Tx脚(PC.12)为第二功能推挽输出模式 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC , &GPIO_InitStructure);
/* 设置UART5的Rx脚(PD.2)为浮空输入脚 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD , &GPIO_InitStructure);
/* 设置UART4的Tx脚(PC.10)为第二功能推挽输出模式 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC , &GPIO_InitStructure);
/* 设置UART4的Rx脚(PC.11)为浮空输入脚 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOC , &GPIO_InitStructure);
/* 设置 GPIOA.0 为推挽输出,最大翻转频率为50MHz*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/**************************************************************************
* 函数名 : SystickInitialisation
* 函数描述 : 设置Systick定时器,重装载时间为20ms
* 输入参数 : 无
* 输出结果 : 无
* 返回值 : 无
**************************************************************************/
void Delay_us(vu32 n)
{
SysTick->LOAD=72*n; //装载计数值,,因为时钟为72M,72次计时1us
SysTick->CTRL=0x00000005; //时钟来源为HCLK(72M),打开定时器
while(!(SysTick->CTRL&0x00010000)); //等待计数到0
SysTick->CTRL=0x00000004; //关闭定时器
}
void Delay_3s(void)
{
vu16 i;
for (i=0;i<15;i++)
Delay_us(200000);
}
void USART_Configuration(void)
{
/* 定义USART初始化结构体 USART_InitStructure */
USART_InitTypeDef USART_InitStructure;
/*
* 波特率为9600bps
* 8位数据长度
* 1个停止位,无校验
* 禁用硬件流控制
* 禁止USART时钟
* 时钟极性低
* 在第2个边沿捕获数据
* 最后一位数据的时钟脉冲不从 SCLK 输出
*/
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;
USART_Init(USART2 , &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;
USART_Init(USART3 , &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;
USART_Init(UART4 , &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;
USART_Init(UART5 , &USART_InitStructure);
/* 使能USART3 */
USART_Cmd(USART3 , ENABLE);
USART_Cmd(USART2 , ENABLE);
USART_Cmd(UART4 , ENABLE);
USART_Cmd(UART5 , ENABLE);
USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);//使能接受中断,在接受移位 寄存器中有数据是产生
USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);
}
void USART_Printf(USART_TypeDef* USARTx,char *pch)
{
/* 这里是判断'\0'是否到了字符串的行尾,这个字符是看不到的,字符串的结束符就是'\0'
* 如果具体还有不懂的请看C语言,这里通过while()循环,逐个字符逐个字符的传输和显示 */
while(*pch != '\0') /* 我们这里用指针表示,*pch就是一个字符,pch就是该字符的地址 */
{
USART_SendData(USARTx,*pch); /* 在这里我们将这个要显示的字符地址传输给另外一个函数 */
/* 当发送完一个字节后,检测一下“TDR(串口的数据传输寄存器)”是否为空了,因为有可能在一些
* 其他某些模式,缓冲寄存器可能会有很多数据,需要一点时间才能清空,其实增加了这句代码之后
* 会让我们的串口驱动更加健壮,在各种情况下都能正常稳定的工作 */
while(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);
/* 以下是我们查看《STM32F103中文手册》获得494页24.6.1节 状态寄存器(USART_SR)章节
这里说的就是我们程序代码里的USART_FLAG_TXE这个标志位,发送完毕后,再进行标志清空,进行下一次传输
TXE:发送数据寄存器空
当TDR寄存器中的数据被硬件转移到移位寄存器的时候,该位被硬件置位。
0:数据还没有被转移到移位寄存器;
1:数据已经被转移到移位寄存器。
注意:单缓冲器传输中使用该位。*/
USART_ClearFlag(USARTx, USART_FLAG_TXE);
pch++; /* 此时pch地址加1,指向下一个字符 */
}
} |
|