#include <16F818.h>
#device *=16
#ignore_warnings 201, 216
#fuses NOWDT
#use delay(internal = 8MHz) // Usar 20 MHz para montaje en físico.
#use rs232(baud=9600, xmit=PIN_B2, rcv=PIN_B1, stream=PC)
#define RS485_RX_BUFFER_SIZE 12
#define RS485_USE_EXT_INT TRUE
#define RS485_RX_PIN PIN_B0 // Data receive pin
#define RS485_TX_PIN PIN_B7 // Data transmit pin
#define RS485_ENABLE_PIN PIN_B3 // Controls DE pin. RX low, TX high.
#define RS485_RX_ENABLE PIN_B2 // Controls RE pin. Should keep low.
int8 OUR_RS485_ID = 0x10;
#define RS485_ID OUR_RS485_ID
#include <rs485.c>
#include <stdlib.h>
int1 flag_rs485=0;
int8 i;
char msg[10];
char dato[10];
#INT_TIMER1
void timer1_isr()
{
// int8 i;
if(rs485_get_message(msg, FALSE))
{
flag_rs485 = 1;
for(i=0; i < msg[1]; ++i)
dato[i] = (msg[i+2]);
//printf("\n\r%d: ", msg[0]);
/*
for(i=0; i < msg[1]; ++i)
putc(msg[i+2]);
printf("\n\r");
*/
}
}
void clean_msg (void)
{
for(i=0;i<sizeof(msg);i++)
{
msg[i] = 0x00;
dato[i] = 0x00;
}
}
void RS485send(char* s, int8 id)
{
int8 size;
for(size=0; s[size]!='\0'; ++size);
rs485_wait_for_bus(FALSE);
while(!rs485_send_message(id, size, s))
delay_ms(OUR_RS485_ID);
}
int1 ask()
{
if(flag_rs485)
{
disable_interrupts(INT_TIMER1);
flag_rs485 = 0;
if(msg[2] == '1')
output_high(PIN_B4);
delay_ms(50);
output_low(PIN_B4);
delay_ms(50);
clean_msg();
// rs485_wait_for_bus(FALSE);
enable_interrupts(INT_TIMER1);
delay_ms(100);
return true;
}
}
void main()
{
//int8 i;
setup_timer_1(T1_INTERNAL | T1_DIV_BY_1);
//enable_interrupts(INT_RDA);
// enable_interrupts(GLOBAL);
rs485_init();
enable_interrupts(INT_TIMER1);
while (true)
{
//if(!input(PIN_A2))
//{
do
{
msg = "L1ON";
RS485send(msg, 0x12);
delay_ms(150);
}
while(!ask());
// while(!input(PIN_A2));
//}
do
{
msg = "L2ON";
RS485send(msg, 0x12);
delay_ms(150);
}
while(!ask());
do
{
msg = "L3OFF";
RS485send(msg, 0x12);
delay_ms(150);
}
while(!ask());
}
}