这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 活动中心 » 合作大赛 » STM32学习-Usart采用循环队列的中断程序

共1条 1/1 1 跳转至

STM32学习-Usart采用循环队列的中断程序

高工
2015-06-12 14:36:01     打赏

Usart采用循环队列的中断程序:Usart采用循环队列的中断程序.rar

/*******************************************************************************

STM32学习日志(14)----Usart使用环形队列的串口驱动程序

编译环境:        EWARM V5.30
硬件环境:        51hei
STM32 FW:   V3.0.0
作者        :        szlihongtao
时间        :          2016-07-11
说明        :  1. 参考了IAR的应用笔记 AppNote430-03.pdf
2.与PC联机通讯,收发测试均OK!
*******************************************************************************/
#include "stm32f10x.h"
#include "stm32_m.h"
#include "sci.h"
//******************************************************************************
static void delayms(INT16U cnt)
{
INT16U i;

while(cnt--)
for (i=0; i<7333; i++);
}
//******************************************************************************
// 时钟设置初始化
//******************************************************************************
static void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus;
/*
RCC_AdjustHSICalibrationValue 调整内部高速晶振(HSI)校准值
RCC_ITConfig 使能或者失能指定的RCC中断
RCC_ClearFlag 清除RCC的复位标志位
RCC_GetITStatus 检查指定的RCC中断发生与否
RCC_ClearITPendingBit 清除RCC的中断待处理位
*/
/* RCC system reset(for debug purpose) */
// 时钟系统复位
RCC_DeInit();

// 使能外部的8M晶振
// 设置外部高速晶振(HSE)
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);

// 使能或者失能内部高速晶振(HSI)
RCC_HSICmd(DISABLE);

// 等待HSE起振
// 该函数将等待直到HSE就绪,或者在超时的情况下退出
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();

if(HSEStartUpStatus == SUCCESS)
{
/* HCLK = SYSCLK */
// 设置AHB时钟(HCLK)
RCC_HCLKConfig(RCC_SYSCLK_Div1);        // 72 MHz

/* PCLK1 = HCLK/2 */
// 设置低速AHB时钟(PCLK1)
RCC_PCLK1Config(RCC_HCLK_Div2);        // 36 MHz

/* PCLK2 = HCLK */
// 设置高速AHB时钟(PCLK2)
RCC_PCLK2Config(RCC_HCLK_Div1);        // 72 MHz

/* ADCCLK = PCLK2/8 */
// 设置ADC时钟(ADCCLK)
RCC_ADCCLKConfig(RCC_PCLK2_Div8);

// 设置USB时钟(USBCLK)
// USB时钟 = PLL时钟除以1.5
RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);

// 设置外部低速晶振(LSE)
RCC_LSEConfig(RCC_LSE_OFF);

// 使能或者失能内部低速晶振(LSI)
// LSE晶振OFF
RCC_LSICmd(DISABLE);

// 设置RTC时钟(RTCCLK)
// 选择HSE时钟频率除以128作为RTC时钟
RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div128);

// 使能或者失能RTC时钟
// RTC时钟的新状态
RCC_RTCCLKCmd(DISABLE);

/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);

/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

/* PLLCLK = 8MHz * 9 = 72 MHz */
// 设置PLL时钟源及倍频系数
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

/* Enable PLL */
// 使能或者失能PLL
RCC_PLLCmd(ENABLE);

/* Wait till PLL is ready */
// 检查指定的RCC标志位设置与否
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}

/* Select PLL as system clock source */
// 设置系统时钟(SYSCLK)
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

/* Wait till PLL is used as system clock source */
// 返回用作系统时钟的时钟源
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}

// 使能或者失能AHB外设时钟
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1
|RCC_AHBPeriph_DMA2
|RCC_AHBPeriph_SRAM
|RCC_AHBPeriph_FLITF
|RCC_AHBPeriph_CRC
|RCC_AHBPeriph_FSMC
|RCC_AHBPeriph_SDIO,DISABLE);
// 使能或者失能APB1外设时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_ALL,DISABLE);

// 强制或者释放高速APB(APB2)外设复位
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ALL,ENABLE);
// 退出复位状态
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ALL,DISABLE);

// 强制或者释放低速APB(APB1)外设复位
RCC_APB1PeriphResetCmd(RCC_APB1Periph_ALL,ENABLE);
// 退出复位状态
RCC_APB1PeriphResetCmd(RCC_APB1Periph_ALL,DISABLE);

// 强制或者释放后备域复位
RCC_BackupResetCmd(ENABLE);

// 使能或者失能时钟安全系统
RCC_ClockSecuritySystemCmd(DISABLE);
}
//******************************************************************************
// NVIC设置
//******************************************************************************
static void NVIC_Configuration(void)
{
}
//******************************************************************************
// GPIO设置
//******************************************************************************
static void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);

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_Init(GPIOC, &GPIO_InitStructure);
//------------------------------------------------------------------------------
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;// USART1 Rx (PA.10)
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);

/* Configure USART1 Tx (PA.09) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
//******************************************************************************
// LED5亮
//******************************************************************************
void Led_RW_ON(void)
{
GPIO_SetBits(GPIOC,GPIO_Pin_4);
}
//******************************************************************************
// LED5灭
//******************************************************************************
void Led_RW_OFF(void)
{
GPIO_ResetBits(GPIOC,GPIO_Pin_4);
}
//******************************************************************************
// 主程序
//******************************************************************************
int main(void)
{
INT16U i;

RCC_Configuration();
GPIO_Configuration();
NVIC_Configuration();
OpenCom();

for (i=0;i<3;++i)
{
Led_RW_ON();
delayms(200);
Led_RW_OFF();
delayms(200);
}
//------------------------------------------------------------------------------
for (;;)
{
putch_int(i++);
CR();
delayms(10);

GPIOC->ODR ^= GPIO_Pin_5;        // led4 toogle
}
}
//******************************************************************************
#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****/
//******************************************************************************
/*
LED2---------PC7
LED3---------PC6
LED4---------PC5
LED5---------PC4

KEY2---------PD3
KEY3---------PD4
*/

//******************************************************************************
#include "stm32_m.h"
#include "stm32f10x.h"
#include "sci.h"
//******************************************************************************
struct struct_sci str_sci;
//******************************************************************************
void isr_USART1_IRQHandler(void)
{
if (USART_GetITStatus(USART1,USART_IT_TC)!= RESET) // 发送中断
{
if (str_sci.ucTXCharCount)                                                // 如果还有发送的字符数
{
USART_SendData(USART1,str_sci.ucTXBuffer[str_sci.ucTXReadIndex++]);
str_sci.ucTXReadIndex &= TXBUFSIZE-1;                 // adjust index
str_sci.ucTXCharCount--;                                         // char sent, dec count
}
else                                                                                          // 发送缓冲区为空
str_sci.bTXBufferEmpty = BUFFER_EMPTY;                 // 设置空标志

USART_ClearITPendingBit(USART1,USART_IT_TC);
}
//------------------------------------------------------------------------------
if (USART_GetITStatus(USART1,USART_IT_RXNE)!= RESET) // 接收中断
{
str_sci.ucRXBuffer[str_sci.ucRXWriteIndex++] =(INT8U)USART_ReceiveData(USART1);;         // store received byte and
str_sci.ucRXWriteIndex &= RXBUFSIZE-1;                         // reset index
str_sci.ucRXCharCount++;                                                         // received, inc count

USART_ClearITPendingBit(USART1,USART_IT_RXNE);
}
}
//******************************************************************************
// 读取若干字节
//******************************************************************************
INT8U ComRd(INT8U buf[], INT8U maxCnt)
{
INT16U i;

for (i=0;i buf[i]=ComRdByte();

return (SUCCESS);
}
//******************************************************************************
// 读取1字节
//******************************************************************************
INT8U ComRdByte(void)
{
INT8U ch;

//RX_INT_DISABLE;                                         // disable rx interrupt (IE2)
USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);

ch = str_sci.ucRXBuffer[str_sci.ucRXReadIndex++]; // get byte from buffer
str_sci.ucRXReadIndex &= RXBUFSIZE-1;                 // adjust index
str_sci.ucRXCharCount--;                                         // one char read, dec count

USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
//RX_INT_ENABLE;                                                 // done, enable int (IE2)

return (ch);
}
//******************************************************************************
// 发送字符串
//******************************************************************************
void putstring(INT8U *ptr,INT8U length)
{
for(;length;--length)
ComWrtByte(*ptr++);
}
//******************************************************************************
void putch_int(uint int1)
{
INT8U i,arr[5];

for(i=0;i<5;++i)
{
arr[4-i]=(int1%10)+'0';
int1/=10;
}

#if 0
for(i=0;i<4;++i)
{
if (arr[i]=='0')
arr[i]=' ';
else
break;
}
#endif
putstring(arr,5);
}
//******************************************************************************
void CR(void)
{
ComWrtByte('\r');
ComWrtByte('\n');                // 回车换行
}
//******************************************************************************
// 写若干字节
//******************************************************************************
INT8U ComWrt(INT8U *buf, INT8U maxCnt)
{
INT8U i;

for (i=0;i ComWrtByte(buf[i]);

return (SUCCESS);
}
//******************************************************************************
// 写1字节
//******************************************************************************
INT8U ComWrtByte(INT8U byte)
{
INT16U i;

//加此句,为了防止一次性发送较多数据时,发送区溢出
//ucTXCharCount表示还未发送的字符
while(GetOutQLen()>(TXBUFSIZE-16))
for(i=0;i<250;++i); // 延时

str_sci.ucTXBuffer[str_sci.ucTXWriteIndex++]= byte;         // load byte to buffer and inc index
str_sci.ucTXWriteIndex &= TXBUFSIZE-1;                         // adjust index to borders of buffer

//TX_INT_DISABLE;                                                 // disable transmit interrupt (in IE2)
USART_ITConfig(USART1, USART_IT_TC, DISABLE);
++str_sci.ucTXCharCount;                 // new char, inc count
USART_ITConfig(USART1, USART_IT_TC, ENABLE);
//TX_INT_ENABLE;                                                 // enable interrupt (in IE2)

if ((str_sci.bTXBufferEmpty)
&& (str_sci.ucTXCharCount))         // buffer had been empty
{
str_sci.bTXBufferEmpty = !BUFFER_EMPTY;         // 复位发送缓冲区空标志,表明有数据等待发送
i=str_sci.ucTXBuffer[str_sci.ucTXReadIndex++]; // load tx register, inc index

USART_SendData(USART1,i);

str_sci.ucTXReadIndex &= TXBUFSIZE-1;                 // adjust index
str_sci.ucTXCharCount--;                                        // char sent, dec count
}

return (SUCCESS);
}
//******************************************************************************
// 清空输入缓冲区
//******************************************************************************
INT8U FlushInQ(void)
{
INT16U i;

for(i=0;i str_sci.ucRXBuffer[i]=0;

str_sci.ucRXReadIndex=0;
str_sci.ucRXWriteIndex=0;
str_sci.ucRXCharCount=0;
return (SUCCESS);
}
//******************************************************************************
// 清空输出缓冲区
//******************************************************************************
INT8U FlushOutQ(void)
{
INT16U i;

for(i=0;i str_sci.ucTXBuffer[i] =0;

str_sci.ucTXReadIndex=0;
str_sci.ucTXWriteIndex=0;
str_sci.ucTXCharCount=0;

str_sci.bTXBufferEmpty=BUFFER_EMPTY; // 发送缓冲区为空标志,flag for synchronization
return (SUCCESS);
}
//******************************************************************************
// 读取端口状态
//******************************************************************************
INT8U GetComStat(void)
{
return (SUCCESS);
}
//******************************************************************************
// 读取输入缓冲区的程度
//******************************************************************************
INT16U GetInQLen(void)
{
return (str_sci.ucRXCharCount);
}
//******************************************************************************
// 读取输出缓冲区的程度
//******************************************************************************
INT16U GetOutQLen(void)
{
return (str_sci.ucTXCharCount);
}
//******************************************************************************
// 打开/设置串口
//******************************************************************************
INT8U OpenCom(void)
{
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
USART_TypeDef* USARTx;
INT16U NVIC_IRQCh;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

NVIC_IRQCh=USART1_IRQn;
USARTx=USART1;

USART_DeInit(USARTx);

//USART_ClockInitStructure.USART_Clock = USART_Clock_Enable;
//USART_ClockInitStructure.USART_CPOL = USART_CPOL_High;
//USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
//USART_ClockInitStructure.USART_LastBit = USART_LastBit_Enable;
//USART_ClockInit(USARTx, &USART_ClockInitStructure);
// 注意,只支持USART1,2,3,而不支持4,5

USART_InitStructure.USART_BaudRate = 38400;
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(USARTx, &USART_InitStructure);

USART_ClearFlag(USARTx,USART_FLAG_TC|USART_FLAG_RXNE);
USART_ITConfig(USARTx, USART_IT_TC, ENABLE);
USART_ITConfig(USARTx, USART_IT_RXNE, ENABLE);

NVIC_InitStructure.NVIC_IRQChannel = NVIC_IRQCh;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

USART_Cmd(USARTx, ENABLE);
return (SUCCESS);
}
//******************************************************************************
// 打开/设置串口
//******************************************************************************
INT8U CloseCom(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
USART_TypeDef* USARTx;
INT16U NVIC_IRQCh;

NVIC_IRQCh=USART1_IRQn;
USARTx=USART1;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,DISABLE);

USART_DeInit(USARTx);

USART_ITConfig(USARTx, USART_IT_TC, DISABLE);
USART_ITConfig(USARTx, USART_IT_RXNE, DISABLE);

NVIC_InitStructure.NVIC_IRQChannel = NVIC_IRQCh;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
NVIC_Init(&NVIC_InitStructure);

USART_Cmd(USARTx, DISABLE);
FlushInQ();
FlushOutQ();
return (SUCCESS);
}
//******************************************************************************
// 设置串口的超时等待时间
//******************************************************************************
INT8U SetComTime(INT8U portNo, INT8U second_ms)
{
portNo=portNo;
second_ms=second_ms;
return (SUCCESS);
}
//******************************************************************************
//******************************************************************************
//******************************************************************************




共1条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]