|
|
View previous topic :: View next topic |
Author |
Message |
SubCreations
Joined: 25 Feb 2013 Posts: 3 Location: Pickens South Carolina
|
Problems with 16F88 and PWM |
Posted: Mon Feb 25, 2013 2:20 pm |
|
|
Hi All,
I have been using the 16F84 for years and finally started using the 16F88.
I found some code from:
http://hades.mech.northwestern.edu/index.php/Driving_a_piezo_speaker_with_a_PIC
That plays Twinkle Twinkle Little Star with just a PIC chip and a capacitor and speaker.
Here is the code:
Code: | /*
Code written by Bobby By, Agatha Lee, Dan Niecestro 02-2009
This code uses a PWM signal from PIC 18F4520 to define the frequencies
that produce the basic notes in one octave of the musical scale,
and plays a simple song on a piezo speaker.
The volume of the speaker can be set by changing the duty cycle value (1~125)
*/
#include <16F88.h>
#fuses INTRC,NOWDT,NOPROTECT,NOMCLR
#use delay(clock=8000000)
//define timer scaling value for each note
#define C 204
#define D 182
#define E 164
#define F 154
#define G 137
#define A 123
#define B 110
#define C2 102
//
#define x 14 //total number of notes in song to be played - modify for specific song
//the song to be played in this demonstration is "Twinkle Twinkle Little Star"
int song[x]={C, C, G, G, A, A, G, F, F, E, E, D, D, C}; //insert notes of song in array
int length[x]={1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2}; //relative length of each note
int i;
void main() {
setup_ccp1(CCP_PWM); // PWM output on pin 6, RB0/CCP1
set_pwm1_duty(30); // the number changes the volume of the piezo speaker
for (i=0; i<x; i++) { //play x notes inside song array
setup_timer_2(T2_DIV_BY_16, song[i], 16);
//set PWM frequency according to entries in song array
delay_ms(400*length[i]); //each note is played for 400ms*relative length
setup_timer_2(T2_DIV_BY_16, 1, 16); //the PWM frequency set beyond audible range
//in order to create a short silence between notes
delay_ms(50); //the silence is played for 50 ms
}
}
|
I was able to get this to work perfectly with a 16F685, but all I get is the first note with the 16F88. In fact, the 16F88 doesn't always play the same sounds when I reinitialize the chip.
I just downloaded the newest compiler from CCS and I just bought a new Mach X Programmer.
Any ideas?
Thanks so much |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Mon Feb 25, 2013 2:52 pm |
|
|
Which chip were you using before?
I'm confused as to whether it was 16F84 or 16F685.
Which version compiler are you using?
Can you do some debugging to see where it's stuck?
Mike |
|
|
SubCreations
Joined: 25 Feb 2013 Posts: 3 Location: Pickens South Carolina
|
Problems with 16F88 and PWM |
Posted: Mon Feb 25, 2013 3:49 pm |
|
|
Thanks, Mike.
Sorry about the confusion. The chip I got it working with was the 16F685.
My compiler is CCS C Compiler, Version 4.140d
I don't know how to step through the program like I used to when I had a serial port and my picstart plus.
When I try, it complains that I don't have USB drivers, but the ones it mentions are all there where it says it's looking.
MPLAB doesn't like the Mach X in the list of possible programmers
Can I just put the 16F88 in the Mach X and expect to step through the program in the CCS debugger? (That is, if I didn't have this USB Driver problem)
Thanks again. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Mon Feb 25, 2013 3:56 pm |
|
|
Add a delay before starting to play the notes.
There is a silicon problem with one revision of these chips where INTRC isn't stable for quite a long time after power on.
Have you got some good HF decoupling right adjacent to the PIC?. Sounds as if you might be resetting the processor with electrical noise from the sounder.
Best Wishes |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Mon Feb 25, 2013 4:45 pm |
|
|
You could try the MPLAB simulator if all else fails.
Mike |
|
|
SubCreations
Joined: 25 Feb 2013 Posts: 3 Location: Pickens South Carolina
|
|
Posted: Mon Feb 25, 2013 4:57 pm |
|
|
That Bypass Capacitor did the trick!
Thanks again! |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|