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

Help to generated square wave!

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



Joined: 08 Aug 2007
Posts: 3

View user's profile Send private message

Help to generated square wave!
PostPosted: Wed Aug 08, 2007 8:40 am     Reply with quote

Hi i'm new on CCS, i need to generated multiple square wave (38Khz,76KHz, 304Khz,19Khz) on portB using 4.864Mhz crystal. I get the following code from Example of CCS:
But i don't know to change the code for this freq. Please help.
Any idea and help very aprreciated
Thanks

#if defined(__PCM__)
#include <16F628a.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4864000)

#elif defined(__PCH__)
#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4864000)
#endif

#define NUM_OUTPUTS 8

//NOTE: periods MUST be multiples of 400
//Periods are in microseconds
#define PERIOD_0 400
#define PERIOD_1 800
#define PERIOD_2 1600
#define PERIOD_3 2000
#define PERIOD_4 20000
#define PERIOD_5 64000
#define PERIOD_6 2000000
#define PERIOD_7 2800000

const long wave_period[NUM_OUTPUTS] = {
PERIOD_0/400, PERIOD_1/400, PERIOD_2/400, PERIOD_3/400,
PERIOD_4/400, PERIOD_5/400, PERIOD_6/400,PERIOD_7/400};

long counter[NUM_OUTPUTS] = {0,0,0,0,0,0,0,0};

int port_b_image;


// This interrupt is used to output the waveforms. The interrupt
// is automatically called ever 200us.
#INT_TIMER1
void wave_timer() {
int i;

set_timer1(0xFC4F); // sets timer to interrupt in 200us
output_b(port_b_image); // outputs the waveform

for(i=0; i<NUM_OUTPUTS; i++) // sets up next output for each pin
{
if((++counter[i]) == wave_period[i]) // if counter is expired
{
counter[i] = 0; // reset counter
if(bit_test(port_b_image,i)) // and set pin as needed
bit_clear(port_b_image,i);
else
bit_set(port_b_image,i);
}
}
}


void main() {

setup_timer_1(T1_INTERNAL|T1_DIV_BY_1); // setup interrupts
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);

port_b_image=0; // initialize variable
output_b(port_b_image);

while(TRUE); // loop forever
}

===================================================
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Wed Aug 08, 2007 9:19 am     Reply with quote

That code only generates up to 25kHz square waves. You might be able to squeeze out 38kHz, but I don't think 76kHz and 304kHz with only a 4.86MHz crystal can be done. You can get some more speed with a faster crystal but 304kHz is going to be hard. You will have to poll timers rather than using interrupts.

Years ago I did a synthesizer (numerically controlled oscillator) that did 32kHz or maybe 35kHz on a PIC16C54 running at 20MHz but it was tough.

If you can make do with even power of 2 divisions of the clock frequency (303.75kHz etc.) it will be easier. Read the PIC datasheet section about timers to get a feel for what can be done.
_________________
The search for better is endless. Instead simply find very good and get the job done.
blinkstar88



Joined: 08 Aug 2007
Posts: 3

View user's profile Send private message

PostPosted: Wed Aug 08, 2007 11:20 am     Reply with quote

SherpaDoug, Thank for the replay.

I want to use this code for FM Encoder stereo from Broadcast Warehouse, they design using crystal 4.864Mhz and Pic16c620. They generated 8 freq in realtime (PortB pin0 = 38Khz, Portb Pin 1 = 114 Khz, Portb Pin 2 = 114Khz, Portb Pin 3 = 38Khz) and at PORTB 4 - 7 = 19KHZ,57KHZ,133KHZ and 95Khz, the sum for each side (PORTB0-3 and other side (portb4 -7) is 304Khz.

Do you have any idea what code will i using to make it work.
It's possible to using PWM for multiple PORTB?

Thanks
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Wed Aug 08, 2007 11:55 am     Reply with quote

It sounds like you are going to have to get real familiar with the hardware of your PIC. The data sheet is only 128 pages as that chip is fairly simple. I suggest you read all about how its clocks and timers work, an afternoon's reading. It will be a good investment for any PIC work in the future.

(Print relevant sections to paper or you will go mad trying to make sense of it on a PC screen.)
_________________
The search for better is endless. Instead simply find very good and get the job done.
blinkstar88



Joined: 08 Aug 2007
Posts: 3

View user's profile Send private message

PostPosted: Thu Aug 09, 2007 6:29 am     Reply with quote

Thanks,
I was read all the datasheet, but i'm still not get the light :( .May be a little code give me away where to start.
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