CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

HCS300 Keeloq encoder problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Kasper



Joined: 14 Jan 2004
Posts: 88
Location: Aurora, Ontario, Canada

View user's profile Send private message Visit poster's website

HCS300 Keeloq encoder problem
PostPosted: Tue Feb 17, 2004 2:06 pm     Reply with quote

Hi.

I am trying to program the Microchip HCS300 Keeloq encoder with the unique serial number, manufactures code etc to make it work with the HCS515 decoder, which I now think I have working (hard to tell without a working encoder)

Thanks in advance to any and all suggestions, and I appologize for the long post.

Kasper
PCM 3.184, MPLAB 6.4 ICD2 WinXP SP1

I have the HCS300 hooked up to a PIC16F877, which receives the codeword and serial number from a PC to program into the chip.

HCS300 -> PIC16F877
S2 -> Porta,0
PWM -> Porta,1
VCC -> Porta,2

http://www.microchip.com/1010/pline/security/keysecat/encoders/devices/hcs300/index.htm

I found this application note AN217 [url] http://www.microchip.com/1010/suppdoc/appnote/all/an217/index.htm
[/url] which seems to do pretty much what I need it to. The relevant section about programming and verifying is this:

Programming:
Code:


M_PROG_INIT    bcf DATA ; DATA=0                 
      bcf CLK ; CLK=0                               
      bsf HCSVDD ; HCS POWER ON                     
      BANK1                                         
      movlw K_MASKPA_PROG                           
      movwf TRISA                                   
      BANK0                                         
      call WAIT_16MSEC                             
                                                   
      M_PROG_SETUP bsf CLK ; DATA=0, CLK=1         
      movlw Tps ; WAIT Program mode Setup Time (Tps)
      call WAIT_WMSEC                               
                                                   
      bsf DATA ; DATA=1, CLK=1                     
      movlw Tph1 ; WAIT Program Hold Time 1 (Tph1) 
      call WAIT_WMSEC

                             
      bcf DATA ; DATA=0, CLK=1                                                                                                               
      movlw Tph2 ; WAIT Program Hold Time 2 (Tph2)                                   
      call WAIT_uS                                                                   
                                                                                     
      M_PROG_BULK_ER bcf CLK ; DATA=0, CLK=0                                         
      movlw Tpbw ; WAIT Program Bulk Write Time (Tpbw)                               
      call WAIT_WMSEC         



which I translated in C to:
Code:

#define TPS         4      // Program mode setup time    TPS    3.5    4.5 ms
#define TPH1      4      //  Hold time 1          TPH1    3.5    — ms
#define TPH2      62      //  Hold time 2          TPH2    50       — us
#define TCLKH      62      //  Clock high time       TCLKH    50       — us
#define TCLKL      62      //  Clock low time          TCLKL    50       — us
#define TWC       60       //  Program cycle time       TWC    50       — ms
#define TBPW       5      //  Bulk Write time       TPBW    4.0    — ms


Count16 = 0;
output_low(CLK);   
output_low(DATA);
output_high(VCC);// powers on the HCS300
delay_ms(16);
output_high(CLK);
delay_ms(TPS);
output_high(DATA);
delay_ms(TPH1);
output_low(DATA);
delay_us(TPH2);
output_low(CLK);
delay_ms(TBPW);

.. So far so good.. as far as I can tell I create the correct waveform for erasing the HCS300 to enter program mode.


Last edited by Kasper on Tue Feb 17, 2004 2:15 pm; edited 1 time in total
Kasper



Joined: 14 Jan 2004
Posts: 88
Location: Aurora, Ontario, Canada

View user's profile Send private message Visit poster's website

PostPosted: Tue Feb 17, 2004 2:10 pm     Reply with quote

Continued from above

for shifting out the data to the HCS300, they had the following code:
Code:



;--------------- ; CLOCK INTO HCS THE WORDS TO BE PROGRAMMED                             
      clrf TMP_CNT ; NUMBER OF WORD TRASMITTED                                       
      movlw WORD0 ; SET INDIRECT PONTER TO INIT EE MAP                               
      movwf FSR                                                                     
                                                                                     
      M_NEW_WORD call PREPARE_WRD                                                   
                                                                               
;--------------- ; OUTPUT WORD ROTATE                                                   
      clrf TXNUM ; NUMBER OF BIT TRASMITTED FOR EACH WORD                           
                                                                               
M_TX_BIT    bsf CLK ; CLK=1                                                       
      clrc                                                                           
      rrf WRD_HI, F ; ROTATE BIT TO OUTPUT                                           
      rrf WRD_LO, F ; into CARRY FLAG                                               
      skpnc                                                                         
      goto M_PROG_DHI                                                               
      nop                                                                           
M_PROG_DLO    bcf DATA ; DATA=0                                                   
      goto M_PROG_BIT                                                               
M_PROG_DHI    bsf DATA ; DATA=1                                                   
                                                                               
M_PROG_BIT    movlw Tclkh                                                         
      call WAIT_uS ; DELAY                                                           
      bcf CLK ; CLK=0                                                               
      movlw Tclkl                                                                   
      call WAIT_uS ; DELAY                                                           
;---------------                                                                         
M_PROG_CHK_WORD incf TXNUM, F ; INCREMENT NUMBER OF BIT TRASMITTED             
      movlw .16 ; CHECK IF END OF WORD TRASMITTED (16 BITS)                         
      xorwf TXNUM, W                                                                 
      skpz                                                                           
      goto M_TX_BIT ; TRASMIT NEXT BIT                                               
                                                                                         
;--------------- ; END OUTPUT WORD                                                       
M_END_WORD    bcf DATA ; DATA=0                                                   
      movlw Twc ; WAIT FOR WORD Write Cycle Time (Twc)                               
      call WAIT_WMSEC                                                               
                                                                                         
;---------------                                                                         
M_CECHK_PRG_END incf TMP_CNT, F ; INCREMENT NUMBER OF WORD PROGRAMMED         
      movlw NUM_WRD ; CHECK NUMBER OF WORD TRASMITTED                               
      xorwf TMP_CNT, W                                                               
      skpz                                                                           
      goto M_NEW_WORD ; PROGRAM NEW WORD                                             
                                                                                         
;----------------------------------------------------------------------------------------
 


which I have translated to

Code:
 

for(i=0;i<(192);i++) {   //12 Words

      output_high(CLK);
      output_bit(DATA,shift_right(TXtemp,TXBufferSize,0)); // shifts out the data from the receiveBuffer
      delay_us(TCLKH);
      output_low(CLK);
      delay_us(TCLKL);
      if(Count16++ >= 16) { // every 16 bits shifted, wait a tad longer to let it write.
         output_low(DATA);
            Count16 = 0;       
             delay_ms(TWC);
      }

} // end i for 
Kasper



Joined: 14 Jan 2004
Posts: 88
Location: Aurora, Ontario, Canada

View user's profile Send private message Visit poster's website

sorry for the multiple posts.
PostPosted: Tue Feb 17, 2004 2:12 pm     Reply with quote

Continued from above

then finally the verify routine

Code:


;----------------------------------------------------------------------------------------
; VERIFY ROUTINE                                                                         
;----------------------------------------------------------------------------------------
M_VERIFY                                                                           
      BANK1                                                                         
      movlw K_MASKPA_VERI ; I/O TRISTATE FOR VERIFY                                 
      movwf TRISA                                                                   
      BANK0                                                                         
      clrwdt                                                                         
      movlw WORD0 ; SET INDIRECT POINTER TO INIT EE MAP                             
      movwf FSR                                                                     
                                                                               
      clrf TMP_CNT ; NUMBER OF WORDS RECIVED                                         
      clrf TXNUM ; NUMBER OF BIT RECEIVED FOR EACH WORD                             
;---------------                                                                         
M_VER_BITIN    clrc ; RECIVE DATA BIT FROM HCS FOR VERIFY                         
      btfsc DATA ; TEST and ROTATE RECEIVED BIT INTO WORD BUFFER                     
      setc                                                                           
      rrf WRD_HI, F                                                                 
      rrf WRD_LO, F                                                                 
      incf TXNUM, F                                                                 
      movlw .16                                                                     
      xorwf TXNUM, W ; TEST IF RECEIVED A COMPLETE WORD                             
      skpz                                                                           
      goto M_VER_CLKHI                                                               
;---------------                                                                         
M_VERIFY_WORD    movf WRD_LO, W ; 16th BIT RECIVED (WORD) -> VERIFY WORD         
      xorwf INDF, W                                                                 
      skpz                                                                           
      goto PROG_ERR ; WORD LOW VERIFY ERROR                                         
      incf FSR, F                                                                   
      movf WRD_HI, W                                                                 
      xorwf INDF, W                                                                 
      skpz       

      goto PROG_ERR ; WORD HIGH VERIFY ERROR                                                                                         
      incf FSR, F                                                 
      incf TMP_CNT, F                                             
      movlw NUM_WRD                                               
      xorwf TMP_CNT, W ; TEST IF RECEIVED ALL THE WORDS PROGRAMMED
      skpnz                                                       
      goto PROG_SUCCESS ; ALL 12 WORDS VERIFIED WITH SUCCESS     
      clrf TXNUM                                                 
;---------------                                                     
M_VER_CLKHI    bsf CLK ; CLK=1                                 
      movlw Tclkh ; WAIT TIME CLOCK HIGH                         
      call WAIT_uS                                                                                                   
M_VER_CLKLO    bcf CLK ; CLK=0                                 
      movlw Tclkl ; WAIT TIME CLOCK LOW                           
      call WAIT_uS                                               
      goto M_VER_BITIN                                           
       
 


which I translated to:

Code:

output_float(DATA);
delay_ms(TWC);   


// now for reading back:

for(i=0;i<(192);i++) {
   shift_right(RXBuffer,txBufferSize,input(DATA)); // shifts in data from the keyloqq   
   output_high(CLK);
   delay_us(TCLKH);   
   output_low(CLK);
   delay_us(TCLKL);
   }// end of for.
output_low(CLK);   
output_low(DATA);
output_low(VCC);
 



the problem is that I can never verify a chip... even though the signals going to it look perfect on the scope, the data which I can shift back from it is garbage. Does anyone have an idea what I should do to make this work?
Kasper



Joined: 14 Jan 2004
Posts: 88
Location: Aurora, Ontario, Canada

View user's profile Send private message Visit poster's website

PostPosted: Tue Feb 17, 2004 3:05 pm     Reply with quote

OK.. I am a dummy.. but I figured that posting the problem would make me solve it.

here is the complete write + verify routine, which works Smile

Code:


void WriteKeyloqq(void) {// now the array is filled with nasty ascii characters, which have to be converted to something useful
long int i;
int Count16;

for (i=0;i<TXBufferSize ;i++) {
   TXBuffer[i] = (Ascii2Hex(Receivebuffer[i*2])<<4) | (Ascii2Hex(Receivebuffer[(i*2) + 1])); //(TXBufferSize-1)-i
}
Count16 = 0;
output_low(CLK);   
output_low(DATA);
output_high(VCC);// powers on the HCS300
delay_ms(16);
output_high(CLK);
delay_ms(TPS);
output_high(DATA);
delay_ms(TPH1);

output_low(DATA);
delay_us(TPH2);
output_low(CLK);
delay_ms(TBPW);
/////////////////////////////////////////////////////////////
//this should enter the Keyloqq encoder in programming mode//
/////////////////////////////////////////////////////////////
for(i=0;i<(192);i++) {   //12 Words

      output_high(CLK);
      output_bit(DATA,shift_right(TXBuffer,TXBufferSize,0)); // shifts out the data from the receiveBuffer
      delay_us(TCLKH);
      output_low(CLK);
      delay_us(TCLKL);
      if(Count16++ >= 15) { // every 16 bits shifted, wait a tad longer to let it write.
         output_low(DATA);
            Count16 = 0;      
             delay_ms(TWC);
      }

} // end i for
//output_low(CLK);   
//output_low(DATA);
output_float(DATA);
delay_ms(TWC);   


// now for reading back:

for(i=0;i<(192);i++) {
   shift_right(RXBuffer,txBufferSize,input(DATA)); // shifts in data from the keyloqq   
   output_high(CLK);
   delay_us(TCLKH);   
   output_low(CLK);
   delay_us(TCLKL);
   }// end of for.
output_low(CLK);   
output_low(DATA);
output_low(VCC);
//now spit out what was put in the receivebuffer.
for(i=0;i<(TXBufferSize);i++) {
   printf("%X ",RXBuffer[i]);
   RXBuffer[i] = 0;
   }
output_high(BUZZER);
delay_ms(100);
output_low(BUZZER);
output_high(LED);
// taadaa.. the keyloqq device should now be programmed.. yay.. if not, it is the computer who is to test the data and reprogram.


}// end function

Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group