#include <16f876.h>
#include <math.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,PUT,NOBROWNOUT
#define RX PIN_C7
#define Tx PIN_C6
#define SDO PIN_C5
#define SDI PIN_C4
#define SCK PIN_C3
#define EN PIN_C2
#use fast_io(A)
#use fast_io(B)
#use fast_io(C)
#use delay (CLOCK=20000000)
#use rs232 (BAUD=19200,XMIT=Tx,RCV=Rx)
#use spi (MASTER,CLOCK_HIGH=5,CLOCK_LOW=5,DI=SDI,DO=SDO,CLK=SCK,MSB_FIRST,MODE=1,STREAM=brujula_out)
#use spi (MASTER,CLOCK_HIGH=5,CLOCK_LOW=5,DI=SDI,DO=SDO,CLK=SCK,LSB_FIRST,MODE=1,STREAM=brujula_in)
#byte porta = 0x05
#byte portb = 0x06
#byte portc = 0x07
#byte portd = 0x08
#byte porte = 0x09
int16 dato_x,dato_y;
const int reset = 0b0000;
const int measure = 0b1000;
const int report = 0b1100;
const int16 negmask = 0b1111100000000000;
void recibe_brujula()
{
int estado=0;
output_low(EN);
spi_xfer(brujula_out,reset,4);
output_high(EN);
delay_us(20);
output_low(EN);
spi_xfer(brujula_out,measure,4);
output_high(EN);
delay_us(20);
output_low(EN);
while (estado != 3)
{
spi_xfer(brujula_out,0b1100,4);
estado = spi_xfer(brujula_in,0b0000,4);
}
dato_x=spi_xfer(brujula_in,reset,11);
dato_y=spi_xfer(brujula_in,reset,11);
}
void main (void)
{
float x,y,angulo;
set_tris_a(0b00000000);
set_tris_b(0b00000010);
set_tris_c(0b10010000);
output_high(EN);
while(TRUE)
{
recibe_brujula();
if (bit_test(dato_x,10)==1) dato_x = dato_x | negmask ;
if (bit_test(dato_y,10)==1) dato_y = dato_y | negmask ;
x=(float)dato_x;
y=(float)dato_y;
angulo*=57.2958;
printf ("Angulo= %f\n\r",angulo
); delay_ms(150);
}
}