1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Pic16f818

Discussion in 'Microcontrollers' started by uaefame, Oct 21, 2008.

  1. uaefame

    uaefame New Member

    Joined:
    Feb 22, 2008
    Messages:
    271
    Likes:
    9
    Location:
    Dubai, UAE
    Good afternoon,

    I am trying to turn an LED on and off every 2 second. I am using PIC 16F818. I am interested in using internal timming and I am setting it to 4MHz.

    Here are my codes
    Code (text):
      list      p=16F818             ; list directive to define processor
        #include <p16F818.inc>         ; processor specific variable definitions

    __config H'3F10'

        movlw          b'01100000'
        movwf          OSCCON

        cblock 0x0c
        d1,d2,d3
        endc

            bsf     STATUS,5
            movlw   00h
            movwf   TRISA
            bcf     STATUS,5

       
           
    START   movlw   02h
            movwf   PORTA
            call    Delay2sec
            movlw   00h
            movwf   PORTA
            call    Delay2sec
            goto    START


    Delay2sec
                ;1999996 cycles
        movlw   0x11
        movwf   d1
        movlw   0x5D
        movwf   d2
        movlw   0x05
        movwf   d3
    Delay2sec_0
        decfsz  d1, f
        goto    $+2
        decfsz  d2, f
        goto    $+2
        decfsz  d3, f
        goto    Delay2sec_0

                ;4 cycles (including call)
        return
        END      
     
    I would like to know why my LED doesn't function as I expect it is the problem in the coding because I was able to turn led on and off using PIC16F84a.

    My header file is as follow
    Code (text):
    Configuration Bits
                __CONFIG    H'3F10'
                        ;sets INTRC-A6 is port i/o
                        ;WDT off
                        ;PUT on
                        ;MCLR tied to VDD
                        ;A5 is I/O
                        ;BOD off
                        ;LVP disabled
                        ;Flash Program Write disabled
                        ;Background Debugger disabled
                        ;CCP function on B2
                        ;Code Protection disabled
     
    Help needed!

    Q> Is there a problem with my coding?
    Q> How can I set an internal timmer? I am following the datasheet page 38 of PIC16F818?

    Thanks in advance
     
  2. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,178
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    hi,
    You have the PIC reserved register area at 0x0c
    No origin
    and the code was not in order.

    This modified code now works in my simulator.:)

    Code (text):
     list      p=16F818             ; list directive to define processor
        #include <p16F818.inc>         ; processor specific variable definitions
     errorlevel -302, -207

      __config H'3F10'

        cblock 0x20;[COLOR="Red"]0c[/COLOR]
        d1,d2,d3
        endc

        org 0x0000

        movlw          b'01100000'
        movwf          OSCCON

       

            bsf     STATUS,5
            movlw   00h
            movwf   TRISA
            bcf     STATUS,5

       
           
    START   movlw   02h
            movwf   PORTA
            call    Delay2sec
            movlw   00h
            movwf   PORTA
            call    Delay2sec
            goto    START


    Delay2sec
                ;1999996 cycles
        movlw   0x11
        movwf   d1
        movlw   0x5D
        movwf   d2
        movlw   0x05
        movwf   d3
    Delay2sec_0
        decfsz  d1, f
        goto    $+2
        decfsz  d2, f
        goto    $+2
        decfsz  d3, f
        goto    Delay2sec_0

                ;4 cycles (including call)
        return
        END
     
    Last edited: Oct 21, 2008
  3. uaefame

    uaefame New Member

    Joined:
    Feb 22, 2008
    Messages:
    271
    Likes:
    9
    Location:
    Dubai, UAE
    Thanks eric

    Can you explain these lines

    Code (text):
    cblock 0x20;0c
    & this one

    Code (text):
    org 0x0000
    & this one

    Code (text):
    errorlevel -302, -207
    Sorry of the troubles

    Thanks in advance
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,205
    Likes:
    640
    Location:
    Derbyshire, UK

    Sets the start address for the GPR's, 0x0C was for the 16F84 and is wrong for more modern devices.

    Sets the 'origin', in this case the start address of the program.

    Turns off some of the assembler warning messages.
     
  6. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,178
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    Its no trouble.:)

    Look at the image for the registers.

    Always get a datasheet for the PIC you are working on.
     

    Attached Files:

    Last edited: Oct 21, 2008
  7. uaefame

    uaefame New Member

    Joined:
    Feb 22, 2008
    Messages:
    271
    Likes:
    9
    Location:
    Dubai, UAE
    Hmm thanks for the explaination I like it!!!!!!!

    BTW, its still not working I will post my circuit i am using
     

    Attached Files:

    Last edited: Oct 21, 2008
  8. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,178
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    hi,
    Thats because you are switching pin 18 in your program, not 17.!

    Code (text):
                   movlw    [COLOR="Red"]02h[/COLOR];;; 01h
            movwf   PORTA
            call    Delay2sec
            movlw   00h
            movwf   PORTA
     
    Last edited: Oct 21, 2008
  9. uaefame

    uaefame New Member

    Joined:
    Feb 22, 2008
    Messages:
    271
    Likes:
    9
    Location:
    Dubai, UAE
    Still not working!!!

    Here is the update of my schematic thanks eric for spotting that out.

    Here is what happen When i turn the power supply on the LED lights on and keep lighting on.


    BTW, if you can help me thanks again :)
     

    Attached Files:

  10. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,178
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    Check the config.

    Rechecking.

    EDIT:
    bit 5 is low so MCLR is internal, you have a resistor attached.
    this shouldnt cause a problem.
     
    Last edited: Oct 21, 2008
  11. uaefame

    uaefame New Member

    Joined:
    Feb 22, 2008
    Messages:
    271
    Likes:
    9
    Location:
    Dubai, UAE
    I print screen my config so you can have a look into it

    Thanks for replying :)
     

    Attached Files:

  12. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,178
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    I have just programmed a 16F818, If I recall correctly dosnt the internal clock frequency default to 32KHz.!!!
    Its working the LED but VERY slowly,,,

    You have to change it to 4MHZ within the program.
     
  13. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,178
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    hi,
    I see what you have done wrong.

    The OSCCON setting is on BANK0 it should be BANK1

    Move the OSCCON and it works OK.

    The LED is flashing at 2secs in a PIC.

    Code (text):

            bsf     STATUS,5
            movlw   00h
            movwf   TRISA
                    movlw          b'01100000'
                movwf          OSCCON

            bcf     STATUS,5
     
     
    Last edited: Oct 21, 2008
  14. uaefame

    uaefame New Member

    Joined:
    Feb 22, 2008
    Messages:
    271
    Likes:
    9
    Location:
    Dubai, UAE
    Amazing eric, I Really should consider you on my robotic team!!

    My robotic team consist of All mechanical students. I am the only guy who know few on electrical stuff. But eric if you join us we will rule hehe :).

    Thanks again
     
    Last edited: Oct 21, 2008
  15. uaefame

    uaefame New Member

    Joined:
    Feb 22, 2008
    Messages:
    271
    Likes:
    9
    Location:
    Dubai, UAE
    General question?

    Does all PIC contain PWM? If not does PIC 16F84a or PIC 16F818 contains it?

    I am trying to control a servo motor using PWM i need to send a signal of 1ms to reach 0 degree 2ms to reach 180degree and so on.
     
  16. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,205
    Likes:
    640
    Location:
    Derbyshire, UK
    Do it in software, it's not asy using the PWM module, although by using interrupts and reseting it continually it can be done.

    The 84 doesn't have PWM, no idea on the 818, check the datasheet.
     
  17. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,178
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    Yes the 16F818 does have PWM.

    Post the program you have written for the PWM, I'll work thru it with you.OK.:)
     
  18. uaefame

    uaefame New Member

    Joined:
    Feb 22, 2008
    Messages:
    271
    Likes:
    9
    Location:
    Dubai, UAE
    Does PWM concept work like this?

    Code (text):
        list      p=16F818             ; list directive to define processor
        #include <p16F818.inc>         ; processor specific variable definitions
        errorlevel -302, -207

    __config H'3F10'

        cblock 0x20
        d1,d2,d3
        endc

        org 0x0000


            bsf     STATUS,5
            movlw   00h
            movwf   TRISA  
            movlw   b'01100000'
            movwf   OSCCON
            bcf     STATUS,5

       
           
    START   movlw   02h
            movwf   PORTA
            call    Delay1ms
            movlw   00h
            movwf   PORTA
            call    Delay1ms
            goto    START

    Delay1ms
                ;993 cycles
        movlw   0xC6
        movwf   d1
        movlw   0x01
        movwf   d2
    Delay1ms_0
        decfsz  d1, f
        goto    $+2
        decfsz  d2, f
        goto    Delay1ms_0

                ;3 cycles
        goto    $+1
        nop

                ;4 cycles (including call)
        return

        END      
    Or there is a different way of doing it?

    If there is please tell me ? Thanks again for offering help !!

    Thanks in advance
     
  19. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,178
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
  20. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,205
    Likes:
    640
    Location:
    Derbyshire, UK
    I thought - WHERE? :D

    Then I remembered, it's NOT for servo control, but just a simple example of how to use the two channel hardware PWM for speed and direction control of a small robot. I added it specifically because I was getting multiple questions a week about how to do this using the PWM modules.
     
  21. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,178
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    hi,
    IIRC you did post the 'above' comment some weeks ago, thats why I knew they PWM did exist on your tutorials.!

    It just shows you, members do read and remember your posts.:p
     

Share This Page