?? 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 + -