|
|
View previous topic :: View next topic |
Author |
Message |
akaka
Joined: 18 Jul 2012 Posts: 14
|
rs485_send_message function always return false |
Posted: Sun Dec 14, 2014 6:56 am |
|
|
Hi all,
I am trying to communicate between 2 pic 18f4550 1 master & 1 slave via rs485.c using max485.
I can't figure out why "rs485_send_message" always returns false & no data received to the Slave. Here is my code:
MASTER:
Code: |
#include <18F4550.h>
#fuses HS, NOWDT, NOLVP, NOBROWNOUT, NOPROTECT, PUT
#use delay(clock=8000000)
//#use rs232(baud=9600, xmit=PIN_B2, rcv=PIN_B1, stream=PC)
//#use rs232(baud=9600,xmit=pin_c6,rcv=pin_c7,enable=pin_c5,stream=RS485)
#define RS485_RX_PIN PIN_C7
#define RS485_TX_PIN PIN_C6 // Data transmit pin
#define RS485_ENABLE_PIN PIN_C5 // Controls DE pin. RX low, TX high.
#define RS485_RX_ENABLE PIN_C5 // Controls RE pin. Should keep low.
#use rs232(baud=9600, xmit=RS485_TX_PIN, rcv=RS485_RX_PIN, enable=RS485_ENABLE_PIN, bits=9, long_data, errors, stream=RS485)
#define RS485_RX_BUFFER_SIZE 64
#define RS485_USE_EXT_INT FALSE
#define enableRTX PIN_C5
#define SLAVE 0x16
int8 OUR_RS485_ID = 0x10;
#define RS485_ID OUR_RS485_ID
#define LCD_ENABLE_PIN PIN_B0 ////
#define LCD_RS_PIN PIN_B1 ////
#define LCD_RW_PIN PIN_B2 ////
#define LCD_DATA4 PIN_B4 ////
#define LCD_DATA5 PIN_B5 ////
#define LCD_DATA6 PIN_B6 ////
#define LCD_DATA7 PIN_B7
#INCLUDE <lcd.c>
#include <rs485.c>
#include <stdlib.h>
//
float a=82.34,b=25.36,c=12.56;
/*int8 buffer[40];
int8 next_in = 0;
int8 next_out = 0;
#INT_RDA
void serial_isr()
{
int8 t;
buffer[next_in] = getc();
t = next_in;
next_in = (next_in+1) % sizeof(buffer);
if(next_in == next_out)
next_in=t; // Buffer full !!
}
*/
void main ( )
{
int8 i=0x15;;//, msg[32];
char msg[10]="main\0";
/* setup_psp(PSP_DISABLED); // PSP birimi devre d?s,?
setup_spi(SPI_SS_DISABLED); // SPI birimi devre d?s,?
setup_timer_1(T1_DISABLED); // T1 zamanlay?c?s? devre d?s,?
setup_timer_2(T2_DISABLED,0,1); // T2 zamanlay?c?s? devre d?s,?
setup_adc_ports(NO_ANALOGS); // ANALOG giris, yok
setup_adc(ADC_OFF); // ADC birimi devre d?s,?
setup_CCP1(CCP_OFF); // CCP1 birimi devre d?s,?
setup_CCP2(CCP_OFF); // CCP2 birimi devre d?s,?*/
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
set_tris_b(0x00);
set_tris_d(0x00);
output_b(0x00);
output_a(0x0f);
//output_high(RS485_RX_ENABLE);
delay_ms(50);
rs485_init();
lcd_init();
delay_ms(50);
printf(lcd_putc,"\fInitialise ");
//output_high(RS485_RX_ENABLE);
while(1) //
{
if(input(pin_a0))
{
delay_ms(10);
lcd_putc("\fSending Message");
while(input(pin_a0));
delay_ms(50);
//rs485_send_message(SLAVE,5,msg);
//rs485_wait_for_bus(TRUE);
output_high(RS485_RX_ENABLE);
if(rs485_send_message(SLAVE, 1, i))
lcd_putc("\fMessage sent");
else
lcd_putc("\fMessage failed");
output_low(RS485_RX_ENABLE);
}
if(input(pin_a1))
{
delay_ms(10);
printf(lcd_putc,"\fCurtain ");
while(input(pin_a1));
output_high(RS485_RX_ENABLE);
printf("%f",b);
delay_ms(50);
output_low(RS485_RX_ENABLE);
}
if(input(pin_a2))
{
delay_ms(10);
printf(lcd_putc,"\fBath");
while(input(pin_a2));
printf("%f",c);
delay_ms(50);
}
}
} |
SLAVE:
Code: |
#INCLUDE <18F4550.H>
#USE DELAY (CLOCK=8000000)
#define LCD_ENABLE_PIN PIN_B0 ////
#define LCD_RS_PIN PIN_B1 ////
#define LCD_RW_PIN PIN_B2 ////
#define LCD_DATA4 PIN_B4 ////
#define LCD_DATA5 PIN_B5 ////
#define LCD_DATA6 PIN_B6 ////
#define LCD_DATA7 PIN_B7
#INCLUDE <lcd.c>
#INCLUDE <stdlib.h>
#define RS485_RX_PIN PIN_C7
#define RS485_TX_PIN PIN_C6 // Data transmit pin
#define RS485_ENABLE_PIN PIN_C5 // Controls DE pin. RX low, TX high.
#define RS485_RX_ENABLE PIN_C5 // Controls RE pin. Should keep low.
#use rs232(baud=9600, xmit=RS485_TX_PIN, rcv=RS485_RX_PIN, enable=RS485_ENABLE_PIN, bits=9, long_data, errors, stream=RS485)
#INCLUDE <input.c>
#USE fast_io(d)
#USE fast_io(c)
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES PUT //Power Up Timer
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOPBADEN //PORTB pins are configured as digital I/O on RESET
#FUSES NOMCLR //Master Clear pin used for I/O
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#DEFINE fast_glcd
#DEFINE enableTRX pin_c5
#define RS485_ID OUR_RS485_ID
#define OUR_RS485_ID 0x16
#include <rs485.c>
#include <stdlib.h>
char gelen_bilgi[10],x_str[10];
int sayac;
float x=0;
void main()
{
int8 i, msg[32];
set_tris_d(0x00);
output_d(0x00);
output_low(enableTRX);
lcd_init();
//glcd_WriteByte(1, (LCDModeSet|LCDMode_XOR)); // Grafik LCD ekran XOR moda go"re ayarlan?yor
//glcd_WriteByte(1, (GLCD_CMD_DISP_ON|LCDDisp_TXT|LCDDisp_GRH)); // Grafik LCD ekran hem metin hem grafik modunda c,al?s,acak s,ekilde ayarlan?yor
delay_ms(50);
printf(lcd_putC,"Welcome: %f",x);//float to string is,lemi gerc,ekles,tiriliyor
//lcd_putC(x_str);//voltaj_str deg(eri ekrana yazd?r?l?yor
while (1)
{
/*for(sayac=0;sayac<5;sayac++)
{
gelen_bilgi[sayac]=getc();
}
x = atof(gelen_bilgi);
x=x+10;*/
if(rs485_get_message(msg, FALSE)) {
lcd_putc("\fMessage Received");
for(i=0; i<msg[1]; ++i)
fputc(msg[i+2], RS485);
lcd_putc(msg[1]);
}
/*glcd_init(240,128); // Grafik LCD ektran haz?rlan?yor
glcd_WriteByte(1, (LCDModeSet|LCDMode_XOR)); // Grafik LCD ekran XOR moda go"re ayarlan?yor
glcd_WriteByte(1, (LCDDispMode|LCDDisp_TXT|LCDDisp_GRH)); // Grafik LCD ekran hem metin hem grafik modunda c,al?s,acak s,ekilde ayarlan?yor
*/
//printf(lcd_putC,"Bilgi: %f",x);//float to string is,lemi gerc,ekles,tiriliyor
//lcd_putC(x_str);//voltaj_str deg(eri ekrana yazd?r?l?yor
}
} |
Here is my schematic: http://imagizer.imageshack.us/a/img905/5289/fDKxuy.png
If anyone has any idea I would be greatful
Regards
Chris |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Sun Dec 14, 2014 8:48 am |
|
|
hmm..
hardware....
I'd remove the 120r resistors labelled R1 and R2 in your schematic as they do not follow the RS485 bus requirements.
As a side note, usually pushbuttons/switches are 'active low' ( grounding is the active signal). though your way will work, it is more suseptable to noise/EMI causing false readings.At the very least add a small cap (say .1mfd) across the resistors to filter out some possible noise.
software....
not too sure if 9 bits for data is proper though I've never used the CCS supplied RS485 driver... is serial ISR should only read 8 bits from the UART...
others who use it should know....
hth
jay |
|
|
akaka
Joined: 18 Jul 2012 Posts: 14
|
|
Posted: Tue Dec 16, 2014 11:54 pm |
|
|
Hi Jay,
Thanks for your response.
I have removed resistors R1 & R2 and 9 bits , but the same thing.
Has any one interfaced a communication pic to pic via rs485?
Thanks In Advance
Chris |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Wed Dec 17, 2014 1:51 am |
|
|
Yes, I suspect hundreds of us.
I can see your problem. It is the use of C5, as the enable pin. This is one of the USB pins on your PIC. Look in the data sheet at the "PORTC I/O summary". Can this pin be used as a normal output?.....
It can only be used as a USB output or normal I/O input. It can't act as a normal output. Hence your enable pin never gets operated. |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Wed Dec 17, 2014 11:25 am |
|
|
What is the purpose of R1 & R2? Is that a model of the worst case resistance of a long transmission line? If you wire is long enough to have 120 Ohms of DC resistance you will probably see a lot of reactance as well. What type of wire or cable are you modeling? _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Thu Dec 18, 2014 3:32 am |
|
|
SherpaDoug wrote: | What is the purpose of R1 & R2? Is that a model of the worst case resistance of a long transmission line? If you wire is long enough to have 120 Ohms of DC resistance you will probably see a lot of reactance as well. What type of wire or cable are you modeling? |
I don't think he was modelling anything.
I suspect the circuit is from a data sheet. One of the common chips has this in the sheet as the 'test' circuit, for simulating the drop with long lines. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|