新聞中心

STM32 定時(shí)器2+串口

作者: 時(shí)間:2016-11-09 來(lái)源:網(wǎng)絡(luò) 收藏
定時(shí)器2+串口發(fā)送
USART.c

#include "STM32Lib\stm32f10x.h"
/**********************************************
**串口配置函數(shù),這里使能了兩個(gè)串口,其中串口2使用了中斷接收模式
**
**********************************************/
u8 Uart1_Get_Flag; //串口1接收到數(shù)據(jù)標(biāo)志
u8 Uart1_Get_Data; //串口1接收的數(shù)據(jù)

本文引用地址:http://m.butianyuan.cn/article/201611/318030.htm

void USART_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;

//使能串口1,PA,AFIO總線
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |
RCC_APB2Periph_AFIO |
RCC_APB2Periph_USART1 ,
ENABLE);

/* A9 USART1_Tx */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //推挽輸出-TX
GPIO_Init(GPIOA, &GPIO_InitStructure);

/* A10 USART1_Rx */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空輸入-RX
GPIO_Init(GPIOA, &GPIO_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_ClockInitStructure.USART_Clock = USART_Clock_Disable;
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;

USART_ClockInit(USART1, &USART_ClockInitStructure);
USART_Init(USART1, &USART_InitStructure);
/* Enable the USARTx */
USART_Cmd(USART1, ENABLE);
//串口1使用接收中斷
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);

//使能串口2時(shí)鐘
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);

// A2 做T2X
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);

// A3 做R2X
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_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_ClockInitStructure.USART_Clock = USART_Clock_Disable;
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;

USART_ClockInit(USART2, &USART_ClockInitStructure);
USART_Init(USART2, &USART_InitStructure);

USART_Cmd(USART2, ENABLE);
//串口2使用接收中斷
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
}

void USART1_Putc(unsigned char c)
{
USART_SendData(USART1, c);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET );
}

void USART1_Puts(char * str)
{
while(*str)
{
USART_SendData(USART1, *str++);
/* Loop until the end of transmission */
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
}

void USART2_Putc(unsigned char c)
{
USART_SendData(USART2, c);
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET );
}

void USART2_Puts(char * str)
{
while(*str)
{
USART_SendData(USART2, *str++);
/* Loop until the end of transmission */
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
}

}

hal.h

#ifndef HAL_H
#define HAL_H

//硬件初始化
extern void ChipHalInit(void);
extern void ChipOutHalInit(void);

//輸出宏定義
//清零
#define LED1_OFF GPIO_ResetBits(GPIOA, GPIO_Pin_8)
//置一
#define LED1_ON GPIO_SetBits(GPIOA, GPIO_Pin_8)

#define LED2_OFF GPIO_ResetBits(GPIOA, GPIO_Pin_7)
#define LED2_ON GPIO_SetBits(GPIOA, GPIO_Pin_7)

#define LED3_OFF GPIO_ResetBits(GPIOC, GPIO_Pin_7)
#define LED3_ON GPIO_SetBits(GPIOC, GPIO_Pin_7)

#define LED4_OFF GPIO_ResetBits(GPIOC, GPIO_Pin_5)
#define LED4_ON GPIO_SetBits(GPIOC, GPIO_Pin_5)

#define LED5_OFF GPIO_ResetBits(GPIOB, GPIO_Pin_9)
#define LED5_ON GPIO_SetBits(GPIOB, GPIO_Pin_9)

#define LED6_OFF GPIO_ResetBits(GPIOB, GPIO_Pin_8)
#define LED6_ON GPIO_SetBits(GPIOB, GPIO_Pin_8)

#define LED7_OFF GPIO_ResetBits(GPIOB, GPIO_Pin_5)
#define LED7_ON GPIO_SetBits(GPIOB, GPIO_Pin_5)

#define LED8_OFF GPIO_ResetBits(GPIOB, GPIO_Pin_0)
#define LED8_ON GPIO_SetBits(GPIOB, GPIO_Pin_0)

//串口
extern void USART1_Putc(u8 c);
extern void USART1_Puts(char * str);
extern void USART2_Putc(u8 c);
extern void USART2_Puts(char * str);

extern u8 flag;
extern u32 count;
extern u8 Uart1_Get_Flag;
extern u8 Uart1_Get_Data;

#endif

TIM.c

#include "STM32Lib\stm32f10x.h"

void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
u16 CCR1_Val = 4000;//捕獲比較值1
u16 CCR2_Val = 2000;//捕獲比較值2
u16 CCR3_Val = 1000;//捕獲比較值3
u16 CCR4_Val = 500; //捕獲比較值4

/* TIM2 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

/* 基礎(chǔ)設(shè)置*/
TIM_TimeBaseStructure.TIM_Period = 8000; //計(jì)數(shù)值
TIM_TimeBaseStructure.TIM_Prescaler = 72-1; //預(yù)分頻,此值+1為分頻的除數(shù),分頻系數(shù)
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //
//向上計(jì)數(shù),從0計(jì)數(shù)到自動(dòng)加載值,然后重新從0開(kāi)始計(jì)數(shù),并產(chǎn)生一個(gè)計(jì)數(shù)溢出事件
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

/* 比較通道1*/
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Inactive; //輸出比較非主動(dòng)模式
TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //極性為正

TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable); //禁止OC1重裝載,其實(shí)可以省掉這句,因?yàn)槟J(rèn)是4路都不重裝的.

/*比較通道2 */
TIM_OCInitStructure.TIM_Pulse = CCR2_Val;

TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Disable);

/* 比較通道3 */
TIM_OCInitStructure.TIM_Pulse = CCR3_Val;

TIM_OC3Init(TIM2, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Disable);

/* 比較通道4 */
TIM_OCInitStructure.TIM_Pulse = CCR4_Val;

TIM_OC4Init(TIM2, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Disable);

/*使能預(yù)裝載*/
TIM_ARRPreloadConfig(TIM2, ENABLE);
/*預(yù)先清除所有中斷位*/
TIM_ClearITPendingBit(TIM2, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4|TIM_IT_Update);

/* 4個(gè)通道和溢出都配置中斷*/
TIM_ITConfig(TIM2, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4|TIM_IT_Update, ENABLE);

/* 允許TIM2開(kāi)始計(jì)數(shù) */
TIM_Cmd(TIM2, ENABLE);
}

NVIC.c

#include "STM32Lib\stm32f10x.h"

void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;

/* Configure one bit for preemption priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

/* Timer2中斷*/
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

/*UART1*/
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}

GPIO.c

#include "STM32Lib\stm32f10x.h"
#include "hal.h"
/*******************************************************************************
* Function Name : GPIO_Configuration
* 設(shè)置PD3,PD4,PD5,PD6為鍵盤輸入
* 設(shè)置PB0,5,8,9; PC5,7; PD7 ;PA8 為輸出LED燈
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

/*允許總線CLOCK,在使用GPIO之前必須允許相應(yīng)端的時(shí)鐘.
從STM32的設(shè)計(jì)角度上說(shuō),沒(méi)被允許的端將不接入時(shí)鐘,也就不會(huì)耗能,
這是STM32節(jié)能的一種技巧,*/

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

/*初始化LED輸出為0*/
LED1_OFF;
LED2_OFF;
LED3_OFF;
LED4_OFF;
LED5_OFF;
LED6_OFF;
LED7_OFF;
LED8_OFF;

/* PB0,5,8,9輸出 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_5|GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //開(kāi)漏輸出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //50M時(shí)鐘速度
GPIO_Init(GPIOB, &GPIO_InitStructure);

/* PC5,7輸出*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //開(kāi)漏輸出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //50M時(shí)鐘速度
GPIO_Init(GPIOC, &GPIO_InitStructure);

/*PA7,輸出*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //開(kāi)漏輸出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //50M時(shí)鐘速度
GPIO_Init(GPIOA, &GPIO_InitStructure);

/*PA8,輸出*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //開(kāi)漏輸出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //50M時(shí)鐘速度
GPIO_Init(GPIOA, &GPIO_InitStructure);

}

hal.c

/***************************************************
**HAL.c
**主要用于芯片硬件的內(nèi)部外圍和外部外圍的初始化,兩大INIT函數(shù)
**在MAIN中調(diào)用,使MAIN函數(shù)中盡量與硬件庫(kù)無(wú)關(guān)
***************************************************/

#include "STM32Lib\stm32f10x.h"

//各個(gè)內(nèi)部硬件模塊的配置函數(shù)
extern void GPIO_Configuration(void); //GPIO
extern void RCC_Configuration(void); //RCC
extern void TIM_Configuration(void); //TIM
extern void USART_Configuration(void); //串口
extern void NVIC_Configuration(void); //NVIC
/*******************************
**函數(shù)名:ChipHalInit()
**功能:片內(nèi)硬件初始化
*******************************/
void ChipHalInit(void)
{
//初始化時(shí)鐘源
RCC_Configuration();

//初始化GPIO
GPIO_Configuration();

//初始化中斷源
NVIC_Configuration();

//初始化串口
USART_Configuration();

//初始化定時(shí)器
TIM_Configuration();
}

/*********************************
**函數(shù)名:ChipOutHalInit()
**功能:片外硬件初始化
*********************************/
void ChipOutHalInit(void)
{

}

stm32f10x_it.c

/*******************************************************************************
* Function Name : TIM2_IRQHandler TIM2中斷
* Description : This function handles TIM2 global interrupt request.
* Input : None
* Output : None
* Return : None
*******************************************************************************/

extern u32 count=0;
extern u8 flag=0;
void TIM2_IRQHandler(void)
{

if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)
{
//必須清空標(biāo)志位/ /
TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);

//點(diǎn)亮LED5//
LED5_ON;
//LED1直接操作寄存器方式的閃爍
GPIOA->ODR^=GPIO_Pin_8;

}
else if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);

//點(diǎn)亮LED6//
LED6_ON;
}
else if (TIM_GetITStatus(TIM2, TIM_IT_CC3) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC3);

// 點(diǎn)亮LED7//
LED7_ON;
}
else if (TIM_GetITStatus(TIM2, TIM_IT_CC4) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC4);

//點(diǎn)亮LED8//
LED8_ON;

}
else if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
//熄滅所有LED//
count++;
flag=1;
if(count>=99999999)
count=0;

LED5_OFF;
LED6_OFF;
LED7_OFF;
LED8_OFF;
}
}

/*******************************************************************************
* Function Name : TIM3_IRQHandler
* Description : This function handles TIM3 global interrupt request.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void TIM3_IRQHandler(void)
{
}

/*******************************************************************************
* Function Name : TIM4_IRQHandler
* Description : This function handles TIM4 global interrupt request.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void TIM4_IRQHandler(void)
{
}

/*******************************************************************************
* Function Name : I2C1_EV_IRQHandler
* Description : This function handles I2C1 Event interrupt request.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void I2C1_EV_IRQHandler(void)
{
}

/*******************************************************************************
* Function Name : I2C1_ER_IRQHandler
* Description : This function handles I2C1 Error interrupt request.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void I2C1_ER_IRQHandler(void)
{
}

/*******************************************************************************
* Function Name : I2C2_EV_IRQHandler
* Description : This function handles I2C2 Event interrupt request.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void I2C2_EV_IRQHandler(void)
{
}

/*******************************************************************************
* Function Name : I2C2_ER_IRQHandler
* Description : This function handles I2C2 Error interrupt request.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void I2C2_ER_IRQHandler(void)
{
}

/*******************************************************************************
* Function Name : SPI1_IRQHandler
* Description : This function handles SPI1 global interrupt request.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SPI1_IRQHandler(void)
{
}

/*******************************************************************************
* Function Name : SPI2_IRQHandler
* Description : This function handles SPI2 global interrupt request.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SPI2_IRQHandler(void)
{
}

/*******************************************************************************
* Function Name : USART1_IRQHandler
* Description : This function handles USART1 global interrupt request.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
extern u8 Uart1_Get_Flag;
extern u8 Uart1_Get_Data;

void USART1_IRQHandler(void)
{
//接收中斷
if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)
{
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
Uart1_Get_Data=USART_ReceiveData(USART1);
Uart1_Get_Flag=1;
}

//溢出-如果發(fā)生溢出需要先讀SR,再讀DR寄存器 則可清除不斷入中斷的問(wèn)題
if(USART_GetFlagStatus(USART1,USART_FLAG_ORE)==SET)
{
USART_ClearFlag(USART1,USART_FLAG_ORE); //讀SR
USART_ReceiveData(USART1); //讀DR
}
}

main.c

/************************************************************
**實(shí)驗(yàn)名稱:Timer
**功能:實(shí)現(xiàn)Timer2的4組比較中斷,此為STM32定時(shí)器的其中一個(gè)功能.4段捕獲比較分別驅(qū)動(dòng)四個(gè)LED點(diǎn)亮,
溢出中斷則負(fù)責(zé)熄滅LED.
對(duì)于其它的定時(shí)器,基本用法均一樣(除T1,T8高級(jí)定時(shí)器)
**注意事項(xiàng):注意要開(kāi)放stm32f10x_conf.h中的 #include "stm32f10x_tim.h" ,#include "misc.h"
**作者:電子白菜
*************************************************************/

#include "STM32Lib\stm32f10x.h"
#include "hal.h"
#define SIZE 0
u8 table[11]={"0123456789 "};
char buffer[10]={"0000000000"};
void Delay(u16 n);

void d_2_char(u32 x)
{
buffer[SIZE+0]=table[x%10000000000/100000];
buffer[SIZE+1]=table[x%1000000000/100000];
buffer[SIZE+2]=table[x%100000000/100000];
buffer[SIZE+3]=table[x%10000000/10000];
buffer[SIZE+4]=table[x%1000000/10000];
buffer[SIZE+5]=table[x%100000/10000];
buffer[SIZE+6]=table[x%10000/1000];
buffer[SIZE+7]=table[x%1000/100];
buffer[SIZE+8]=table[x%100/10];
buffer[SIZE+9]=table[x%10];
}
int main(void)
{

ChipHalInit(); //片內(nèi)硬件初始化
ChipOutHalInit(); //片外硬件初始化

for(;;)
{
if(flag==1)
{
flag=0;
d_2_char(count);
USART1_Puts(buffer);
USART1_Puts("rn");
//Delay(2000);
}

}

}

//延遲函數(shù)
void Delay(u16 speed)
{
u16 i;
while(speed!=0)
{
speed--;
for(i=0;i<400;i++);
}
}

RCC.c

#include "STM32Lib\stm32f10x.h"

RCC_ClocksTypeDef RCC_ClockFreq;

void RCC_Configuration(void)
{
SystemInit();//源自system_stm32f10x.c文件,只需要調(diào)用此函數(shù),則可完成RCC的配置.具體請(qǐng)看2_RCC

/**************************************************
獲取RCC的信息,調(diào)試用
請(qǐng)參考RCC_ClocksTypeDef結(jié)構(gòu)體的內(nèi)容,當(dāng)時(shí)鐘配置完成后,
里面變量的值就直接反映了器件各個(gè)部分的運(yùn)行頻率
***************************************************/
RCC_GetClocksFreq(&RCC_ClockFreq);

/* 這個(gè)配置可使外部晶振停振的時(shí)候,產(chǎn)生一個(gè)NMI中斷,不需要用的可屏蔽掉*/
//RCC_ClockSecuritySystemCmd(ENABLE);
}

管理員在2009年8月13日編輯了該文章文章。 -->

650) this.width=650;" src="/image/default/tu_8.png">-->


關(guān)鍵詞: STM32定時(shí)器2串

評(píng)論


技術(shù)專區(qū)

關(guān)閉