学前班
最后登录1970-1-1
在线时间 小时
注册时间2014-7-18
|
#include "stm32f10x_tim.h"
#include "sys.h"
#include "delay.h"
#include "usart.h"
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //ê1ÄüPB,PE¶Ë¿úê±Öó
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All; //LED0-->PB.5 ¶Ë¿úÅäÖÃ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //íÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO¿úËù¶èÎa50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); //¸ù¾Yé趨2Îêy3õê¼»ˉGPIOB.5
GPIO_SetBits(GPIOA,GPIO_Pin_All); //PB.5 êä3ö¸ß
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //ê1ÄüPB,PE¶Ë¿úê±Öó
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //LED0-->PB.5 ¶Ë¿úÅäÖÃ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //íÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO¿úËù¶èÎa50MHz
GPIO_Init(GPIOB, &GPIO_InitStructure); //¸ù¾Yé趨2Îêy3õê¼»ˉGPIOB.5
GPIO_SetBits(GPIOB,GPIO_Pin_7); //PB
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //LED0-->PB.5 ¶Ë¿úÅäÖÃ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //íÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO¿úËù¶èÎa50MHz
GPIO_Init(GPIOB, &GPIO_InitStructure); //¸ù¾Yé趨2Îêy3õê¼»ˉGPIOB.5
}
void Tim2_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
TIM_DeInit(TIM2);
TIM_TimeBaseStructure.TIM_Period = 20000;
TIM_TimeBaseStructure.TIM_Prescaler =36000; //éèÖÃóÃà′×÷ÎaTIMxê±ÖóÆμÂê3yêyμÄÔ¤·ÖÆμÖμ
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòéϼÆêyÄ£ê½
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
TIM_Cmd(TIM2,ENABLE);
TIM_ClearFlag(TIM2,TIM_FLAG_Update);
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
}
int main(void)
{
const u16 a[10] ={0xd00,0xfddf,0xb04,0x944,0x9c2,0x960,0x920,0xdc4,0x900,0x940 };
u8 t=0;
u16 time,juli,i;
delay_init();
NVIC_Configuration(); //éèÖÃNVICÖD¶Ï·Ö×é2:2λÇàÕ¼óÅÏ輶£¬2λÏìó|óÅÏ輶
uart_init(9600);
LED_Init();
Tim2_Init();//′®¿ú3õê¼»ˉÎa9600
while(1)
{
GPIO_SetBits(GPIOB,GPIO_Pin_7);
TIM2->CNT=0;
TIM_Cmd(TIM2,ENABLE);
delay_us(1000);
GPIO_ResetBits(GPIOB,GPIO_Pin_7);
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_6)==1)
{
TIM_Cmd(TIM2,DISABLE);
time=TIM2->CNT;
}
juli=340*(time/2);
i=juli%10;
GPIO_Write(GPIOA,a[i]);
delay_ms(1000);
}
}
为什么这个time的值一直是0?
|
|