#include <33FJ128GP802.h>
#FUSES WDT //Watch Dog Timer
#FUSES NOWRTB //Boot block not write protected
#FUSES NOBSS //No boot segment
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOWRTss //Program memory not write protected
#FUSES PR_PLL //Pimary oscillator
#FUSES HS
#FUSES PUT128 //Power On Reset Timer value 128ms
#FUSES NOIESO //Internal External Switch Over mode disabled
#FUSES NORSS //No secure segment RAM
#FUSES NOSSS //No secure segment
#FUSES NOWRTSS //Secure segment not write protected
#FUSES NORBS //No Boot RAM defined
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOJTAG //JTAG disabled
#use delay(clock=48M) // Cristal externo de 8MHZ
#use rs232(baud=57600,parity=N,xmit=PIN_B8,rcv=PIN_B9,bits=8)
#include <can-PIC24.c>
//definimos los pines RS232 y ECAN
#Pin_select U1TX=PIN_B8 // Transmision RS232 (pin 17)
#Pin_select U1RX=PIN_B9 // Recepcion RS232 (pin 18)
#Pin_select C1RX=PIN_B10 // Recepcion ECAN
#Pin_select C1TX=PIN_B11 // Transmision ECAN
struct rx_stat rxstat;
int32 rx_id;
int8 in_data[8]={0,0,0,0,0,0,0,0},out_data[8]={0,0,0,0,0,0,0,0}, rx_len;
int32 solpos(void) // Funcion que solicita informacion del modulo CAN
{
int32 pos;
can_putd(0x080, out_data[0], 0, 0 , 0 , 1 ); //solicitacion RTR
while ( can_kbhit()!= 1) //espera retornar valor
{
delay_cycles( 2 ); //funciona con 2 (no se por que)
}
if ( can_kbhit() == 1 )
{
if(can_getd(rx_id, &in_data[0], rx_len, rxstat))
{
pos=make32(in_data[0],in_data[1],in_data[2]); // reconstruye valor para 32 bits
}
}
return pos;
}
void main()
{ int32 posicion;
setup_adc(ADC_CLOCK_INTERNAL );
can_init();
while (true)
{
output_toggle(pin_b6); // cambia estado de un led para verificacion
posicion=solpos(); // llama a una funcion que lee valores de CAN
printf("%d\t",in_data
[0]); // imprime valor mas significativo via rs232 printf("%d\n",in_data
[2]); // valor menos significativo delay_ms(250); // tiempo de espera
}
}