help for 1MHz to 14MHz pwm using pic16f877a

Discussion in 'Embedded Systems and Microcontrollers' started by pany2008, Nov 17, 2010.

PWM for MHz using pic does linearity being achived

Poll closed Nov 19, 2010.
  1. Yes

    0 vote(s)
    0.0%
  2. No

    2 vote(s)
    100.0%
  1. pany2008

    Thread Starter Member

    Aug 17, 2008
    11
    0
    Hi,

    I am having problem for pwm using pic16f877a, I am not getting the pulse output from microcontroller. Please can you help me out for seeing my source code if their is any thing wrong or missing then please help me out.

    Code ( (Unknown Language)):
    1.  
    2. #include<pic.h>
    3. #include<math.h>
    4. #include "ioports1.h"
    5. #include "delay.h"
    6. #include "lcd.h"
    7.  
    8. __CONFIG (HS & DEBUGDIS & WDTDIS & BORDIS & PWRTEN & LVPDIS & PROTECT);
    9.  
    10. unsigned int dummy_sec;
    11.  
    12. void Sec(unsigned int);
    13.  
    14. void main()
    15. {
    16.     Sec(100);
    17.     init_ports();
    18.     Sec(50);
    19.     lcd_init(); //LCD initialized
    20.     Sec(25);
    21.  
    22.         lcd_line1(0); //LCD address specified AT LINE 1
    23.         lcd_string("  PANKAJ  PATEL  "); //Displays message "LCD INTERFACING"
    24.         lcd_line2(0); //LCD address specified AT LINE 2
    25.         lcd_string(" PWM EXAMPLE    "); //Displays message "LCD INTERFACING"
    26.  
    27.     Sec(200);
    28.     init();
    29.  
    30.     for(;;)
    31.     {
    32.         GIE = 1;
    33.         lcd_line1(0); //LCD address specified AT LINE 1
    34.         lcd_string("  PANKAJ  PATEL  "); //Displays message "LCD INTERFACING"
    35.         lcd_line2(0); //LCD address specified AT LINE 2
    36.         lcd_string(" PWM EXAMPLE    "); //Displays message "LCD INTERFACING"
    37.      }
    38. }
    39.  
    40. init_ports()
    41. {
    42.        T_CPU_LED = 0;  // pin enable to output
    43.        CPU_LED = 0;
    44. }
    45.  
    46. init()
    47. {
    48.        TMR1H = 0xfc;
    49.     TMR1L = 0x17;
    50.     TMR1CS = 0;
    51.     T1CKPS0 = 0;
    52.     T1CKPS1 = 0;
    53.     T1CON = 0b00000001;
    54.     TMR1ON = 1;    
    55.     TMR1IF = 0;
    56.     TMR1IE = 1;
    57.     PR2 = 0b00000000;
    58.     T2CON = 0b00000100;
    59.     CCPR1L = 0b00000000;
    60.     CCP1CON = 0b00011100;
    61.     TMR2ON = 1;    
    62.     TMR2IF = 0;
    63.     TMR2IE = 1;
    64.     PEIE = 1;
    65.     GIE = 1;
    66. }
    67.  
    68. void Sec(unsigned int itime)
    69. {
    70.     unsigned int i;
    71.     unsigned char j;
    72.         for(i=0;i<itime;i++)
    73.             for(j=0;j<165;j++);
    74. }
    75.  
    76.  
    77. void interrupt ISR()
    78. {
    79.    if(TMR1IF)
    80.     {
    81.         PIE1 = 0;    
    82.         TMR1IF = 0;
    83.         TMR1ON = 0;
    84.         CPU_LED = !CPU_LED;
    85.         ++dummy_sec;
    86.         if(dummy_sec > 1000)
    87.             {
    88. //                CPU_LED = !CPU_LED;
    89.                 dummy_sec = 0;
    90.             }
    91.  
    92.         TMR1H = 0xfc;
    93.         TMR1L = 0x17;
    94.         TMR1ON = 1;
    95.         PIE1 = 1;
    96.     }
    97.  
    98. }
    99.  
    100.  
     
    Last edited by a moderator: Nov 17, 2010
  2. bertus

    Administrator

    Apr 5, 2008
    15,638
    2,344
    Hello,

    On wich speed is the pic running?
    I think the 1 to 14 Mhz is way to high for PWM at the pic.

    Bertus
     
  3. pany2008

    Thread Starter Member

    Aug 17, 2008
    11
    0
    The pic is running on 20Mhz can please check the code i am not getting any pwm output from the code which i have post. if i can get even 10Mhz maximum the also it will be fine for me.
     
  4. bertus

    Administrator

    Apr 5, 2008
    15,638
    2,344
  5. pany2008

    Thread Starter Member

    Aug 17, 2008
    11
    0
    I have check out the that thread already, but not able to get the code because i have a grip in hitech, that the only thing i have used till now. It will be real help full if help me out that if any thing is wrong in my code.
     
  6. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    Run it through the MPLAB simulator.

    PS it'll top out at 5MHz @ 50% duty. Perhaps a 24H could muster 14MHz but would also be a 1 or 2 bit PWM.

    What's it for?

    PPS useless poll.
     
    pany2008 likes this.
  7. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    Yup, max 5MHz with 20MHz clock. Is the CCP pin definately set to output?
    In MPLAB simulator you can't see PWM with a resolution greater than the instruction cycle so running PWM at max frequency you will not see anything. You can reduce the frequency for debugging and then assume it will work.
     
    pany2008 likes this.
  8. pany2008

    Thread Starter Member

    Aug 17, 2008
    11
    0
    Thanks for the help, for me right now 5Mhz @50% duty cycle will be ok, Can please verify my code because in the current code no output pwm is coming i think their might be something wrong in the code please correct my mistake in code. I am using it for the Medical instrument (ultrasound).
     
  9. pany2008

    Thread Starter Member

    Aug 17, 2008
    11
    0
    Thanks for the help can please me out to verify my code, because i am not getting any output from above code, please correct my mistake in code.
     
  10. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    Isn't ultrasound in the kilohertz region? Where will you find an ultrasound transducer that works in the MHz?
     
  11. pany2008

    Thread Starter Member

    Aug 17, 2008
    11
    0
    No the Ultrasound transducer for therapy comes in the Mhz range.
     
  12. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    Do you have a part number for the transducer.
     
  13. pany2008

    Thread Starter Member

    Aug 17, 2008
    11
    0
    Currently we have manufacture it here locally in India, we have done a copy of transducer from one imported machine and we have checked out it in details the machine works with 1 to 5MHz frequency output to the transducer, and their transducer is also manufactured by them. Our transducer which we have made work perfectly with their machine now we have to develop the machine so for that i need the help.
     
  14. pany2008

    Thread Starter Member

    Aug 17, 2008
    11
    0
    Can provide me code for pic16f877a for hitech compiler, i am really needing it.

    PS you posted in a thread that is for explanation of the topic and not for the question.
     
    Last edited by a moderator: Nov 18, 2010
  15. nickelflipper

    Active Member

    Jun 2, 2010
    280
    35
    You have set up the PWM for software interrupt, but have enabled the hardware interrupts?
    <EDIT with fresh cup of coffee, I see CCP1CON is set for PWM P1A-P1D high>

    I have my doubts as to being able to output the max clock of 5Mhz for the 20Mhz osc. The implication is that would require a 100% duty cycle which would be DC, yes/no? The best setting I could get on an 18lf14k22 with the internal osc of 16Mhz, and 4X PLL, was 8Mhz (50% duty cycle).

    Here is a snippet of GCBasic code, that followed the 18f manual. No luck with PR2 = 0 at all.

    Code ( (Unknown Language)):
    1. CCP1CON = 0
    2. TMR2 = 0
    3. PR2 = 0x01        ;set TMR2 register for max speed
    4. CCPR1L = 0x00    ;50% duty cycle
    5. dir PortC out    ;P1A as pwm out
    6. PIE1 = 0
    7. PIR1 = 0
    8. Set TMR2ON ON
    9. CCP1CON = b'00101100'    '50% duty with PR2 = 0x01, Single output pwm as controlled by pulse steering mode
    10. Set STRA ON                'Steer pwm to P1A for ECCP module
    11.  
    12. Main:
    13.  
    14. Goto Main
    15. <EDIT not in software interrupt mode so deleted clearing TMR2IF code>
     
    Last edited: Nov 18, 2010
  16. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    @Pankaj D. Patel
    Then you cross post to other forums. It is often a good idea to post link(s) to the other forums http://forum.htsoft.com/all/showflat.php/Cat/0/Number/195092/an/0/page/0#Post195092
    To solve your problem. I think it would be a good idea to take some time and read the datasheet. Then you have a better understanding on how your chip actually work. Sit down on your lab bench with a scope. And to some test so you can check if your ideas are correct. Then you can move on and expand your program. And if you are stuck. Post your problems with a good explanation of what you have done. And why and how you struggle.
     
  17. pany2008

    Thread Starter Member

    Aug 17, 2008
    11
    0
    I am person who try maximum time and when i am not able to solve my problem then i ask someone to help me out, to the clear i am just new to pic till i have done only 3 project using pic, but if you ask for 89c52 then for that i have done almost 400 industrial project in that.
    Why i am asking their also that now i am underwater totally so i am waiting for some to get me out off water and save my life.
    I have studied a lot the datasheet and followed the instruction but not able get the result i was behind the pwm from last 15 days, then a last i came out all for solving my problem.
     
  18. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    I've looked over you settings for PR2, T2CON, CCPR1L and CCP1CON and they look fine for a frequency of crystal/4 and a duty cycle of 25%.
    You shouldn't set TMR2IE because it will generate interrupts faster than it is possible to deal with them - although at that point PWM should already be operating so this isn't the real problem.
    I don't use C, but I can't see where the CCP1 pin is set as output.
    I see T_CPU_LED = 0 and CPU_LED = 0 but I don't know if they are defined as the CCP1 pin.
    How are you testing the code - a simulator or in hardware, and what are you using to measure the output?
     
    pany2008 likes this.
  19. pany2008

    Thread Starter Member

    Aug 17, 2008
    11
    0
    Now i am getting the output from pwm but it is really getting very low, just only 25Khz at 50% duty cycle, i am using the hardware, measure the output on scope and frequency meter.
     
  20. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    Very odd, what frequency do you measure on the OSC1/OSC2 pins (where the crystal is)? Also if this frequency is continuous with no gaps - which would indicate the PIC is resetting.
    Best to check the PWM output stays the same at the same time because the oscillator is very sensitive to disturbance.
     
    Last edited: Nov 18, 2010
Loading...