Hola!
Finalmente terminei lo codigo del PIC que recibe da CAN-BUS e comunica com el PC ( el terminal).
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES HS //High speed Osc (> 4mhz)
#FUSES NOPROTECT //Code not protected from reading
#FUSES BROWNOUT //Reset when brownout detected
#FUSES BORV21 //Brownout reset at 2.1V
#FUSES PUT //Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES NOIESO //Internal External Switch Over mode enabled
#FUSES FCMEN //Fail-safe clock monitor enabled
//#FUSES PBADEN //PORTB pins are configured as analog input channels on RESET
#FUSES BBSIZ1K //1K words Boot Block size
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#FUSES NOCPB //No Boot Block code protection
#FUSES NOLPT1OSC //Timer1 is not configured for low-power operation
//#FUSES MCLR //Master Clear pin enabled
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include "can-18F4580.c"
#define PIC_ID 25//identificação deste PIC
void main()
{
//recv message
struct rx_stat rxstat;
int32 rx_id;
int in_data[8];
int rx_len;
int data = 9;
int out_data[8];
int i = 0;
int8 buffer[8];
//clear recv buffer
for(i=0;i<8;i++)
{
in_data[i]=0;
}
can_init();
//can_set_mode(CAN_OP_LISTEN);
while(TRUE)
{
// mensagem recebida
if ( can_kbhit() )
{
if(can_getd(rx_id, &in_data[0], rx_len, rxstat)) //se existem dados no buffer in_data, guarda os dados recebidos nas variáveis
{
if(rx_id == PIC_ID) // Se a mensagem é para este PIC
{
data =~ (in_data[0]);
if(bit_test(data, 4))
printf("Botão 1 está OFF.\n");
if(bit_test(data, 5))
printf("Botão 1 está ON.\n");
if(bit_test(data, 6))
printf("Botão 2 está OFF.\n");
if(bit_test(data, 7))
printf("Botão 2 está ON.\n");
}
}
}//fim do tratamento da mensagem recebida
buffer[0] = getc();
if(buffer[0] == "0" )//liga Led 1
{
out_data[0] = 0;
can_putd(26, &out_data[0], 1, 1, 1, 0);
}
if(buffer[0] == "1" )//desliga Led 1
{
out_data[0] = 1;
can_putd(26, &out_data[0], 1, 1, 1, 0);
}
if(buffer[0] == "2")//liga Led 2
{
out_data[0] = 2;
can_putd(26, &out_data[0], 1, 1, 1, 0);
}
if(buffer[0] == "3")//desliga Led 2
{
out_data[0] = 3;
can_putd(26, &out_data[0], 1, 1, 1, 0);
}
}
}
Eu quería que cuándo no PC alguien enviar una instrucción para ligar / desligar los leds, el PIC tivesse una funcion qué eran chamada automáticamente, como las interrupciones, pero en mi codigo eu tenho la funcion - buffer[0] = getc(); - dentro del ciclo while(TRUE).
Saludos, Sérgio Silva