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

Using #int_timer2 to on/off buzzer

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



Joined: 07 Sep 2003
Posts: 51

View user's profile Send private message Yahoo Messenger

Using #int_timer2 to on/off buzzer
PostPosted: Fri Dec 19, 2003 1:37 am     Reply with quote

Mabuhay!

I have a problem with the code below... it would not generate a monotone on my buzzer.. anybody in the forum who can check and review my code.. I plan to generate a 100Hz monotone using interrupt timer2 at PortA4 of PIC18F452.

The code listed below:

Code:
#include <18F452.h>              // Target PIC Microcontroller IC
#device  *=16                    // use a 16-bit pointer
#device ICD=TRUE

#fuses HS,NOPROTECT,NOWDT        // PIC MCU Configuration

#use delay(clock=20000000)       // 20MHz Crystal clock speed


//**********************************************************************
// Configure PortA4 for buzzer output
#bit BuzzBIT=0xF80.4

const int tones[] = { 70,74,76,83,88,94,99,105,112,118,126,133,141 };
int tone_reqd;

//**********************************************************************
#int_timer2
void tick(void) {
   static int tick;
   static int toggle;

   if (tick) --tick;
   else {
      tick=tones[tone_reqd];
      toggle^=1;
      BuzzBIT=toggle;
   }
}

//**********************************************************************
main()
{
   setup_timer_2(T2_DIV_BY_4,40,1);
   enable_interrupts(int_timer2);
   enable_interrupts(global);
   tone_reqd=1;
   while (1) ;
}


I'll appreciate any help.

Thank you.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Fri Dec 19, 2003 8:58 am     Reply with quote

Didn't look at your code, but RA4 stands out. That is the first thing I look for when the hardware guys do designed. They always forget to add a pullup. Refer to the datasheet if you are trying to drive some device. You will need to add a pullup resistor or tie one pin of the buzzer to 5V.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Fri Dec 19, 2003 9:10 am     Reply with quote

It also look like your frequecy is around 156Hz.

20000000/4 = 5000000 = internal clock
5000000/4 = 1250000 = timer2 increments
1250000/(256-40) = 5787 = timer2 interrupts
5787/74 = 78 is the 1/2 frequecy

Now I am not sure what you are trying to drive, but I don't think it is a subwoofer Very Happy That sounds like too low a frequency. Most piezo's are in the KHz.
rrb011270



Joined: 07 Sep 2003
Posts: 51

View user's profile Send private message Yahoo Messenger

PostPosted: Sun Dec 21, 2003 6:37 pm     Reply with quote

Mark wrote:
Didn't look at your code, but RA4 stands out. That is the first thing I look for when the hardware guys do designed. They always forget to add a pullup. Refer to the datasheet if you are trying to drive some device. You will need to add a pullup resistor or tie one pin of the buzzer to 5V.


Based on the data sheet, the RA4 is an open drain output... so I use the buzzer as a pull-up with a diode protection. I guess this will work coz wen I invoke output_low the buzzer will sound... there is no need for a resistor as pull-up coz the buzzer will serve similar to a resistor...
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