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. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,328
    Likes:
    653
    Location:
    Derbyshire, UK
    ONLINE
    You're only showing part of the complete code, so it's out of context - but I would imagine that before it enters the code section you've shown it was set to bank 1, so the first instruction is to return to bank 0 (the place you normally want to be). In this particular case, the next line sets bank 1 again, so it's likely that BOTH lines could be left out if required.

    It's also highly like that the different code segments were 'cut and pasted' together, which is probably why it occurred - but it does no harm.
     
  2. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    170
    Location:
    Toronto, Canada
    Since you're so stuck on banksel why not learn the STATUS, RP0 method instead.
     
  3. uaefame

    uaefame New Member

    Joined:
    Feb 22, 2008
    Messages:
    271
    Likes:
    9
    Location:
    Dubai, UAE
    I am doing fine, Thanks again for your concern blueroomelectronics. Thanks everyone for replying

    Can someone explain these lines? Why .126 not .128??

    I don't know why I became interested in PIC so suddenly. Maybe because of the mystery that it covers.
     
    Last edited: Oct 25, 2008
  4. dave

    Dave New Member

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


     
  5. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,238
    Likes:
    645
    Location:
    Ex Yorks' Hants UK

    hi Hesham,

    Do you have a copy of the 16F818 datasheet.?

    Study the attached image.
    Its important that you understand the Bank selection.

    I would suggest 'learners' of assembly language, use the STATUS register bits RP1 amd RP0 for Bank selection.

    Once they understand the Bank selection process then start using the Banksel directive.


    EDIT:
    The 126 [7Eh], as explained in the program comments, at 127 [ 7Fh] 0111,1111 the motor is running forward, permanently ON.

    For the value 7Fh is 0111,1111.
    the next increment would be 128 [80h] 1000,0000, which in the comments is explained as a motor reversal.[ at the slowest speed].

    255 [FFh] 1111,111 is top speed in reverse.

    OK.?
     

    Attached Files:

    Last edited: Oct 26, 2008
  6. uaefame

    uaefame New Member

    Joined:
    Feb 22, 2008
    Messages:
    271
    Likes:
    9
    Location:
    Dubai, UAE
    We set 126? why 127 is still motor running forward?

    Another question what is the different between postscaler and prescaler?

    Can someone show me a flow diagram how to set the period to (1/280hz) & duty cycle to 22%? any idea? using PIC16F818.



    Thanks in advance
     
  7. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,238
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
    Hi Hesham,
    Most of the questions you are asking are explained in the PIC 16F818 datasheet, if you dont have one, goto Microchip Technology Inc. - a Leading Provider of Microcontroller and Analog Semiconductors and download a copy.
     
    Last edited: Oct 29, 2008
  8. uaefame

    uaefame New Member

    Joined:
    Feb 22, 2008
    Messages:
    271
    Likes:
    9
    Location:
    Dubai, UAE
    Let rephrase my question:

    Anyone know a good tutorial about Pwm REALLY basic tutorial!!!!

    I somehow get lost reading the manual! But I will try my best :)

    I didn't know that everything is explained in the manual? Then why there is tutorial over the internet.

    As mentioned, some times the manual get abit hard, therefore I am really interested in easy tutorial easy easy easy like one that eric keep explaining !!

    Hehe If I recommend a writer! ErIC :)

    yOU like explaining in a nice way :)

    Thanks
     
    Last edited: Oct 29, 2008
  9. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    170
    Location:
    Toronto, Canada
    Are you sure you want a PWM tutorial not a SERVO tutorial? PWM is similar but not the same as Servo.
    PWM is for speed control on DC motors, lights etc...
    Servo is a pulse no greater than 2ms at 20ms intervals.
    I've used the hardware PWM channel for a servo but it's very course.
     
  10. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,238
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
    hi Hesham,
    As Bill points out, you are looking for Servo information not PWM for motor control.

    I will have a look thru my database, see if I can find anything useful.:)

    I know I keep asking but have you built the 555 test rig,?
    With an oscilloscope you could study the servo's characteristics.

    It would be a useful tool if you plan to do lots of servo projects.
     
  11. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,238
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
  12. uaefame

    uaefame New Member

    Joined:
    Feb 22, 2008
    Messages:
    271
    Likes:
    9
    Location:
    Dubai, UAE
    Here are the codes written for driving a servo motor.

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

        __config H'3F10'

         
        cblock  0x20        ;start of general purpose registers
        count           ;used in delay routine
        count1          ;used in delay routine
        counta          ;used in delay routine
        countb          ;used in delay routine
        temp            ;temp storage
        ;rate           ;adc value
        endc



    Fwd1    equ 0x00        ;pin for left motor reverse
    Rev1    equ 0x01        ;pin for left motor forward

    ;pins 1 and 2 are the 1 PWM channel  

        org 0x0000
        nop         ;for bootloader compatibility
        nop
        nop
        goto    START
        org 0x0010

    START   call    Initialise

    MainLoop:
       
        movlw d'64'
        call Speed
        call Long_Delay

        movlw d'192'
        call Speed
        call Long_Delay

        movlw d'10'
        call Speed
        call Long_Delay

        movlw d'228'
        call Speed
        call Long_Delay

        goto MainLoop


    Initialise:

        banksel OSCCON
        movlw   b'01100000' ;uae1
        movwf   OSCCON
       
        banksel ADCON1      ;turn off A2D
        movlw   0x0E; AN0 analog, rest digital, left justified [255]
        movwf   ADCON1

        banksel PORTA
        banksel TRISB
        movlw   0       ;set PORTB as all outputs
        movwf   TRISB
        banksel PORTB

        movf    CCP1CON,W   ;set CCP1 as PWM
        andlw   0xF0
        iorlw   0x0C
        movwf   CCP1CON

        movlw   .126        ;set highest PWM value
        banksel PR2     ;over this (127) is permanently on
        movwf   PR2
        banksel TMR2

        movf    T2CON,W     ;set prescaler to 4;;;16
        andlw   0xF8        ;PWM at 2500HZ
        iorlw   0x01;;;2
        movwf   T2CON

        movf    T2CON,W     ;set postscaler to 1
        andlw   0x07
        iorlw   0x00
        movwf   T2CON

        clrf    CCPR1L      ;set PWM to zero

        bsf T2CON,TMR2ON    ;and start the timer running
        return

    Speed:                  ;use value in W to set speed (0-127)
        movwf   temp
        btfsc   temp,7      ;if more than 128 set speed in reverse
        call    ReverseMtr  ;so '1' is very slow forward
        btfss   temp,7      ;and '129' is very slow reverse
        call    ForwardMtr
        andlw   0x7F
        movwf   CCPR1L
        return


    ReverseMtr:
        bsf PORTB,Rev1  ;set pins for reverse
        bcf PORTB,Fwd1
        return

    ForwardMtr:
        bcf PORTB,Rev1  ;set pins for forward
        bsf PORTB,Fwd1
        return


    ;Delay routines  

    Long_Delay
        return

        movlw   .50     ;delay 5 seconds
        call    Delay100W
        return

    Delay100W movwf count       ;delay W x 100mS
    d2  call    Delay100    ;maximum delay 25.5 seconds
        decfsz  count   ,f
        goto    d2
        return

    Delay255 movlw  0xff        ;delay 255 mS
        goto    d0
    Delay100 movlw  .100        ;delay 100mS
        goto    d0
    Delay50 movlw   .50     ;delay 50mS
        goto    d0
    Delay20 movlw   .20     ;delay 20mS
        goto    d0
    Delay10 movlw   .10     ;delay 10mS
        goto    d0
    Delay1  movlw   .1      ;delay 1mS
        goto    d0
    Delay5  movlw   0x05        ;delay 5.000 ms (4 MHz clock)
    d0  movwf   count1
    d1  movlw   0xE7
        movwf   counta
        movlw   0x04
        movwf   countb
    Delay_0 decfsz  counta, f
        goto    $+2
        decfsz  countb, f
        goto    Delay_0

        decfsz  count1  ,f
        goto    d1
        return

    ;end of Delay routines  

        end
    Btw, The servo motor didn't rotate?
    I am actually still confuse how to set the period and the off and on time of the square wave.

    Thanks in advance
     
    Last edited: Nov 6, 2008
  13. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    170
    Location:
    Toronto, Canada
    You have almost no chance of building a line following 18 servo spider robot before the December deadline. My advice is to stop worrying about it and concentrate on learning C and PIC basics and enter the contest next year. You're in way over your head IMO.
     
  14. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,328
    Likes:
    653
    Location:
    Derbyshire, UK
    ONLINE
    That code is from my PWM tutorial, and doesn't feed a servo motor, it's for feeding two DC motors via H-bridges, so your servo isn't going to move (they don't 'rotate').

    I agree with Bill, you don't have a prayer of meeting your deadline - you've been asking questions for ages, and still haven't even started.
     
  15. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,238
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
    hi Hesham,
    That program is not for driving a servo.!

    Also a servo motor does not 'rotate' [ limited to about 180deg]

    Did you build the 555 servo tester.?

    You must get a grasp of the Basics otherwise you will be posting for weeks and miss the deadline.

    I would like to help, but you are not following our advice...:)

    EDIT: I think there is an echo in here.:D
     
    Last edited: Nov 6, 2008
  16. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,161
    Likes:
    340
    Location:
    Brisbane Australia
    As I stated two weeks ago, clicky. I like helping people but Uaefame is either too stupid or too lazy to learn anything. When he starts asking questions that can't be answered in 30 seconds with a quick google I may be less critical.

    Mike.
     
  17. uaefame

    uaefame New Member

    Joined:
    Feb 22, 2008
    Messages:
    271
    Likes:
    9
    Location:
    Dubai, UAE
    I took these codes from Pommie post the one he posted in page 2.

    Code (text):
    ;*******************************************************************
    ;                           16F877 PWM Program
    ;*******************************************************************

            include "p16f877.inc"

            errorlevel  -302
            radix   dec

        __CONFIG _CP_OFF & _DEBUG_ON & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _HS_OSC & _WRT_OFF & _LVP_OFF & _CPD_OFF

            cblock  20h
    OnTime:2
    OffTime:2
            endc

            cblock  71h
    int_work
    int_status
    int_pclath
            endc

            org     0h
            nop
            goto    start
            nop
            nop

    interupt    movwf   int_work
            swapf   STATUS,W
            movwf   int_status
            bcf STATUS,RP0
            bcf STATUS,RP1
            movfw   PCLATH
            movwf   int_pclath
            clrf    PCLATH

            btfsc   PORTB,0
            goto    TurnOff
    ; turn on the output and write the on time
            nop;        delay to make both path identical
            bsf PORTB,0;<<<<<<<<<< Break Point
            movfw   OnTime+1
            movwf   CCPR1H
            movfw   OnTime
            movwf   CCPR1L
            goto    DonePWM

    TurnOff     bcf PORTB,0;<<<<<<<<<< Break Point
            movfw   OffTime+1
            movwf   CCPR1H
            movfw   OffTime
            movwf   CCPR1L
    DonePWM
            bcf PIR1,CCP1IF;    reset special event trigger interupt

            movfw   int_pclath
            movwf   PCLATH
            swapf   int_status,W
            movwf   STATUS
            swapf   int_work,F; swap to file
            swapf   int_work,W; swap to work
            retfie


    start       bsf STATUS,RP0
            bcf STATUS,RP1
            bsf STATUS,IRP; all indirest access is to 100h - 1ffh
             movlw  (0<<NOT_RBPU|0<<INTEDG|0<<T0CS|0<<T0SE|0<<PSA|B'000'<<PS0)
            movwf   OPTION_REG
            movlw   b'11111110'
            movwf   TRISB
            bcf STATUS,RP0
            movlw   (b'01'<<T1CKPS0|0<<T1OSCEN|0<<NOT_T1SYNC|0<<TMR1CS|1<<TMR1ON)
            movwf   T1CON;      enable timer 1
            movlw   low(45000)
            movwf   CCPR1L
            movwf   OnTime
            movlw   high(45000)
            movwf   CCPR1H
            movwf   OnTime+1
            movlw   low(5000)
            movwf   OffTime
            movlw   high(5000)
            movwf   OffTime+1
            movlw   (0<<CCP1X|0<<CCP1Y|b'1011'<<CCP1M0);    enable special event trigger on CCP1
            movwf   CCP1CON;   
            bsf STATUS,RP0
            bsf PIE1,CCP1IE;    enable CCP1 interupt
            bcf STATUS,RP0

            movlw   (1<<GIE|1<<PEIE|0<<T0IE|0<<INTE|0<<RBIE|0<<T0IF|0<<INTF|0<<RBIF)
            movwf   INTCON;     enable Peripheral interupts

    Loop       
            goto    Loop


            END
    The codes is written for p16F876A
    I changed the include file "p16F877.inc" and the title to 16F877 when I run this it give me error still.

    What you think is wrong? BTW these code is written for PIC16F876A and it appear that the manual of both of them is same. Therefore, I assume the codes must be similar.

    My interested is to assemble these codes in my PIC16F877 if you have any idea what wrong while changing the codes from 16F876A till PIc 16F877 please tell me.

    Thanks again for the ace forum :)
     
  18. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    170
    Location:
    Toronto, Canada
    The 877 is virtually the same as the 877A
    The CMCON is not there so just ignore it
    It uses a different programming algorithm (not applicable)

    What error are you getting?
     
  19. uaefame

    uaefame New Member

    Joined:
    Feb 22, 2008
    Messages:
    271
    Likes:
    9
    Location:
    Dubai, UAE
    This one? Strange do you think i need to remove it?
     
  20. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    170
    Location:
    Toronto, Canada
    Just remove it.
     
    Last edited: Nov 11, 2008
  21. uaefame

    uaefame New Member

    Joined:
    Feb 22, 2008
    Messages:
    271
    Likes:
    9
    Location:
    Dubai, UAE
    When I am so close to experiement something out, something goes wrong.

    Strange how the odds work against you.

    The PIC16F877 is so big that it doesn't easily fit or can't even fit to the breadboard.

    Any ideas? Do you think i need another way to do it?
     
    Last edited: Nov 11, 2008

Share This Page