Rede de sensores - barramento RS485

Software e Hardware para uC PIC

Moderadores: andre_luis, 51, guest2003, Renie

Rede de sensores - barramento RS485

Mensagempor ivrj » 17 Nov 2009 11:39

Galera, estou fazendo uma pequena rede de sensores e para a comunicação vou utilizar o barramento rs485, para um projeto da faculdade.

Estou tentando fazer uma simulação aqui no proteus antes de montar o hardware mas não estou tendo nenhum sucesso.

Tentei usar aquele exemplo do CCS mas parece que ele não funciona, dai fico na duvida se fiz algo errado.

Seguem abaixo o codigo C e o circuito no proteus.


Código: Selecionar todos
/////////////////////////////////////////////////////////////////////////
////                             EX_RS485_CHAT.C                     ////
////                                                                 ////
////     Converts RS485 data and puts it onto RS232 data line.       ////
////                                                                 ////
////  Jumpers:                                                       ////
////     PCM,PCH    pin B2 to RS232 RX, pin B1 to RS232 TX           ////
////                                                                 ////
////                                                                 ////
////  The following conditional compilation lines are used to        ////
////  include a valid device for each compiler.  Change the device,  ////
////  clock and RS232 pins for your hardware if needed.              ////
/////////////////////////////////////////////////////////////////////////
////        (C) Copyright 1996,2005 Custom Computer Services         ////
//// This source code may only be used by licensed users of the CCS  ////
//// C compiler.  This source code may only be distributed to other  ////
//// licensed users of the CCS C compiler.  No other use,            ////
//// reproduction or distribution is permitted without written       ////
//// permission.  Derivative programs created using this software    ////
//// in object code form are not restricted in any way.              ////
/////////////////////////////////////////////////////////////////////////

#include <18f4550.h>
#device *=16
#fuses HS, NOWDT, NOLVP, NOBROWNOUT, NOPROTECT, PUT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_B2, rcv=PIN_B1, ENABLE=PIN_B0)

#define  RS485_RX_BUFFER_SIZE 64
#define  RS485_USE_EXT_INT    TRUE

int8 OUR_RS485_ID = 0;
#define RS485_ID OUR_RS485_ID

#include <rs485.c>
#include <stdlib.h>

int8 in_char = 0;
int8 next_in = 0;
int8 next_out = 0;
int8 msg[64];


#INT_TIMER1
void timer1_isr()
{
   int8 i;
   printf("timer");
   if(rs485_get_message(msg, FALSE))
   {
      printf("\n\r mensagem recebida %d: ", msg[0]);

      for(i=0; i < msg[1]; ++i)
         putc(msg[i+2]);

      printf("\n\r");
   }
}

void RS485send(char* s, int8 id)
{
   int8 size;

   for(size=0; s[size]!='\0'; ++size);
   printf("esperando pelo barramento");
   rs485_wait_for_bus(FALSE);
   printf("barramento liberado para envio");
   while(!rs485_send_message(id, size, s))
      delay_ms(OUR_RS485_ID);
   printf("Envio efetuado!");
}

char PCgetc()
{
   in_char = 0;

   while(!kbhit());
   in_char = getc();
   return in_char;
}

int8 PCgetInt()
{
   int8 i, s[3];
   
   for(i=0; (s[i]=PCgetc()) != '\r' && i<3; ++i);

   return atoi(s);
}

char* PCgetMsg()
{
   int8 i;

   for(i=0; (msg[i] = PCgetc()) != '\r' && i<64; ++i);

   msg[i] = '\0';

   return &(msg[0]);
}

void get_string(char* s, unsigned int8 max) {
   unsigned int8 len;
   char c;

   --max;
   len=0;
   do {
     c=getc();
     if(c==8) {  // Backspace
        if(len>0) {
          len--;
          putc(c);
          putc(' ');
          putc(c);
        }
     } else if ((c>=' ')&&(c<='~'))
       if(len<=max) {
         s[len++]=c;
         putc(c);
       }
   } while(c!=13);
   s[len]=0;
}


signed int8 get_int() {
  char s[5];
  signed int8 i;

  get_string(s, 5);

  i=atoi(s);
  //printf("get int %d", i);
  return(i);
}
#INT_RDA
void serial_isr()

   printf("INT RDA");
   if(kbhit())
      in_char = get_int;
   //in_char = fgetc(PC);
}

void main()
{
   int8 i, send_addr = 0;
   char command;
   int8 inteiro;
   char *teste;
   
   setup_timer_1(T1_INTERNAL | T1_DIV_BY_1);

   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);

   rs485_init();

   while(OUR_RS485_ID == 0 || OUR_RS485_ID >= 255)
   {
      printf("Please choose a network ID (1-255): ");
     
      //inteiro = get_int();
      //printf("%s",inteiro);
      OUR_RS485_ID = PCgetInt();
      //OUR_RS485_ID = inteiro;
   }

   printf("\n\rYour ID is: %d\n\r\n\rIf you require assistance, press h.\n\r", OUR_RS485_ID);
   
   while(command != 'Q')
   {
         enable_interrupts(INT_TIMER1);

         command = toupper(PCgetc());

         //disable_interrupts(INT_TIMER1);

         switch(command)
         {
            case 'S':
               if(send_addr == 0 || send_addr > 255)
               {
                  printf("\n\r\n\rEnter send address (1-255): ");
                  send_addr =get_int();
               }

               printf("\n\r%d:>", send_addr);
               //RS485send(PCgetint(), send_addr);
               RS485send(teste, send_addr);
               printf("Msg enviada \n\r");
               break;

            case 'C':
               send_addr = 0;

               while(send_addr == 0 || send_addr > 255)
               {
                  printf("\n\r\n\rEnter send address (1-255): ");
                  send_addr = PCgetint();
                  printf("\n\r");
               }
               break;

            case 'I':
               OUR_RS485_ID = 0;

               while(OUR_RS485_ID == 0 || OUR_RS485_ID >= 255)
               {
                  printf("\n\rPlease choose a network ID (1-255): ");
                  OUR_RS485_ID = PCgetint();
               }
               printf("\n\rYour ID is: %d\n\r\n\r", OUR_RS485_ID);
               break;

            case 'H':
               printf("\n\rCommands: (S)end message, (C)hange Send Address, ");
               printf("Change (I)D, (H)elp, (Q)uit.\n\r");
               break;

            default:
               if(command != 'Q')
                  printf("\n\rInvalid command!\n\r");
         }
   }
}



e aqui segue o esquematico:

Imagem


Se facilitar, segue o download do circuito no proteus: http://www.mediafire.com/?mnnzzdn1znw

Agradeço muito se alguem puder ajudar!
ps.: apenas usei o 18f4550 pq são os que tenho disponíveis aqui para testes.
ivrj
Bit
 
Mensagens: 13
Registrado em: 05 Mai 2009 22:18

Voltar para PIC

Quem está online

Usuários navegando neste fórum: Nenhum usuário registrado e 1 visitante

x