|
|
View previous topic :: View next topic |
Author |
Message |
khalis
Joined: 12 Feb 2009 Posts: 54
|
RS485 Problem (PIC18F252) |
Posted: Mon Feb 16, 2009 3:28 am |
|
|
Hello again...
I was trying to establish communication between pc and pic18f252 using rs485. I modified one of coding that I found in this forum but it seemed didn't work...there was nothing displayed on hyperterminal window. I used RA4 to enable serial. The coding as below.
Code: |
#include <18F252.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP, NOBROWNOUT, PUT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, stream=Network)
#include <rs485.c>
void Network_fputc(char c)
{
fputc(c, Network);
}
//===============================
void main() {
while(1){
delay_ms(1000);
fputc('1', Network);
fputc('2', Network);
printf(Network_fputc, "\r\nHello World!\r\n");
}
} |
|
|
|
Ttelmah Guest
|
|
Posted: Mon Feb 16, 2009 4:37 am |
|
|
Obvious comment. You say "I used RA4 to enable serial". Study the data sheet for RA4. What is needed to make this go high?.....
Best Wishes |
|
|
khalis
Joined: 12 Feb 2009 Posts: 54
|
|
Posted: Mon Feb 16, 2009 8:11 pm |
|
|
i have looked the rs485.c several times but still can't understand the flow of the coding..It was hard for me to identify where is initialization of rs485, when the data receive and transmit... |
|
|
khalis
Joined: 12 Feb 2009 Posts: 54
|
|
Posted: Mon Feb 16, 2009 8:56 pm |
|
|
Finally the output was display on hyperterminal..but when i'm using serial port monitor in PCW, it just several hex code + bad checksum..can u explain?
If I want to communicate with several pic(s) and pc, what I suppose to do?
thanks |
|
|
khalis
Joined: 12 Feb 2009 Posts: 54
|
|
Posted: Mon Feb 16, 2009 9:38 pm |
|
|
So here is the coding that seemed working without problem
Code: | #include <18F252.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP, NOBROWNOUT, PUT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, stream = PC)
// SPI modes
#define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1 (SPI_L_TO_H)
#define SPI_MODE_2 (SPI_H_TO_L)
#define SPI_MODE_3 (SPI_H_TO_L | SPI_XMIT_L_TO_H)
void PC_fputc(char c)
{
fputc(c, PC);
}
void main()
{
int8 lsb;
int8 msb;
signed int16 result1;
signed int32 result2;
output_high(PIN_C0);
setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_4);
while(1)
{
set_tris_a(0b00000000);
output_low(PIN_C0);
msb = spi_read(0x00);
lsb = spi_read(0x00);
output_high(PIN_C0);
result1 = make16(msb, lsb);
result2 = (((int32)result1 * 12500)/ 32);
delay_ms(500);
output_high(PIN_A4);
delay_ms(1000);
printf(PC_fputc,"\r\nThermistor Value = %7.6w\r\n",result2 );
output_low(PIN_A4);
//printf("Thermistor Value = %7.5w\n\r",result2);
delay_ms(1000);
}
} |
I'm glad if you can give a clue to create an ID for this pic..after this I try to send the data through rs485 with modbus protocol... |
|
|
|
|
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
|