21.12.2013 Views

Maskinnära programmering 6B2266

Maskinnära programmering 6B2266

Maskinnära programmering 6B2266

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Programexempel frekvensmätning med 16F628<br />

/* fmeas62x.c frequency measurement for 16F628 */<br />

/* input frequency 100 Hz - 10 kHz at CCP1-pin */<br />

#include "16f628.h"<br />

#include "math16.h"<br />

#pragma config |= 0x3ff1<br />

/* function prototypes */<br />

void unslongtoa( unsigned long ); /* in ascnum.c */<br />

void initserial(); /* in seriF62x.c */<br />

bit putchar( char ); /* in seriF62x.c */<br />

void delay( char ); /* in delays.c */<br />

void string_out( void ); /* in stringIO.c */<br />

char text1( char ); /* function prototype */<br />

char s[11]; /* Global char array for digits */<br />

#include "seriF62x.c"<br />

#include "ascnum.c"<br />

#include "stringIO.c"<br />

#include "delays.c"<br />

void main(void)<br />

{<br />

char i;<br />

uns16 T, f, t1, t2;<br />

initserial();<br />

TRISB.3 = 1; /* CCP1-pin is input */<br />

/* Settings */<br />

/* "--"."Prescale 1/1"."osc shut off"."synchr"."fosc/4"."on" */<br />

T1CON = 0b00.00.0.0.0.1 ;<br />

/* "--"."--"."Capture each pos edge" */<br />

CCP1CON = 0b00.00.0101 ;<br />

while(1)<br />

{<br />

CCP1IF = 0 ; /* reset flag */<br />

while (CCP1IF == 0 ); /* wait for capture */<br />

t1 = CCPR1H*256;<br />

t1 += CCPR1L;<br />

CCP1IF = 0 ; /* reset flag */<br />

while (CCP1IF == 0 ); /* wait for next capture */<br />

t2 = CCPR1H*256;<br />

t2 += CCPR1L;<br />

/* Calculations */<br />

T = t2 - t1; /* calculate period */<br />

f = 1000000/T; /* calculate frequency */<br />

31

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!