Continue to Site

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.

  • 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.

Programming PIC to generate square wave

Status
Not open for further replies.

elec123

New Member
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
 
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.
 
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.
 
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%).
 
elec123 said:
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:
https://www.mikroe.com/en/compilers/mikrobasic/pic/index.htm
https://www.mikroe.com/en/books/picbook/picbook.htm
https://www.mikroe.com/en/books/picbasicbook/00.htm
 
Whats so large about an adjustable PWM? OP's post will consume more character based bytes then OP's objective.
 
donniedj said:
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
 
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.
 
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 =)
 
vinke said:
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.
 
eblc1388 said:
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.
 
hi.. I want to generate variable frequency square wave, sine wave n triangular wave using PIC18F4520. we are using DAC in our proj. how can i start? also is it necessary to program in assembly language or shud we tryembedded C?? thanks.
 
You could use any pic with a timer/interrupt module and then set the ON and OFF times that way :). I could probably get you some example code in c if it helps but im at work at the minute :)
 
Whilst I don't like reviving old threads.... as the topic is the same, we'll leave it for now..

Generating a square wave is the easiest tasks a micro can perform.... So am I right in thinking there's more to this tale???

Help me to help you.... As we are talking about MPLAB (and a details from your other post) You are using a pic18... There in lies my knowledge..

What language...
What speed...
Have you written some code...
Are you using the port registers or the latch registers...

Please fill in what you can..
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top