CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Problems with 16F88 and PWM

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
SubCreations



Joined: 25 Feb 2013
Posts: 3
Location: Pickens South Carolina

View user's profile Send private message

Problems with 16F88 and PWM
PostPosted: Mon Feb 25, 2013 2:20 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Feb 25, 2013 2:52 pm     Reply with quote

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

View user's profile Send private message

Problems with 16F88 and PWM
PostPosted: Mon Feb 25, 2013 3:49 pm     Reply with quote

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: 19326

View user's profile Send private message

PostPosted: Mon Feb 25, 2013 3:56 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Feb 25, 2013 4:45 pm     Reply with quote

You could try the MPLAB simulator if all else fails.

Mike
SubCreations



Joined: 25 Feb 2013
Posts: 3
Location: Pickens South Carolina

View user's profile Send private message

PostPosted: Mon Feb 25, 2013 4:57 pm     Reply with quote

That Bypass Capacitor did the trick!

Thanks again! Very Happy
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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