📄 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 led_shine();
/* ================================================================== */
/* 加密区 */
asm(" .word #0ffffh");
asm(" .word #0ffffh");
asm(" .word #0ffffh");
asm(" .word #0ffffh");
/*=================================================================== */
/* 定义全局变量 */
WORD nor_flag=0,speed_counter=0;
WORD current_counter=0;
unsigned char direction_flag=0;
int adc_vdc=0;
extern unsigned int sci_buff[2];
extern int n_ref,t23;
extern int cmp1,cmp2,cmp3;
extern int ia,ib,ic;
extern int temp;
int u=0;
/* =============================================================== */
int adc_ia_ref=0,adc_ib_ref=0,adc_ic_ref=0,adc_vdc_ref=0;
int current_sam_count=0,current_sam_flag=0;
int adc_comp_a_2=0,adc_comp_b_2=0,adc_comp_c_2=0,adc_vdc_2=0;
int adc_comp_a_1=0,adc_comp_b_1=0,adc_comp_c_1=0,adc_vdc_1=0;
int adc_temp=0;
/* =============================================================== */
/* 中断屏蔽子程序 */
void inline disable()
{
asm(" setc INTM");
}
/* =============================================================== */
/* 中断使能子程序 */
void inline enable()
{
asm(" clrc INTM");
}
/* =============================================================== */
/* 主函数 */
main()
{
KICK_DOG;
*ACTRA = 0x0fff;
disable(); //中断屏蔽
sytem_init(); //系统时钟建立
spi_init(); //SPI初始化
io_init(); //I/O口初始化
ev_init(); //事件管理器初始化
// sci_init(); //串口初始化
adc_init();
// cap_init(); //cap初始化
variable_initial();
current_counter = CUR_COUNTER_CON; //电流PI循环次数
speed_counter = SPEED_COUNTER_CON; //速度PI循环次数
*T1CON = *T1CON|0x0040; //开启定时器1
enable(); //中断使能
*ADCTRL2 = 0x2000; //软件启动ADC
Double_ram_ini(); //初始化,往设定参数页地址置初值
while(1)
{
KICK_DOG; //watchdog,must require!!!
//var_query_dram();
var_set_dram();
}
}
/* ================================================================= */
/* 电流采样子程序 */
void cur_sample(void)
{
if(current_sam_flag==1)
{
adc_comp_b_1 = *RESULT0 >> 6; //是周期中断,电流采样
adc_comp_b_1 = adc_comp_b_1-adc_ib_ref;
adc_comp_c_1 = *RESULT1 >> 6; //是周期中断,电流采样
adc_comp_c_1 = adc_comp_c_1-adc_ic_ref;
adc_vdc_1 = *RESULT2 >> 6; //是周期中断,电流采样
adc_vdc_1 = adc_vdc_1-adc_vdc_ref;
*ADCTRL2 = 0x4000; //ADC指针复位
*ADCTRL2 = 0x2000;
}
adc_vdc=(adc_vdc_1+adc_vdc_2)>1;
if(cmp2>500)
ib=adc_comp_b_2;
else
ib=adc_comp_b_1;
if(cmp3>500)
ic=adc_comp_c_2;
else
ic=adc_comp_c_1;
//软件立即启动ADC
}
/* ==================================================================*/
/* 定时器1周期中断子程序 */
void interrupt t1_perint() //定时器1周期中断----测试用
{
int flag;
flag = *EVAIFRA & 0x0280;
if (flag == 0x0200) //如果不是周期中断,退出
{
if(current_sam_flag==1)
{
adc_comp_b_2 = *RESULT0 >> 6; //是周期中断,电流采样
adc_comp_b_2 = adc_comp_b_2-adc_ib_ref;
adc_comp_c_2 = *RESULT1 >> 6; //是周期中断,电流采样
adc_comp_c_2 = adc_comp_c_2-adc_ic_ref;
adc_vdc_2 = *RESULT2 >> 6; //是周期中断,电流采样
adc_vdc_2 = adc_vdc_2-adc_vdc_ref;
*ADCTRL2 = 0x4000; //ADC指针复位
*ADCTRL2 = 0x2000;
}
else
{
current_sam_count++;
if(current_sam_count>2000)
{
if(current_sam_count>2008)
{
adc_ib_ref=adc_ib_ref>>3;
adc_ic_ref=adc_ic_ref>>3;
adc_vdc_ref=adc_vdc_ref>>3;
current_sam_count=0;
current_sam_flag=1;
}
else
{
adc_temp = *RESULT0 >> 6; //是周期中断,电流采样
adc_ib_ref = adc_ib_ref+adc_temp;
adc_temp = *RESULT1 >> 6; //是周期中断,电流采样
adc_ic_ref = adc_ic_ref+adc_temp;
adc_temp = *RESULT2 >> 6; //是周期中断,电流采样
adc_vdc_ref = adc_vdc_ref+adc_temp;
*ADCTRL2 = 0x4000; //ADC指针复位
*ADCTRL2 = 0x2000;
}
}
}
*EVAIFRA = 0X0200;
}
else
{
cur_sample();
led_show();
if (n_ref >=0) direction_flag=0; //正转标志
else if (n_ref <0) direction_flag=1; //反转标志
flag = *EVAIFRA & 0x0080;
if (flag != 0x0080) //如果不是周期中断,退出
{
asm( " clrc INTM");
return;
}
// -------------- 速度计算PID调节部分开始 -------------------------
t23=*T2CNT;
asy_motor_control();
*CMPR1=cmp1;
*CMPR2=cmp2;
*CMPR3=cmp3;
//----------------- 速度计算PID调节部分结束 -----------------------
/*--------------- 电流采样PID调节部分开始 ------------------------ */
//--------------- 电流采样PID调节部分结束 -------------------------
// data_save_dram(); //数据保存至双口RAM中
// led_shine(); //闪灯子程序,作测试用!!!
*EVAIFRA = 0X0080; //清中断标志,以进行下次中断
}
asm( " clrc INTM");
return;
}
/*=================================================================== */
/* 空中断子程序 */
void interrupt nothing()
{
enable();
return;
}
/* ==================================================================== */
/* 闪灯子程序,测试用!!!! */
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 + -