View previous topic :: View next topic |
Author |
Message |
assimhussain
Joined: 05 Aug 2011 Posts: 9 Location: United Kingdom
|
Frequency/Time period measurement with PIC16F917. |
Posted: Fri Aug 05, 2011 7:25 am |
|
|
Hi. I am trying make a closed loop speed controller system. I'm struggling to measure the frequency/time period from optical encoder.
I tried to measure the time period using CCP1.
This program given below, I found it from some book.
How could I know the value at CCP_1. I tried to use it with debugger. But the value at CCP_1 keeps on changing for same period waveform.
Code: |
#include <16f917.h>
#device ADC=8
#use delay(clock=4000000)
#int_ccp1
void isr_ccp1()
{
set_timer1(0);
clear_interrupt(INT_CCP1);
}
void main()
{
setup_timer_1(T1_INTERNAL);
get=get_timer1();
setup_ccp1(CCP_CAPTURE_FE);
enable_interrupts(GLOBAL);
enable_interrupts(INT_CCP1);
}
} |
Could anyone please give me a code to find the frequency.
I am using a PIC16F917 Microcontroller.
The crystal on demo board is a 32.768 KHz crystal.
thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Aug 05, 2011 1:31 pm |
|
|
Quote: | I am using a PIC16F917 Microcontroller.
The crystal on demo board is a 32.768 KHz crystal.
|
What's the manufacturer and model number of the demo board ?
Post a link to the web page for the board.
Quote: | #include <16f917.h>
#device ADC=8
#use delay(clock=4000000)
|
Where is your #fuses line ? It should be the next line after the #include.
What is your CCS compiler version ?
What are the voltage levels (low and high) of the input signal to the PIC ?
What is the frequency of the input signal ?
Read these threads on measuring frequency with the PIC by using
the CCP module in Capture mode, using CCP interrupts.
http://www.ccsinfo.com/forum/viewtopic.php?t=33153
http://www.ccsinfo.com/forum/viewtopic.php?t=29963
If you have any questions about that code, ask it here in this thread.
Do not post a question in those threads. |
|
|
assimhussain
Joined: 05 Aug 2011 Posts: 9 Location: United Kingdom
|
|
Posted: Mon Aug 08, 2011 6:36 am |
|
|
Hi. Thanks for your reply.
I use Mechatronics PICDEM Demo board
http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&dDocName=en023837
I really don't know about the Fuse line. I am just a beginner in this field.
CCS PCM C Compiler Version 4.038, 58276
Voltage levels:
Low - 0 Volt (approx)
High - 5 volt (approx)
Frequency of the Input signal varies. The signal is the waveform from a optical encoder which is used as as speed sensor. So the frequency varies according to the speed. I check the waveform with a oscilloscope. It varies from 17 Hz to 300 Hz. I need to detect the change in frequency to implement the control function. _________________ Assim
Husqvarna UK |
|
|
assimhussain
Joined: 05 Aug 2011 Posts: 9 Location: United Kingdom
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Aug 08, 2011 2:30 pm |
|
|
Quote: | I'm struggling to measure the frequency/time period from optical encoder.
|
What frequency is the encoder supposed to produce, in normal operation ?
It calls the count() function. If you look at the example, it shows that
the input signal pin must be given to the count() function as a parameter.
You need to look at the schematic of your Mechatronics board and decide
what PIC pin is to be used to measure the encoder frequency. Then put
that pin into the Count() function, as shown in the example program.
With regard to the LCD, use the CCS forum's search page to search for
Mechatronics LCD. Set it to "Search for all terms". You will find a sample
program. |
|
|
assimhussain
Joined: 05 Aug 2011 Posts: 9 Location: United Kingdom
|
|
Posted: Tue Aug 09, 2011 4:28 am |
|
|
At normal operation the encoder waveform gives about 40-50 Hz and in my control system I planned to adjust the PWM given to motor when it goes down.
Yes. I managed to run the LCD program. Its working perfect.
for the frequency count. I tried to test your program on this thread. could you please tell me what all changes are required to run it on PIC16F917. http://www.ccsinfo.com/forum/viewtopic.php?t=41877&highlight=code+measure+frequency |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Aug 09, 2011 8:22 pm |
|
|
Quote: | I tried to test your program on this thread. Could you please tell me what all changes are required to run it on PIC16F917 ? |
Did it work ? Did you use the first program (shown for the 16F84a) ?
That's the one to use with the 16F917. It compiles without errors for
the 16F917. |
|
|
assimhussain
Joined: 05 Aug 2011 Posts: 9 Location: United Kingdom
|
|
Posted: Wed Aug 10, 2011 3:22 am |
|
|
yes its compiled without any errors... but i couldnt make the display work.. so cant say if the program is working... |
|
|
assimhussain
Joined: 05 Aug 2011 Posts: 9 Location: United Kingdom
|
|
Posted: Wed Aug 10, 2011 9:36 am |
|
|
Do I need to make any other connections that other than xmit=PIN_B1, rcv=PIN_B0, and input ? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Aug 10, 2011 10:07 am |
|
|
What PIC pin are using for the input signal ?
The input pin is specified below. In the example below, I have pin B0.
What pin do you have there ? And, is your input signal actually connected
to that pin ?
Quote: |
while(1)
{
result = count(PIN_B0, 1000);
printf("%lu\n\r", result);
delay_ms(500);
} |
|
|
|
assimhussain
Joined: 05 Aug 2011 Posts: 9 Location: United Kingdom
|
|
Posted: Thu Aug 11, 2011 6:52 am |
|
|
I'm using pin B0 itself.
Can I ask you a different question... maybe a stupid one.
Whenever I try to use the registers in CCS directly like we do in Hi-tech complier rather than using functions it returns a error.
eg: using PORTB = 0b00000111; etc returns error 'Undefined Identifier'
how can I use the registers direcly to make changes in program. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Aug 11, 2011 1:21 pm |
|
|
I used the program below with the count.c file from the other thread in
the link, and it works perfectly. This was tested with your version,
vs. 4.038. I used a BK Precision model 4011 function generator to put
out a 50 Hz squarewave with 0 to 5v voltage levels, and I connected it
to pin B0 (also the ground lead on the function generator is connected to
ground on the board). This is the output in the TeraTerm window on my PC.
It's working.
Quote: |
50
50
50
50
50
50
50
50
50 |
Here's the main program, modified for use with the 16F917:
Code: |
#include <16F917.H>
#fuses INTRC_IO, NOWDT, PUT, BROWNOUT
#use delay(clock=4M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#include "count.c"
//======================================
void main()
{
int16 result;
// Measure the number of counts in one second
// from the input signal on Pin B0. This will
// be the frequency (in Hz) of that signal.
while(1)
{
result = count(PIN_B0, 1000); // count period = 1000 ms
printf("%lu \n\r", result);
}
} |
Quote: |
Whenever I try to use the registers in CCS directly like we do in Hi-tech
compiler rather than using functions it returns a error.
|
Use #byte statements to define the register addresses. Example:
Or
Code: |
#byte PortB = getenv("SFR:PORTB")
|
Then you can use the PortB variable in your program, to read it or to
write to it.
But be aware, on this forum we normally don't like to look at direct
register code in a program. Only if it's necessary to do some function
that CCS doesn't support with built-in functions, or to fix a bug.
In other words, don't post a program with lots of direct register access
and ask for help. We prefer that you use the CCS functions if possible. |
|
|
|