Thanks for the info. Yes I am a little worried about the lack of "multitasking" but I should be fine. I just need to generate an alarm with the peizo, flash some lights, and dump a byte in the TXREG. So even if have to "alarm" for 1.5 seconds, then flash my light, handle TXREG, and repeat, I am OK with that.Yes, it can be done, and I have done it at even higher frequencies. Something to consider is that while doing software PWM, you will either be occupying the PIC fully generating the signal, be using carefully timed loops, or be using an interrupt driven routing (say off TMR2) . The latter is relatively easy to do in Assembly language, as the timing can be critical. Remember, 2 kHz is 500 us per pulse, and you can do a lot of processing between having to set or clear your pulses.
The advantage of using hardware PWM is that it effectively multitasks the MCU. That is, the PWM runs by itself and the MCU can do other things until it needs to change the PWM.
+1 The approach John P describes is a good one and was used in these threads to do just that - including the software PWM to drive the beeper. Lots of stuff to wade through in the threads but it's in there.You can make a virtue out of this 2KHz tone. You need to set up an interrupt at a 4KHz rate, and use no other interrupts. Then when you service the interrupt, test for whether the beeper is sounding, and if so, complement the output (note 4KHz not 2KHz, as the output has to go both high and low in 1/2000 sec). Also decrement the count representing the time that the beeper is on.
Then you have the option of checking for anything else that the processor might need to do, like drop a character into the UART's buffer or decide whether lights should be on or off. You could do these things directly in the interrupt, or you could set a flag (I always call it "tick") and return to the main() routine. Then if the flag is found to be set you clear it, and do all those things in main() instead. For simple designs, it's pretty much a judgment call which approach to take.
|Thread starter||Similar threads||Forum||Replies||Date|
|O||PIC16F628A output toggle...yes I know its ancient.||Microcontrollers||18|
|O||Proper way to clear RCIF | PIC16F628A XC8||Microcontrollers||2|
|O||PIC16F628A - Help with clarification on datasheet||Datasheets, Manuals & Parts Identification||16|
|O||PIC16F628A Global Variables and Flags. XC8||Microcontrollers||9|
|N||Genius Software to Burn pic16F628A||Software & IDEs||10|