这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 高校专区 » 漓东e学堂 » 1010 【更新】labview的安装过程

共71条 5/8 |‹ 3 4 5 6 7 8 跳转至
菜鸟
2014-11-22 17:54:47     打赏
41楼
那几段比较重复,所以是复制粘贴的,改完管脚、中断线忘了改中断打印了现在整改了

高工
2014-11-25 21:54:52     打赏
42楼

再养肥点

准备设置精华帖


菜鸟
2014-11-25 23:02:49     打赏
43楼

写了通用定时器TIM2和TIM3的定时

TIM2定时为每0.5秒闪烁一次

TIM3定时为每1秒闪烁一次

频率、定时时间的具体计算方法写在了注解上

计算方法刚开始有点迷糊不记得了

是从http://wj86843248.blog.163.com/blog/static/6864301220132974130478/这个网站上看的

目前没有办法写上高级定时器TIM8

目测是库的问题,求解决中!

还有,发现了一个很有用的STM32库函数查询的PDF

可以查询各种库函数,也有使用范例

STM32固件库说明(中文版).pdf


视频地址:http://player.youku.com/player.php/sid/XODM1MDg0NTYw/v.swf
 

 

#include "stm32f10x.h"
#include "misc.h"


void RCC_Configuration(void);
void NVIC_Configuration(void);
void GPIO_Configuration(void);
void TIM3_Configuration(void);
void TIM2_Configuration(void);

void RCC_Configuration(void)
{
SystemInit();//24兆
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);//TIM3外设时钟打开,TIM3是挂在APB1上的,一
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//TIM2外设时钟打开,TIM2是挂在APB1上的,一
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);//GPIOC外设时钟打开
}

void NVIC_Configuration(void)//设置中断,二
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//抢占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;//响应式优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
	
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);
}

void GPIO_Configuration(void)//三,LED配置
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_5|GPIO_Pin_6;;//使用到PC1.2.5.6
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}

void TIM2_Configuration(void)//四,定时器配置
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period = 4999;//计4999个数就产生一次更新事件,4999+1=5000,5000*0.1ms=0.5s
TIM_TimeBaseStructure.TIM_Prescaler = 2399;//预分频系数为2399,则计数器的时钟为24MHZ/2399+1=10kHZ,1/10000=0,0001.即0.1ms
//预分频数取值范围为0~65535
TIM_TimeBaseStructure.TIM_ClockDivision = 0;//
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数模式,从0开始向上
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);//将配置应用到TIM3中
}

void TIM3_Configuration(void)//四,定时器配置
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period = 9999;//计9999个数就产生一次更新事件,9999+1=10000,10000*0.1ms=1s
TIM_TimeBaseStructure.TIM_Prescaler = 2399;//预分频系数为2399,则计数器的时钟为24MHZ/2399+1=10kHZ,1/10000=0,0001.即0.1ms
TIM_TimeBaseStructure.TIM_ClockDivision = 0;//
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数模式,从0开始向上
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);//将配置应用到TIM3中
}

int main(void)
{
RCC_Configuration();
NVIC_Configuration();
GPIO_Configuration();
TIM2_Configuration();
TIM3_Configuration();

TIM_ClearFlag(TIM2, TIM_FLAG_Update);
TIM_ClearFlag(TIM3, TIM_FLAG_Update);
TIM_ARRPreloadConfig(TIM2, DISABLE);
TIM_ARRPreloadConfig(TIM3, DISABLE);

TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM2, ENABLE);//开启定时器TIM3
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM3, ENABLE);//开启定时器TIM3

while (1) {
}
}

#include "stm32f10x_it.h"
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) //检测是否发生溢出更新事件
	{
	TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
	GPIO_WriteBit(GPIOC, GPIO_Pin_2|GPIO_Pin_6, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_2|GPIO_Pin_6)));//取反库函数
	}
}

void TIM3_IRQHandler(void)
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //检测是否发生溢出更新事件
	{
	TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
	GPIO_WriteBit(GPIOC, GPIO_Pin_1|GPIO_Pin_5, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_1|GPIO_Pin_5)));//取反库函数
	}
}


 


 

 


菜鸟
2014-11-25 23:34:37     打赏
44楼
还有待学习

高工
2014-12-01 13:44:16     打赏
45楼
很不错的帖子,置顶给其它童鞋学习

菜鸟
2014-12-01 16:13:28     打赏
46楼

用PWM调节LED亮度大小

由于RGB的3个颜色的LED亮度不同

根据不同的组合可以得到不同的颜色

PA1-通道2-Blue

PA2-通道3-Red

PA3-通道4-Green

之前由于TIM_SetCompare4(TIM2,temp2);之后的延时过小看不到明显变化

后来在 

   TIM_SetCompare2(TIM2,temp0);delay_ms(400); //
   TIM_SetCompare3(TIM2,temp1);delay_ms(400);//  
   TIM_SetCompare4(TIM2,temp2);delay_ms(400);

之后都加上了相对比较大的延时

所以在每一个通道的占空比发生变化后组合都不同

产生了更多种的颜色组合

话说, TIM_SetCompare2(TIM2,temp0);里temp的取值范围到底是多少啊

之前设置得太小变化不明显

所以就设到了5000,会有什么后遗症么?


视频地址:http://player.youku.com/player.php/sid/XODM5MDk0NjEy/v.swf
 

#include "stm32f10x.h"
#include "stm32_eval.h"
#include <stdio.h>
#define VREF 3.3
int temp0,temp1,temp2;

/*延时函数 微秒*/ 
void delay_us(u32 n)  
{  
    u8 j;  
    while(n--)  
    for(j=0;j<10;j++);  
}  
/*延时函数 毫秒*/ 
void  delay_ms(u32 n)  
{  
   while(n--)  
    delay_us(1000);  
}

GPIO_InitTypeDef   GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;

void RCC_Configuration(void)
{
 SystemInit();
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);
 GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);
 GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOD, &GPIO_InitStructure);
 GPIO_ResetBits(GPIOD,GPIO_Pin_2);
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE);
 GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
 GPIO_SetBits(GPIOC,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
}

void USART_int(long BaudRate)
{
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);
       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);
    /* PA10 USART1_Rx  */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

  USART_InitStructure.USART_BaudRate = BaudRate;//??????
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;//???????8bit
  USART_InitStructure.USART_StopBits = USART_StopBits_1;//????1
  USART_InitStructure.USART_Parity = USART_Parity_No;//????
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//??????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);
  USART_Cmd(USART1, ENABLE);
 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
 USART_Cmd(USART1, ENABLE);
}


void PWM_Config()
{uint16_t PrescalerValue = 0;
 TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;//定义结构体
  TIM_OCInitTypeDef  TIM_OCInitStructure;

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//打开TIM2,TIM2挂在AP1上
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO , ENABLE);//一、开启功能复用
 
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//所需的3个管脚,复用输出
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
 TIM_Cmd(TIM2, ENABLE);

  PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;//二、配置TIM2
  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 0x0FFE;
  TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
 
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//三、脉冲宽度调制模式1
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
 
  /* PWM1 Mode configuration: Channel2 *///四、
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//选择输出比较状态 
  TIM_OCInitStructure.TIM_Pulse = 0xFFFF;//IM_Pulse-设置了待装入捕获比较寄存器的脉冲值,取值在0x0000-0xFFFF之间                                        
  TIM_OC2Init(TIM2, &TIM_OCInitStructure);
 
 /* PWM1 Mode configuration: Channel3 */
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = 0xFFFF;//0x0000最亮,0xFFFF熄灭
  TIM_OC3Init(TIM2, &TIM_OCInitStructure);
 
 /* PWM1 Mode configuration: Channel4 */
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = 0xFFFF;
  TIM_OC4Init(TIM2, &TIM_OCInitStructure);
  TIM_ARRPreloadConfig(TIM2, ENABLE);//reload-重新装载初始值
}

void colour(int z)
{
   switch(z)//分别点亮相应的颜色
 {
  case 0 : temp0=5000,temp1=2000,temp2=3000;/*橙色*/  break;
  case 1 : temp0=5000,temp1=0,temp2=0;      /*黄色*/  break;
  case 2 : temp0=4000,temp1=5000,temp2=100; /*绿色*/  break;
  case 3 : temp0=100,temp1=150,temp2=5000;  /*紫色*/  break;
  case 4 : temp0=1,temp1=5000,temp2=1;      /*水蓝*/  break;
  default: printf(" error\n");  
  }
}

void PWM_TEST()
{
  int i=0;
 for(i=0;i<6;i++)
 {
      colour(i);  //temp1-Red,temp2-Green,temp0-Blue
    TIM_SetCompare2(TIM2,temp0);delay_ms(400); //设置TIM2捕获比较器2寄存器的值
   TIM_SetCompare3(TIM2,temp1);delay_ms(400);//TIM——通道数(定时器,数值) 
   TIM_SetCompare4(TIM2,temp2);delay_ms(400);
  }
}

int main(void)
{
  RCC_Configuration();
  USART_int(115200);
 
 printf(" config done...\r\n");
 PWM_Config();
 delay_ms(1000);
 while(1)
 {
  PWM_TEST();
 }
}

#ifdef  USE_FULL_ASSERT
void assert_failed(uint8_t* file, uint32_t line)
{
  while (1)
  {
  }
}
#endif
#ifdef __GNUC__
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */

PUTCHAR_PROTOTYPE
{
  USART_SendData(EVAL_COM1, (uint8_t) ch);
  while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)
  {}
  return ch;
}

#ifdef  USE_FULL_ASSERT
void assert_failed(uint8_t* file, uint32_t line)
{
  while (1)
  {
  }
}
#endif

 


菜鸟
2014-12-01 17:05:57     打赏
47楼
 好方法~

菜鸟
2014-12-01 18:34:58     打赏
48楼
就只会用switch而已

高工
2014-12-01 21:05:20     打赏
49楼
swtich最好把default也考虑进去,要不然很容易出bug的

菜鸟
2014-12-02 12:57:05     打赏
50楼
酱紫?加上了

共71条 5/8 |‹ 3 4 5 6 7 8 跳转至

回复

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