PIC18F452 Instruction

Discussion in 'Embedded Systems and Microcontrollers' started by StatEP, May 22, 2007.

  1. StatEP

    Thread Starter New Member

    May 3, 2007
    5
    0
    is this program good?
    PORTA 2: the sensor input.
    PORTA3 connect to PORTA0.

    Code ( (Unknown Language)):
    1.  
    2.  
    3.     list p=18f452
    4.  
    5.     ; Include file, change directory if needed
    6.     include "p18f452.inc"
    7.  
    8.  
    9.     ; Start at the reset vector
    10. Reset_Vector  code 0x000
    11.     goto Start                           ; Start application beyond vector area
    12.     code    0x002A  
    13. Start
    14.     clrf    PORTB                    ;Clear PORTB
    15.     clrf    TRISB                     ;PORTB all outputs,
    16.                                  ; A/D result on LEDs
    17.  
    18.     movlw   B'00010001'          ;Fosc/8, A/D enabled
    19.     movwf   ADCON0
    20.     movlw   B'00001010'              ;Left justify,2 analog channel
    21.     movwf   ADCON1               ;Vres+ = 5mV
    22.  
    23.     movlw   B'11000111'          ;TMR0 prescaler, 1:256
    24.     movwf   T0CON
    25.  
    26. Main
    27.     btfss   INTCON,TMR0IF           ;Wait for Timer0 to timeout
    28.     goto    Main
    29.     bcf INTCON,TMR0IF
    30.     bsf ADCON0,GO            ;Start A/D conversion
    31. Wait
    32.     btfss   PIR1,ADIF              ;Wait for conversion to complete
    33.     goto    Wait
    34.  
    35.  
    36.     NOP
    37.     MOVFF   ADRESH,WREG
    38.     MOVFF   WREG,W
    39.     SUBLW   B'11111111'
    40.     BNZ     Less_than_5mv       ;if <> 0xff, go less than 5mv
    41.     MOVFF   ADRESL,WREG         ;else
    42.     ANDLW   0x80               
    43.     SUBLW   0x80
    44.     BNZ     Less_than_5mv   ;if <> 0xc0, go less than 5mv
    45.     GOTO    Than_5mv                ;otherwise, >5mv
    46.  
    47. Less_than_5mv
    48.     MOVFF   W,WREG
    49.     NOP
    50.     ADDLW   0x00
    51.    
    52.     ADDLW   B'10011010'     ;0x66 + 0x99 -> WREG
    53.     BZ     Sub_P2           ;if Zero, go check Less or high than 2mv
    54.     BNC   Less_2mv    ;if no Carry, Less Than 2mv
    55.     GOTO    Less_5mv_than_2mvS1
    56.  
    57. Sub_P2
    58.     MOVFF   ADRESL,WREG
    59.     ADDLW   0x00
    60.     ANDLW   0xc0               
    61.  
    62.     ADDLW   B'01000000'
    63.     BC     Less_5mv_than_2mvS1
    64.     goto      Less_2mv     
    65.  
    66. Than_5mv
    67.     MOVLW   B'00000100'         ;RB2 ON
    68.     goto    Disp_PB
    69.  
    70. Less_5mv_than_2mvS1
    71.     MOVLW   B'00000010'         ;RB1 ON
    72.     goto    Disp_PB
    73.  
    74. Less_2mv
    75.     MOVLW   B'00000001'         ;RB0 ON
    76.     goto    Disp_PB
    77. Disp_PB
    78.     movwf   PORTB               ;Write A/D result to PORTB
    79.  
    80.     clrf    PORTB
    81. WaitPush            ;Pause while switch is pressed
    82.     btfss   PORTA,4
    83.     goto    WaitPush
    84.  
    85.     movwf   PORTB
    86.     goto    Main        ;Do it again
    87.  
    88.     end
    89.  
    _______________________________________________________________________________________________________

    This code is to read the values made from the sensor. Upper limit set. LED Lights up when it goes over the limit.
    i'm open to suggestion
     
Loading...