PIC 16F690 PWM's ports selection - How to?


Closed Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    891

    Default PIC 16F690 PWM's ports selection - How to?

    Hello,

    I'm struggling a little in finding how to assign either port P1A (PORTC.5) or P1B (PORTC.4) to output the PWM signal, one at the time (= not simultaneously) on a PIC16F690.

    I drive a loudspeaker via a transistor and to modulate its volume, the transistor's base is connected to a 1k2 resistor on PORTC.5 and 15k resistor PORTC.4.

    Reading the DS, I need to set registers CCP1CON.P1M<1:0> and CCP1CON.CCP1M<3:0> as well as, maybe, PSTRCON.STRA and PSTRCON.STRB.

    Here's my code for testing. Unfortunately, it doesn't work

    Anyone an idea if feasible? How?
    Code:
    @ __Config _FCMEN_OFF & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_OFF &_PWRTE_OFF & _WDT_OFF & _INTOSC
    
    Registers   76543210
    OPTION_REG = %10000101 'PORT A&B Pull-Ups disabled (look WPUA & WPUB)
    ANSEL      = %00000000 'Select analog inputs Channels 0 to 7
    ANSELH     = %00000010 'Select analog inputs Channels 8 to 11
    ADCON0     = %00000000 'A/D Module ON/OFF (Bit5:2 select channels 0:11)
    ADCON1     = %00000000 'A/D control register
    CM1CON0    = %00000000 'Comparator1 Module is OFF
    CM2CON0    = %00000000 'Comparator2 Module is OFF
    INTCON     = %00000000 'INTerrupts CONtrol (TMR0 ON)
    TRISA      = %00000000 'Set Input/Output (0 to 5)
    PORTA      = %00000000 'Ports High/Low (0 to 5)
    TRISB      = %00000000 'Set Input/Output (4 to 7)
    PORTB      = %00000000 'Ports High/Low (4 to 7)
    TRISC      = %00000000 'Set Input/Output (0 to 7)
    PORTC      = %00000000 'Ports High/Low (0 to 7)
    
    ' HPWM registers
    T2CON      = %00000101 'Timer2 = ON (bit2), prescaler (bits1:0) 00=1, 01=4, 1x=16
    CCP1CON    = %00001100 'Select PWM Mode
    PR2        = 255       'frequency is around 1kHz
    
    DutyCycle VAR WORD
    DutyCycle = (PR2+1)*2
    CCPR1L    = DutyCycle >> 2
    CCP1CON.5 = DutyCycle.1
    CCP1CON.4 = DutyCycle.0
    
    '--------------------------------------
    TEST:
    
    PSTRCON.0 = 1 'PWM on PORTC.5
    PAUSE 1000
    PSTRCON.0 = 0
    
    PSTRCON.1 = 1 'PWM on PORTC.4
    PAUSE 1000
    PSTRCON.1 = 0
    
    GOTO TEST:
    END
    Roger

  2. #2
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: PIC 16F690 PWM's ports selection - How to?

    Just a quickie, when you say it doesn't work, have you scoped the pins?

    My immediate thought is, what are your oscillator settings ...could it be that you're outputting a frequency that you can't hear (assuming that's what you're using to determine whether it works or not) dues to a combination of PR2 & your oscillator setting?

  3. #3
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default Re: PIC 16F690 PWM's ports selection - How to?

    The above ought to work. As it is right now, you toggle a PWM from a channel to another, but never both together. To alter the Volume, you'll need a fixed duty on 1 channel, and a variable one on the other.. Not sure if this specific allow to do it.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  4. #4
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    891


    Did you find this post helpful? Yes | No

    Default Re: PIC 16F690 PWM's ports selection - How to?

    @Mister_e,
    Quote Originally Posted by mister_e View Post
    The above ought to work
    Ça veut dire que mon code devrait être bon?

    I have only one fixed dutycycle. To modify the volume, I vary the current on the transistor's base.

    @HankMcSpank,
    Yup, I scoped them all and I have never a signal on P1B (PORTC.4)
    Roger

  5. #5
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default Re: PIC 16F690 PWM's ports selection - How to?

    It works as expected here!

    Name:  Snap1.jpg
Views: 1832
Size:  15.3 KB

    Have you tested PORTC.4 to see if it's blown, or you have a problem with something connected to it?
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  6. #6
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    891


    Did you find this post helpful? Yes | No

    Default Re: PIC 16F690 PWM's ports selection - How to?

    Well....

    .... a new PIC did the trick.

    Thanks a lot for your help.

    The biggest surprise is not that I'm a little bit stupid, it's more that I am a witness of a miracle: I understood (a part of) the datasheet by myself!!!!

    Still not going to tell anyone how much hours I spent on this one too...
    Roger

  7. #7
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default Re: PIC 16F690 PWM's ports selection - How to?

    shush on that.... ta geulle :P
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts