#include "stm32f10x.h"
#include "led.h"
#include "time.h"
#include "delay.h"
#include "LCD_GUI.H"
#include "ili93xx.H"
#include "key.h"
#include "AT24CXX.h"
#include "num-str.h"
#include "userfunction.h"
//u16 meaning()
//{
// u8 i=0;
// u16 sum=0;
// u16 *pdat;
// //???®??????????
// for(i=0;i<10;i++)
// {
// *pdat = Get_Value();
// sum=sum+(*pdat);
//
// }
// return sum/10;
//
//}
int main(void)
{
// u32 oldcnt;
u8 i=0;
u8 j=0;
u16 sum=0;
u16 sum1=0;
u16 *pdat;
u16 *pdatt;
u16 *pdatt1;
u16 *pdatt2;
u8 test_buf[30];
u8 test_buf1[30];
u8 test_buf00[30];
u8 test_buf11[30];
u8 tmd[10]={0};
s32 PID_Speed;
s32 PID_Speed1;
delay_init(72);
LCD_Init();
LED_Init(); //????????LED??????????????
Key_Init();
AT24CXX_Init();
TIM6_Int_Init(9990,7199); //10Khz????????????????10K????1s
TIM3_CH2_Excnt_Init(0xffff,0);
TIM2_CH2_Excnt_Init(0xffff,0);
//mean=meaning();
pdat = Get_Value();
// if(i<=10)
// {
// tmd[i++]=*pdat;
// sum=+tmd[i++];
// }
pdatt= (u16*)Get_Valuee();
// if(j<=10)
// {
// tmd[j++]=*pdat;
// sum=+tmd[j++];
// }
// pdatt1= (u16*)Get_Valuee1();
// pdatt2= (u16*)Get_Valuee2();
// for(j=0;j<10;j++)
// {
// tmd[j]= *pdat;
// sum=sum+tmd[j];
// }
//
while(1)
{
PID_Speed = User_PidSpeedControl1(100,*pdat);
PID_Speed1 = User_PidSpeedControl1(100,*pdatt);
delay_ms(1000);
// for(j=0;j<10;j++)
// {
// Number_TO_String(test_buf,sum);
// LCD_Dis_String(30,40," ",0xf800,0xffff);
// LCD_Dis_String(30,40,test_buf,0xf800,0xffff);
// }
Number_TO_String(test_buf,*pdatt);
LCD_Dis_String(30,40," ",0xf800,0xffff);
LCD_Dis_String(30,40,test_buf,0xf800,0xffff);
Number_TO_String(test_buf11,PID_Speed);
LCD_Dis_String(30,80," ",0xf800,0xffff);
LCD_Dis_String(30,80,test_buf11,0xf800,0xffff);
Number_TO_String(test_buf1,*pdat);
LCD_Dis_String(120,40," ",0xf800,0xffff);
LCD_Dis_String(120,40,test_buf1,0xf800,0xffff);
Number_TO_String(test_buf00,PID_Speed1);
LCD_Dis_String(120,80," ",0xf800,0xffff);
LCD_Dis_String(120,80,test_buf00,0xf800,0xffff);
}
}
// 定时器配置 外部脉冲计数
#include "time.h"
#include "led.h"
#include "stm32f10x.h"
static u16 count=0;
static u16 countt=0;
//static u16 sum =0;
//static u16 sum1=0;
//static u16 c=0;
u16 *Get_Value()
{
return &count;
}
u16 *Get_Valuee()
{
return &countt;
}
//u16 *Get_Valuee1()
//{
// if(c>=10)
// {
// c=0;
// return &sum;
// }
//}
//u16 *Get_Valuee2()
//{
// if(c>=10)
// {
// c=0;
// return &sum1;
// }
//}
/**
* @Brief ?¨?±?÷6????·??????ò
* @Input none;
* @Output none;
* @Return none;
*
*
*/
void TIM6_IRQHandler(void)
{
TIM6->SR &= ~(1<<0); //????????±ê??
LED4 = !LED4;
count=TIM3->CNT ;
countt=TIM2->CNT ;
// if (c<=10)
// {
// c++;
// sum=sum+count ;
// sum1=sum1+countt ;
// }
//
TIM3->CNT = 0;
TIM2->CNT = 0;
// if(count >=10000) count = 1;
}
/*************************************************************************
?? ?? ?? : TIM6_Int_Init
???????? : ?¨???¨?±?÷6 ??????????
???????? : arr??×?????×°???? psc???±???¤·?????
???????? : ??
·? ?? ?? : ??
************************************************************************/
void TIM6_Int_Init(u16 arr,u16 psc)
{
//u8 priority;
RCC->APB1ENR |= 1<<4; //TIM6?±??????
TIM6->ARR = arr; //?è?¨?????÷×?????×°??//????1ms
TIM6-&gtSC = psc; //?¤·????÷7200,????10Khz???????±??
//?????±????????APB1??2±?????APB1??36M
TIM6->DIER |= 1<<0; //???í?ü??????
TIM6->CR1 |= 0x01; //?????¨?±?÷3
/* ???????? */
NVIC_EnableIRQ(TIM6_IRQn); //NVIC_EnableIRQ()????????????????????
}
void TIM2_CH2_Excnt_Init(u16 arr,u16 psc)
{
RCC->APB1ENR|=1<<0; //TIM2
RCC->APB2ENR|=1<<2; //PORTA
GPIOA->CRL&=0XFFFFFF0F; //PA1
GPIOA->CRL|=0X00000080; //PA1
GPIOA->ODR|=1<<1; //PA1
TIM2->ARR=arr; //1
TIM2-&gtSC=psc; //2
TIM2->CCMR1|=1<<8; //CC2S=01
TIM2->CCMR1|=0<<12; //IC2F=0000
TIM2->CCER|=1<<5; //CC1P=0
TIM2->SMCR|=6<<4; //(TI2FP2)
TIM2->SMCR|=7<<0; //1
TIM2->CR1|=1<<0; //,CEN=1
}
void TIM3_CH2_Excnt_Init(u16 arr,u16 psc)
{
RCC->APB1ENR|=1<<1; //TIM3
RCC->APB2ENR|=1<<2; //PORTA
GPIOA->CRL&=0X0FFFFFFF; //PA7
GPIOA->CRL|=0X80000000; //PA7
GPIOA->ODR|=1<<7; //PA7
TIM3->ARR=arr; //
TIM3-&gtSC=psc; //
TIM3->CCMR1|=1<<8; //CC2S=01
TIM3->CCMR1|=0<<12; //IC2F=0000
TIM3->CCER|=1<<5; //CC1P=0
TIM3->SMCR|=6<<4; //(TI2FP2)
TIM3->SMCR|=7<<0; //
TIM3->CR1|=1<<0; //CEN=1
}
//PID 程序
#include "stm32f10x.h"
#include "UserFunction.h"
//#include "UserConfiguration.h"
//extern s32 CNT2,CNT3,CNT4,CNT5,V2,V3,V4,V5;
//??????PID??·¨
void PID_AbsoluteMode(PID_AbsoluteType* PID)
{
if(PID->kp < 0) &nbspID->kp = -PID->kp;
if(PID->ki < 0) &nbspID->ki = -PID->ki;
if(PID->kd < 0) &nbspID->kd = -PID->kd;
if(PID->errILim < 0) &nbspID->errILim = -PID->errILim;
&nbspID->errP = PID->errNow; //???????????ó????????kp????
&nbspID->errI += PID->errNow; //?ó????·???????ki????
if(PID->errILim != 0) //??·???????????
{
if( PID->errI > &nbspID->errILim) PID->errI = PID->errILim;
else if(PID->errI < -PID->errILim) PID->errI = -PID->errILim;
}
PID->errD = PID->errNow - PID->errOld;//?ó????·???????kd????
PID->errOld = PID->errNow; //±??????????ó??
PID->ctrOut = PID->kp * PID->errP + PID->ki * PID->errI + PID->kd * PID->errD;//??????????PID????
}
/*******************************************************************************************************/
//??????PID??·¨
void PID_IncrementMode(PID_IncrementType* PID)
{
float dErrP, dErrI, dErrD;
if(PID->kp < 0) PID->kp = -PID->kp;
if(PID->ki < 0) PID->ki = -PID->ki;
if(PID->kd < 0) PID->kd = -PID->kd;
dErrP = PID->errNow - PID->errOld1;
dErrI = PID->errNow;
dErrD = PID->errNow - 2 * PID->errOld1 + PID->errOld2;
PID->errOld2 = PID->errOld1; //???×?ó????·?
PID->errOld1 = PID->errNow; //???×?ó????·?
/*??????PID????*/
PID->dCtrOut = PID->kp * dErrP + PID->ki * dErrI + PID->kd * dErrD;
if(PID->kp == 0 && PID->ki == 0 && PID->kd == 0) PID->ctrOut = 0;
else PID->ctrOut += PID->dCtrOut;
}
/*****************************************???ú?????·??·?***********************************************/
s32 spdTag, spdNow, control;//?¨????????±ê?????????ù????????????
PID_AbsoluteType PID_Control;//?¨??PID??·¨???á????
//void User_PidSpeedControl(s32 SpeedTag)
//{
// spdNow = V2; spdTag = SpeedTag;
// PID_Control.errNow = spdTag - spdNow; //???????????????ó??
//
// PID_Control.kp = 15; //????±?????????15
// PID_Control.ki = 5; //??????·???????5
// PID_Control.kd = 5; //??????·???????5
// PID_Control.errILim = 1000; //?????ó????·???????1000 ??????-1000
// PID_AbsoluteMode(&PID_Control); //??????????PID??·¨
//
// control = PID_Control.ctrOut; //??????????
//
// //UserMotorSpeedSetOne(control); //·???PWM??????????????????????
//}
s32 User_PidSpeedControl1(s32 SpeedTag,u32 SpeedNow)
{
spdNow = SpeedNow; spdTag = SpeedTag;
PID_Control.errNow = spdTag - spdNow; //???????????????ó??
PID_Control.kp = 10; //????±?????????15
PID_Control.ki = 5; //??????·???????5
PID_Control.kd = 5; //??????·???????5
PID_Control.errILim = 1000; //?????ó????·???????1000 ??????-1000
PID_AbsoluteMode(&PID_Control); //??????????PID??·¨
control = PID_Control.ctrOut; //??????????
return control;
//UserMotorSpeedSetOne(control); //·???PWM??????????????????????
} |
|