miercuri, 15 ianuarie 2014

Afișarea codurilor telecomenzii NEC

      Despre protocolul NEC se pot găsi o mulțime de informații pe internet, iar pentru decodarea codului am găsit pe forumul picbasic ceva ce se poate folosi la realizarea unui montaj care afișează pe un LCD 2x16 adresa și comanda butonului apăsat. Eu am folosit 16F886, dar se poate adapta ușor la orice PIC.


 '****************************************************************  
 '* Name  : Decode NEC3.BAS                   
 '* Author : Nica Florin                     
 '* Notice : Decodare semnale telecomanda de modulator FM     
 '*     :                           
 '* Date  : 19.12.2013                      
 '* Version : 1.0                         
 '* Notes  : 16F886                        
 '*     :                           
 '****************************************************************  
   
 @ __config _CONFIG1,_WDT_OFF & _MCLRE_OFF & _LVP_OFF & _CP_OFF  
 @ __config _CONFIG2, _WRT_OFF  
      ;oscilator intern 4 MHz  
     
   ANSEL=0             ;porturile A si B sunt digitale  
   ANSELH=0  
   ADCON0.0=0  
   SSPCON.5=0            ;dezactiveaza portC serial  
   RCSTA.7=0  
 ;******** legaturi display 2x16 caractere *******  
  DEFINE LCD_DREG  PORTC       ;PortC folosit pentru date  
      DEFINE LCD_DBIT  4     ;pinul RC4 este primul pin de date conectat  
      DEFINE LCD_RSREG  PORTC   ;portC folosit pt. pinul RS  
      DEFINE LCD_RSBIT  2         ;RC2 - Register select pin  
      DEFINE LCD_EREG  PORTC   ;foloseste PortC pentru pinul E  
      DEFINE LCD_EBIT  3         ;RC3 - Enable pin  
      DEFINE LCD_BITS  4         ;bus pe 4 biti  
      DEFINE LCD_LINES  2         ;LCD cu doua randuri de caractere  
      Pause 500  
 ;******* variabile si constante ***********  
   Leader VAR WORD      ' will be up to 900 for a 9mS leader pulse  
   BtnVal VAR BYTE[32]    ' holds 32 pulse results  
   DByte1 VAR BYTE      ' address byte  
   DByte2 VAR BYTE      ' inverse of address byte  
   DByte3 VAR BYTE      ' command byte  
   DByte4 VAR BYTE      ' inverse of command byte  
   X   VAR BYTE      ' loop count  
   
     
   buzzer    var PortC.1    ; pin 12 - conexiune buzzer  
   IR_Receiver var PORTB.0    ; pin 21 - conexiune TSOP1736  
   
     
 ;******* intrari/iesiri **********    
   TrisB.0=1                           ;RB0 input                       
   TrisC.1=0               ;RC1 output       
    
  Main:   
    PULSIN IR_Receiver,0,Leader    ' leader pulse is ~9mS low-going  
    IF Leader < 850 THEN Main  
      
    FOR X = 0 TO 31          ' grab 32 incoming pulses  
      PULSIN IR_Receiver,1,BtnVal(X) ' now measuring high-going pulse widths  
    NEXT X  
      
    ' now we'll decode 4 bytes from 32 pulses  
    FOR X = 0 TO 7           ' sort 1st 8 pulses  
      IF BtnVal[X] > 150 THEN    ' > 150 x 10uS = > 1.5mS pulse period  
        DByte1.0[X]=1  
      ELSE  
        DByte1.0[X]=0  
      ENDIF  
    NEXT X  
      
    FOR X = 8 TO 15          ' sort 2nd 8 pulses, etc....  
      IF BtnVal[X] > 150 THEN  
        DByte2.0[X-8]=1  
      ELSE  
        DByte2.0[X-8]=0  
      ENDIF  
    NEXT X  
      
    FOR X = 16 TO 23  
      IF BtnVal[X] > 150 THEN  
        DByte3.0[X-16]=1  
      ELSE  
        DByte3.0[X-16]=0  
      ENDIF  
    NEXT X  
      
    FOR X = 24 TO 31  
      IF BtnVal[X] > 150 THEN  
        DByte4.0[X-24]=1  
      ELSE  
        DByte4.0[X-24]=0  
      ENDIF  
    NEXT X  
      Sound buzzer,[85,10]  
        
    Lcdout "Adress  ", dec DByte1  
    Lcdout $FE,$C0  
    Lcdout "Command ", dec DByte3  
    pause 3000  
    Lcdout $FE,1        ;clear display  
    Lcdout $FE,2        ;return home  
      
    GOTO Main  
                        
   

Niciun comentariu:

Trimiteți un comentariu