⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pic.txt

📁 pic单片机对红外设备控制得一个源程序
💻 TXT
📖 第 1 页 / 共 5 页
字号:
  MOVLW    RTC_NUM
  MOVWF    TMR0        ;setup for next interrupt
  CLRWDT

  ;Do interrupt actions
  INT_HANDLER 

  ;Clear interrupt sources
; BCF      INTCON,RBIF ;clear interrupt from RB<7:4>
; BCF      INTCON,INTF ;clear interrupt from RB0
  BCF      INTCON,T0IF ;clear interrupt from timer 0

  ;Restore registers and return
  SWAPF    TEMP_STAT,W ;get and unswap STATUS
  MOVWF    STATUS      ;restore STATUS
  SWAPF    TEMP_W,F    ;swap TEMP_W
  SWAPF    TEMP_W,W    ;unswap and restore W
  RETFIE               ;return from interrupt

;***** General Macros and Functions
;Select page 1
PAGE_1        MACRO
  BSF         STATUS,RP0   
  ENDM

;Select page 0
PAGE_0        MACRO
  BCF         STATUS,RP0   
  ENDM

;Main Inits
GEN_INIT
  ;Note!  This also sets up general operation of Ports.  If they
  ;are digital or analog, pullups enabled or not etc.
  ;Setup PORTA options
  IF   AD_ENABLE == TRUE
    PAGE_1      
    MOVLW     B'00000000'    ;all pins analog
    MOVWF     ADCON1^H'80'   ;setup PORTA function
    PAGE_0
  ELSE
    PAGE_1      
    MOVLW     B'00000011'    ;all pins digital
    MOVWF     ADCON1^H'80'   ;setup PORTA function
    PAGE_0
  ENDIF
  ;PortB no pullup, Prescaler to WDT.  See Page 2-355 '94 edition
  PAGE_1
  CLRWDT
  MOVLW       B'10001000' 
  MOVWF       OPTION_REG^H'80'
  PAGE_0
  ;other variables
  CLRF        TMR0
  CLRF        TASK_INDEX

  ;Call the init functions of modules that are needed
  IF  LCD_ENABLE == TRUE
    CALL      LCD_INIT
  ENDIF
  IF  R2_ENABLE == TRUE
    CALL      R2_INIT
  ENDIF
  IF  R4_ENABLE == TRUE
    CALL      R4_INIT
  ENDIF
  IF  IR_ENABLE == TRUE
    CALL      IR_INIT
  ENDIF
  IF  TW_ENABLE == TRUE
    CALL      TW_INIT
  ENDIF

  ;GIE enable, T0IE enable for interrupt mechanism
  MOVLW       B'10100000'
  MOVWF       INTCON
  CLRWDT
  RETURN

;***** X-10 FUNCTIONS.  Due to computed gotos, this has to be
;below program memory location FF.  Warnings are built in if the above is
;not met.
IF   TW_ENABLE == TRUE
;defs
  TW_PORT     EQU       PORTA
  TW_60       EQU       00H  ;60Hz crossing input
  TW_FROM     EQU       01H  ;data from house input
  TW_TO       EQU       02H  ;data to house output
  TW_10       EQU       (RUNSEC * D'11')/D'10000'
  TW_05       EQU       (RUNSEC * D'05')/D'10000' 

;Returns house code when given x10 code in W
TW_TO_HOUSE
  ANDLW       H'F'      ;mask upper nibble
  ADDWF       PCL,F     ;computed goto
  RETLW       'M'       ;returned if input = 0
  RETLW       'N'
  RETLW       'O'
  RETLW       'P'
  RETLW       'C'
  RETLW       'D'
  RETLW       'A'
  RETLW       'B'
  RETLW       'E'
  RETLW       'F'
  RETLW       'G'
  RETLW       'H'
  RETLW       'K'
  RETLW       'L'
  RETLW       'I'
  RETLW       'J'       ;returned if input = F
TW_TOH_END
IF TW_TOH_END > H'FE'
  CALL        PAGE_ERROR 
  ;Due to computed gotos, this should be in program memory below 0xFF
  ;See Application Note AN556, example 5
ENDIF

;Returns unit or function code when given x10 code in W
TW_TO_KEY
  ANDLW       H'1F'     ;mask upper 3 bits
  ADDWF       PCL,F     ;computed goto
  RETLW       'D'       ;returned if input = 0x0
  RETLW       'E'
  RETLW       'F'
  RETLW       'G'
  RETLW       '3'
  RETLW       '4'
  RETLW       '1'
  RETLW       '2'
  RETLW       '5'
  RETLW       '6'
  RETLW       '7'
  RETLW       '8'
  RETLW       'B'
  RETLW       'C'
  RETLW       '9'
  RETLW       'A'       ;returned if input = 0xf
  RETLW       'u'       ;all units off
  RETLW       'r'       ;hail request
  RETLW       'd'       ;dim
  RETLW       'n'       ;extended data (analog)
  RETLW       't'       ;on
  RETLW       'p'       ;pre-set dim
  RETLW       'a'       ;all lights off
  RETLW       'l'       ;status = off
  RETLW       'o'       ;all lights on
  RETLW       'h'       ;hail acknowledge
  RETLW       'b'       ;bright
  RETLW       's'       ;status = on
  RETLW       'f'       ;off
  RETLW       'p'       ;pre-set dim
  RETLW       'x'       ;extended code
  RETLW       'q'       ;status request
TW_TOK_END
IF TW_TOK_END > H'FE'
  CALL        PAGE_ERROR 
  ;Due to computed gotos, this should be in program memory below 0xFF
  ;See Application Note AN556, example 5
ENDIF

;Given a key code (in ASCII), this returns the X-10 code
TW_TO_XKEY
  MOVWF       TW_MATCH       ;  match = w;
  CLRF        TW_INDEX       ;  index = 0;
TW_TEST_KEY                  ;  while {
  MOVFW       TW_INDEX
  CALL        TW_TO_KEY      ;    if (w == match) {
  SUBWF       TW_MATCH,W
  SKPZ
  GOTO        TW_RETRY_KEY
  MOVFW       TW_INDEX       ;      return index
  RETURN
TW_RETRY_KEY                 ;    } else {
  MOVLW       D'32'          ;      if (index < 32) {
  SUBWF       TW_INDEX,W
  SKPNC
  GOTO        TW_NONE_KEY    ;      goto none_found
  INCF        TW_INDEX,F     ;      index ++;
  GOTO        TW_TEST_KEY    ;  } 
TW_NONE_KEY                  ;  none_found
  RETLW       H'80'          ;    return h'80'
  
;Given a house code (in ASCII), this returns the X-10 code
TW_TO_XHOUSE
  MOVWF       TW_MATCH       ;  match = w;
  CLRF        TW_INDEX       ;  index = 0;
TW_TEST                      ;  while {
  MOVFW       TW_INDEX
  CALL        TW_TO_HOUSE    ;    if (w == match) {
  SUBWF       TW_MATCH,W
  SKPZ
  GOTO        TW_RETRY
  MOVFW       TW_INDEX       ;      return index
  RETURN
TW_RETRY                     ;    } else {
  MOVLW       D'16'          ;      if (index < 16) {
  SUBWF       TW_INDEX,W
  SKPNC
  GOTO        TW_NONE_FOUND  ;        goto none_found
  INCF        TW_INDEX,F     ;      index ++;
  GOTO        TW_TEST        ;  } 
TW_NONE_FOUND                ;  none_found
  RETLW       H'80'          ;    return h'80'

;Initialize tw523 stuff
TW_INIT                      ;tw_init() {
  ;Ports
  PAGE_1
  BSF         TW_PORT,TW_60  ;  1 IS INPUT
  BSF         TW_PORT,TW_FROM;  0 IS OUTPUT
  BCF         TW_PORT,TW_TO
  PAGE_0
  CLRF        TW_FLAGS       ;  tw_flags = 0;
  BCF         TW_FLAGS,TW_PREV
  BTFSC       TW_PORT,TW_60  ;  tw_prev = input(tw_port,tw_60);
  BSF         TW_FLAGS,TW_PREV

  CLRF        TW_O_PHASE     ;  tw_o_phase = 0;
  CLRF        TW_O_HOUSE     ;  tw_o_house = 0;
  CLRF        TW_O_KEY       ;  tw_o_key = 0;
;Reset variables for start of x10 reception
TW_RESET
  CLRF        TW_SAMPLE      ;  tw_sample = 0;
  CLRF        TW_PHASE       ;  tw_phase = 0;
  CLRF        TW_HOUSE       ;  tw_house = 0;
  CLRF        TW_KEY         ;  tw_key = 0;
  RETURN                     ;}

;Get data from X10 interface.  New_tw() gets called if a valid
;message is received
TW_GET                       ;tw_get() {
  TSTF        TW_SAMPLE      ;  if (tw_sample == 0) {
  SKPZ
  GOTO        TW_SAMPLE_DATA ;    //check zero crossing
  BTFSS       TW_PORT,TW_60  ;    if (input(tw_port,tw_60) == 1) {
  GOTO        TW_60_LO
  BTFSC       TW_FLAGS,TW_PREV;     if (tw_prev == 0) {
  RETURN
  MOVLW       TW_05          ;        tw_sample = tw_05;
  MOVWF       TW_SAMPLE
  BSF         TW_FLAGS,TW_PREV;       tw_prev = 1;
  RETURN                     ;      }
TW_60_LO                     ;    } else {
  BTFSS       TW_FLAGS,TW_PREV;     if (tw_prev == 1) {
  RETURN
  MOVLW       TW_05          ;        tw_sample = tw_05;
  MOVWF       TW_SAMPLE
  BCF         TW_FLAGS,TW_PREV;       tw_prev = 0;
  RETURN                     ;      }
TW_SAMPLE_DATA               ;    }
  MOVLW       D'1'           ;  } else if (tw_sample == 1) {
  SUBWF       TW_SAMPLE,W
  SKPZ
  GOTO        TW_WAIT        ;    // sample data
  CLRF        TW_SAMPLE      ;    tw_sample = 0;
  BSF         TW_FLAGS,TW_CARRIER
  BTFSC       TW_PORT,TW_FROM;    tw_carrier = ~input(tw_port,tw_from);
  BCF         TW_FLAGS,TW_CARRIER
  MOVLW       D'12'          ;    if (tw_phase >= 12) {
  SUBWF       TW_PHASE,W
  SKPC
  GOTO        TW_HOUSECODE   ;      // sample key code
  INCF        TW_PHASE,F     ;      tw_phase ++;
  BTFSS       TW_PHASE,W     ;      if (tw_phase,W == 1) {
  GOTO        TW_KEY_HALF
  CLRC                       ;        // first half bit
  RRF         TW_KEY,F       ;        tw_key >>
  BTFSC       TW_FLAGS,TW_CARRIER ;   if (tw_carrier = 1)
  BSF         TW_KEY,4       ;          set tw_key,4;
  GOTO        TW_SAMPLE_END
TW_KEY_HALF                  ;      } else {
                             ;        // second half bit
  BTFSS       TW_FLAGS,TW_CARRIER ;   if (tw_carrier == 1) {
  GOTO        TW_KEY_ELSE
  BTFSC       TW_KEY,4       ;          if (tw_key,4 != 0)
  CALL        TW_RESET       ;            tw_reset;
  GOTO        TW_SAMPLE_END
TW_KEY_ELSE                  ;        } else {
  BTFSS       TW_KEY,4       ;          if (tw_key,4 != 1)
  CALL        TW_RESET       ;            tw_reset;
  GOTO        TW_SAMPLE_END  ;        }
TW_HOUSECODE                 ;      }
  MOVLW       D'4'           ;    } else if (tw_phase >= 4) {
  SUBWF       TW_PHASE,W     ;      // sample house code
  SKPC
  GOTO        TW_SYNC_B
  INCF        TW_PHASE,F     ;      tw_phase ++;
  BTFSS       TW_PHASE,W     ;      if (tw_phase,W == 1) {
  GOTO        TW_HOUSE_HALF
  CLRC                       ;        // first half bit
  RRF         TW_HOUSE,F     ;        tw_house >>
  BTFSC       TW_FLAGS,TW_CARRIER;    if (tw_carrier = 1)
  BSF         TW_HOUSE,3     ;          set tw_house,3;
  GOTO        TW_SAMPLE_END
TW_HOUSE_HALF                ;        } else {
                             ;        // second half bit
  BTFSS       TW_FLAGS,TW_CARRIER ;   if (tw_carrier == 1) {
  GOTO        TW_HOUSE_ELSE
  BTFSC       TW_HOUSE,3     ;          if (tw_house,3 != 0)
  CALL        TW_RESET       ;            tw_reset;
  GOTO        TW_SAMPLE_END
TW_HOUSE_ELSE                ;        } else {
  BTFSS       TW_HOUSE,3     ;          if (tw_house,3 != 1)
  CALL        TW_RESET       ;            tw_reset;
  GOTO        TW_SAMPLE_END  ;        }
TW_SYNC_B
  MOVLW       D'3'           ;    } else if tw_phase == 3) {
  SUBWF       TW_PHASE,W
  SKPZ
  GOTO        TW_SYNC_A
  INCF        TW_PHASE,F     ;      tw_phase ++;
  BTFSC       TW_FLAGS,TW_CARRIER;  if (tw_carrier == 1)
  CLRF        TW_PHASE       ;        tw_phase = 0;
  GOTO        TW_SAMPLE_END  ;      }
TW_SYNC_A                    ;    } else {
  INCF        TW_PHASE,F     ;      tw_phase ++;
  BTFSS       TW_FLAGS,TW_CARRIER;  if (tw_carrier == 0)
  CLRF        TW_PHASE       ;        tw_phase = 0;
  GOTO        TW_SAMPLE_END
TW_SAMPLE_END                ;    }
  MOVLW       D'22'          ;    if (tw_phase == 22) {
  SUBWF       TW_PHASE,W
  SKPZ
  RETURN
  CALL        TW_NEW         ;      new_tw();
  CALL        TW_RESET       ;      tw_reset();
  RETURN                     ;    }
TW_WAIT                      ;  } else {
                             ;    wait until sample time
  DECF        TW_SAMPLE,F    ;    tw_sample --;
                             ;  }
  RETURN                     ;}

;trigger send.  Does not restore W register
TW_SEND                      ;tw_send() {
  ;Store data in converted form
  MOVFW       TW_T_HOUSE     ;  tw_t_house = converted(tw_t_house);
  CALL        TW_TO_XHOUSE
  MOVWF       TW_T_HOUSE
  BTFSC       TW_T_HOUSE,7   ;  if (tw_t_house,7 == 1)
  RETURN                     ;    return; // invalid command
  MOVFW       TW_T_KEY       ;  tw_t_key = converted(tw_t_key);
  CALL        TW_TO_XKEY
  MOVWF       TW_T_KEY
  BTFSC       TW_T_KEY,7     ;  if (tw_t_key,7 == 1)
  RETURN                     ; 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -