#include "DSP2833x_Device.h"     // DSP281x Headerfile Include File
#include "DSP2833x_SWPrioritizedIsrLevels.h" 
#include "filter_bat2.h"
#include "measure.h"

#include "RS485.h"
#include "message.h"
#include "DAC.h"
#include "package.h"
#include "tools.h"

#include "peripher.h"

#define dCLK_1()	GpioDataRegs.GPBSET.bit.GPIO63=1
#define dCLK_0()	GpioDataRegs.GPBCLEAR.bit.GPIO63=1
#define dCS_1()		GpioDataRegs.GPBSET.bit.GPIO59=1
#define dCS_0()		GpioDataRegs.GPBCLEAR.bit.GPIO59=1

void dOUT(int x)
{
	if(x)	GpioDataRegs.GPBSET.bit.GPIO60=1;
	else	GpioDataRegs.GPBCLEAR.bit.GPIO60=1;
}       

void wast()
{
  int i;

	for(i=0;i<25;i++);
}       

void dSEND(unsigned int word)
{    
  int i;

	dCS_0(); DSP28x_usDelay(1L); //4

	for(i=0;i<16;i++)
	{
		dCLK_1(); DSP28x_usDelay(1L);	//8
		dOUT((word & 0x8000)?1:0); word = word<<1; DSP28x_usDelay(1L);	//10;
		dCLK_0(); DSP28x_usDelay(1L); //30

	}
	dCS_1(); //DSP28x_usDelay(1L); //4
}       


/*
void dSEND_A(unsigned int word)
{    
	dSEND(0x9001);

	word = word & 0xFFF;
	word |= 0x8000;
	dSEND(word);
}

void dSEND_B(unsigned int word)
{    
	dSEND(0x9001);

	word = word & 0xFFF;
	dSEND(word);     
}       
*/

void Init_DAC(void)
{    
  unsigned int word;

//	word = 0x9001;
	word = 0x9002;
//	word = 0xD002;
	dSEND(0x9002);
//	word = 0x18FF;
//	dSEND(word);

}       

void Anal_output(long vrot, long maxx)
{
  long out;
//static int x;
	out = DAC_min() + vrot *  DAC_max() / maxx - vrot * DAC_min() / maxx ;
//x=!x;

	if(cCalibrDac)	out=DAC_cal();

	out = out & 0xFFF;

//	out = 0xFFF;
//	out |= 0x8000;

//	out |= 0x1000;
//	dSEND(out);     

//	out = 0xFFF;
//	dSEND(out|0x5000);     

//	out |= 0x8000;
//		dSEND(0x9002);

//if(x)	dSEND(out|0xC000);     
//else	dSEND(out|0x4000);    
 
//	if(Mode==adr_LOA1)//это для 163заказ
//	dSEND(out|0x4000); 
//	else 
	dSEND(out|0x8000);
	modbus[125] = out; 
   
//	dSEND(out|0xC000);     

}

