#include <16F883.h> #use delay(clock=16000000) #fuses NOWDT,HS, PUT, NOPROTECT, BROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // Jumpers: 8 to 11, 7 to 12 #use fast_io(A) // means you want to take control of port I/O direction instead of letting the complier do it automatically #use fast_io(B) // the complier does nothing and you have to control the pins #use fast_io(C) // RS232 -> usb BAUD is transferring data at max of 9600 bits/sec // xmit pin transmission pin ; rcv recieving pin eg. PIC16F883 at RC7 that where RS232 will recieve data #define BUFFER_SIZE 32 // specify the maximum number of bytes to the buffer before writing to the network BYTE buffer[BUFFER_SIZE]; BYTE next_in = 0; BYTE next_out = 0; int32 adc_value; float adc_volt; char x; #int_rda void serial_isr() { int t; buffer[next_in]=getc(); t=next_in; next_in=(next_in+1) % BUFFER_SIZE; if(next_in==next_out) next_in=t; // Buffer full !! } #define bkbhit (next_in!=next_out) BYTE bgetc() { //reads and returns a byte from file or device BYTE c; while(!bkbhit) ; c=buffer[next_out]; next_out=(next_out+1) % BUFFER_SIZE; return(c); } /*----------------------------------------------------------------------*/ /* LTC2449 drivers how will the drivers be included */ /*----------------------------------------------------------------------*/ #define ADC_CLK PIN_A0 #define ADC_DOUT PIN_A1 #define ADC_DIN PIN_A3 // pin numbers on the LTC2449 #define ADC_CS PIN_A2 void ADCInit() { // initializes an analog to digital converter given its resolution, minimumand maximum voltage and time precision. output_high(ADC_CS); //does not return any value output_low(ADC_CLK); } int ADC_Is_Busy(void) // takes in the function and converts it from analog and digital { byte b; b=input_c(); return(bit_test(b,0)); } #define ADC_CS_Low() output_low(ADC_CS) #define ADC_CS_High() output_high(ADC_CS) byte ShiftDataByte(byte x) { int i,j; byte dout; byte din; byte b; din=x; dout=0; delay_us(2); for(i=8; i>0; i--) { j=i-1; if(bit_test(din,j)) { output_high(ADC_DIN); } else { output_low(ADC_DIN); } b=input_a(); if(bit_test(b,1)) { bit_set(dout,j); } else { bit_clear(dout,j); } delay_us(2); output_high(ADC_CLK); delay_us(50); output_low(ADC_CLK); delay_us(50); } return(dout); } void ADC_Mux_Set(byte x) { byte din_mode; byte din_speed; byte data1,data2,data3,data4; byte sign; /* | 8| 4| 2| 1| 8| 4| 2| 1| - din_mode | 8| 4| 2| 1| 8| 4| 2| 1| - din_speed | 31| 30| 29| 28| 27|26|25|24| 23| 22| 21| 20| 19| | 1| 0| EN|SGL|ODD|A2|A1|A0|OSR3|OSR2|OSR1|OSR0|TWOX| EN = 0 DATA NEJSOU INTERPRETOVANA VCETNE ADRESY | data is not interpreted including addresses| EN = 1 DATA SE INTERPRETUJI |data is interpreted| SGL = 0 DIFERENCIALNI CTENI |differentials cteni| SGL = 1 PRIME CTENI A NEGATIVE INPUT JE NA COM VYVODU |prime cteni and negative input is on com| ODD - V DIFF MODU URCUJE POLARITU DIFF VSTUPU |diff mode for the polarity of diff input| 0 - SUDY JE |even is| +, LICHY JE |odd is| - 1 - SUDY JE |even is| -, LICHY JE |odd is|+ V PRIMEM MODU URCUJE LICHY/SUDY KANAL CILI LSB ADRESY | first mode urcuje odd/ even channel cili lsb addresses ADDR - V DIFF MODU PRIMO ADRESA PARU VSTUPU 0 - 7 |diff mode primo address for paru input | V PRIMEM MODU MSB CAST ADRESY | primem mode msb cast addresses| OSR3-OSR0 RYCHLOST PREVODU | transfer speed TWOX POCET CYKLU PREVODU | twox number of cycle transfer| */ din_mode=x & 0x1f; din_mode|=0xA0; din_speed=0x80; /* printf("Mode %x\n\r",din_mode); printf("Speed %x\n\r",din_speed); */ while(ADC_Is_Busy()); ADC_CS_Low(); data4=ShiftDataByte(din_mode); data3=ShiftDataByte(din_speed); data2=ShiftDataByte(0); data1=ShiftDataByte(0); /* data4=ShiftDataByte(0xaa); data3=ShiftDataByte(0xaa); data2=ShiftDataByte(0xaa); data1=ShiftDataByte(0xaa); */ ADC_CS_High(); /* printf("Data 1 %x",data4); printf("%x",data3); printf("%x",data2); printf("%x\n\r",data1); */ sign=0; if (!bit_test(data4,5)) { data1=~data1; data2=~data2; data3=~data3; data4=~data4; sign=1; } bit_clear(data4,7); bit_clear(data4,6); bit_clear(data4,5); /* printf("Data 2 %x",data4); printf("%x",data3); printf("%x",data2); printf("%x\n\r",data1); */ adc_value=data4; adc_value<<=8; adc_value|=data3; adc_value<<=8; adc_value|=data2; adc_value<<=8; adc_value|=data1; adc_value>>=13; adc_volt=(2.5*adc_value)/65535; if (sign==1) adc_volt*=-1.0; } void ADCConvert(void) { } void PrintADC(void) { char s[10]; int i; char sum; ADCConvert(); sprintf(s,"%02.6f ",adc_volt); for(i=0;i<8;i++) sum+=s[i]; sum&=0x7; s[8]=sum|0x30; s[9]=0; printf("%s",s); } void PrintADC_U(void) { ADCConvert(); printf("%02.8f\n\r",adc_volt); } void ReadADCDiff(byte x) { byte addr=0; if (bit_test(x,0)) addr=8; x>>=1; x&=0x7; addr|=x; ADC_Mux_Set(addr); } void ReadADC(byte x) { byte addr=0; if (bit_test(x,0)) addr=8; x>>=1; x&=0x7; addr|=x; addr|=0x10; ADC_Mux_Set(addr); } void DACInit() // digital to analog driver initialization { setup_timer_2(T2_DIV_BY_1, 0x3f, 1); //CCP -> capture, compare PWN setup_ccp1(CCP_PWM); // Pulse Width Modulation -> PWN technique for getting analog results with digital means. setup_ccp2(CCP_PWM); // Digital control is used to create a square wave, a signal switched between on and off. set_pwm1_duty(0); set_pwm2_duty(0); } #define TLV_DATA_L() output_high(PIN_B4) #define TLV_DATA_H() output_low(PIN_B4) #define TLV_CLK_L() output_high(PIN_B5) // TLV stands for Tag length value -> it is used for large buffer of bytes, #define TLV_CLK_H() output_low(PIN_B5) // perhaps being filled from some serial device, and you need to know what is in that buffer. #define TLV_LOAD_L() output_high(PIN_B6) #define TLV_LOAD_H() output_low(PIN_B6) #define TLV_LDAC_L() output_low(PIN_B7) #define TLV_LDAC_H() output_high(PIN_B7) #define DACTIME 500 void TLVDACInit() // tag value length digital to analog converter -> Initializes the system run-time */ { TLV_LDAC_H(); // init -> useful for initializations required for the run-time system TLV_DATA_L(); TLV_CLK_L(); TLV_LOAD_H(); delay_us(DACTIME); delay_us(DACTIME); } void TLVDACOut(byte addr,byte data) { int i; // Address send if (bit_test(addr,1)) { TLV_DATA_H(); } else { TLV_DATA_L(); } delay_us(DACTIME); TLV_CLK_H(); delay_us(DACTIME); TLV_CLK_L(); delay_us(DACTIME); if (bit_test(addr,0)) { TLV_DATA_H(); } else { TLV_DATA_L(); } delay_us(DACTIME); TLV_CLK_H(); delay_us(DACTIME); TLV_CLK_L(); delay_us(DACTIME); // RNG bit is zero TLV_DATA_L(); delay_us(DACTIME); TLV_CLK_H(); delay_us(DACTIME); TLV_CLK_L(); delay_us(DACTIME); // Data send for (i=8;i>0;i--) { if (bit_test(data,i-1)) TLV_DATA_H(); else TLV_DATA_L(); delay_us(DACTIME); TLV_CLK_H(); delay_us(DACTIME); TLV_CLK_L(); delay_us(DACTIME); } TLV_LOAD_L(); delay_us(DACTIME); TLV_LOAD_H(); delay_us(DACTIME); TLV_LDAC_L(); delay_us(DACTIME); TLV_LDAC_H(); delay_us(DACTIME); } int GetXChar(byte *x) // This function returns the character read as an unsigned char cast to an integer { long timeout; timeout=100; *x=0; while(!bkbhit) { delay_ms(1); timeout--; if (timeout==0) return(0); } *x=bgetc(); return(1); } #define SetOutputVoltage(x) set_pwm2_duty(x) #define SetTrimVoltage(x) set_pwm1_duty(x) #define BrickPowerOn() output_high(PIN_B0) #define BrickPowerOff() output_low(PIN_B0) #define BrickTermoOn() output_high(PIN_B1) #define BrickTermoOff() output_low(PIN_B1) #define Brick200VOn() output_high(PIN_B3) #define Brick200VOff() output_low(PIN_B3) #define BrickStart() output_low(PIN_B2) #define BrickStop() output_high(PIN_B2) #define TestLEDOn() output_high(PIN_A4) #define TestLEDOff() output_low(PIN_A4) void main() { setup_adc_ports(NO_ANALOGS); setup_adc(ADC_OFF); setup_spi(FALSE); setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); setup_timer_1(T1_DISABLED); setup_timer_2(T2_DISABLED,0,1); enable_interrupts(global); enable_interrupts(int_rda); set_tris_a(0x22); set_tris_b(0x0); set_tris_c(0x81); output_b(0); ADCInit(); DACInit(); TLVDACInit(); while (1) { /* ReadADC(0); delay_us(100); */ while(!bkbhit); x=bgetc(); switch (x) { case '1':BrickPowerOn();break; case '2':BrickPowerOff();break; case '3':Brick200VOn();break; case '4':Brick200VOff();break; case '5':BrickTermoOn();break; case '6':BrickTermoOff();break; case 'r':BrickStart();break; case 'o':BrickStop();break; case 'l':TestLEDOn();break; case 'h':TestLEDOff();break; case '9': if (GetXChar(&x)) output_b(x); //gets the character of the output_b break; case 'a': if (GetXChar(&x)) SetOutputVoltage(x); break; case 'b': if (GetXChar(&x)) SetTrimVoltage(x); break; case 'g': /*Brick input current*/ ReadADCDiff(1); ReadADCDiff(1); PrintADC(); break; case 'i': /* Current from 15V startup */ /* start up voltage is supposed to be reduced to ? */ ReadADCDiff(14); ReadADCDiff(14); PrintADC(); break; case 'p': /* Brick output voltage */ ReadADCDiff(6); ReadADCDiff(6); PrintADC(); break; case 't': /* Brick input voltage */ ReadADCDiff(2); ReadADCDiff(2); PrintADC(); break; case 's': /* Brick output current */ ReadADCDiff(4); ReadADCDiff(4); PrintADC(); break; case 'e': /* Temp 1 */ ReadADCDiff(10); ReadADCDiff(10); PrintADC(); break; case 'f': /* Temp 2 */ ReadADCDiff(12); ReadADCDiff(12); PrintADC(); break; case 'm': /* Temp 3 */ /* Temp 3 is it supposed to removed or is it still required?*/ ReadADCDiff(8); ReadADCDiff(8); PrintADC(); break; case 'z': if (GetXChar(&x)) TLVDACOut(0,x); break; case 'x': if (GetXChar(&x)) TLVDACOut(1,x); break; case 'c': if (GetXChar(&x)) TLVDACOut(2,x); break; case 'v': if (GetXChar(&x)) TLVDACOut(3,x); break; case 'n': TLVDACOut(0,0); TLVDACOut(1,0); TLVDACOut(2,0); TLVDACOut(3,0); break; break; case 'G': /*Brick input current*/ ReadADCDiff(1); ReadADCDiff(1); PrintADC_U(); break; case 'I': /* Current from 15V startup */ ReadADCDiff(14); ReadADCDiff(14); PrintADC_U(); break; case 'P': /* Brick output voltage */ ReadADCDiff(6); ReadADCDiff(6); PrintADC_U(); break; case 'T': /* Brick input voltage */ ReadADCDiff(2); ReadADCDiff(2); PrintADC_U(); break; case 'S': /* Brick output current */ ReadADCDiff(4); ReadADCDiff(4); PrintADC_U(); break; case 'E': /* Temp 1 */ ReadADCDiff(10); ReadADCDiff(10); PrintADC_U(); break; case 'F': /* Temp 2 */ ReadADCDiff(12); ReadADCDiff(12); PrintADC_U(); break; case 'M': /* Temp 3 */ /* Should be removed */ ReadADCDiff(8); ReadADCDiff(8); PrintADC_U(); break; case 'd': if (GetXChar(&x)) { ReadADC(x); ReadADC(x); PrintADC(); } break; case 'D': if (GetXChar(&x)) { ReadADCDiff(x); ReadADCDiff(x); PrintADC(); } break; case 'w': if (GetXChar(&x)) { ReadADC(x); ReadADC(x); PrintADC_U(); } break; case 'W': if (GetXChar(&x)) { ReadADCDiff(x); ReadADCDiff(x); PrintADC_U(); } break; case 'q': /* Module Address, should change and be unique for each module 1-63 */ printf("39"); break; default:; } x=0; } }