Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. ;**********************************************************************
  2. ;    Filename:  Walkie talkie alarm.asm                                  *
  3. ;    Date:  08.4.12                                                      *
  4. ;    File Version:                                                    *
  5. ;    Author:  Steve                                            *
  6. ;    PIC: 12F675                                                      *
  7. ;**********************************************************************
  8. ;   PINOUT:                                                           *
  9. ;   1,  +5V                                                           *
  10. ;   2,  TTL output to walkie talkie (GP5)                             *
  11. ;   3,  Analog in from sensor   (AN3)(GP4)                            *
  12. ;   4, MCLR - was GP3   MUST BE PULLED UP -poxy ICD3 won't let you use it  *
  13. ;   5,  GP3 (unused)                                                  *
  14. ;   6,  Analog in from sensor   (AN1)(GP1)                            *
  15. ;   7,  Analog in from sensor   (AN0)(GP0)                            *
  16. ;   8,  GND                                                           *
  17. ;**********************************************************************
  18.     list      p=12f675           ; list directive to define processor
  19.     #include <P12F675.inc>        ; processor specific variable definitions
  20. ;   errorlevel  -302              ; suppress message 302 from list file
  21.     __CONFIG   _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT
  22.  
  23. ;***** VARIABLE DEFINITIONS
  24.     #define     poswindow       d'10'
  25.  
  26. one_mS_loop             EQU     0x21
  27. n_mS_loop               EQU     0x22
  28. flags                   EQU     0x25
  29. counter                 EQU     0x26
  30. temp                    EQU     0x28
  31. max_A                   EQU     0x2A
  32. max_B                   EQU     0x2B
  33. max_C                   EQU     0x2C
  34.  
  35.  
  36. ;**********************************************************************
  37.     ORG     0x000             ; processor reset vector
  38.     goto    Init              ; go to beginning of program
  39. ;****************************************************************
  40. Init:
  41.    
  42.     bsf     STATUS,RP0      ; set file register bank to 1  
  43.     movlw   B'111111'       ; IP 0,1,2,3,4,5
  44.     movwf   TRISIO          ;              
  45.     call    0x3FF           ; retrieve factory calibration value
  46.     movwf   OSCCAL          ; update register with factory cal value
  47.     movlw   B'00001111'     ; Pulups on,n/a,internal clock,n/a,WDT has prescaler,/128
  48.     movwf   OPTION_REG      ;
  49.     clrf    INTCON          ;interupts off
  50.     movlw   b'1000000'
  51.     movwf   PIE1            ;Perif interupts off - Just EEdone interupt on 
  52.     clrf    VRCON           ;Vref Off (power off the comparator voltage, saves power)
  53.     clrf    WPU             ;
  54.     movlw   B'00000111'     ; n/a...,comparitor off
  55.     movwf   CMCON
  56.     movlw   B'01011011'     ;n/a,clock/8,GPIO 3,1,0  analog
  57.     movwf   ANSEL
  58.     bcf     STATUS,RP0      ; set file register bank to 0
  59.     movlw   B'00000001'     ;left justified ADC OP, ref vdd, Ch0 selected.
  60.     movwf   ADCON0
  61.     clrf    GPIO            ;Clear Port
  62.     movlw   0x07       
  63.     movwf   CMCON           ;Comparator Off
  64.     movlw   .250 ;300ms
  65.     call    the_wait
  66.     movlw   .150 ;300ms
  67.     call    the_wait
  68.     movlw   .150 ;300ms
  69.     call    the_wait
  70.     movlw   .150 ;300ms
  71.     call    the_wait        ;just a power up delay to let sensors settle down
  72.  
  73.     call    set_thresholds ;get stored values from EEPROM
  74.      
  75. ;*******************************************************************
  76. ; MAIN
  77. ;*******************************************************************
  78. main:
  79.     btfsc   GPIO,2          ;learn button pressed?
  80.     goto    learn_mode
  81. CH0:
  82.     bcf     ADCON0,2
  83.     bcf     ADCON0,3    ;ch0
  84.     movlw   2
  85.     call the_wait       ;let input settle
  86.     bsf     ADCON0,1    ;start ADC conversion
  87.     btfsc   ADCON0,1    ;done yet?
  88.     goto $-1            ; no - jump back a line
  89.     movfw   max_A       ;get threshold value   
  90.     subwf   ADRESH,W            ;check if value greater than max
  91.     btfsc   STATUS,C    ;if subtraction result was less than 0...  
  92.     bsf     flags,0     ;set a flag to say alarm should be triggered
  93. CH1:   
  94.     bsf     ADCON0,2
  95.     bcf     ADCON0,3    ;ch1
  96.     movlw   2
  97.     call the_wait
  98.     bsf     ADCON0,1
  99.     btfsc   ADCON0,1
  100.     goto $-1
  101.     movfw   max_B          
  102.     subwf   ADRESH,W            ;check if value greater than max
  103.     btfsc   STATUS,C
  104.     bsf     flags,0
  105. CH3:   
  106.     bsf     ADCON0,2
  107.     bsf     ADCON0,3    ;ch3
  108.     movlw   2
  109.     call the_wait
  110.     bsf     ADCON0,1
  111.     btfsc   ADCON0,1
  112.     goto $-1
  113.     movfw   max_C          
  114.     subwf   ADRESH,W            ;check if value greater than max
  115.     btfsc   STATUS,C
  116.     bsf     flags,0
  117.    
  118.     btfss   flags,0             ;will add de-bouncing here in due course
  119.     goto main
  120.    
  121. alarm:
  122.     clrf    GPIO
  123.     nop                     ;make sure that the output will be low when enabled
  124.     bsf     STATUS,RP0      ; set file register bank to 1  
  125.     movlw   B'011111'       ; IO5 is  output
  126.     movwf   TRISIO          ;           i.e. press the call button on the walkie talkie
  127.     bcf     STATUS,RP0
  128.  
  129.     movlw   .250 ;250ms
  130.     call    the_wait
  131.     movlw   .250 ;250ms
  132.     call    the_wait
  133.     movlw   .250 ;250ms
  134.     call    the_wait
  135.     movlw   .250 ;250ms
  136.     call    the_wait
  137.    
  138.     bsf     STATUS,RP0      ; set file register bank to 1  
  139.     movlw   B'111111'       ; IO5 is  input
  140.     movwf   TRISIO          ;           i.e release the call button on the walkie talkie           
  141.     bcf     STATUS,RP0
  142.     call    the_wait
  143.     movlw   .250 ;250ms
  144.     goto    main
  145.  
  146.    
  147. the_wait:
  148.     movwf   n_mS_loop       ;simple delay looks at W and delays W uS
  149. mid_wait_loop:
  150.     movlw   b'11111001'
  151.     movwf   one_mS_loop
  152. inner_wait_loop:
  153.     nop
  154.     decfsz  one_mS_loop,f
  155.     goto    inner_wait_loop
  156.     decfsz  n_mS_loop,f
  157.     goto    mid_wait_loop
  158.     return 
  159.    
  160. learn_mode:
  161.     movlw   .20
  162.     call    the_wait
  163.     bcf     ADCON0,2
  164.     bcf     ADCON0,3    ;Select ch0
  165.     movlw   2
  166.     call the_wait
  167.     bsf     ADCON0,1    ;get an ADC reading
  168.     btfsc   ADCON0,1   
  169.     goto $-1
  170.     movf    ADRESH,W    ;stick it in W
  171.     bsf     STATUS,RP0
  172.     movwf   EEDATA      ;put it ready to write to EEPROM
  173.     movlw   d'00'
  174.     movwf   EEADR       ;in address 00 
  175.     call    WriteEEPROM ;call routine to write to EEPROM
  176.    
  177.    
  178.     movlw   .20
  179.     call    the_wait
  180.     bsf     ADCON0,2
  181.     bcf     ADCON0,3    ;ch1
  182.     movlw   2
  183.     call the_wait
  184.     bsf     ADCON0,1
  185.     btfsc   ADCON0,1
  186.     goto $-1
  187.     movf    ADRESH,W
  188.     bsf     STATUS,RP0
  189.     movwf   EEDATA
  190.     movlw   d'01'
  191.     movwf   EEADR
  192.     call    WriteEEPROM
  193.    
  194.    
  195.     movlw   .20
  196.     call    the_wait
  197.     bsf     ADCON0,2
  198.     bsf     ADCON0,3    ;ch3
  199.     movlw   2
  200.     call the_wait
  201.     bsf     ADCON0,1
  202.     btfsc   ADCON0,1
  203.     goto $-1
  204.     movf    ADRESH,W
  205.     bsf     STATUS,RP0
  206.     movwf   EEDATA
  207.     movlw   d'02'
  208.     movwf   EEADR
  209.     call    WriteEEPROM
  210.    
  211.    
  212.     movlw   .40
  213.     call    the_wait
  214.     movlw   .240   
  215.     call    the_wait
  216.     goto  Init      ;all done, restart again
  217.    
  218.    
  219. set_thresholds:
  220.     ;A nominal in 00, B nominal in 01
  221.     bsf     STATUS,RP0
  222.     movlw   d'00'
  223.     movwf   EEADR           ;EEPROM address 00
  224.     call    ReadEEPROM      ;read the stored value 
  225.     movwf   temp
  226.     addlw   poswindow       ;add safety margin set at top of this code
  227.     movwf   max_A           ;store as threshold for channel A (channel 0)
  228.  
  229.     movlw   d'01'
  230.     movwf   EEADR
  231.     call    ReadEEPROM
  232.     movwf   temp
  233.     addlw   poswindow
  234.     movwf   max_B
  235.  
  236.     movlw   d'02'
  237.     movwf   EEADR
  238.     call    ReadEEPROM
  239.     movwf   temp
  240.     addlw   poswindow
  241.     movwf   max_C
  242.     return
  243.    
  244.    
  245.        
  246. ReadEEPROM:                         ;EEADR has address, data goes into W
  247.         bsf     STATUS,RP0
  248. wait_read: 
  249.         btfsc   EECON1,WR   ; wait for end of previous write
  250.         goto    wait_read
  251.         movwf   EEADR       ; select address
  252.         bsf     EECON1,RD   ; initiate READ
  253.         movf    EEDATA,W    ; transfer Byte to W
  254.         bcf     STATUS,RP0
  255.         return
  256.  
  257. WriteEEPROM:                        ;EEDATA is data, EEADR is address
  258.         bsf     STATUS,RP0           
  259. wait_write:
  260.         btfsc   EECON1,WR   ; wait for end of previous write
  261.         goto    wait_write
  262.         bsf     EECON1,WREN
  263.         movlw   0x55
  264.         movwf   EECON2      ; Write 55h
  265.         movlw   0xAA
  266.         movwf   EECON2      ; Write AAh
  267.         bsf     EECON1,WR   ; Set WR bit
  268.         bcf     EECON1,WREN
  269.         bcf     STATUS,RP0
  270.         return
  271. ;any comments h.ps.steve@gmail.com
  272.     end