Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Thread Tools Display Modes
Old 11th October 2007, 01:44 PM   (permalink)
Default Programming PIC to generate square wave

Hi,

I am relatively new to programming so am looking for some advice...

I want to programme a PIC to produce a square wave with a period of 50us (f = 20kHz) and need to be able to set the on and off times of the square wave.

I need this as this will control an igbt's switching in a DC-DC boost converter.

What limited programming i have done has been on a 16f84 but looking at some datasheets i have found that the 16f877 has a PWM mode where i think it is possible to set the on and off times of the square wave.

What are people's opinions of this? would the 16f877 be a better choice?

I am planning on writing the code in assembler (MPLAB IDE), i'm trying to keep the code as simple as possible but would the use of interrupts be the best way to do it?

Any help or suggestions on the choice of PIC and implementation in the code would be very helpful.

Thanks
elec123 is offline   Reply With Quote
Old 11th October 2007, 02:17 PM   (permalink)
Default

Unless you need lots of I/O then use a 16F628 or better still a 16F88. Both have the PWM module and an internal oscillator as well. Implementing any sort of SMPS is tricky with a pic, however, I vaguely remember a AN about it. Have a search on Microchips site for SMPS.

Mike.
Pommie is online now   Reply With Quote
Old 11th October 2007, 02:20 PM   (permalink)
Default

You can use the 16F877(40-pin) but the 16F628A, which is 18-pin PIC to replace the old 16F84, has PWM too.

Get the datasheet from Microchip and have a look at section 9.3.2 where it said using a 20MHz clocking, you can have 10-bit PWM resolution at 19.53KHz. If you want higher PWM frequency than that, the resolution would decrease. But I think 19.52KHz is close enough for your purpose.

You don't need interrupt to make hardware PWM works. Once the PWM has been setup, no more user programming is required except to change/adjust the duty cycle, by writing different values to the CCPR1L and the CCP1CON[5..4] bits. That's it, everything is taken care of, a ~20KHz PWM output with your desired duty cycle.
__________________
L.Chung
eblc1388 is offline   Reply With Quote
Old 11th October 2007, 02:39 PM   (permalink)
Default

thanks for your quick replies, will look into the 16F628A....
elec123 is offline   Reply With Quote
Old 11th October 2007, 03:08 PM   (permalink)
Default

Using a 20MHz crystal as Clock, TMR2 prescalar=1, setting PR2 to 249(0xF9) instead of 0xFF would give you an exact 20KHz PWM frequency output.

Just make sure that the CCPR1L value be 248(0xF8) or less at all times else the PWM output will never go LOW(duty cycle exceeds 100%).
__________________
L.Chung
eblc1388 is offline   Reply With Quote
Old 12th October 2007, 09:05 AM   (permalink)
Default

Quote:
Originally Posted by elec123
Hi,

I am relatively new to programming so am looking for some advice...

I want to programme a PIC to produce a square wave with a period of 50us (f = 20kHz) and need to be able to set the on and off times of the square wave.

I need this as this will control an igbt's switching in a DC-DC boost converter.

What limited programming i have done has been on a 16f84 but looking at some datasheets i have found that the 16f877 has a PWM mode where i think it is possible to set the on and off times of the square wave.

What are people's opinions of this? would the 16f877 be a better choice?

I am planning on writing the code in assembler (MPLAB IDE), i'm trying to keep the code as simple as possible but would the use of interrupts be the best way to do it?

Any help or suggestions on the choice of PIC and implementation in the code would be very helpful.

Thanks
Hey elec123,
i dont think writing your code in assembly will be the simplest way.Try some high level language like mikrobasic.Its much simpler than assembly,its really helpful when your writing large programs.The PWM your trying to do could be done in 2 or more lines of code if you use mikrobasic.The compiler trial version is free to download(trial version is limited to 2K words of code,2K words of code is really big,so there is no chance that your program exceed that limit).Also you can get much support if you are stuck as it has a forum dedicated to its use,manual to download and free online books about programming PICs in mikrobasic.take a look at the websites for more info:
http://www.mikroe.com/en/compilers/m.../pic/index.htm
http://www.mikroe.com/en/books/picbook/picbook.htm
http://www.mikroe.com/en/books/picbasicbook/00.htm
__________________
Have a nice day.Be happy!!
$ViNkE
vinke is offline   Reply With Quote
Old 13th October 2007, 01:49 AM   (permalink)
Default

Whats so large about an adjustable PWM? OP's post will consume more character based bytes then OP's objective.
donniedj is offline   Reply With Quote
Old 13th October 2007, 04:03 AM   (permalink)
Default

Quote:
Originally Posted by donniedj
Whats so large about an adjustable PWM? OP's post will consume more character based bytes then OP's objective.
When i said large ,i wasnt making alllusion to PWM.I was just talking about another case where one is writing a large program and that it would much less time consuming and takes much less lines of code to write in a high level language rather than assembly
__________________
Have a nice day.Be happy!!
$ViNkE
vinke is offline   Reply With Quote
Old 13th October 2007, 04:23 AM   (permalink)
Default

For a DC-DC boost converter I would use the highest frequency possible. You can get 78.2Khz with 8bit resolution which should give you ample resolution. You will need to implement a simple PID control loop to give it a stable output.
I used a very simple PI loop in a 16F684 based 11-15V 20Amp booster I designed to eliminate the voltage drop between my truck and camper. It also functions as a battery charger controller when plugged into 110vac by regulating the rectified 12V output from the standard power converter.
__________________
--- The days of the digital watch are numbered. ---
kchriste is offline   Reply With Quote
Old 13th October 2007, 05:16 AM   (permalink)
Default

With the higher frequency there's going to be higher switching losses so it really depends on the exact components to be used, at higher frequencies like 78khz, dedicated high speed drivers are more inteligent. For a simple IGBT run from a micro controller a lower frequency might be more sensible, and flame resistant =)
__________________
"Because I be what I be. I would tell you what you want to know if I
could, mum, but I be a cat, and no cat anywhere ever gave anyone a
straight answer, har har."
Sceadwian is offline   Reply With Quote
Old 13th October 2007, 06:52 AM   (permalink)
Default

Quote:
Originally Posted by vinke
I was just talking about another case where one is writing a large program and that it would much less time consuming and takes much less lines of code to write in a high level language rather than assembly
Then learn a quick lesson from this guy.

He starts off his solar irrigation project using a trial version compiler with a 2K code limit, which he claims he doesn't know about, and reaches the code space limit soon after. He asked about the price of the full version but it costs $149 and he can't find the money to pay for it, yet. He is now stuck.

I would have guessed he can probably done the whole project comfortably within 1K code size using ASM instead.
__________________
L.Chung
eblc1388 is offline   Reply With Quote
Old 13th October 2007, 01:15 PM   (permalink)
Default

Quote:
Originally Posted by eblc1388
Then learn a quick lesson from this guy.

He starts off his solar irrigation project using a trial version compiler with a 2K code limit, which he claims he doesn't know about, and reaches the code space limit soon after. He asked about the price of the full version but it costs $149 and he can't find the money to pay for it, yet. He is now stuck.

I would have guessed he can probably done the whole project comfortably within 1K code size using ASM instead.
This guy is me.I was using the version 2.0.4(i got it from a friend) thats why i reached the 2k limit ,but with the new version ive downloaded ,v6.0, the code doesnt exceed that limit.probably because of the improved code optimization in the new version.Also with practice i learned how to reduce my code words without affecting the program.some people might be more at ease with asm ,but for me its high level language.
__________________
Have a nice day.Be happy!!
$ViNkE
vinke is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Latest
Question about Inchworm+ Quan Micro Controllers 54 28th October 2007 12:20 AM
Grr... high frequency square wave dandare99 Electronic Projects Design/Ideas/Reviews 10 27th January 2007 08:53 PM
square wave to sine wave tony Ben Electronic Projects Design/Ideas/Reviews 2 26th January 2005 04:04 AM
circuit help: square and sin wave input = square wave output spyghost Electronic Projects Design/Ideas/Reviews 2 28th April 2004 01:08 AM
How can I convert a square wave to sine wave?? kken6248 General Electronics Chat 6 1st April 2004 03:27 PM



All times are GMT. The time now is 09:08 AM.


Electronic Circuits  |  Electronics Wiki
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.