/Modules/TDC/GP201A/SW/PICinterface/main.c
1,10 → 1,11
#include "main.h"
 
#define VERSION 0.1
#define VERSION 0.2
 
#define START PIN_D4
#define STOP1 PIN_D5
#define STOP2 PIN_D6
 
#include "GP2.h"
 
#define ONE_WIRE_PIN PIN_E2
12,8 → 13,6
 
void main()
{
float temperature;
 
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_CLOCK_DIV_2);
setup_psp(PSP_DISABLED);
27,29 → 26,7
setup_vref(FALSE);
 
TDC_reset();
MRange=TDC_MRANGE2;
hitin1=TDC_HITIN1_1;
hitin2=TDC_HITIN2_0;
hit1=TDC_MRANGE2_HIT1_START;
hit2=TDC_MRANGE2_HIT2_1CH1;
en_int=(TDC_INT_ALU | TDC_INT_ENDHIT | TDC_INT_TIMEOUT);
en_err_val=TDC_ERRVAL_EN;
delval1=0x0;
delval2=0x0;
delval3=0x0;
calibrate=TDC_CALIBRATE_DIS;
disautocal=TDC_AUTOCAL_EN;
firenum=TDC_FIRENUM_1;
div_fire=TDC_DIV_FIRE_2;
Tcycle=TDC_TCYSLE_LONG;
portnum=TDC_TPORTNUM_2;
fakenum=TDC_TFAKENUM_2;
TDC_update_registers();
 
output_low(START);
output_low(STOP1);
output_low(STOP2);
56,92 → 33,215
 
delay_ms(50);
 
//write raw register values
/* output_low(TDC_ENABLE);
spi_xfer(TDC_stream,0x80338AE8,32);
output_high(TDC_ENABLE);
/*
1 0 0 0 0 ADR2 ADR1 ADR0 Write into address ADR
1 0 1 1 0 ADR2 ADR1 ADR0 Read from address ADR
0 1 1 1 0 0 0 0 Init
0 1 0 1 0 0 0 0 Power On Reset
0 0 0 0 0 0 0 1 Start_Cycle
0 0 0 0 0 0 1 0 Start_Temp
0 0 0 0 0 0 1 1 Start_Cal_Resonator
0 0 0 0 0 1 0 0 Start_Cal_TDC
*/
 
output_low(TDC_ENABLE);
spi_xfer(TDC_stream,0x81211400,32);
output_high(TDC_ENABLE);
output_low(TDC_ENABLE);
spi_xfer(TDC_stream,0x82E03200,32);
output_high(TDC_ENABLE);
int32 ble,ret32;
int16 ret16;
int8 ret8;
 
output_low(TDC_ENABLE);
spi_xfer(TDC_stream,0x83083300,32);
output_high(TDC_ENABLE);
 
output_low(TDC_ENABLE);
spi_xfer(TDC_stream,0x84203400,32);
output_high(TDC_ENABLE);
 
output_low(TDC_ENABLE);
spi_xfer(TDC_stream,0x85080000,32);
output_high(TDC_ENABLE);
*/
TDC_start_cal_resonator();
delay_ms(50);
printf("calibrate: %LX, %LX, %LX, %LX \r\n", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4));
// TDC_start_cal();
// delay_ms(50);
while(true)
while(TRUE)
{
temperature = ds1820_read();
TDC_reset();
delay_ms(100);
//----------------------------------------------- Nastaveni registru
output_low(TDC_ENABLE);
ble=0;
ble=(8<<28)|(0<<24);
ble|=(0<<20)|(0<<16)|(0<<14)|(3<<12)|(1<<10)|(0<<9)|(0<<8)|(0<<7)|(1<<6)|(1<<5)|(0<<4)|(1<<3)|(0<<2)|(0<<1)|0;
spi_xfer(TDC_stream,ble,32);
output_high(TDC_ENABLE);
output_low(TDC_ENABLE);
ble=0;
ble=(8<<28)|(1<<24);
ble|=(2<<20)|(1<<16)|(0<<15)|(1<<14)|(0<<11)|(4<<8)|0;
spi_xfer(TDC_stream,ble,32);
output_high(TDC_ENABLE);
// TDC_init();
output_low(TDC_ENABLE);
ble=0;
ble=(8<<28)|(2<<24);
ble|=(1<<21)|(1<<20)|(1<<19)|0;
spi_xfer(TDC_stream,ble,32);
output_high(TDC_ENABLE);
output_low(TDC_ENABLE);
ble=0;
ble=(8<<28)|(3<<24);
ble|=(0<<22)|(1<<21)|(1<<20)|(1<<19)|0;
spi_xfer(TDC_stream,ble,32);
output_high(TDC_ENABLE);
output_low(TDC_ENABLE);
ble=0;
ble=(8<<28)|(4<<24);
ble|=(4<<19)|0;
spi_xfer(TDC_stream,ble,32);
output_high(TDC_ENABLE);
output_low(TDC_ENABLE);
ble=0;
ble=(8<<28)|(5<<24);
ble|=(0<<21)|(0<<20)|(0<<19)|(0<<16)|0;
spi_xfer(TDC_stream,ble,32);
output_high(TDC_ENABLE);
//----------------------------------------------- Vypis registru
output_low(TDC_ENABLE);
ret8=0;
ret8=(0b1011<<4)|0;
spi_xfer(TDC_stream,ret8,8);
ret32=spi_xfer(TDC_stream,0,32);
output_high(TDC_ENABLE);
printf("- %LX ", ret32);
output_low(TDC_ENABLE);
ret8=0;
ret8=(0b1011<<4)|1;
spi_xfer(TDC_stream,ret8,8);
ret32=spi_xfer(TDC_stream,0,32);
output_high(TDC_ENABLE);
printf("%LX ", ret32);
output_low(TDC_ENABLE);
ret8=0;
ret8=(0b1011<<4)|2;
spi_xfer(TDC_stream,ret8,8);
ret32=spi_xfer(TDC_stream,0,32);
output_high(TDC_ENABLE);
printf("%LX ", ret32);
output_low(TDC_ENABLE);
ret8=0;
ret8=(0b1011<<4)|3;
spi_xfer(TDC_stream,ret8,8);
ret32=spi_xfer(TDC_stream,0,32);
output_high(TDC_ENABLE);
printf("%LX ", ret32);
output_low(TDC_ENABLE);
ret8=0;
ret8=(0b1011<<4)|4;
spi_xfer(TDC_stream,ret8,8);
ret16=spi_xfer(TDC_stream,0,16);
output_high(TDC_ENABLE);
printf("[%Lu %Lu %Lu %Lu %Lu %Lu %Lu] ", (1&(ret16)>>12), (1&(ret16)>>11), (1&(ret16)>>10), 1&(ret16)>>9, 7&(ret16)>>6, 7&(ret16)>>3, 7&ret16);
output_low(TDC_ENABLE);
ret8=0;
ret8=(0b1011<<4)|5;
spi_xfer(TDC_stream,ret8,8);
ret8=spi_xfer(TDC_stream,0,8);
output_high(TDC_ENABLE);
printf("%X\r\n", ret8);
//----------------------------------------------- Mereni
TDC_init();
delay_ms(50);
printf("Temp: %f \r\n", temperature);
delay_ms(50);
printf("status: %LX \r\n", TDC_get_status());
 
// TDC_start_cycle();
 
// delay_us(10);
TDC_start_cycle();
delay_ms(200);
output_high(STOP2); // Merime jenom jednim kanalem (druhy zrejme musi byt v H)
output_high(START);
// delay_us(1);
output_low(START);
delay_us(1);
delay_us(100);
// output_low(START);
output_high(STOP1);
output_low(STOP1);
delay_us(1);
output_high(STOP1);
// delay_us(10);
// delay_us(500);
// output_high(STOP1);
// delay_us(10);
output_low(STOP1);
// delay_us(500);
// output_high(STOP1);
delay_us(10);
// output_low(STOP1);
output_low(START);
output_low(STOP1);
delay_us(1);
output_high(STOP1);
output_low(STOP1);
delay_us(1);
//----------------------------------------------- Pocitani
int32 nn;
for(nn=3;nn<=5;nn++)
{
delay_ms(500);
output_low(TDC_ENABLE);
ret8=0;
ret8=(0b1011<<4)|0;
spi_xfer(TDC_stream,ret8,8);
ret32=spi_xfer(TDC_stream,0,32);
output_high(TDC_ENABLE);
printf("* %LX ", ret32);
output_low(TDC_ENABLE);
ret8=0;
ret8=(0b1011<<4)|1;
spi_xfer(TDC_stream,ret8,8);
ret32=spi_xfer(TDC_stream,0,32);
output_high(TDC_ENABLE);
printf("%LX ", ret32);
output_low(TDC_ENABLE);
ret8=0;
ret8=(0b1011<<4)|2;
spi_xfer(TDC_stream,ret8,8);
ret32=spi_xfer(TDC_stream,0,32);
output_high(TDC_ENABLE);
printf("%LX ", ret32);
output_low(TDC_ENABLE);
ret8=0;
ret8=(0b1011<<4)|3;
spi_xfer(TDC_stream,ret8,8);
ret32=spi_xfer(TDC_stream,0,32);
output_high(TDC_ENABLE);
printf("%LX ", ret32);
output_low(TDC_ENABLE);
ret8=0;
ret8=(0b1011<<4)|4;
spi_xfer(TDC_stream,ret8,8);
ret16=spi_xfer(TDC_stream,0,16);
output_high(TDC_ENABLE);
printf("[%Lu %Lu %Lu %Lu %Lu %Lu %Lu] ", (1&(ret16)>>12), (1&(ret16)>>11), (1&(ret16)>>10), 1&(ret16)>>9, 7&(ret16)>>6, 7&(ret16)>>3, 7&ret16);
output_low(TDC_ENABLE);
ret8=0;
ret8=(0b1011<<4)|5;
spi_xfer(TDC_stream,ret8,8);
ret8=spi_xfer(TDC_stream,0,8);
output_high(TDC_ENABLE);
printf("%X\r\n", ret8);
// Next calculation
output_low(TDC_ENABLE);
ble=0;
ble=(8<<28)|(1<<24);
ble|=(nn<<20)|(1<<16)|(0<<15)|(1<<14)|(0<<11)|(4<<8)|0x00;
spi_xfer(TDC_stream,ble,32);
output_high(TDC_ENABLE);
}
}
 
// output_high(STOP2);
// delay_us(10);
// output_low(START);
output_high(STOP1);
// output_low(STOP2);
// delay_us(500);
// output_high(STOP2);
// delay_us(10);
output_low(STOP1);
// delay_us(500);
// output_high(STOP2);
// delay_us(10);
// output_low(STOP2);
 
 
// delay_ms(10);
TDC_start_temp();
 
delay_ms(10);
printf("status: %LX \r\n", TDC_get_status());
delay_ms(50);
printf("measured: %LX, %LX, %LX, %LX \r\n", TDC_get_measurement(1), TDC_get_measurement(2), TDC_get_measurement(3), TDC_get_measurement(4));
delay_ms(500);
};
}