main函数
#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "usart.h"
#include "exti.h"
#include "encode.h"
#define prd 1000
u16 cnt4,cnt3;
s32 CNT4,CNT3;
s32 V4,V3;
extern s32 rcnt4,rcnt3;
int main(void)
{
s32 CNT4_temp,CNT4_last,rcnt4_last,CNT3_temp,CNT3_last,rcnt3_last;
u16 count;//±àÂëÆ÷¼Æêy
delay_init(); //Ñóê±oˉêy3õê¼»ˉ
uart_init(9600); //′®¿ú3õê¼»ˉÎa115200
EXTI_PB1_Config();//±àÂëÆ÷ZÏà1éáãDÅoÅía2¿ÖD¶Ï¿ú3õê¼»ˉ
EXTI_PB2_Config();//±àÂëÆ÷ZÏà1éáãDÅoÅía2¿ÖD¶Ï¿ú3õê¼»ˉ
TIM_Init();//¶¨ê±Æ÷±àÂëÆ÷½ó¿ú3õê¼»ˉ
LED_Init(); //LED¶Ë¿ú3õê¼»ˉ
KEY_Init(); //3õê¼»ˉóë°′¼üᬽóμÄó2¼t½ó¿ú
TIM8->CCR1 = 3000; //PC6éÏàíÂÛÄüêä3ö1.65VμçÑ1 Îó2î0.1V
TIM8->CCR2 = 0; //PC7éÏàíÂÛÄüêä3ö1.98VμçÑ1 Îó2î0.1V
TIM8->CCR3 = 3000; //PC8éÏàíÂÛÄüêä3ö2.31VμçÑ1 Îó2î0.1V
TIM8->CCR4 = 0;
while(1)
{
cnt4 = TIM4->CNT/4;//»ñ衼ÆêyÖμ
CNT4_last = CNT4; //
CNT4_temp = (rcnt4-rcnt4_last) * prd + cnt4; //rcnt2êDZàÂëÆ÷μĶ¨ê±Æ÷òç3ö′Îêy
V4 = (CNT4_temp - CNT4_last)*3.1415926*125/1000; //V2êÇò»¶¨ê±¼äÄú1a±àêμ¼êêÕμ½μÄpwmÖüÆúêy
if(CNT4_temp>1000)
{
CNT4 = CNT4_temp-1000;
}
else CNT4 = CNT4_temp;
rcnt4_last = rcnt4;
cnt3 = TIM2->CNT/4;//»ñ衼ÆêyÖμ
CNT3_last = CNT3; //
CNT3_temp = (rcnt3-rcnt3_last) * prd + cnt3; //rcnt2êDZàÂëÆ÷μĶ¨ê±Æ÷òç3ö′Îêy
V3 = (CNT3_temp - CNT3_last)*3.1415926*125/1000; //V2êÇò»¶¨ê±¼äÄú1a±àêμ¼êêÕμ½μÄpwmÖüÆúêy
if(CNT3_temp>1000)
{
CNT3 = CNT3_temp-1000;
}
else CNT3 = CNT3_temp;
rcnt3_last = rcnt3;
// printf("V4 ");
// printf("V4 = %d
,TIM4->CNT=%d
£¬rcnt4=%d
,CNT4_last=%d
,CNT4_temp=%d
£¬rcnt4_last=%d
,CNT4=%d
",V4,TIM4->CNT/4,rcnt4,CNT4_last,CNT4_temp,rcnt4_last,CNT4);
// printf("V3 = %d
,TIM2->CNT=%d
£¬rcnt3=%d
,CNT3_last=%d
,CNT3_temp=%d
,rcnt3_last=%d
,CNT3=%d
",V3,TIM2->CNT/4,rcnt3,CNT3_last,CNT3_temp,rcnt3_last,CNT3);
printf("V4 = %d
,V3 = %d
",V4,V3);
delay_ms(5000);//ÿ¸ô1s′òó¡ò»′αàÂëÆ÷½Ç¶è,óÃêÖè¥2|¶ˉ±àÂëÆ÷ ê1ÆäÂyËùDy×a
}
}
外部中断函数
#include "exti.h"
#include "nvic.h"
#include "led.h"
#include "sys.h"
#include "delay.h"
//ZÏà1éáã
void EXTI_PB1_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
/* config the extiline(PA1) clock and AFIO clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB| RCC_APB2Periph_AFIO,ENABLE);
/* config the NVIC(PA1) */
NVIC_Config(1);
/* EXTI line gpio config(PA1) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // éÏà-êäèë
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* EXTI line(PA1) mode config */
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource1);
EXTI_InitStructure.EXTI_Line = EXTI_Line1;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //ϽμÑØÖD¶Ï
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
void EXTI_PB2_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
/* config the extiline(PA1) clock and AFIO clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB| RCC_APB2Periph_AFIO,ENABLE);
/* config the NVIC(PA1) */
NVIC_Config(2);
/* EXTI line gpio config(PA1) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // éÏà-êäèë
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* EXTI line(PA1) mode config */
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource2);
EXTI_InitStructure.EXTI_Line = EXTI_Line2;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //ϽμÑØÖD¶Ï
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
编码器定时器设置函数
#include "stm32f10x.h"
#include "encode.h"
#include "misc.h"
#include "nvic.h"
#include "sys.h"
#include "delay.h"
void TIM4_Mode_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
GPIO_PinRemapConfig(GPIO_Remap_TIM4, ENABLE);
//PB6 ch1 A,PB7 ch2
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);//ê1ÄüTIM4ê±Öó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);//ê1ÄüGPIODê±Öó
GPIO_StructInit(&GPIO_InitStructure);//½«GPIO_InitStructÖDμÄ2Îêy°′è±ê¡Öμêäèë
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//PA6 PA7¸¡¿Õêäèë
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
NVIC_Config(4);
TIM_DeInit(TIM4);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = 999*4; //é趨¼ÆêyÆ÷ÖØ×°Öμ TIMx_ARR = 999*4
TIM_TimeBaseStructure.TIM_Prescaler = 0; //TIM3ê±ÖóÔ¤·ÖÆμÖμ
TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV1 ;//éèÖÃê±Öó·Ö¸î T_dts = T_ck_int
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòéϼÆêy
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_BothEdge ,TIM_ICPolarity_BothEdge);//ê1óñàÂëÆ÷Ä£ê½3£¬éÏéyÏÂ½μ¶¼¼Æêy
TIM_ICStructInit(&TIM_ICInitStructure);//½«½á11ìåÖDμÄÄúèYè±ê¡êäèë
TIM_ICInitStructure.TIM_ICFilter = 6; //Ñ¡Ôñêäèë±è½ÏÂË2¨Æ÷
TIM_ICInit(TIM4, &TIM_ICInitStructure);//½«TIM_ICInitStructureÖDμÄÖ¸¶¨2Îêy3õê¼»ˉTIM3
// TIM_ARRPreloadConfig(TIM4, ENABLE);//ê1ÄüԤװÔØ
TIM_ClearFlag(TIM4, TIM_FLAG_Update);//Çå3yTIM3μĸüD±ê־λ
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);//ÔËDD¸üDÂÖD¶Ï
//Reset counter
TIM4->CNT = 0;//
TIM_Cmd(TIM4, ENABLE); //Æô¶ˉTIM4¶¨ê±Æ÷
}
void TIM8_Configuration(void)//TIM8ËÄí¨μà·Ö±eêä3ö
WM1/PWM2/PWM3/PWM4,¶Ôó|òy½ÅÎaPC6/PC7/PC8/PC9
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 9999;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC1Init(TIM8, &TIM_OCInitStructure);
TIM_OC2Init(TIM8, &TIM_OCInitStructure);
TIM_OC3Init(TIM8, &TIM_OCInitStructure);
TIM_OC4Init(TIM8, &TIM_OCInitStructure);
TIM8->CCR1 = 0; //PC6éÏàíÂÛÄüêä3ö1.65VμçÑ1 Îó2î0.1V
TIM8->CCR2 = 0; //PC7éÏàíÂÛÄüêä3ö1.98VμçÑ1 Îó2î0.1V
TIM8->CCR3 = 0; //PC8éÏàíÂÛÄüêä3ö2.31VμçÑ1 Îó2î0.1V
TIM8->CCR4 = 0; //PC9éÏàíÂÛÄüêä3ö2.64VμçÑ1 Îó2î0.1V
TIM_Cmd(TIM8, ENABLE);
TIM_CtrlPWMOutputs(TIM8, ENABLE);
}
void TIM2_Mode_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
//PB6 ch1 A,PB7 ch2
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//ê1ÄüTIM4ê±Öó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//ê1ÄüGPIODê±Öó
GPIO_StructInit(&GPIO_InitStructure);//½«GPIO_InitStructÖDμÄ2Îêy°′è±ê¡Öμêäèë
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//PA6 PA7¸¡¿Õêäèë
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
NVIC_Config(3);
TIM_DeInit(TIM3);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = 999*4; //é趨¼ÆêyÆ÷ÖØ×°Öμ TIMx_ARR = 999*4
TIM_TimeBaseStructure.TIM_Prescaler = 0; //TIM3ê±ÖóÔ¤·ÖÆμÖμ
TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV1 ;//éèÖÃê±Öó·Ö¸î T_dts = T_ck_int
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòéϼÆêy
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12, TIM_ICPolarity_BothEdge ,TIM_ICPolarity_BothEdge);//ê1óñàÂëÆ÷Ä£ê½3£¬éÏéyÏÂ½μ¶¼¼Æêy
TIM_ICStructInit(&TIM_ICInitStructure);//½«½á11ìåÖDμÄÄúèYè±ê¡êäèë
TIM_ICInitStructure.TIM_ICFilter = 6; //Ñ¡Ôñêäèë±è½ÏÂË2¨Æ÷
TIM_ICInit(TIM2, &TIM_ICInitStructure);//½«TIM_ICInitStructureÖDμÄÖ¸¶¨2Îêy3õê¼»ˉTIM3
// TIM_ARRPreloadConfig(TIM4, ENABLE);//ê1ÄüԤװÔØ
TIM_ClearFlag(TIM2, TIM_FLAG_Update);//Çå3yTIM3μĸüD±ê־λ
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);//ÔËDD¸üDÂÖD¶Ï
//Reset counter
TIM2->CNT = 0;//
TIM_Cmd(TIM2, ENABLE); //Æô¶ˉTIM4¶¨ê±Æ÷
}
void TIM_Init(void)
{
TIM4_Mode_Config();
TIM8_Configuration();
TIM2_Mode_Config();
}
:'(:'(
中断执行函数定时器设置函数
//ía2¿ÖD¶Ï1£¬±àÂëÆ÷ZÏà1éáã óÅÏ輶--¢ù 0 0
void EXTI1_IRQHandler(void)
{
TIM4->CNT = 0;//ÿ′Îóöμ½Ïà¶Ôáã(ZDÅoÅ)¾í½«¼Æêy1é0
TIM_Cmd(TIM4, ENABLE);
if (TIM4 -> CR1 & 0X0010) //D¡DÄ×¢òa
{
rcnt4 -= 1;
}
else rcnt4 += 1;
// LED1=!LED1; //LED0ìáê¾½øèëÖD¶Ï
EXTI_ClearITPendingBit(EXTI_Line1);
}
void EXTI2_IRQHandler(void)
{
TIM2->CNT = 0;//ÿ′Îóöμ½Ïà¶Ôáã(ZDÅoÅ)¾í½«¼Æêy1é0
TIM_Cmd(TIM2, ENABLE);
if (TIM2 -> CR1 & 0X0010) //D¡DÄ×¢òa
{
rcnt3 -= 1;
}
else rcnt3 += 1;
// LED0=!LED0; //LED0ìáê¾½øèëÖD¶Ï
EXTI_ClearITPendingBit(EXTI_Line2);
}
一周热门 更多>