PWM code works in simulator (ISIS Proteus), but not on my breadboard.

Discussion in 'Programmer's Corner' started by Bjornk, Apr 29, 2013.

  1. Bjornk

    Thread Starter New Member

    Jan 28, 2013
    12
    0
    Hi.
    When I use this PWM code below to Nerdegutta (thanks by the way) I get it to work in ISIS Proteus,
    but not when I try it on a breadboard with a pic 16f628A and a led. I can provide the Proteus schema if necessary.

    I connected the led to RB3. I also tryed to use an transistor if the output current from the pic was to low to drive the led.

    My question: Is there something I need to activate or do on the microcontroller to get PWM to work?

    Any other tips or thoughts regarding this problem appreciated.


    Thanks



    Bjørn


    C-Code:

    Code ( (Unknown Language)):
    1.       /*
    2.  
    3. Program:        main.c
    4. Description:    PWM Test
    5. PIC:            PIC 16 F 628
    6. IDE:            MPLAB
    7. Compiler:        Hi Tech C
    8. Date:            Mars 2011
    9. Author:            Jens Christoffersen
    10. WEB:            [URL="http://www.nerdegutta.org/"]www.nerdegutta.org[/URL]
    11. */
    12.  
    13. /* Include & definitions */
    14.  
    15. #define _XTAL_FREQ 4000000
    16.  
    17. /* Configuration */
    18. __CONFIG    (WDTDIS &
    19. PWRTEN &
    20. MCLREN &
    21. BOREN &
    22. LVPDIS &
    23. DATUNPROT &
    24. UNPROTECT &
    25. INTIO);
    26.  
    27. /* Prototyping function */
    28.  
    29. /* Gobal variables */
    30. static unsigned char retning;
    31.  
    32. static void Intr(void) interrupt 0
    33. {
    34.  if(T0IF)    // Har vi fått en timer interupt?
    35.  {
    36.  T0IF=0;
    37.  
    38.  if(retning)    // teller oppover
    39.  {
    40.  CCPR1L++;
    41.  if(CCPR1L == 255)
    42.  retning=0;
    43.  }
    44.  else
    45.  {
    46.  CCPR1L--;
    47.  if(CCPR1L == 0)
    48.  retning=1;
    49.  }
    50.  }
    51. } // end static void Intr(void)
    52.  
    53. /* Functions */
    54.  
    55. /* Main Program */
    56.  
    57. void main()
    58. {
    59. retning=0;
    60.  
    61. TRISA = 0b11111111;
    62. TRISB = 0b00000000;
    63.  
    64. PORTA = 0b00000000;
    65. PORTB = 0b00000000;
    66.  
    67. PR2 = 255; // Setter PWM periode
    68. CCPR1L = 1; // Setter PWM duty cycle
    69. CCP1CON = 4|8; // Setter PWM mode
    70. CCP1X = 1;  
    71.  
    72. T2CON = 0x00;
    73. T2CKPS0 = 1; // Set timer 2 prescaler to 1:16
    74. T2CKPS1 = 1; // These bits are in T2CON
    75. TMR2ON = 1; // Enable timer 2
    76.  
    77. // Set up timer0 interrupt
    78. T0CS=0;    // Internal clock source
    79. PSA=0;    // Assign prescaler to timer0
    80. PS2=0;    PS1=1;    PS0=0;
    81. INTCON=0;
    82. GIE=1;
    83. T0IE=1;
    84. TMR0=0;
    85.  
    86. while (1); // Fortsetter inn i evigheten, og forbi
    87.  
    88.  
    89. }
     
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    You have a resistor from MCLR to Vdd, right? You have MCLR enabled in your configuration. If you don't have one, either add a 10k resistor, or disable MCLR.


    If this doesn't solve your problem, take a screenshot of your schematic, and upload it here...
     
  3. Bjornk

    Thread Starter New Member

    Jan 28, 2013
    12
    0
    Hi, and thanks for the reply.

    I tryed to turn off the MCLR, but I still can't get it to work.
    By the way I use MPLabX and comp. Hi-tech C.
    And I can't get any volt readings on my multimeter on port RB3.

    It's so frustrating not getting this to work.

    Any thoughts regrading this problem is apprisiated.

    I'll added my very simple schematics.


    Code ( (Unknown Language)):
    1.     /*
    2.  
    3. Program:        main.c
    4. Description:    PWM Test
    5. PIC:            PIC 16 F 628
    6. IDE:            MPLAB
    7. Compiler:        Hi Tech C
    8. Date:            Mars 2011
    9. Author:            Jens Christoffersen
    10. WEB:            www.nerdegutta.org
    11. */
    12.  
    13. /* Include & definitions */
    14.  
    15. #define _XTAL_FREQ 4000000
    16. #include <pic.h>
    17. #include <htc.h>
    18. #include <pic16f628a.h>
    19.  
    20. //* Configuration */
    21.  
    22. __CONFIG(FOSC_INTOSCCLK &
    23.         WDTE_OFF &
    24.         PWRTE_OFF &
    25.         MCLRE_OFF &
    26.         BOREN_ON &
    27.         LVP_OFF &
    28.         CPD_OFF &
    29.         CP_OFF);
    30.  
    31.  
    32. /* Prototyping function */
    33.  
    34. /* Gobal variables */
    35. static unsigned char retning;
    36.  
    37. #define MOTOR_A     RB3     // Waterpump
    38.  
    39. #define MOTOR_A_TRIS TRISB3
    40.  
    41. static void Intr(void) interrupt 0
    42. {
    43.  if(T0IF)    // Har vi fått en timer interupt?
    44.  {
    45.  T0IF=0;
    46.  
    47.  if(retning)    // teller oppover
    48.  {
    49.  CCPR1L++;
    50.  if(CCPR1L == 255)
    51.  retning=0;
    52.  }
    53.  else
    54.  {
    55.  CCPR1L--;
    56.  if(CCPR1L == 0)
    57.  retning=1;
    58.  }
    59.  }
    60. } // end static void Intr(void)
    61.  
    62. /* Functions */
    63.  
    64. /* Main Program */
    65.  
    66.  
    67. void main()
    68. {
    69.         MOTOR_A_TRIS = 0;
    70. retning=0;
    71.  
    72.  
    73. PR2 = 255; // Setter PWM periode
    74. CCPR1L = 1; // Setter PWM duty cycle
    75. CCP1CON = 4|8; // Setter PWM mode
    76. CCP1X = 1;
    77.  
    78. T2CON = 0x00;
    79. T2CKPS0 = 1; // Set timer 2 prescaler to 1:16
    80. T2CKPS1 = 1; // These bits are in T2CON
    81. TMR2ON = 1; // Enable timer 2
    82.  
    83. // Set up timer0 interrupt
    84. T0CS=0;    // Internal clock source
    85. PSA=0;    // Assign prescaler to timer0
    86. PS2=0;    PS1=1;    PS0=0;
    87. INTCON=0;
    88. GIE=1;
    89. T0IE=1;
    90. TMR0=0;
    91.  
    92. while (1); // Fortsetter inn i evigheten, og forbi
    93.  
    94.  
    95. }
     
  4. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    First, it looks like your LED is the wrong direction, flat side should be toward ground.

    Second, you need a resistor to limit the current to the LED, a 1k is a typical value.

    Third, your LED is on pin 18, not 9. This is RA1.
     
    Bjornk likes this.
  5. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    You should also be aware they no PIC will supply enough current to drive a motor, you'll need to fix that when moving to an actual motor...
     
    Bjornk likes this.
  6. Bjornk

    Thread Starter New Member

    Jan 28, 2013
    12
    0
    Thanks tshuck, it's working now!

    Alot of rookie mistakes there... But I have learned my lesson and to use more time constructing the circuit.

    Thanks again!
     
  7. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    That's great! Mistakes are how you learn what to look for, do consider yourself on the road for great debugging skills! ;)
     
  8. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,388
    1,605
    The school of hard knocks is built entirely out of bricks made from mistakes.
     
  9. jayanath

    New Member

    Dec 11, 2010
    17
    0
    I have following code. but all leds are fading and getting on together. no running action. how can i make chaser action with fading effect. i mean comet tail. pls help me?



    #include <pic.h>
    #define FADE_RATE 12
    #define INITIAL_WIDTH 128
    void fade()
    {
    unsigned char rate = FADE_RATE;
    unsigned char pulse_width = INITIAL_WIDTH;
    unsigned char count;
    while(pulse_width){
    PORTB= 0b11111111;
    for(count=0; count<pulse_width; count++){
    asm("NOP");
    }
    PORTB=0b00000000;
    for(; count; count++){
    asm("NOP");
    }
    if(!rate--){
    pulse_width--;
    rate = FADE_RATE;
    }
    }
    }
    int main(void) {
    char b;
    TRISB = 0b00000000;
    while(1){
    for(b=0x02; b; b<<=1){
    PORTB = b;
    fade(b>>1);
    }
    for(b=0x40; b; b>>=1){
    PORTB = b;
    fade(b<<1);
    }
    }
    }
     
  10. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    How about you start your own thread instead of stealing someone else's? ;)

    Your implementation is not the same as your function definition - fade() is not the same as fade(char).
     
Loading...