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.

* rbint62x.c RB-int reads rotary switch */<br />

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

#include "int16Cxx.h"<br />

#pragma config |= 0x3ff0<br />

int counter; /* global pulsecounter */<br />

char trans; /* global for transitions */<br />

#pragma bit lightdiode @ PORTA.2<br />

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

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

#define ON 1<br />

#define OFF 0<br />

#pragma origin 4<br />

interrupt int_server( void )<br />

{<br />

int_save_registers<br />

if( RBIF == 1 ) /* is it the RB pin change-interrupt? */<br />

{ /* this time it's obvius that it is! */<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 />

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

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

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

RBIF = 0; /* Reset RB-change flag before leaving */<br />

}<br />

int_restore_registers<br />

}<br />

void main( void )<br />

{<br />

/* the main program */<br />

CM0=1; CM1=1; CM2=1; /* no comparators on PORTA */<br />

OPTION.7 = 0; /* internal pullup resistors on */<br />

TRISA.2 = 0; /* PA2 is output to lightdiode */<br />

PORTA.2 = 0;<br />

TRISB = 0b11000000; /* only inputs quad_A and quad_B */<br />

trans = 0;<br />

counter = 0;<br />

RBIE = 1; /* local enable */<br />

GIE = 1; /* global enable */<br />

}<br />

while( 1 )<br />

{<br />

if ( counter == 0 ) lightdiode = ON;<br />

else lightdiode = OFF;<br />

}<br />

17

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

Saved successfully!

Ooh no, something went wrong!