注册 登录
电子工程世界-论坛 返回首页 EEWORLD首页 频道 EE大学堂 下载中心 Datasheet 专题
袁伟康2013的个人空间 https://home.eeworld.com.cn/space-uid-498889.html [收藏] [复制] [分享] [RSS]
日志

半个月stm32学习笔记

已有 637 次阅读2013-11-1 18:39 |个人分类:stm32

半个月stm32学习笔记
(三)集GPIO点灯,外部中断查询,串口通讯简单运算器,通用定时器输出PWM波与编码器 源码
/************************************************************************
              

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

#include "stm32f10x.h"
//#include "string.h"
//#include"stdio.h"
/***********************************************************************
  变量定义
************************************************************************/
//int flag,pwmval=600,t;
int encoder_num;
///***********************************************************************
//
//  printf函数的配置
//
//
//************************************************************************/
// // 发送数据
//
//int fputc(int ch, FILE *f)
//{
//
//   USART_SendData(USART1, (unsigned char) ch);// USART1 可以换成 USART2 等
// 
//   while (!(USART1->SR & USART_FLAG_TXE));
// 
//   return (ch);
//
//}
//
//   // 接收数据
//
//int GetKey (void) 
//{
//
//   while (!(USART1->SR & USART_FLAG_RXNE));
// 
//   return ((int)(USART1->DR & 0x1FF));
//
//}

/***********************************************************************
  
  外设时钟使能
************************************************************************/
void RCC_Configuration(void)

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_USART1 |
                       RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE);
        /*  使能外设时钟 */
 RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM2|RCC_APB1Periph_TIM3|RCC_APB1Periph_TIM4, ENABLE); 
}
/*******************************************************************************  
                       
        全部用到的引脚将在此配置
*******************************************************************************/
void GPIO_Configuration(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;//声明GPIO结构
  /*************** USART1的配置  **************/
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;    //   推挽复用输出
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //   推挽复用输出
    GPIO_Init(GPIOA, &GPIO_InitStructure);      
    /*********** led灯管脚配置PB12  *************/
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;//管脚的选择
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//速度的选择
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出模式选择
 GPIO_Init(GPIOB, &GPIO_InitStructure);
 /***************PWM管脚配置********************/
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 ; 
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// GPIO_Init(GPIOB, &GPIO_InitStructure);

// GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,  ENABLE);//设置 TIM3_CH2重映射到PB5上
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;//管脚的选择
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
// GPIO_Init(GPIOB, &GPIO_InitStructure);
// GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6;
//  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
//  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//  GPIO_Init(GPIOA, &GPIO_InitStructure);
// GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6;
//    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
//    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//    GPIO_Init(GPIOB, &GPIO_InitStructure);
 /***************外部中断管脚配置********************/
// GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_0;
//    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
//    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//    GPIO_Init(GPIOA, &GPIO_InitStructure);
//
// GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_1;
//    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
//    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//    GPIO_Init(GPIOA, &GPIO_InitStructure);

   /***************按键管脚配置********************/
//    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;//管脚的选择
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//速度的选择
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //推挽输出模式选择
// GPIO_Init(GPIOB, &GPIO_InitStructure);
//
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;//管脚的选择
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//速度的选择
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //推挽输出模式选择
// GPIO_Init(GPIOB, &GPIO_InitStructure);
 
 /***************编码器管脚配置********************/
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
 GPIO_Init(GPIOA, &GPIO_InitStructure);

}
/*******************************************************************************
            所有中断在此配置
*******************************************************************************/
void NVIC_Configuration(void)
{
    NVIC_InitTypeDef NVIC_InitStructure;
//    #ifdef VECT_TAB_RAM
//    NVIC_SetVectorTable(NVIC_VectTab_RAM,0x0);//设置向量表的位置和偏移//向量表在RAM,偏移量为0
//    #else
//    NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0);//向量表在FLASH,偏移量为0
//    #endif
 /**********串口中断的配置**************/
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =0; // 先占优先级
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
   /**********通用定时器4中断配置**************/
//   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);  //        中断分组
//   NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;//           中断通道
//   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =0; // 先占优先级
//   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //     响应优先级
//   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//         通道中断使能
//   NVIC_Init(&NVIC_InitStructure);//
   /**********通用定时器3中断配置**************/
   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);  //        中断分组
   NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;//           中断通道
   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =0; // 先占优先级
   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //     响应优先级
   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//         通道中断使能
   NVIC_Init(&NVIC_InitStructure);//
   /**********通用定时器2中断配置**************/
//   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);  //        中断分组
//   NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;//           中断通道
//   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =0; // 先占优先级
//   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //     响应优先级
//   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//         通道中断使能
//   NVIC_Init(&NVIC_InitStructure);//
  
   /**********外部中断配置**************/
//   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);  //        中断分组
//   NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;//           中断通道
//   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =0; // 先占优先级
//   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //     响应优先级
//   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//         通道中断使能
//   NVIC_Init(&NVIC_InitStructure);//
//  
//   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);  //        中断分组
//   NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;//           中断通道
//   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =0; // 先占优先级
//   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //     响应优先级
//   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//         通道中断使能
//   NVIC_Init(&NVIC_InitStructure);//                        
}
/*****************************************************************************
               定时器中断编码器模式在此配置
*****************************************************************************/
void TIM3_Encoder_Configuration()//利用定时器3对一个脉冲进行计数
{
 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
 TIM_ICInitTypeDef TIM_ICInitStructure;
    TIM_TimeBaseStructure.TIM_Period = 0xFFFF; ////ARR寄存器得值
 TIM_TimeBaseStructure.TIM_Prescaler =0x00; //设置用来作为TIMx时钟频率除数的预分频值 
 TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割0
 TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
 TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
 
 TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI1,TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
                                //编码模式1   //TI1极性,TIM输入捕获上升沿   //TI2极性
 TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;//
 TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;//
 TIM_ICStructInit(&TIM_ICInitStructure);
 TIM_ICInitStructure.TIM_ICFilter = 0;//输入比较滤波器
   TIM_ICInit(TIM3, &TIM_ICInitStructure);

 TIM_ETRClockMode1Config(TIM3,TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_NonInverted,0);//
 //配置外部时钟模式1,TIM_ExtTRGPSC_OFF//外部时钟触发预分频 ,TIM_ExtTRGPolarity_NonInverted//外部时钟极性。高电平或上升沿有效//不滤波
   TIM_SetCounter(TIM3, 0);
 TIM_ClearFlag(TIM3, TIM_FLAG_Update);
 TIM_ITConfig(TIM3, TIM_IT_Update|TIM_IT_Trigger, ENABLE); //TIM3溢出中断使能
 TIM_Cmd(TIM3, ENABLE); 
 
}
/*****************************************************************************
               定时器中断精准延时在此配置
*****************************************************************************/
//void TIM2_Configuration()
//{
// TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//
//// TIM_DeInit(TIM2);//复位TIM3定时器
//              //f=72M/(TIM_Prescaler+1)*(1+TIM_Period),
// TIM_TimeBaseStructure.TIM_Period = 999; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值//10ms
// TIM_TimeBaseStructure.TIM_Prescaler =7200-1; //设置用来作为TIMx时钟频率除数的预分频值  7200分频,每100毫秒记1次数
// TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割0
// TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
// TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
// TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
// 
//// TIM_ClearFlag(TIM2, TIM_FLAG_Update);//清除TIM1溢出中断标志
// TIM_ITConfig(TIM2, TIM_IT_Update|TIM_IT_Trigger, ENABLE); //TIM1溢出中断使能
//                
// TIM_Cmd(TIM2, ENABLE);  //使能TIM3外设
//}
/*****************************************************************************
               定时器中断PWM输出在此配置
*****************************************************************************/
//void TIM4_PWM_Configuration(u16 Ped,u16 Prr )
//{
// TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
// TIM_OCInitTypeDef TIM_OCInitStructure;
//
//  TIM_TimeBaseStructure.TIM_Period = Ped; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值//10ms//ARR寄存器得值
// TIM_TimeBaseStructure.TIM_Prescaler =Prr; //设置用来作为TIMx时钟频率除数的预分频值  7200分频,每100毫秒记1次数
// TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割0
// TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
// TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
// TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
//
// TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//TIM脉宽调制模式1
//// TIM_OCInitStructure.TIM_Channel = TIM_Channel_2;//使用TIM通道1
// TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//比较输出使能 比较输出使能 比较输出使能
//
//// TIM_OCInitStructure.TIM_Pulse = 0;//设置了待装入捕获比较寄存器的脉冲值//CCR寄存器的值
//
// TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//极性高输出
// TIM_OC1Init(TIM4, & TIM_OCInitStructure);
//
// TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);  //控制TIM3在OC1上使能预装载寄存器
// TIM_Cmd(TIM4, ENABLE);  //使能TIM3外设
//}

/*****************************************************************************
           USART1 配置函数
  
*****************************************************************************/
void USART1_Configuration(unsigned int baud)
{
 USART_InitTypeDef USART_InitStructure;
 USART_ClearITPendingBit(USART1,USART_IT_RXNE);//清除USARTx的中断待处理位
 USART_ClearFlag(USART1,USART_FLAG_TC);  //清除标志位

 USART_InitStructure.USART_BaudRate =baud;
 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_Tx | USART_Mode_Rx; 
 USART_Init(USART1, &USART_InitStructure);
 ////    /******同步模式另外需要配置*****/
//// USART_InitStructure.USART_Clock = USART_Clock_Disable;//指定USART时钟使能还是失能
//// USART_InitStructure.USART_CPOL = USART_CPOL_High;//指定了下SLCK引脚上时钟输出的极性
//// USART_InitStructure.USART_CPHA = USART_CPHA_1Edge;//指定了下SLCK引脚上时钟输出的相位,和CPOL位一起配合来产生用户希望的时钟/数据的采样关系
//// USART_InitStructure.USART_LastBit = USART_LastBit_Enable;//控制是否在同步模式下,在SCLK引脚上输出最后发送的那个数据字 (MSB)对应的时钟脉冲
 USART_ITConfig(USART1, USART_IT_RXNE , ENABLE);
//// USART_ITConfig(USART1, USART_IT_TXE, ENABLE);//影响进不了中断
    USART_Cmd(USART1, ENABLE);
}

/*****************************************************************************
               外部中断在此配置
*****************************************************************************/
//void EXTI_Configuration()
//{
// EXTI_InitTypeDef EXTI_InitStructure;
//
// GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);//设置中断口
//
// EXTI_InitStructure.EXTI_Line = EXTI_Line0 ;
// EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
// EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
// EXTI_InitStructure.EXTI_LineCmd = ENABLE;
// EXTI_Init(&EXTI_InitStructure);
//
// GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource1);
//
// EXTI_InitStructure.EXTI_Line = EXTI_Line1 ;
// EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
// EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
// EXTI_InitStructure.EXTI_LineCmd = ENABLE;
// EXTI_Init(&EXTI_InitStructure);
//}
/******************************************************************************
        初始化时钟晶振 72MHZ
*******************************************************************************/
void SysClock_Init(void)
{
 ErrorStatus HSEStartUpStatus;    
 RCC_DeInit();
 RCC_HSEConfig(RCC_HSE_ON);
 HSEStartUpStatus = RCC_WaitForHSEStartUp();
 if(HSEStartUpStatus == SUCCESS){
     FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
  FLASH_SetLatency(FLASH_Latency_2);
     RCC_HCLKConfig(RCC_SYSCLK_Div1);   
     RCC_PCLK2Config(RCC_HCLK_Div1);  
     RCC_PCLK1Config(RCC_HCLK_Div2); 
     RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); 
     RCC_PLLCmd(ENABLE); 
     while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET){
      ;
  } 
     RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); 
     while(RCC_GetSYSCLKSource() != 0x08){
      ;
  }
 }
}
/*******************************************************
  Delay_ms 函数
*******************************************************/
//void delay_ms(u16 time)
//{
// TIM_SetAutoreload(TIM2, 10*time-1);
// flag=0;
// while(!flag); 
//}
/*******************************************************
  按键查询_1 函数
*******************************************************/
//void keyscan()
//{
// int flag1=0;
// if(flag1==0)
// {
//  if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0)==RESET)
//  {
//   GPIO_ResetBits(GPIOB, GPIO_Pin_13);
//   GPIO_SetBits(GPIOB, GPIO_Pin_14);
//   pwmval=pwmval+1;
//   t=pwmval;
//   delay_ms(5);
//   if(pwmval==1000)
//    flag1=1;
//  }
//  if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1)==RESET)
//  {
//   GPIO_SetBits(GPIOB, GPIO_Pin_13);
//   GPIO_ResetBits(GPIOB, GPIO_Pin_14);
//   pwmval=pwmval-1;
//   t=pwmval;
//   delay_ms(5);
//   if(pwmval==1)
//    flag1=1;
//  }
// }
//
//
// if(flag1==1)
// {
//  if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0)==RESET)
//  {
//   GPIO_ResetBits(GPIOB, GPIO_Pin_13);
//   GPIO_SetBits(GPIOB, GPIO_Pin_14);
//   pwmval=pwmval-1;
//   t=pwmval;
//   delay_ms(5);
//   if(pwmval==1)
//    flag1=0;
//  }
//  if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1)==RESET)
//  {
//   GPIO_SetBits(GPIOB, GPIO_Pin_13);
//   GPIO_ResetBits(GPIOB, GPIO_Pin_14);
//   pwmval=pwmval+1;
//   t=pwmval;
//   delay_ms(5);
//   if(pwmval==1000)
//    flag1=0;
//  }
// }
//}
/*******************************************************
  MAIN 函数
*******************************************************/
int main(void)
{
   SysClock_Init();
   RCC_Configuration();
   GPIO_Configuration();
   NVIC_Configuration();
// EXTI_Configuration();
// TIM2_Configuration();
// TIM4_PWM_Configuration(1000,0 ); //对时钟不进行分频,72000HZ
 TIM3_Encoder_Configuration();
// GPIO_ResetBits(GPIOB, GPIO_Pin_13);
// GPIO_SetBits(GPIOB, GPIO_Pin_14);
//  TIM_SetCompare1(TIM3,pwmval);
   USART1_Configuration(9600);   // 配置串口1,波特率9600
   while(1)
   { 
  
  encoder_num=TIM_GetCounter(TIM3);
//   keyscan();
//   TIM_SetCompare1(TIM4,pwmval);//设置TIMx捕获比较1寄存器值    
   }
}
 
 
 
it.c
/**
  ******************************************************************************
  * @file    Project/STM32F10x_StdPeriph_Template/stm32f10x_it.c
  * @author  MCD Application Team
  * @version V3.4.0
  * @date    10/15/2010
  * @brief   Main Interrupt Service Routines.
  *          This file provides template for all exceptions handler and
  *          peripherals interrupt service routine.
  ******************************************************************************
  * @copy
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
  */
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_it.h"
//#include"stdio.h"
//#include "string.h"
//extern void delay_ms(u16 time);
//extern int pwmval,flag,t;
static int sum;
extern int encoder_num;
/** @addtogroup STM32F10x_StdPeriph_Template
  * @{
  */
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/******************************************************************************/
/*            Cortex-M3 Processor Exceptions Handlers                         */
/******************************************************************************/
/*******************************************************************************
* Function Name  : EXTI0_IRQHandler
* Description    : This function handles USART1 global interrupt request.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
//void EXTI0_IRQHandler()
//{
// if(EXTI_GetITStatus(EXTI_Line0) != RESET)
// {
//  pwmval=0;
//        EXTI_ClearITPendingBit(EXTI_Line0);
// } 
//}
/*******************************************************************************
* Function Name  : EXTI1_IRQHandler
* Description    : This function handles USART1 global interrupt request.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
//void EXTI1_IRQHandler()
//{
// if(EXTI_GetITStatus(EXTI_Line1) != RESET)
// {
//  pwmval=t;
//        EXTI_ClearITPendingBit(EXTI_Line1);
// } 
//}
/*******************************************************************************
* Function Name  : USART1_IRQHandler
* Description    : This function handles USART1 global interrupt request.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void USART1_IRQHandler(void)

 int i,k,index=0;
   if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)    // 如果是串口接收中断
 { 
//  if((USART_ReceiveData(USART1))!=' ')      //
//  {
//    sum=sum*10+(USART_ReceiveData(USART1)-48);
//  }
//  
  if((USART_ReceiveData(USART1))==' ')
  { 
   char str[10];
//   encoder_num=100;
//   TIM_SetCompare1(TIM3,sum);
////   sum=10000-sum;
//   k=sum;
   sum=encoder_num;
   k=sum;
   for(i=0;k>0;i++)    /*index保存i的位数*/
   {
          k = k / 10;
          index++;
      }
      for(i=0;i<index;i++)
   {
          str[index-i-1]=sum%10+48;    /*记得加'0'*/
          sum=sum/ 10;
      }
   for(i=0;i<index;i++)
   {
    USART_SendData(USART1, str[i]);
       while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
   }
   sum=0;
   USART_ClearITPendingBit(USART1,USART_IT_RXNE);
  }
 }
}
/*****************************************************************/
//void USART1_IRQHandler(void)
//{ 
// int i,k,index=0;
//  if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)    // 如果是串口接收中断
// { 
//  if((USART_ReceiveData(USART1))!=' ')      //
//  {
//    sum=sum*10+(USART_ReceiveData(USART1)-48);
//  }
//  
//  if((USART_ReceiveData(USART1))==' ')
//  { 
//   char str[5];
//   pwmval=sum;
////   TIM_SetCompare1(TIM3,sum);
////   sum=10000-sum;
//   k=sum;
//   for(i=0;k>0;i++)    /*index保存i的位数*/
//   {
//          k = k / 10;
//          index++;
//      }
//      for(i=0;i<index;i++)
//   {
//          str[index-i-1]=sum%10+48;    /*记得加'0'*/
//          sum=sum/ 10;
//      }
//   for(i=0;i<index;i++)
//   {
//    USART_SendData(USART1, str[i]);
//       while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
//   }
//   sum=0;
//  }
// }
//}
/*****************************************************************/
//void USART1_IRQHandler(void)
//{
// static int j=0;
// static char str[5];
// char str_1[5];
// int i=1,k,n=0,index=0,m;
//    if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)// 如果是串口接收中断
// { 
//  USART_ClearITPendingBit(USART1,USART_IT_RXNE);//清除USARTx的中断待处理位
//  USART_ClearFlag(USART1,USART_FLAG_TC);  //清除标志位
//  if(USART_ReceiveData(USART1)!=' ')
//  {
//   str[j]=USART_ReceiveData(USART1); // 将接收到的字节发送出去
//   while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
//   j++; 
//  }
//  if(USART_ReceiveData(USART1)==' ')
//  {
//     m=strlen(str);
//   for(j=0;j<m-1;j++)
//    i=i*10;
//   for(j=0;j<m;j++)
//   {
//    n=(str[j]-48)*i+n;
//    i=i/10;
//   }
//   for(j=0;j<m;j++)
//    str[j]='\0';
//   n=10000-n;
//   k=n;
//   while(n>0)/*index保存x的位数*/
//   {
//          n = n / 10;
//          index++;
//      }
//      for(j=0;j<index;j++)
//   {
//          str_1[index-j-1] = k % 10 +48;/*记得加'0'*/
//          k = k / 10;
//      }
//   for(j=0;j<index;j++)
//   { 
//    
//     USART_SendData(USART1,str_1[j]);
//     while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); 
//   }
//   j=0;
//  }
// }
//  
//}
/*****************************************************************/
//void USART1_IRQHandler(void)
//{
// char value;
// if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)// 如果是串口接收中断
// {
//  USART_ClearITPendingBit(USART1,USART_IT_RXNE);//清除USARTx的中断待处理位
//  USART_ClearFlag(USART1,USART_FLAG_TC);  //清除标志位
//  value=USART_ReceiveData(USART1);
//  while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); 
// }
//
// USART_SendData(USART1,value); 
//}
/*****************************************************************
* Function Name  : TIM2_IRQHandler
* Description    : This function handles USART1 global interrupt request.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************/
//void TIM2_IRQHandler(void) 
//{
//    if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM1 中断源
// {   
//  TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
//  flag=1;
//  }
//}
/*****************************************************************
* Function Name  : TIM3_IRQHandler
* Description    : This function handles USART1 global interrupt request.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************/
//void TIM3_IRQHandler(void) 
//{
//  if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM1 中断源
// {   
//  TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
//  }
//}
/*****************************************************************/
void TIM3_IRQHandler(void)       

 if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)//判断是否为更新中断
 { 
  
 }
 TIM_ClearITPendingBit(TIM3, TIM_IT_CC1|TIM_IT_Update);//清除中断和捕获标志位
}
/**
  * @brief   This function handles NMI exception.
  * @param  None
  * @retval None
  */
void NMI_Handler(void)
{
}
/**
  * @brief  This function handles Hard Fault exception.
  * @param  None
  * @retval None
  */
void HardFault_Handler(void)
{
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1)
  {
  }
}
/**
  * @brief  This function handles Memory Manage exception.
  * @param  None
  * @retval None
  */
void MemManage_Handler(void)
{
  /* Go to infinite loop when Memory Manage exception occurs */
  while (1)
  {
  }
}
/**
  * @brief  This function handles Bus Fault exception.
  * @param  None
  * @retval None
  */
void BusFault_Handler(void)
{
  /* Go to infinite loop when Bus Fault exception occurs */
  while (1)         {
  }
}
/**
  * @brief  This function handles Usage Fault exception.
  * @param  None
  * @retval None
  */
void UsageFault_Handler(void)
{
  /* Go to infinite loop when Usage Fault exception occurs */
  while (1)
  {
  }
}
/**
  * @brief  This function handles SVCall exception.
  * @param  None
  * @retval None
  */
void SVC_Handler(void)
{
}
/**
  * @brief  This function handles Debug Monitor exception.
  * @param  None
  * @retval None
  */
void DebugMon_Handler(void)
{
}
/**
  * @brief  This function handles PendSVC exception.
  * @param  None
  * @retval None
  */
void PendSV_Handler(void)
{
}
/**
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval None
  */
void SysTick_Handler(void)
{
}
/******************************************************************************/
/*                 STM32F10x Peripherals Interrupt Handlers                   */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_stm32f10x_xx.s).                                            */
/******************************************************************************/
/**
  * @brief  This function handles PPP interrupt request.
  * @param  None
  * @retval None
  */
/*void PPP_IRQHandler(void)
{
}*/
/**
  * @}
  */

/******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

热门文章