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

Square wave time period code

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







Square wave time period code
PostPosted: Tue Jan 01, 2008 12:45 am     Reply with quote

hi!

can anyone help me to write C-code to find period of a square wave given as input(pin 6 mains sync input)to pic12f683.this is to find out on what frequency i am working i.e 50 or 60Hz and to position the pulse in centre of mains cycle.
i also need to output 10KHz ,15% duty cycle pulses on pin 5 for 1 second after N/8 pulses
John P



Joined: 17 Sep 2003
Posts: 331

View user's profile Send private message

PostPosted: Tue Jan 01, 2008 12:32 pm     Reply with quote

The frequency determination seems pretty trivial. Just get an interrupt at the same point in each cycle, start a timer and next time you get the interrupt, read the timer and see if it's over a threshold (somewhere between 20msec for 50Hz, 16.67msec for 60Hz).

Getting a known point on the waveform isn't so easy. Can you be certain it's a sine wave, and have you got a reliable way to turn it into a digital signal that the processor can read, or are you reading it as an analog level? You'd have to do something like checking for the rising wave at one side of the peak, start a timer and stop it when the voltage is at the same point on the descending side, then take half the timer value and use it to say "This is where the peak is". But that tells you the peak of a cycle based on the previous cycle. It depends on how accurate you need to be.
rahulbiliye
Guest







square wave time period code
PostPosted: Wed Jan 02, 2008 2:05 am     Reply with quote

Thanks John.. but as i am a new to this PIC microcontroller.i request u to please send the C code to calculate period of input square wave given to pic12f683 and also to output 10Khz, 15%dutycycle pulses
John P



Joined: 17 Sep 2003
Posts: 331

View user's profile Send private message

PostPosted: Thu Jan 03, 2008 11:24 am     Reply with quote

Sorry, I'm one of those mean people who are happy to discuss general principles of how a program might work to solve a particular problem, but I'm not going to write code to do it. If you've got the processor and the compiler, that's your job. You've started with some simple projects, and that's good. The information you need is all in the manual.
SherpaDoug



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

View user's profile Send private message

PostPosted: Fri Jan 04, 2008 9:35 am     Reply with quote

To make it rock-bottom simple you could just wait for the pin to go high, wait about 18ms and check the pin again. If the line is 60 Hz it will go high again in about 17ms and you will see the pin high. If the line is 50 Hz it won't go high until 20ms so the pin should be low. Once you know which frequency you have, just wait for the start of a cycle and wait 1/2 cycle and you must be at the middle of the cycle.
That is crude but real simple, and it is a place to start. Like John P I don't write code for people. You learn better by working out the minutia yourself.
_________________
The search for better is endless. Instead simply find very good and get the job done.
gjs_rsdi



Joined: 06 Feb 2006
Posts: 468
Location: Bali

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

50,60hz
PostPosted: Sun Jan 06, 2008 4:19 pm     Reply with quote

if oscillator 8MHz, PC=500ns
timer1 prescaler 1:2
using GP2/pin5 with CCP1 will capture 50hz as 20000us & 60Hz as 16666us. don't forget to set timer1 to zero after the CCP1 interrupt.
using GP1/pin6, you can use interrupt on change with timer1. you will get interrupt on 10000us for 50hz & 8333us for 60hz. in this case, the first interrupt (FE) will give you the sine center, the second he end (RE).don't forget to set timer1 to zero after the interrupt on change.
for the pulse out, is not clear to me what you want to do (N/8 pulses).
you can use the PWM module or just simple timer2 interrupt and the PR register for the pulse high and the period.
or timer2 interrupt
setup_timer_2(T2_DIV_BY_1,100,2); will give you interrupt every 100us/10000hz (1,15,2 for 15% high; 1,85,2 for 85% low)
joseph
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