//-- ISO11784/11785 Decoder PIC 18F452 20 MHz //Compiler XC8 //Compiler option:C90 //RFID tag 134.2 kHz //--Timer 0: //--Timer 1: Measure RB0 rising edge times, 1,6us/tick =data //--Timer 2: //--Timer 3: Measure clock period CCP1 capture mode, 0,2 us/tick //-- //-- 01.3.2020 P.Viljakainen--- //Last edit 01.09.2020 //-------------------------------------------------------------------- // // // // // PIC18F452 Configuration Bit Settings // // 'C' source line config statements // // CONFIG1H #pragma config OSC = HS // Oscillator Selection bits (HS oscillator) #pragma config OSCS = OFF // Oscillator System Clock Switch Enable bit (Oscillator system clock switch option is disabled (main oscillator is source)) // CONFIG2L #pragma config PWRT = OFF // Power-up Timer Enable bit (PWRT disabled) #pragma config BOR = OFF // Brown-out Reset Enable bit (Brown-out Reset disabled) #pragma config BORV = 20 // Brown-out Reset Voltage bits (VBOR set to 2.0V) // CONFIG2H #pragma config WDT = OFF // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit)) #pragma config WDTPS = 128 // Watchdog Timer Postscale Select bits (1:128) // CONFIG3H #pragma config CCP2MUX = OFF // CCP2 Mux bit (CCP2 input/output is multiplexed with RB3) // CONFIG4L #pragma config STVR = ON // Stack Full/Underflow Reset Enable bit (Stack Full/Underflow will cause RESET) #pragma config LVP = ON // Low Voltage ICSP Enable bit (Low Voltage ICSP enabled) // CONFIG5L #pragma config CP0 = OFF // Code Protection bit (Block 0 (000200-001FFFh) not code protected) #pragma config CP1 = OFF // Code Protection bit (Block 1 (002000-003FFFh) not code protected) #pragma config CP2 = OFF // Code Protection bit (Block 2 (004000-005FFFh) not code protected) #pragma config CP3 = OFF // Code Protection bit (Block 3 (006000-007FFFh) not code protected) // CONFIG5H #pragma config CPB = OFF // Boot Block Code Protection bit (Boot Block (000000-0001FFh) not code protected) #pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM not code protected) // CONFIG6L #pragma config WRT0 = OFF // Write Protection bit (Block 0 (000200-001FFFh) not write protected) #pragma config WRT1 = OFF // Write Protection bit (Block 1 (002000-003FFFh) not write protected) #pragma config WRT2 = OFF // Write Protection bit (Block 2 (004000-005FFFh) not write protected) #pragma config WRT3 = OFF // Write Protection bit (Block 3 (006000-007FFFh) not write protected) // CONFIG6H #pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write protected) #pragma config WRTB = OFF // Boot Block Write Protection bit (Boot Block (000000-0001FFh) not write protected) #pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM not write protected) // CONFIG7L #pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (000200-001FFFh) not protected from Table Reads executed in other blocks) #pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (002000-003FFFh) not protected from Table Reads executed in other blocks) #pragma config EBTR2 = OFF // Table Read Protection bit (Block 2 (004000-005FFFh) not protected from Table Reads executed in other blocks) #pragma config EBTR3 = OFF // Table Read Protection bit (Block 3 (006000-007FFFh) not protected from Table Reads executed in other blocks) // CONFIG7H #pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot Block (000000-0001FFh) not protected from Table Reads executed in other blocks) # define Mask0=0xFFFFFFFE # define Mask1=0x00000001 // #pragma config statements should precede project file includes. // Use project enums instead of #define for ON and OFF. #include #include #include #include "delay.h" #include "lcd.h" # include "rs232.h" void initialize(void); void Decode(unsigned char); void Detect_header(unsigned char); void Sort(unsigned char); void Display_data(void); void viive(void); void Resetoi(void); void Ints_off(void); void Set_Pulsedetect_ints(void); void Set_Ext_ints(void); void CIR_ints(void); void Erase_read_data(void); void Erase_pulse_data(void); void CRC_table(void); unsigned int Reflect16(unsigned int); unsigned char Reflect8(unsigned char); unsigned int Compute_CRC16(void); void Getdatabits(void); unsigned char Checkfor5(unsigned char); void BintoBCD(void); unsigned int tol=35; // Tolerance = 20x1,6=32us unsigned int T1; // Diferential Manchester decoding short pulse in counts unsigned int T2; // decoding mid pulse unsigned int T3; // decoding long pulse unsigned char headercnt=0; // number of consequtive "1"'s found in possible header unsigned char Detstart; // Start point for header detect in data stream unsigned char CDet; // Card in reader detect counter bit CIR; // Card in reader bit unsigned char Bitcounter; // Bit counter in data sorting (0...127) bit Timerstart; // Detect first falling edge on ext interrupt RB0 bit Startdetect; // Indicate header found in data bitstream unsigned int crctable16[256]; // Lookup table for CRC-16 KERMIT unsigned int Generator; // CRC-16 polynomial unsigned int curByte; // Byte to be processed in CRC calculation unsigned int dividend; // result of XOR function in CRC calc unsigned char crcbit; // Bit to be processed in CRC calc unsigned char Timedata_low[245]; // Times between two rising edges of RB0, low bytes unsigned char Timedata_high[245]; unsigned int Pulsetime_high_1=0; // Clock pulse capture first timer value unsigned char Pulsetime_low_1=0; // Clock pulse capture first timer value unsigned int Pulsetime_high_2=0; // Clock pulse capture 2nd timer value unsigned char Pulsetime_low_2=0; // Clock pulse capture 2nd timer value unsigned int Pulsetime_avg=0; // Calculated average clock pulse period for 16 periods unsigned char Avg_count; // Amount of timer3 readings in the average unsigned int Pulsetime; // Variable for 2nd timer 3 value unsigned int Pulsetime_first; // Variable for 2nd timer 3 value bit Time_valid; bit Pulsestart; bit Pulse_rdy; unsigned char Timehigh; unsigned long Sorter_0; // Decoded data , 32-bits, (last-in) unsigned long Sorter_1; // 32-bits unsigned long Sorter_2; // 32-bits unsigned long Sorter_3; // 32-bits (first-in) unsigned char Data[8]; // 64 bits containing data included in CRC calc unsigned int CRC_Read; // CRC value read directly in data stream unsigned char i=1; // First timer value position unsigned char T_sel; // Switch value for time interval unsigned char Detect_var; unsigned int Bittime_high; unsigned char Bittime_low; unsigned int Bittime; unsigned char Poss_start; unsigned char Askel; bit Loytyi; // Header found bit bit Allparity; bit negative; bit flag1; // Deco0de flag Tint found bit flag2; // Decode flag T short foud after Tint unsigned char pos; unsigned int crc; // calculated CRC from data bytes unsigned char Read_try; unsigned long Data21; unsigned long Data31; unsigned int Data11; unsigned long Ncode_low; // binary input, 32 bits unsigned char Ncode_high; // binary input, 6 bits unsigned char KY; //Tens & ones in BCD unsigned char TS; //Thousands and hundredsin BCD unsigned char STKT; //Hundreds of thousands & tens of thousands in BCD unsigned char KMM; //Tens of millions and millions in BCD unsigned char BSM; //Billions and hudreds of millions in BCD unsigned char SBKB; //Hundreds of billions and tens of billions in BCD bit yli; unsigned char c; unsigned int h;// display delay unsigned int dispnr; /*--- interrupt vector start ----------------------------------------------------------*/ void interrupt tc_int(void) { if(INT2IF) // Interrupt on RB2/INT2 pin 35 to detect tag { INT2IF=0; if (CDet<200) CDet++; else { CIR=1; //Card in reader detected LATC3=1; INT2IE=0; } } if(INT0IF)// Interrupt on RB0/INT0 pin 33 data { if (Timerstart==1) { TMR1H=0; // reset Timer1 at first rising edge on RB0 interrrupt TMR1L=0; //Writes also TMR1H contents to TMR1 high byte in16 bit mode Timerstart=0; } else { Timedata_low[i]=TMR1L; // Store timer bytes on every subsequent rising edge, also Read TMR1L places TMR1H in buffer Timedata_high[i]=TMR1H; // after which TMR1H can be read TMR1H=0; // and reset to time to next rising edge. first load TMR1H=0 to buffer, TMR1L=0; // and write to TMR1 low, simultaneous system write to TMR1H } if (i==245) { GIE=0; INT0IE=0; // Enough pulses read to contain header and data,disable external interrupt? Startdetect=1; // Ready to start detecting header } else { INT0IF=0; // Reset RB0 interrupt and increase counter i++; } } if (CCP1IF) // Interrupt on RC2/CCP1 pin 17 to detect clock pulse, every 16th pulse edge if (Pulsestart) { Pulsetime_high_1= CCPR1H;//Note! Must be read to a variable before next interrupt! Pulsetime_low_1= CCPR1L; Pulsestart=0; } else { Pulsetime_high_2= CCPR1H; //Note! Must be read to a variable before next interrupt! Pulsetime_low_2= CCPR1L; CCP1IE=0; Pulse_rdy=1; } CCP1IF=0; } /*--- interrupt vector end ----------------------------------------------------------*/ void main(void) { GIE=0; initialize(); //Init I/O- pins, timers, LCD, interrupt lcd_string(" FDX-B "); lcd_line2(); lcd_string("(c) P.V. 2020 "); viive(); viive(); LATC0=1; // Activate frontend shutdown CRC_table(); // Pre-Calculate CRC lookup table for all possible byte values viive(); // Mask transients in power-up Timerstart=1; //Enable Timer1 reset on first rising edge on RB0 interrrupt cold: LATC0=0; // Shutdown off lcd_line1(); lcd_string(" Wait for tag "); lcd_line2(); lcd_string(" --- "); CIR_ints(); // Start of detecting card in reader //--------------------------------------------start clock pulse length detect -------------------------------------------- while(1) { // LATB4^=1; while(CIR) // Come here only after transponder detected pulses RB2/INT2 pin 35 { //clock pulse acquisition starts viive(); // Delay to let transponder get positioned while(Time_valid==0) { Pulsestart=1; // to ge get first timer reading Set_Pulsedetect_ints(); // CCP1 interrupts on while(Avg_count<10) { if(Pulse_rdy==1) // Sets in CCP1 interrupt when two times read { Pulsetime_first=(unsigned int)((unsigned int)(Pulsetime_high_1<<8)+(Pulsetime_low_1)); Pulsetime=(unsigned int)((unsigned int)(Pulsetime_high_2<<8)+(Pulsetime_low_2)); if(Pulsetime>Pulsetime_first) { Pulsetime_avg=Pulsetime_avg+(Pulsetime-Pulsetime_first); Avg_count++; } Pulse_rdy=0; Pulsestart=1; CCP1IE=1; // Turn back on two get next time } }// end of average count while // Timer3 0,2us/tick; Tperiod=Mx2/10/16= M/80; M= Timer3 arvo (596 counts for 16 periods)) // T1 = 64 x Tperiod = 64/80x M // Timer1 1,6 us/tick = 16/10 us/tick = 8/5us/tick // T1 ticks= (64/80)/(8/5) M = 1/2M (nominal 596/2= 298) // T2 ticks = 3/2 T1 // T3 tick = 2T1 // lcd_line1(); // lcd_string(" Detect timing "); Pulsetime_avg=(unsigned int)Pulsetime_avg/Avg_count; T1=(unsigned int)(Pulsetime_avg/4);// nominal 149 T2=(unsigned int)(3*Pulsetime_avg/8); T3=(unsigned int)(Pulsetime_avg/2); if((T1>180)||(T1<120)) // Sanity check for 134.2 kHz nominal clock period reading // orig 170/130 { Pulsetime_avg=0; Avg_count=0; Pulse_rdy=0; Pulsestart=1; Time_valid=0; // if(T1>180) // { // lcd_line2(); // lcd_string(" bad clock T1>"); // // } // else // { // lcd_line2(); // lcd_string(" bad clock T1<"); // // } CCP1IE=1; // Turn back on two get next time } else { Time_valid=1; } } // end of while "time valid" // lcd_line2(); // lcd_string(" time valid "); //--------------------------------------------end clock pulse length detect -------------------------------------------- Resetoi(); //Timevalue[245] arvojen luku RB0 keskeytyksen nousevista reunoista lcd_line1(); lcd_string(" Data reading"); //--------------------------------------------start read 245 time intervals -------------------------------------------- Set_Ext_ints(); // INT0 enabled while (Allparity==0) { if((Startdetect==1)&&(Read_try<3)) // 245 aikav li luettu keskeytyksess RB0/INT0 { Detect_header(Detstart); // Detect header kutsutaan oletusaloitusarvolla, asettaa uuden Poss_start arvon (headerin viimeinen aikav li)) if (Loytyi==0) { Ints_off; Erase_pulse_data(); Erase_read_data(); Resetoi(); LATC0=1; // Activate frontend shutdown goto cold; } if(Poss_start<=117) { Decode((unsigned char)(Poss_start)); // Aloitetaan dekoodaus seuraavasta, ja sortataan Sorter_x muuttujiin 128 bitti Getdatabits(); crc=Compute_CRC16(); if(CRC_Read==crc) { Allparity=1; } else { Allparity=0; Read_try++; } } else { lcd_line2(); lcd_string(" RESET "); // Ints_off; // LATC0=1; // Activate frontend shutdown Resetoi(); Erase_read_data(); Erase_pulse_data(); goto cold; } } //end of statdetect }//end of allparity //--------------------------------------------end read /decode -------------------------------------------- if(Allparity==1) { Ints_off; Display_data(); } Erase_pulse_data(); Erase_read_data(); Resetoi(); // CIR_ints(); goto cold; }// end of card detect while }//end of endless loop while }//end of main void Resetoi(void) { Bitcounter=0; Poss_start=0; Detstart=1;// oli 1 headercnt=0; Loytyi=0; Bittime=0; Bittime_high=0; Bittime_low=0; Allparity=0; i=1; Startdetect=0; Timerstart=1; Time_valid=0; LATB7=0; Read_try=0; crc=0; CRC_Read=0; Sorter_0=0; Sorter_1=0; Sorter_2=0; Sorter_3=0; // for (char b=0;b<8;b++) // { // Data[b]=0; // } // for (char b=0;b<246;b++) // { // // Timedata_low[b]=0; // Timedata_high[b]=0; // } } void Erase_read_data(void) { KY=0; TS=0; STKT=0; KMM=0; BSM=0; SBKB=0; Sorter_0=0; Sorter_1=0; Sorter_2=0; Sorter_3=0; } void Erase_pulse_data(void) { CIR=0; CDet=0; LATC3=0; Pulsetime_avg=0; Pulse_rdy=0; Avg_count=0; i=1; } void viive(void) { for (h=1;h<65000;h++) delay; for (h=1;h<65000;h++) delay; for (h=1;h<65000;h++) delay; for (h=1;h<65000;h++) delay; for (h=1;h<65000;h++) delay; for (h=1;h<65000;h++) delay; } void Display_data(void) { BintoBCD(); lcd_line1(); //2x16 character HD44780, ylarivi 0x00-0x0F alarivi 0x40-0x4F lcd_string(" ID code "); lcd_line2(); lcd_BCDtodec((unsigned char)((SBKB&0xF0)>>4)); //SB lcd_BCDtodec((unsigned char)(SBKB&0xF)); //KB lcd_BCDtodec((unsigned char)((BSM&0xF0)>>4)); //B lcd_BCDtodec((unsigned char)(BSM&0xF)); //SM lcd_BCDtodec((unsigned char)((KMM&0xF0)>>4)); //KM lcd_BCDtodec((unsigned char)(KMM&0xF)); //M lcd_BCDtodec((unsigned char)((STKT&0xF0)>>4)); //ST lcd_BCDtodec((unsigned char)(STKT&0xF)); //KT lcd_BCDtodec((unsigned char)((TS&0xF0)>>4)); //T lcd_BCDtodec((unsigned char)(TS&0xF)); //S lcd_BCDtodec((unsigned char)((KY&0xF0)>>4)); //K lcd_BCDtodec((unsigned char)(KY&0xF)); //Y viive(); viive(); viive(); viive(); } void Decode(unsigned char alku) { while ((Bitcounter < 128)&&(alku<=245)) { Timehigh=Timedata_high[alku]; Bittime_high=(unsigned int)(Timehigh<<8); // 16 bit var Bittime_low= Timedata_low[alku]; Bittime = (unsigned int)(Bittime_high + Bittime_low);// 16 bit var if((Bittime >= (T1- tol)) && (Bittime <= (T1 + tol))) T_sel=1; else if ((Bittime >= (T2 - tol)) && (Bittime <= (T2 + tol))) T_sel=2; else if((Bittime >= (T3 - tol)) && (Bittime <= (T3 + tol))) T_sel=3; switch (T_sel) { case 1: // Lyhyt pulssi { Sort(0); //-> Decoded Bit is " 0 " } break; case 2: // 1,5- kertainen pulssi { if(flag1==1) { Sort(0); Sort(1); flag1=0; // null flag } else { flag1=1; // ensimm inen keskipitk luettu Sort(1); } } break; case 3: // 2- kertainen pulssi { Sort(1); // Bits are "1 1" Sort(1); } break; default: { lcd_line1(); lcd_string(" Decode error "); Resetoi(); } } if (alku < 245) alku ++; } } void Detect_header(unsigned char beg) { Detect_var = beg; headercnt = 0; Loytyi = 0; while ((Loytyi == 0)&&(Detect_var<=245)) { Timehigh=Timedata_high[Detect_var]; Bittime_high=(unsigned int)(Timehigh<<8); // 16 bit var Bittime_low= Timedata_low[Detect_var]; Bittime = (unsigned int)(Bittime_high + Bittime_low);// 16 bit var if ((Bittime >= (T1 - tol)) && (Bittime <= (T1 + tol))) // lyhyt l ytyi headercnt ++; // else if (headercnt>0) headercnt=0; if (headercnt == 10) { Poss_start = Detect_var; Loytyi = 1; } else if (Detect_var <= 245) Detect_var ++; } } void Sort(unsigned char Askel) { switch (Askel) { case 0: //add Bit "0" { if (Bitcounter<=31) // { Sorter_3=(Sorter_3 & Mask0); // add 0 if (Bitcounter<31) Sorter_3<<=1; Bitcounter++; } else if ((Bitcounter>31)&&(Bitcounter<=63)) { Sorter_2=(Sorter_2 & Mask0); // add 0 if (Bitcounter<63) Sorter_2<<=1; Bitcounter++; } else if ((Bitcounter>63)&&(Bitcounter<=95)) { Sorter_1=(Sorter_1 & Mask0); // add 0 if (Bitcounter<95) Sorter_1<<=1; Bitcounter++; } else if ((Bitcounter>95)&& (Bitcounter<=127)) { Sorter_0=(Sorter_0 & Mask0); // add 0 if (Bitcounter<127) Sorter_0<<=1; Bitcounter++; } } break; case 1: //add Bit " 1 " { if (Bitcounter<=31) // { Sorter_3=(Sorter_3 | Mask1); // 1 if (Bitcounter<31) //eliminate last shift Sorter_3<<=1; Bitcounter++; } else if ((Bitcounter>31)&&(Bitcounter<=63)) { Sorter_2=(Sorter_2 | Mask1); // 1 if (Bitcounter<63) Sorter_2<<=1; Bitcounter++; } else if ((Bitcounter>63)&&(Bitcounter<=95)) { Sorter_1=(Sorter_1 | Mask1); // 1 if (Bitcounter<95) Sorter_1<<=1; Bitcounter++; } else if ((Bitcounter>95)&& (Bitcounter<=127)) { Sorter_0=(Sorter_0 | Mask1); // 1 if (Bitcounter<127) Sorter_0<<=1; Bitcounter++; } } break; }// end of switch } void CRC_table(void) { Generator=0x1021; for (dividend=0;dividend<256;dividend++) { curByte=(unsigned int)(dividend); for (crcbit=0;crcbit<8;crcbit++) { if((curByte&0x0001)!=0) { curByte>>=1; curByte^=Reflect16(Generator); } else curByte>>=1; } crctable16[dividend]=(unsigned int) curByte; } } unsigned int Reflect16(unsigned int val) { unsigned int resVal = 0; for (int i = 0; i < 16; i++) { if ((val & (1 << i)) != 0) { resVal |= (unsigned int)(1 << (15 - i)); } } return resVal; } unsigned char Reflect8(unsigned char val8) { unsigned int resVal8 = 0; for (int i = 0; i < 8; i++) { if ((val8 & (1 << i)) != 0) { resVal8 |= (unsigned int)(1 << (7 - i)); } } return resVal8; } unsigned int Compute_CRC16() { crc=0; for (char b=0;b<8;b++) { crc=(unsigned int)(crc^Data[b]); pos=(char)(crc & 0xFF); crc=(unsigned int)(crc>>8); crc=(unsigned int)(crc^(unsigned int)(crctable16[pos])); } return crc; } void Getdatabits(void) { Data[0]=Reflect8(((Sorter_3&0x3FC00000)>>22)); Data[1]=Reflect8(((Sorter_3&0x1FE000)>>13)); Data11=Data[1]; Data[2]=Reflect8((Sorter_3&0x1FF0)>>4); Data21=Data[2]; Data[3]=Reflect8(((Sorter_3&0x7)<<5)+((Sorter_2&0xF8000000)>>27)); Data31= Data[3]; Data[4]=Reflect8(((Sorter_2&0x3FC0000)>>18)); Data[5]=Reflect8(((Sorter_2&0x1FE00)>>9)); Data[6]=Reflect8((Sorter_2&0x1FF)); Data[7]=Reflect8(((Sorter_1&0x7F800000)>>23)); CRC_Read=Reflect16((((Sorter_1&0x3FC000)>>14)<<8)+((Sorter_1&0x1FE0)>>5)); } void BintoBCD(void) { Ncode_low=(Ncode_low & 0x00FFFFFF)|((Data31<<24)); Ncode_low=(Ncode_low & 0xFF00FFFF)|((Data21<<16)); Ncode_low=(Ncode_low & 0xFFFF00FF)|(((Data11<<8))); Ncode_low=(Ncode_low & 0xFFFFFF00)|((Data[0])); Ncode_high=(unsigned long)(Data[4]&0x3F); for (unsigned char loop=1;loop<=38;loop++) { SBKB<<=1; SBKB=(unsigned char)((SBKB | (unsigned char)((BSM & 0b10000000)>> 7))); BSM<<=1; BSM=(unsigned char)((BSM | (unsigned char)((KMM & 0b10000000)>> 7))); KMM<<=1; KMM=(unsigned char)((KMM |(unsigned char) ((STKT & 0b10000000)>> 7))); STKT<<=1; STKT=(unsigned char)((STKT | (unsigned char)((TS & 0b10000000)>> 7))); TS<<=1; TS=(unsigned char)((TS | (unsigned char)((KY & 0b10000000)>> 7))); if(yli) KY<<=1; if(loop<=6) { KY=(unsigned char)(((KY | ((unsigned char)((Ncode_high& 0b100000) >>5))))); Ncode_high<<=1; } else { KY=(unsigned char)((KY | ((unsigned char)((Ncode_low& 0x80000000) >>31)))); Ncode_low<<=1; } yli=1; if(loop<38) { KY=(unsigned int)((unsigned int)((KY & 0b11110000))|(unsigned int)((Checkfor5((unsigned int)(KY & 0b00001111))))); KY=(unsigned int)((unsigned int)((KY & 0b00001111)) | (unsigned int)((Checkfor5((unsigned int)(KY & 0b11110000)>>4)<<4))); TS=(unsigned int)((unsigned int)((TS & 0b11110000)) |(unsigned int)((Checkfor5((unsigned int)(TS & 0b00001111))))); TS=(unsigned int)((unsigned int)((TS & 0b00001111)) |(unsigned int)((Checkfor5((unsigned int)(TS & 0b11110000)>>4)<<4))); STKT=(unsigned int)((unsigned int)((STKT & 0b11110000)) |(unsigned int)((Checkfor5((unsigned int)(STKT & 0b00001111))))); STKT=(unsigned int)((unsigned int)((STKT & 0b00001111)) |(unsigned int)((Checkfor5((unsigned int)(STKT & 0b11110000)>>4)<<4))); KMM=(unsigned int)((unsigned int)((KMM & 0b11110000)) |(unsigned int)((Checkfor5((unsigned int)(KMM & 0b00001111))))); KMM=(unsigned int)((unsigned int)((KMM & 0b00001111)) |(unsigned int)((Checkfor5((unsigned int)(KMM & 0b11110000)>>4)<<4))); BSM=(unsigned int)((unsigned int)((BSM & 0b11110000)) |(unsigned int)((Checkfor5((unsigned int)(BSM & 0b00001111))))); BSM=(unsigned int)((unsigned int)((BSM & 0b00001111)) |(unsigned int)((Checkfor5((unsigned int)(BSM & 0b11110000)>>4)<<4))); SBKB=(unsigned int)((unsigned int)((SBKB & 0b11110000)) |(unsigned int)((Checkfor5((unsigned int)(SBKB & 0b00001111))))); SBKB=(unsigned int)((unsigned int)((SBKB & 0b00001111)) |(unsigned int)((Checkfor5((unsigned int)(SBKB & 0b11110000)>>4)<<4))); } } } unsigned char Checkfor5(unsigned char col) { if(col>=5) col=col+3; return col; } void Ints_off(void) { GIE= 0; PEIE=0; TMR0IE=0; TMR1IE=0; TMR3IE=0; INT0IE=0; INT2IE=0; RBIE=0; CCP1IE=0; } void Set_Pulsedetect_ints(void) { GIE= 1; PEIE=1; TMR0IE=0; TMR1IE=0; TMR3IE=0; INT0IE=0; INT2IE=0; RBIE=0; CCP1IE=1; TMR0IF=0; INT0IF=0;// external interrupt occurred INT2IF=0; RBIF=0; CCP1IF=0; // Capture interrupt occurred } void Set_Ext_ints(void) { GIE= 1; PEIE=0; TMR0IE=0; INT0IE=1; INT2IE=0; RBIE=0; CCP1IE=0; TMR0IF=0; INT0IF=0;// external interrupt occurred INT2IF=0; RBIF=0; CCP1IF=0; // Capture interrupt occurred } void CIR_ints(void) { GIE= 1; PEIE=0; TMR0IE=0; INT0IE=0; INT2IE=1; RBIE=0; CCP1IE=0; TMR0IF=0; INT0IF=0; // external interrupt occurred INT2IF=0; // RB2 interrupt occurred (card detect)) RBIF=0; CCP1IF=0; // Capture interrupt occurred } void initialize() { //ADCON1 = 0xB2; ADCON1 = 0x83; // 10-bit, right justified A,VREF,A,A,A TRISA=0b00111; //RA0 & RA1 & RA2 = input muut output TRISB=0b00000101; // RB0, RB2 input , RB4 detector led // nRBPU=1; // Sets in INTCON2 not here TRISD = 0b00000000; //D-portti outputeiksi TRISC = 0b10000100; //C6,C7 USART comm, RC2 input,RC0 SHD (Capture mode)) PORTD=0x0; TRISE0=0; //RE0=output(SD),RE1=SPARE input, RE2=output(COND) TRISE1=0; TRISE2=0; ADON=1; // turn on the A2D conversion module lcd_init(); // Display module initialization delay(); LATB7=0; // Led indicators off LATB4=0; LATC3=0; IPEN=0; // interrupt compatibility mode CCP1CON=0b00000111; // Capture mode every 16 rising edge T3CON=0b11000101; // CCP1R1 clock pulse detection Timer3 16-bit operation, 1:1 prescale, intclock, timer on, 0,2us/tick T1CON=0b10110001; //Data bits 16-bit operation, 1:8 prescale, osc dis, internal clock, timer on, 1,6us/tick INTCON2 = 0b11110001; /* RB weak Pull ups off, interrupt RB0 rising edge*/ INTCON3 = 0b00000000; RCSTA = 0x90; // RX Setting, 8bit, enable receive, (oli 90h) TXSTA = 0x24; // TX Setting, 8bit, Asinchronius mode, High speed SPBRG = 129; // Set Baudrade - 9600 (from datasheet baudrade table) }