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

How to start PIC16F676 ...??

Discussion in 'Microcontrollers' started by koolguy, Feb 17, 2012.

  1. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,305
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    You can if you want.... I gave you this to show how I would do it.
    8 bit PWM.. The timer0 has an 8 bit register.. if you make a pulse width of

    64 counts high with 192 counts low... 64 + 192 = 256........... 25%
    128 counts high with 128 counts low... 128 +128 = 256......... 50%
    192 counts high with 64 counts low... 192 + 64 = 256........... 75%

    The amount of bits used defines the accuracy.

    You could have a 7 bit running at 4khz.. but you will only have 128 bits precision.
     
    Last edited: Mar 26, 2012
    • Like Like x 1
  2. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Please guide me to do in assembly it will be difficult!
     
  3. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,305
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    It's not difficult!!! It's very nearly the same.

    Code (text):

        list p = 16f676
        #include<p16f676.inc>
        __config(0x314)

    Tog equ 20      ; didn't use
    Pulse   equ 21      ; ADC result
    OnPulse equ 22      ; High time
    OfPulse equ 23      ; Low time
    d1  equ 24      ; delay var
    d2  equ 25      ; delay var
    d3  equ 26      ; delay var

        org 0
        goto    init        ; bypass int vector
        org 4
        goto    ISR     ; Interrupt vector

    init
        banksel TRISC
        clrf    TRISC       ; Outputs
        movlw   0xFF
        movwf   TRISA       ; Inputs
        movlw   0x01
        movwf   ANSEL       ; RA0 for pot
        movlw   0x50
        movwf   ADCON1      ; Fosc/16
        clrf    OPTION_REG  ; TMR0 pre-scaler 1:2
        banksel PORTC
        movlw   0x07
        movwf   CMCON       ; No comparators
        clrf    ADCON0      ; ADC left justify, ADC off
        clrf    INTCON      ; no int's
        bsf INTCON,T0IE ; set TMR0 interrupt only
        bsf INTCON,GIE  ; set global int's
       
    loop
        call    readadc     ; get pulse width from RA0
        movf    Pulse,w    
        sublw   .255        ;
        movwf   OfPulse     ; the off pulse could easily
        movf    Pulse,w     ; have been the comp of the pulse
        movwf   OnPulse
        goto    loop   

    ISR
        btfsc   PORTC,0     ; First we toggle RC0
        goto    t1
        bsf PORTC,0     ; On
        movf    OnPulse,w   ; (set tmr0 high time)
        goto    t2 
    t1  bcf PORTC,0     ; or off
        movf    OfPulse,w   ;  (set tmr0 low time)
    t2  movwf   TMR0
        bcf INTCON,T0IF ; clear the interrupt
        retfie          ; all done

    readadc
        bsf ADCON0,ADON ; switch on
        call    delay40     ; have to wait for module
        bsf ADCON0,GO   ; start conversion
        call    delay40     ; TAD  
        btfsc   ADCON0,GO   ; wait for ADC
        goto    $-1     ;
        movf    ADRESH,w    ; 8 bit result
        movwf   Pulse       ; in pulse
        return

    delay40             ; about 38mS
        clrf    d1      ; 256 iterations
        movlw   30
        movwf   d2      ; 30 iterations
        decfsz  d1
        goto    $ -1
        decfsz  d2
        goto    $ -3
        return 

        end

     
    If I keep doing things for you... you'll never learn.....
     
    • Like Like x 1
  4. dave

    Dave New Member

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


     
  5. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9

    Hi,

    What is the function of this instruction??

     
    Last edited: Mar 27, 2012
  6. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,235
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
    Last edited: Mar 27, 2012
  7. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,305
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    Ditto!!!
     
    • Like Like x 1
  8. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    The code are were fine and when i couple this uC to my TX and Rx of 433MHz with HT12E/D to one of bit of data the motor work slightly fine at full speed but when i slow down the speed it doesn't run continuously (with few mS break and run)!!
    at what freq this PWM is generated???
     
  9. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,305
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    Around 2khz... Like I said before, You can lift the frequency by using 7 bit to around 4khz and if you use an external xtal you'll get higher values...

    What frequency does the motor need?
     
    • Like Like x 1
  10. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    As i said the motor does not work well with RX and Tx coupled with Holtek encoder and decoder at low speed, the motor is not running continuously at low speed?
    and why the PWM is at 2KHZ as i have listen it should be around 20Khz
     
  11. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    Is software pwm going to work with serial running at the same time. When you receive data
    your not going to update the pwm pin why that happen's. It will be on hold till the data is received

    You really need hardware PWM to make this work

    Or some really tricky code that can time slice without loss packets
     
    Last edited: Apr 1, 2012
    • Like Like x 1
  12. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Dear, I am using external rx and tx of my last wireless bot project, the output of PWM is given to data pin of HT 12E.
     
  13. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,305
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    You wont get 20khz from a software PWM You'll need a hardware solution as Burt said.... If you are sending the PWM over the air it would be difficult to contol as you would need to send a signal VERY frequently There would be less time to produce the PWM signal....

    Are you sending a voltage signal to the pic...THEN controlling the motor or is the PWM signal coming out of the decoder?

    If its the decode, you could have a pic16f676 at either end.. one to send the pulse and one to control the motor.
     
    • Like Like x 1
  14. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    I am controlling the ADC with pot. then the output signal is feed to encoder of TX....then this signal is decoded with HT 12D and then given as it is to H-BRIDGES!

    so, at what maximum freq can i get PWM from software?
     
  15. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,305
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    Assume the timer can do 1:1... 1mhz / 256 = roughly 4khz (7bit will be 8khz). But I set the pre-scaler to 1:2 so we're at half that.. 2khz but to create the software version the interrupt routine takes a few cycles .. so its smaller that that.

    With an external 20mhz.. using the wdt pre-scale of 1:1 5mhz / 256 = nearly 20khz if you use a 7 bit PWM you may get 39khz.

    But there will be some errors transmitting the signal continuously...
     
    • Like Like x 1
  16. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    There is no way that what RITESH KAKKAR wants to do will work the chip your using could do software PWM but your also doing software Uart. You'll be using the timer to do the PWM
    which would take most of the clock cycles at even 20 Mhz if your wanting 20Khz output.

    You then would have to stop PWM to receive data to update the PWM.

    You really need a chip with hardware PWM and UART . I was thinking this chip had hardware UART

    Your PWM would have to be updated and let you receive data so it would have to be slower then the Uart so it could happen in sync
     
    • Like Like x 1
  17. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,324
    Likes:
    653
    Location:
    Derbyshire, UK
    I suggest you read the datasheet for the HT12D, it'sd a crude remote control chip, NOT a data chip and comnpletelymuseless for what you're trying to do.

    However, your entire scheme is completely flawed - you're not going to send 20KHz PWM over a licence free data link, which would be completely the wrong way to do it anyway. You send a couple of data bytes telling the receiver what to do - a processor at the receiver end then genetrates the PWM signals in accordance with those instructions.

    You either do the coding/encoding in software (as per my tutorials), or use a 'proper' encode/decoder chip (not crude remore chips).

    PWM for feeding two motors via H-bridges is covered in another of my tutorials, but you NEED hardware PWM, and you don't need (or want) anything like 20KHz.
     
    • Like Like x 1
  18. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,305
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE

    Exactly.... two pic's as I said in post 112.... There is also the pic16f677 which DOES have a PWM module (and more memory).
     
    • Like Like x 1
  19. sahu

    sahu Member

    Joined:
    Oct 18, 2009
    Messages:
    468
    Likes:
    3
    ASM is not difcult more then C ???
    if posibol can u sher it with C ..
     
  20. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,305
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    Sahu.. look back a few posts... I did it in C first.... post 99
     
    Last edited: Apr 2, 2012
  21. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Hi again,

    What is CONVERSION CLOCK and why it is used in this program??


    movlw 0x50
    movwf ADCON1 ; Fosc/16
     

Share This Page