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 CCS Technical Support

Create do re mi with 16f887 using 8Mhz crystal

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



Joined: 03 Sep 2010
Posts: 8

View user's profile Send private message

Create do re mi with 16f887 using 8Mhz crystal
PostPosted: Mon Nov 22, 2010 4:21 am     Reply with quote

Code:
 
const int s_table[] = {128,176,218,246,255,246,218,176,
                            128,79,37,9,0,9,37,79};

static int t0preload,frequency=150;

#int_timer2
void pwm_load(void) {
   setup_ccp1(CCP_PWM);
    set_pwm1_duty(s_table[s_tableidx++ % 8]);
   set_timer2(t0preload);

}

main()
{
   setup_ccp1(CCP_PWM);

        t0preload = 100;//256 - (62500 / frequency);
   setup_timer_2(T2_DIV_BY_4,frequency,1);

   
       blah blah....

}

I try to create a code as modified from
http://www.ccsinfo.com/forum/viewtopic.php?t=36540

I try to work out a code to generate a good Do Re Mi Tone for my project.
It doesn't seem to work well, the tone generated is generally some kind of noise with 2 tones. I have no idea what I have done wrong. Guess my knowledge on timer with PWM is very limited. Quite confused with the frequency component as stated in the thread.... Anybody can help? Appreciated. Crying or Very sad
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Nov 24, 2010 3:29 pm     Reply with quote

Try modifying the CCS example file, Ex_Tones.c and the driver file for it, tones.c.

I did this. I edited the Ex_tones.c file and removed the data for their
"Happy Birthday" music. I added the DoReMi notes. It works.

These are the changes I made above main():
Code:
#define SIZE 8

const struct
{
long tone;
long length;
}doremi[] = {
C_note[0], 400
D_note[0], 400,
E_note[0], 400,
F_note[0], 400,
G_note[0], 400,
A_note[0], 400,
B_note[0], 400,
C_note[1], 800  // Use next higher octave for last note
};


In main(), I edited the code in the for() loop to use the 'doremi' data:
Code:

 generate_tone(doremi[i].tone, doremi[i].length);


In the Tones.c file, I changed the output pin to be Pin C2, because my
PicDem2-Plus board has a beeper on pin C2:
Code:
#define TONE_PIN  PIN_C2


To make it work with an 8 MHz crystal, edit the #use delay() statement
so it has 8 MHz in it:
Code:
#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=8000000)
derricko



Joined: 03 Sep 2010
Posts: 8

View user's profile Send private message

PostPosted: Sun Nov 28, 2010 7:33 pm     Reply with quote

Thank you PCM, I will give a try Very Happy
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Sun Jan 16, 2011 1:31 pm     Reply with quote

Quote:

In the Tones.c file, I changed the output pin to be Pin C2, because my
PicDem2-Plus board has a beeper on pin C2:


In looking at the PicDem2-Plus board schematics, I see they are creating sort of a fake "H drive" to the Piezo buzzer" but with their configuration, there is always a DC voltage applied to the piezo element. In reading the spec sheets for a number of them, it indicates you should not have a DC bias on the piezo element. Has this been a problem for those with the PicDem board or does it seem to work just fine ?



mikey
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
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