21.12.2013 Views

Maskinnära programmering 6B2266

Maskinnära programmering 6B2266

Maskinnära programmering 6B2266

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

* quad62x.c read rotary encoder with PC-terminal program */<br />

/* SERIAL COMMUNICATION (RS232) 9600 baud. */<br />

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

#include "Math16.h"<br />

#pragma config |= 0x3ff0<br />

char s[11]; /* global string buffer */<br />

/* function prototypes */<br />

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

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

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

void inttoa( int ); /* in ascnum.c */<br />

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

#pragma bit quad_A @ PORTB.6<br />

#pragma bit quad_B @ PORTB.7<br />

#include "seriF62x.c"<br />

#include "stringIO.c"<br />

#include "ascnum.c"<br />

#include "delays.c"<br />

void main(void)<br />

{<br />

TRISB.6 = 1; /* quad_A is input */<br />

TRISB.7 = 1; /* quad_B is input */<br />

char trans; /* to store transition */<br />

trans = 0;<br />

int counter;<br />

counter = 0;<br />

delay( 10 );<br />

initserial(); /* Initialise serial port */<br />

putchar('\r');<br />

putchar('\n');<br />

while(1)<br />

{<br />

/* read encoder new value */<br />

trans.0 = quad_A;<br />

trans.1 = quad_B;<br />

/* compare with old value */<br />

if( trans == 0b00.01 ) counter ++;<br />

if( trans == 0b01.00 ) counter --;<br />

/* this is the delay between encoder readings */<br />

inttoa( counter );<br />

string_out();<br />

putchar('\r'); /* Write on the same line! */<br />

}<br />

}<br />

/* replace old value with new value */<br />

trans.2 = trans.0;<br />

trans.3 = trans.1;<br />

Ta med filerna: seriF62x.c, stringIO.c, ascnum.c, delays.c<br />

10

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

Saved successfully!

Ooh no, something went wrong!