📄 a_dc.c
字号:
/* ===================================================================== */
/* File name : DC.C */
/* Author: JZMING */
/* Data: 2006.8.20 */
/* Version: 1.0 */
/* Organization: DSP MOTO CONTROL PROGROM
/* Zhejiang Tianhuang Techonology Industry Ltd., */
/* Description : C-PROGRAM TO CONTROL A DC USE F2407A */
/* ================================================================== */
/* 头文件 */
#include "TYPEDEFS.H"
#include "F2407_c.H"
#include "CONSTANT.H"
#include "self_define.h"
#include <stdio.h>
#include <math.h>
/* ================================================================== */
/* 函数申明 */
void delay (WORD ms);
void var_init(void);
void speed_pid_ini(void);
void cur_pid_ini(void);
void led_shine();
/* ================================================================== */
/* 加密区 */
asm(" .word #0ffffh");
asm(" .word #0ffffh");
asm(" .word #0ffffh");
asm(" .word #0ffffh");
/*=================================================================== */
/* 定义全局变量 */
WORD nor_flag=0,speed_counter=0,test_counter=250;
WORD current_counter=0;
WORD capt; //hall位置值,脉冲的占空比值
unsigned char direction_flag=0;
int temp_counter=0,temp_counter1=0,temp_counter2=0;
float n_feed=0,pulse_counter=0; //必须双精度数
float adc_comp=0; //必须双精度数
extern int process_point, set_point,dead_band;
extern float p_gain, i_gain, d_gain, integral_val,new_integ,result;
extern struct _pid warm,*pid;
extern unsigned int sci_buff[2];
extern int speed_err,current_given;
extern int current_err,current_uref;
extern int speed_kp,speed_ki,speed_sum;
extern int current_kp,current_ki,current_sum;
struct _Speed_pid speed_pid;
struct _Current_pid current_pid;
int u=0;
/* =============================================================== */
/* 中断屏蔽子程序 */
void inline disable()
{
asm(" setc INTM");
}
/* =============================================================== */
/* 中断使能子程序 */
void inline enable()
{
asm(" clrc INTM");
}
/* =============================================================== */
/* 主函数 */
main()
{
disable(); //中断屏蔽
sytem_init(); //系统时钟建立
spi_init(); //SPI初始化
io_init(); //I/O口初始化
var_init(); //变量初始化
ev_init(); //事件管理器初始化
sci_init(); //串口初始化
adc_init();
current_counter = CUR_COUNTER_CON; //电流PI循环次数
speed_counter = SPEED_COUNTER_CON; //速度PI循环次数
*T1CON = *T1CON | 0x0042; //开启定时器1
enable(); //中断使能
*ADCTRL2 = 0x2000; //软件启动ADC
speed_kp=100;
speed_ki=40;
speed_sum=0;
current_kp=10;
current_ki=5;
current_sum=0;
while(1)
{
}
}
/* ================================================================= */
/* 速度调节器变量装载子程序 */
void speed_pid_ini(void)
{
p_gain = speed_pid.SPEED_KP_CON;
i_gain = speed_pid.SPEED_KI_CON;
d_gain = speed_pid.SPEED_KD_CON;
dead_band = speed_pid.SPEED_DEAD_BAND;
integral_val = speed_pid.SPEED_INTER_VAR;
}
/* ================================================================= */
/* 电流调节器变量装载子程序 */
void cur_pid_ini(void)
{
p_gain = current_pid.CUR_KP_CON;
i_gain = current_pid.CUR_KI_CON;
d_gain = current_pid.CUR_KD_CON;
dead_band = current_pid.CUR_DEAD_BAND;
integral_val = current_pid.CUR_INTER_VAR;
}
/* ================================================================= */
/* 电流采样子程序 */
void cur_sample(void)
{
WORD adc_ia,adc_ib;
adc_ia = *RESULT0 >> 6; //是周期中断,电流采样
adc_ia += adc_ia_ref; //修正电流参数
adc_comp = (float)adc_ia-512;
*ADCTRL2 = 0x4000; //ADC指针复位
*ADCTRL2 = 0x2000; //软件立即启动ADC
}
/* ==================================================================*/
/* 定时器1周期中断子程序 */
void interrupt t1_perint() //定时器1周期中断----测试用
{
WORD flag;
int temp_value;
KICK_DOG;
cur_sample();
/* u++;
if(u>100)
{
*SCITXBUF=((int)n_feed);
u=0;
}*/
if (speed_pid.n_ref >=0) direction_flag=0; //正转标志
else if (speed_pid.n_ref <0) direction_flag=1; //反转标志
flag = *EVAIFRA & 0x0080;
if (flag != 0x0080) //如果不是周期中断,退出
{
enable();
return;
}
temp_value=(sci_buff[0]*256)+sci_buff[1]; //串口发送的值
speed_pid.n_ref = (float)temp_value; //速度给定值
// -------------- 速度计算PID调节部分开始 -------------------------
speed_counter--;
if (speed_counter ==0)
{
temp_counter1 = *T4CNT;
temp_counter=temp_counter1-temp_counter2;
temp_counter2=temp_counter1;
//*T4CNT = 0;
pulse_counter = (float)temp_counter;
n_feed = pulse_counter*1000*60/4096; //计算反馈的转速
speed_counter = SPEED_COUNTER_CON; //赋初值
pulse_counter=speed_pid.n_ref-n_feed;
speed_err=pulse_counter;
speed_pi_on();
// *CMPR1 = 500-current_given; //更新占空比
// *CMPR2 = 500-current_given;
}
//----------------- 速度计算PID调节部分结束 -----------------------
/*--------------- 电流采样PID调节部分开始 ------------------------ */
current_counter--;
if (1)
{
current_counter = CUR_COUNTER_CON; //赋初值
//电流采样计算
current_err=current_given-adc_comp;
// if (current_err<10&¤t_err>-10)
// current_err=0;
current_cal_pi();
if (direction_flag == 0) //正转
{
if(current_uref > 500) current_uref = 500;
if (current_uref < 0) current_uref = 0;
}
else if (direction_flag ==1) //反转
{
if (current_uref > 0) current_uref = 0;
if (current_uref <- 500) current_uref = -500;
}
// current_pid.cur_feedback = adc_comp; //电流反馈值
//current_pid.cur_pid_out=10.152;
*CMPR1 = 500-current_uref; //更新占空比
*CMPR2 = 500-current_uref;
}
//--------------- 电流采样PID调节部分结束 -------------------------
// data_save_dram(); //数据保存至双口RAM中
// led_shine(); //闪灯子程序,作测试用!!!
*EVAIFRA = 0X0080; //清中断标志,以进行下次中断
enable();
return;
}
/* =================================================================== */
/* 空中断子程序 */
void interrupt nothing()
{
enable();
return;
}
/* =================================================================== */
/* 变量初始化子程序 */
void var_init(void)
{
speed_pid.n_ref = 0;
speed_pid.n_feedback = 0;
capt = 0;
current_pid.cur_pid_out = 0;
p_gain = (float)0;
i_gain = (float)0;
d_gain = (float)0;
integral_val = (float)0;
new_integ = 0;
// speed_pid.SPEED_KP_CON = (float) 0.15;
// speed_pid.SPEED_KI_CON = (float) 0.0003;
speed_pid.SPEED_KP_CON = (float) 0.05;
speed_pid.SPEED_KI_CON = (float) 0.01;
speed_pid.SPEED_KD_CON = (float) 0.18;
speed_pid.SPEED_INTER_VAR = (float) 0.01;
speed_pid.SPEED_DEAD_BAND = 0;
speed_pid.SPEED_PID_OUT_MAX_CON = 0X0080;
speed_pid.SPEED_PID_OUT_MIN_CON = 0XFF80;
current_pid.CUR_KP_CON = (float) 0.1;
current_pid.CUR_KI_CON = (float) 0.0003;
current_pid.CUR_KD_CON = (float) 0.18;
// current_pid.CUR_KP_CON = (float) 0.15;
// current_pid.CUR_KI_CON = (float) 0.0003;
// current_pid.CUR_KD_CON = (float) 0.18;
current_pid.CUR_INTER_VAR = (float) 0.01;
current_pid.CUR_DEAD_BAND = 0;
current_pid.CUR_PID_OUT_MAX_CON = 0X4B0;
current_pid.CUR_PID_OUT_MIN_CON =0XFB50;
}
/* ==================================================================== */
/* 闪灯子程序,测试用!!!! */
void led_shine()
{
nor_flag = ~nor_flag;
if (nor_flag)
{
asm( " clrc xf");
delay(100);
}
else
{
asm( " setc xf");
delay(100);
}
}
/* ==================================================================== */
/* 延时子程序,测试用!!!!! */
void delay (unsigned int ms)
{
WORD i,j,k;
KICK_DOG;
for (i=0;i<=ms;i++)
{
for(j=0;j<200;j++)
{
k++;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -