⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 基于dsp2812的温控 带AD校准和PWM输出
💻 C
字号:
/**********************************************************************
* File: Main.c -- File for practice
* Devices: TMS320F2812, TMS320F2811, TMS320F2810
* Author: shengtao
* History:
***********************************************************************/

#include "DSP281x_Device.h"				// Peripheral address definitions
//#include "Tec.h"

/**********************************************************************
* Function: main()
*
* 
**********************************************************************/
int ResultA0,ResultB0;
Uint16 adcount;
PID	TEC1;
Uint16 iadc;
Uint16 HighActualCount;
Uint16 LowActualCount;
void calerror(void);
extern Uint16 Avg_HighActualCount;
extern Uint16 Avg_LowActualCount;
extern Uint16 Sum_HighActualCount;
extern Uint16 Sum_LowActualCount;
extern float CalGain;
extern float CalOffset;

void main(void)
{
/*** Initialization ***/
	int i;
	DINT;          //
	// Disable CPU interrupts and clear all CPU interrupt flags:
	IER = 0x0000;  //
   	IFR = 0x0000;  //
	InitSysCtrl();						// Initialize the CPU (FILE: SysCtrl.c)
	InitPieCtrl();						// Enable the PIE (FILE: PieCtrl.c)
	InitPieVectTable();
	InitGpio();							// Initialize the shared GPIO pins (FILE: Gpio.c)
	EALLOW;	// This is needed to write to EALLOW protected registers
	PieVectTable.ADCINT=&ADCINT_ISR;
	EDIS;   // This is needed to disable write to EALLOW protected registers
	InitAdc();							// Initialize the ADC (FILE: Adc.c)
	InitEv();
	Initadcalib();
	// Initialize GPAMUX and GPBMUX 
		   EALLOW;
		   // Enable PWM pins
		   GpioMuxRegs.GPAMUX.all = 0x00FF; // EVA PWM 1-6  pins
		   GpioMuxRegs.GPBMUX.all = 0x00FF; // EVB PWM 7-12 pins
		   EDIS;
    // 使能ADC中断: PIE-Group1 , interrupt 6
	//	PieCtrlRegs.PIEIER1.bit.INTx6 = 1;	                    
	
	// 使能 CPU INT1 (连接到CPU-Timer 0中断) 
    //IER = 1;
	iadc = 0;
    	/*** Setup the TEC1 parameters ***/
	TEC1.r = VREF;						// Reference setpoint (0-3.0 volts)
	TEC1.ui1 = 0.0;						// Initialize ui1 to 0.0
	TEC1.e1 = 0.0;						// Initialize e1 to 0
	TEC1.y1 = 0;						// Initialize y1 to 0
	TEC1.y2 = 0;						// Initialize y2 to 0
	TEC1.y3 = 0;						// Initialize y3 to 0
	TEC1.y4 = 0;						// Initialize y4 to 0
	TEC1.u_max = PWM_period;			// Total control limit
	TEC1.ui_max = PWM_period;			// I-mode control limit
        TEC1.Kp = 750.0;					// Kp
    	TEC1.Ki = 0.4;						// Ki'
    	TEC1.Kd = 125000.0;					// Kd'
//    InitGptimer2(sample_period);		// Initialize timer2 (FILE: TecFunctions.c)
//	InitGptimer3(1500);
	InitPwm(PWM_period);				// Enable the PWM (FILE: TecFunctions.c)

	
	EALLOW;	// This is needed to write to EALLOW protected registers
	EvaRegs.CMPR1 = 0x0100;				
	EvaRegs.CMPR2 = 0x0200;
	EvaRegs.CMPR3 = 0x0030;
    EvbRegs.ACTRB.all = 0x0666;
    EvaRegs.ACTRA.all = 0x0999;
	EDIS;   // This is needed to disable write to EALLOW protected registers

	EALLOW;
	GpioMuxRegs.GPFMUX.bit.SPISOMIA_GPIOF1 = 0;	
	GpioMuxRegs.GPFDIR.bit.GPIOF1=1;//output
	GpioDataRegs.GPFDAT.bit.GPIOF1 = 1;	
	EDIS;

	EINT;   // Enable Global interrupt INTM
	ERTM;	// Enable Global realtime interrupt DBGM

	for(;;)
	{
		GpioDataRegs.GPFDAT.bit.GPIOF1 = 1;	
		for(i=0;i<1000;i++)
			DelayUs(500);
		GpioDataRegs.GPFDAT.bit.GPIOF1 = 0;	
		for(i=0;i<1000;i++)
			DelayUs(500);
	}
			
}

void calerror(void)
{

		//Uint16 k;
		HighActualCount=AdcRegs.ADCRESULT0>>4;//读采样数据
		LowActualCount =AdcRegs.ADCRESULT1>>4;
		if(iadc >= 40)
		    {
		    	ResultA0 = HighActualCount * CalGain - CalOffset;
				ResultB0 = LowActualCount * CalGain - CalOffset;
			}

		if(iadc >= SAMPLES)//采样满规定次数
		{
		
		Sum_HighActualCount += HighActualCount;
		Sum_LowActualCount += LowActualCount;
		if(iadc == 39 )
		{
		Avg_HighActualCount = Sum_HighActualCount/SAMPLES;//多次采样取平均值
		Avg_LowActualCount = Sum_LowActualCount/SAMPLES;

		CalGain = (float32)(HIGH_IDEAL_COUNT - LOW_IDEAL_COUNT)/(Avg_HighActualCount  - Avg_LowActualCount);//计算增益系数
		          
		CalOffset = (float32)Avg_LowActualCount*CalGain - LOW_IDEAL_COUNT; //计算偏置
		}
		}
			
		iadc++;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -