[mw_shl_code=c,true]#include "stm32f10x.h"
#include "delay.h"
void Usart_Init(void);
void TPAD_Init(void);
unsigned int TPAD_Get_Val(void);
void USART_SendString(unsigned int num);
//UDIS½ûÖ1¸üDÂ
int main()
{
unsigned int dat;
delay_init();
TPAD_Init();
Usart_Init();
while(1)
{
dat = TPAD_Get_Val();
USART_SendString(dat);
delay_ms(10);
}
return 0;
}
void USART_SendString(unsigned int num)
{
short int i = 0;
unsigned char temp[20];
do
{
temp = num % 10;
num /= 10;
i++;
}while(num);
i--;
for(; i >= 0; i--)
{
USART_SendData(USART1, temp - 0 + '0');
while(!USART_GetFlagStatus(USART1, USART_FLAG_TC));
USART_ClearFlag(USART1, USART_FLAG_TC);
}
USART_SendData(USART1, 0x0d);
while(!USART_GetFlagStatus(USART1, USART_FLAG_TC));
USART_ClearFlag(USART1, USART_FLAG_TC);
USART_SendData(USART1, 0x0a);
while(!USART_GetFlagStatus(USART1, USART_FLAG_TC));
USART_ClearFlag(USART1, USART_FLAG_TC);
}
void TPAD_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA , GPIO_Pin_1);
delay_ms(2);//
TIM_TimeBaseStructure.TIM_Period = 60000;
TIM_TimeBaseStructure.TIM_Prescaler = 0; //?
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //?
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x00;
TIM_ICInit(TIM2, &TIM_ICInitStructure);
TIM_ClearITPendingBit(TIM2 ,TIM_IT_Update);
TIM_ITConfig(TIM2, TIM_IT_Update | TIM_IT_CC2, DISABLE);
// TIM_Cmd(TIM2, ENABLE);
}
unsigned int TPAD_Get_Val(void)
{
unsigned int count = 0;
GPIO_InitTypeDef GPIO_InitStructure;
TIM_SetCounter(TIM2, 0);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_Cmd(TIM2, ENABLE);
while(1)
{
if(TIM_GetFlagStatus(TIM2, TIM_FLAG_Update))
{
count += 0x10000;
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
if(count >= 0xffff0000)
{
count = 0;
}
}
if(TIM_GetFlagStatus(TIM2, TIM_FLAG_CC2OF) != RESET)
{
count |= TIM_GetCapture2(TIM2);
TIM_ClearFlag(TIM2, TIM_FLAG_CC2OF);
break;
}
}
return count;
}
void Usart_Init(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(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);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_DeInit(USART1);
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_Mode = USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_TC, DISABLE);
USART_Cmd(USART1 ,ENABLE);
}
[/mw_shl_code]
奇怪的是,为什么我的程序一直会卡死在TPAD_Get_Val这个函数的while循环中,也就是一直没有触发捕获,不解,想了好久了。。
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
我明天再看看
一周热门 更多>