连接好OLED 屏幕后下载程序OLED 屏幕什么也不显示;
然后我把主程序中
TIM3_Cap_Init(0XFFFF,72-1);
TIM4_Cap_Init(0XFFFF,72-1); //以1Mhz的频率计数
这俩行初始化代码注释掉之后,OLED 屏幕显示r=和p=,也就是以下语句
OLED_ShowString(0,3,"r=",16);
OLED_ShowString(65,3,"p=",16);
这是什么原因,跪求啊大佬们
附主程序和TIMER.C
main.c
#include "stm32f10x.h"
#include "delay.h"
#include "usart.h"
#include "timer.h"
#include "math.h"
#include "oled.h"
extern u8 TIM3CH1_CAPTURE_STA; //输入捕获状态
extern u8 TIM3CH2_CAPTURE_STA;
extern u16 TIM3CH1_CAPTURE_VAL; //输入捕获值
extern u8 TIM4CH1_CAPTURE_STA; //输入捕获状态
extern u8 TIM4CH2_CAPTURE_STA;
extern u16 TIM4CH1_CAPTURE_VAL; //输入捕获值
int main(void)
{
u32 temp1;
u32 temp2;
u32 temp3;
u32 temp4;
double t1=(double)(temp1/1000000);
double t2=(double)(temp2/1000000);
double t3=(double)(temp3/1000000);
double t4=(double)(temp4/1000000);
double A=(1-340*340*t1*t1)*t1*t1;
double B=(1-340*340*t2*t2)*t2*t2;
double C=(1-340*340*t3*t3)*t3*t3;
double D=(1-340*340*t4*t4)*t4*t4;
double p;
double r;
delay_init();//延时
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断分组
uart_init(115200); //串口初始化为115200
TIM3_Cap_Init(0XFFFF,72-1);
TIM4_Cap_Init(0XFFFF,72-1); //以1Mhz的频率计数
OLED_Init();
OLED_Clear();
while(1)
{
if((TIM3CH1_CAPTURE_STA&0X80)&(TIM4CH1_CAPTURE_STA&0X80))//捕获事件1、3
{
temp1=TIM3CH1_CAPTURE_STA&0X3F;
temp1*=65536;//溢出时间总和
temp1+=TIM3CH1_CAPTURE_VAL;//时间差1
temp3=TIM4CH1_CAPTURE_STA&0X3F;
temp3*=65536;//溢出时间总和
temp3+=TIM4CH1_CAPTURE_VAL;//时间差2
p=acos(sqrt((A-340*340*340*340*t1*t1*t3*t3*(t1*t1-t3*t3))/(A-C)));
r=sqrt((1/4*340*340*A/t1/t1*(A-C))/(1-2*340*340*t1*t1+340*340*t3*t3-340*340*340*340*t1*t1*t3*t3+340*340*340*340*t1*t1*t1*t1));
OLED_ShowNum(20,3,p,3,16);
OLED_ShowNum(85,3,r,3,16);
OLED_ShowString(0,3,"r=",16);
OLED_ShowString(65,3,"p=",16);
break;
}
if((TIM3CH1_CAPTURE_STA&0X80)&(TIM4CH2_CAPTURE_STA&0X80))//捕获事件1、4
{
temp1=TIM3CH1_CAPTURE_STA&0X3F;
temp1*=65536;//溢出时间总和
temp1+=TIM3CH1_CAPTURE_VAL;//时间差1
temp4=TIM4CH1_CAPTURE_STA&0X3F;
temp4*=65536;//溢出时间总和
temp4+=TIM4CH1_CAPTURE_VAL;//时间差2
p=3.14-acos(sqrt((A-340*340*340*340*t1*t1*t4*t4*(t1*t1-t4*t4))/(A-D)));
r=sqrt((1/4*340*340*A/t1/t1*(A-D))/(1-2*340*340*t1*t1+340*340*t4*t4-340*340*340*340*t1*t1*t4*t4+340*340*340*340*t1*t1*t1*t1));
OLED_ShowNum(20,3,p,3,16);
OLED_ShowNum(85,3,r,3,16);
OLED_ShowString(0,3,"r=",16);
OLED_ShowString(65,3,"p=",16);
break;
}
if((TIM3CH2_CAPTURE_STA&0X80)&(TIM4CH1_CAPTURE_STA&0X80))//捕获事件2、3
{
temp2=TIM3CH1_CAPTURE_STA&0X3F;
temp2*=65536;//溢出时间总和
temp2+=TIM3CH1_CAPTURE_VAL;//时间差1
temp3=TIM4CH1_CAPTURE_STA&0X3F;
temp3*=65536;//溢出时间总和
temp3+=TIM4CH1_CAPTURE_VAL;//时间差2
p=6.28-acos(sqrt((B-340*340*340*340*t2*t2*t3*t3*(t2*t2-t3*t3))/(B-C)));
r=sqrt((1/4*340*340*B/t2/t2*(B-C))/(1-2*340*340*t2*t2+340*340*t3*t3-340*340*340*340*t2*t2*t3*t3+340*340*340*340*t2*t2*t2*t2));
OLED_ShowNum(20,3,p,3,16);
OLED_ShowNum(85,3,r,3,16);
OLED_ShowString(0,3,"r=",16);
OLED_ShowString(65,3,"p=",16);
break;
}
if((TIM3CH2_CAPTURE_STA&0X80)&(TIM4CH2_CAPTURE_STA&0X80))//捕获事件2、4
{
temp2=TIM3CH1_CAPTURE_STA&0X3F;
temp2*=65536;//溢出时间总和
temp2+=TIM3CH1_CAPTURE_VAL;//时间差1
temp4=TIM4CH1_CAPTURE_STA&0X3F;
temp4*=65536;//溢出时间总和
temp4+=TIM4CH1_CAPTURE_VAL;//时间差2
p=3.14+acos(sqrt((B-340*340*340*340*t2*t2*t4*t4*(t2*t2-t4*t4))/(B-D)));
r=sqrt((1/4*340*340*B/t2/t2*(B-D))/(1-2*340*340*t2*t2+340*340*t4*t4-340*340*340*340*t2*t2*t4*t4+340*340*340*340*t2*t2*t2*t2));
OLED_ShowNum(20,3,p,3,16);
OLED_ShowNum(85,3,r,3,16);
OLED_ShowString(0,3,"r=",16);
OLED_ShowString(65,3,"p=",16);
break;
}
else
{
OLED_ShowString(0,3,"r=",16);
OLED_ShowString(65,3,"p=",16);
}
}
}
TIMER.C
#include "timer.h"
#include "usart.h"
/*===================================================
定时器3 配置
===================================================*/
void TIM3_Cap_Init(u16 arr,u16 psc)
{
TIM_ICInitTypeDef TIM3_ICInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStrue;
NVIC_InitTypeDef NVIC_InitStrue;
GPIO_InitTypeDef GPIO_InitStrue;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);//使能定时器时钟(TIM3)
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//使能GPIOA时钟
GPIO_InitStrue.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;//清除之前输入??
GPIO_InitStrue.GPIO_Mode = GPIO_Mode_IPD;//设置输入
GPIO_Init(GPIOA,&GPIO_InitStrue);
GPIO_ResetBits(GPIOA,GPIO_Pin_6|GPIO_Pin_7);//设置下拉??
//初始化定时器
TIM_TimeBaseInitStrue.TIM_Period = arr;//自动装载值
TIM_TimeBaseInitStrue.TIM_Prescaler = psc; //预分频系数
TIM_TimeBaseInitStrue.TIM_ClockDivision = TIM_CKD_DIV1; //
TIM_TimeBaseInitStrue.TIM_CounterMode = TIM_CounterMode_Up; //计数模式(向上)
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStrue);
//初始化捕获??
TIM3_ICInitStructure.TIM_Channel = TIM_Channel_1;//选择输入端1
TIM3_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;//上升沿捕获
TIM3_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;//映射到TI1上
TIM3_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;//不分频
TIM3_ICInitStructure.TIM_ICFilter = 0x00;//不滤波
TIM_ICInit(TIM3,&TIM3_ICInitStructure);
TIM3_ICInitStructure.TIM_Channel = TIM_Channel_2;//选择输入端2
TIM3_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;//上升沿捕获
TIM3_ICInitStructure.TIM_ICSelection = TIM_ICSelection_IndirectTI;//映射到TI1上
TIM3_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;//不分频
TIM3_ICInitStructure.TIM_ICFilter = 0x00;//不滤波
TIM_ICInit(TIM3,&TIM3_ICInitStructure);
//配置中断优先级
NVIC_InitStrue.NVIC_IRQChannel = TIM3_IRQn;//TIM3中断
NVIC_InitStrue.NVIC_IRQChannelSubPriority = 2; //响应优先级
NVIC_InitStrue.NVIC_IRQChannelPreemptionPriority = 0;//抢断优先级
NVIC_InitStrue.NVIC_IRQChannelCmd = ENABLE;//IRQ使能
NVIC_Init(&NVIC_InitStrue);//初始化NVIC寄存器
//开启定时器中断
TIM_ITConfig(TIM3,TIM_IT_Update|TIM_IT_CC1|TIM_IT_CC2,ENABLE);
//使能定时器
TIM_Cmd(TIM3,ENABLE);
}
u8 TIM3CH1_CAPTURE_STA=0; //输入捕获状态
u8 TIM3CH2_CAPTURE_STA=0;
u16 TIM3CH1_CAPTURE_VAL; //输入捕获值
//定时器3中断服务程序
void TIM3_IRQHandler(void)
{
if((TIM3CH1_CAPTURE_STA&0X80)==0)//1还未成功捕获
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)//溢出
{
if(TIM3CH1_CAPTURE_STA&0X40)//1已经捕获到高电平了
{
if((TIM3CH1_CAPTURE_STA&0X3F)==0X3F)//1高电平太长了
{
TIM3CH1_CAPTURE_STA|=0X80;//标记成功捕获了一次1
TIM3CH1_CAPTURE_VAL=0XFFFF;
}else TIM3CH1_CAPTURE_STA++;
}
}
if((TIM3CH2_CAPTURE_STA&0X80)==0)//2还未成功捕获
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)//溢出
{
if(TIM3CH2_CAPTURE_STA&0X40)//1已经捕获到高电平了
{
if((TIM3CH2_CAPTURE_STA&0X3F)==0X3F)//2高电平太长了
{
TIM3CH2_CAPTURE_STA|=0X80;//标记成功捕获了一次2
TIM3CH1_CAPTURE_VAL=0XFFFF;
}else TIM3CH2_CAPTURE_STA++;
}
}
if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)//捕获1发生捕获事件
{
if(TIM3CH2_CAPTURE_STA&0X40)//CH2跳变
{
TIM3CH2_CAPTURE_STA|=0X80;//标记成功捕获2
TIM3CH1_CAPTURE_VAL=TIM_GetCapture1(TIM3);//把值送入VAL1
}else
{
TIM_SetCounter(TIM3,0); //计数器记0
TIM3CH1_CAPTURE_STA|=0X40; //标记1跳变
TIM_OC1PolarityConfig(TIM3,TIM_ICPolarity_Rising);//将定时器设置为上升沿触发
}
}
if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)//捕获2发生捕获事件
{
if(TIM3CH1_CAPTURE_STA&0X40)//CH1跳变
{
TIM3CH1_CAPTURE_STA|=0X80;//标记成功捕获1
TIM3CH1_CAPTURE_VAL=TIM_GetCapture1(TIM3);//把值送入VAL1
}else
{
TIM_SetCounter(TIM3,0); //计数器记0
TIM3CH2_CAPTURE_STA|=0X40; //标记2跳变
TIM_OC1PolarityConfig(TIM3,TIM_ICPolarity_Rising);//将定时器设置为上升沿触发
}
}
}
}
}
/*===================================================
定时器4 配置
===================================================*/
void TIM4_Cap_Init(u16 arr,u16 psc)
{
TIM_ICInitTypeDef TIM4_ICInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStrue;
NVIC_InitTypeDef NVIC_InitStrue;
GPIO_InitTypeDef GPIO_InitStrue;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);//使能定时器时钟(TIM4)
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//使能GPIOB时钟
GPIO_InitStrue.GPIO_Mode = GPIO_Mode_IPD;//设置输入
GPIO_InitStrue.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;//清除之前输入??
GPIO_Init(GPIOB,&GPIO_InitStrue);
GPIO_ResetBits(GPIOB,GPIO_Pin_6|GPIO_Pin_7);//设置下拉??
//初始化定时器
TIM_TimeBaseInitStrue.TIM_Period = arr;//自动装载值
TIM_TimeBaseInitStrue.TIM_Prescaler = psc; //预分频系数
TIM_TimeBaseInitStrue.TIM_CounterMode = TIM_CounterMode_Up; //计数模式(向上)
TIM_TimeBaseInitStrue.TIM_ClockDivision = TIM_CKD_DIV1; //
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStrue);
//初始化捕获
TIM4_ICInitStructure.TIM_Channel = TIM_Channel_1;//选择输入端1
TIM4_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;//上升沿捕获
TIM4_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;//映射到TI3上
TIM4_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;//不分频
TIM4_ICInitStructure.TIM_ICFilter = 0x00;//不滤波
TIM_ICInit(TIM4,&TIM4_ICInitStructure);
TIM4_ICInitStructure.TIM_Channel = TIM_Channel_2;//选择输入端2
TIM4_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;//上升沿捕获
TIM4_ICInitStructure.TIM_ICSelection = TIM_ICSelection_IndirectTI;//映射到TI3上
TIM4_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;//不分频
TIM4_ICInitStructure.TIM_ICFilter = 0x00;//不滤波
TIM_ICInit(TIM4,&TIM4_ICInitStructure);
//配置中断优先级
NVIC_InitStrue.NVIC_IRQChannel = TIM4_IRQn;//TIM4中断
NVIC_InitStrue.NVIC_IRQChannelSubPriority = 2; //响应优先级
NVIC_InitStrue.NVIC_IRQChannelPreemptionPriority = 0;//抢断优先级
NVIC_InitStrue.NVIC_IRQChannelCmd = ENABLE;//IRQ使能
NVIC_Init(&NVIC_InitStrue);//初始化NVIC寄存器
//开启定时器中断
TIM_ITConfig(TIM4,TIM_IT_Update|TIM_IT_CC1|TIM_IT_CC2,ENABLE);
//使能定时器
TIM_Cmd(TIM4,ENABLE);
}
u8 TIM4CH1_CAPTURE_STA=0; //输入捕获状态
u8 TIM4CH2_CAPTURE_STA=0;
u16 TIM4CH1_CAPTURE_VAL; //输入捕获值
//定时器4中断服务程序
void TIM4_IRQHandler(void)
{
if((TIM4CH1_CAPTURE_STA&0X80)==0)//1还未成功捕获
{
if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)//溢出
{
if(TIM4CH1_CAPTURE_STA&0X40)//1已经捕获到高电平了
{
if((TIM4CH1_CAPTURE_STA&0X3F)==0X3F)//1高电平太长了
{
TIM4CH1_CAPTURE_STA|=0X80;//标记成功捕获了一次1
TIM4CH1_CAPTURE_VAL=0XFFFF;
}else TIM4CH1_CAPTURE_STA++;
}
}
if((TIM4CH2_CAPTURE_STA&0X80)==0)//2还未成功捕获
{
if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)//溢出
{
if(TIM4CH2_CAPTURE_STA&0X40)//1已经捕获到高电平了
{
if((TIM4CH2_CAPTURE_STA&0X3F)==0X3F)//2高电平太长了
{
TIM4CH2_CAPTURE_STA|=0X80;//标记成功捕获了一次2
TIM4CH1_CAPTURE_VAL=0XFFFF;
}else TIM4CH2_CAPTURE_STA++;
}
}
if (TIM_GetITStatus(TIM4, TIM_IT_CC1) != RESET)//捕获1发生捕获事件
{
if(TIM4CH2_CAPTURE_STA&0X40)//CH2跳变
{
TIM4CH2_CAPTURE_STA|=0X80;//标记成功捕获2
TIM4CH1_CAPTURE_VAL=TIM_GetCapture1(TIM4);//把值送入VAL1
}else
{
TIM_SetCounter(TIM4,0); //计数器记0
TIM4CH1_CAPTURE_STA|=0X40; //标记1跳变
TIM_OC1PolarityConfig(TIM4,TIM_ICPolarity_Rising);//将定时器设置为上升沿触发
}
}
if (TIM_GetITStatus(TIM4, TIM_IT_CC2) != RESET)//捕获2发生捕获事件
{
if(TIM4CH1_CAPTURE_STA&0X40)//CH1跳变
{
TIM4CH1_CAPTURE_STA|=0X80;//标记成功捕获1
TIM4CH1_CAPTURE_VAL=TIM_GetCapture1(TIM4);//把值送入VAL1
}else
{
TIM_SetCounter(TIM4,0); //计数器记0
TIM4CH2_CAPTURE_STA|=0X40; //标记2跳变
TIM_OC1PolarityConfig(TIM4,TIM_ICPolarity_Rising);//将定时器设置为上升沿触发
}
}
}
}
}
一周热门 更多>