Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

RS485 Help with a PIC and a PC

Status
Not open for further replies.

joeman112

Junior Member level 2
Junior Member level 2
Joined
Jun 11, 2011
Messages
22
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,459
Hi,
Ive got a USB to RS485 converter for PC and trying to communicate with PIC16f887.
I am trying to simulate in Proteus, Here is my Schematic:
485Schematic.png

Here is my Code

Code:
unsigned char ch;

void InitUsart(void) {

        // TX Pin - output
        TRISC=0b01000000;
        TRISD=0b00000000;
        PORTD=0;
        // RX Setting, 8bit, enable receive,
        RCSTA = 0x90;
        // TX Setting, 8bit, Asinchronius mode, High speed
        TXSTA = 0x24;
        // Set Baudrade - 9600 (from datasheet baudrade table)
        SPBRG = 129;
}

// main function
void main( void ) {
       InitUsart();
        UART1_Init(9600);
        while(1){
                 if(UART1_Data_Ready()){
                      ch = UART1_Read();
                      PORTD=10;
                      UART1_Write(ch);
                      PORTD=0;
                 }
        }
}

I flick the switch to transmit, then put it back before the recieve (my previous code version had a delay in before writing)
Any ideas?
Thanks
Joe
 

Ive put an Oscilloscope on and just constantly send 'A' from the PIC.
Oscilloscope.png

Code:
Code:
 PORTD=10;
        while(1){

        UART1_Write('A');
 

I know i have had lots of fun with UART on the PICs before, I would suggest go through the UART section of the data sheet in detail, make sure you have all registers set as they are supposed to be for both UART and the 9600 Baud Rate you want.

After that to transmit make sure the Tx is ready to accept new data and when you want to recieve make sure the overflow has not triggered as it will not recieve any more bytes (caught me for ages once)

maybe take the RS485 out of the equation for a while and just try RS232 until you get it going, may be simpler,

Post back with any progress

Stephen

- - - Updated - - -

I know i have had lots of fun with UART on the PICs before, I would suggest go through the UART section of the data sheet in detail, make sure you have all registers set as they are supposed to be for both UART and the 9600 Baud Rate you want.

After that to transmit make sure the Tx is ready to accept new data and when you want to recieve make sure the overflow has not triggered as it will not recieve any more bytes (caught me for ages once)

maybe take the RS485 out of the equation for a while and just try RS232 until you get it going, may be simpler,

Post back with any progress

Stephen
 
Thanks Stephen,
I had RS232 working on the actual board before trying RS485. Seems as I knew this worked, I tried to replicate it in Proteus, it didnt work!!!
I checked everything then realized I had my Chip rate set at 1Mhz(the default), but I was programming for a chip set at 20MHz.
As soon as I changed that, my code and Proteus diagram starting working!
Thanks for the suggestion, it helped me work out why it didnt work.

Joe
 

Just realized it only works for sending from the PIC to the PC.
So communication from the PC to PIC doesn't work.
I hooked up the oscilloscope and found that the voltage is halved. Any suggestions?
Look at the green and Yellow plot. Ive moved them so they are on top of each other.
Half Voltage.png
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top