Connect with us

First steps with PIC16F819

Discussion in 'Electronic Design' started by Robbs, Jul 26, 2003.

Scroll to continue with content
  1. Robbs

    Robbs Guest

    I've written/plagiarized the following, as my first attempt to do anything
    with a microcontroller:

    ------------------------------------------------------begin code------


    list p=16f819 ; list directive to define processor
    #include <p16F819.inc> ; processor specific variable definitions

    errorlevel -302 ; suppress message 302 from list file

    __CONFIG _CP_OFF & _WRT_ENABLE_OFF & _CPD_OFF & _CCP1_RB2 & _DEBUG_OFF &
    _LVP_OFF & _BODEN_OFF & _MCLR_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_IO


    main

    BANKSEL PORTA ; select bank of PORTA
    CLRF PORTA ; Initialize PORTA by
    ; clearing ouput
    ; data latches
    BANKSEL ADCON1 ; Select Bank of ADCON1
    MOVLW 0x06 ; Configure all pins
    MOVWF ADCON1 ; as digital inputs
    MOVLW 0x00 ; Value used to
    ; inititalize data
    ; direction
    MOVWF TRISA ; Set RA<7:0> as outputs
    MOVLW 0xAA ; Output '10101010b' to
    ; PORTA (I hope)


    ; initialize eeprom locations

    ORG 0x2100
    DE 0x00, 0x01, 0x02, 0x03


    END ; directive 'end of program'

    -----------------------------------------------------------end
    code-------------


    Will this do what I hope it will, simply output 10101010b (0xAA) to PORTA?
    If so, is it safe to check the pins with a voltmeter between the I/O pin and
    Vss? would a series resistor be wise?

    Thank you very much,

    Robbs
     
  2. Robbs

    Robbs Guest

    Funny how seeing your post in an actual ng can make you see errors. Sorry,
    with corrections:
    ------------------------------------------------------------

    list p=16f819 ; list directive to define processor
    #include <p16F819.inc> ; processor specific variable definitions

    errorlevel -302 ; suppress message 302 from list file

    __CONFIG _CP_OFF & _WRT_ENABLE_OFF & _CPD_OFF & _CCP1_RB2 & _DEBUG_OFF &
    _LVP_OFF & _BODEN_OFF & _MCLR_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_IO


    main

    BANKSEL PORTA ; select bank of PORTA
    CLRF PORTA ; Initialize PORTA by
    ; clearing ouput
    ; data latches
    BANKSEL ADCON1 ; Select Bank of ADCON1
    MOVLW 0x06 ; Configure all pins
    MOVWF ADCON1 ; as digital inputs
    MOVLW 0x00 ; Value used to
    ; inititalize data
    ; direction
    MOVWF TRISA ; Set RA<7:0> as outputs
    MOVLW 0xAA ; Output '10101010b' to
    ; PORTA (in theory)


    ; initialize eeprom locations

    ORG 0x2100
    DE 0x00, 0x01, 0x02, 0x03


    END ; directive 'end of program'
     
  3. Robbs

    Robbs Guest

    Sorry. With corrections:



    list p=16f819 ; list directive to define processor
    #include <p16F819.inc> ; processor specific variable definitions

    errorlevel -302 ; suppress message 302 from list file

    __CONFIG _CP_OFF & _WRT_ENABLE_OFF & _CPD_OFF & _CCP1_RB2 & _DEBUG_OFF &
    _LVP_OFF & _BODEN_OFF & _MCLR_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_IO


    main

    BANKSEL PORTA ; select bank of PORTA
    CLRF PORTA ; Initialize PORTA by
    ; clearing ouput
    ; data latches

    BANKSEL TRISA ; Select Bank of TRISA
    MOVLW 0x00 ; Value used to
    ; inititalize data
    ; direction
    MOVWF TRISA ; Actually write the
    ; value to the register

    BANKSEL PORTA ; select bank of PORTA
    MOVLW 0xAA ; Output '10101010b' to
    ; PORTA (in theory)
    MOVWF PORTA ; Set RA<7:0> as outputs



    ; initialize eeprom locations

    ORG 0x2100
    DE 0x00, 0x01, 0x02, 0x03


    END ; directive 'end of program'


    ------------------------------------------end code-----------
    Will this do what I hope it will, simply output 10101010b (0xAA) to PORTA?
    If so, is it safe to check the pins with a voltmeter between the I/O pin and
    Vss? would a series resistor be wise?

    Is the last bit of code necessary, under 'initialize eeprom locations'? it
    was part of the example code supplied with MPLABS, and I'm not sure exactly
    what it does.

    I realize that one pin on PORTA (pin 5, i think) is an input only.

    Thank you very much,

    Robbs
     
  4. Ishaan Dalal

    Ishaan Dalal Guest

    CLRF TRISA will do just as well.
    Code looks OK, comments are skewed -- MOVLW 0xAA moves the literal to the W
    reg, while MOVWF PORTA actually outputs 0xAA to PORTA.
    Yes; no series resistor needed; but 8 LEDs with series resistors might be
    easier to decode.
    Not for whatever your main code does.
    Then that pin is permanently tri-stated -- you will see nothing on that pin,
    unless you force it by using a pull-up or pull-down resistor.
     
  5. Leon Heller

    Leon Heller Guest

    It would be a good idea to end main with something like

    stop:
    goto stop

    checking with a meter is OK, you don't need a resistor.

    Leon
     

  6. Your program will do unexpected things after it crashes at the end of your code.
    This is not a PC, you must provide some code to either rerun some of the same
    code, some other code or do nothing. The way you wrote it, it will run whatever
    garbage follows your code.
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-