Connect with us

rs485 code help/suggestions winavr gcc

Discussion in 'Microcontrollers, Programming and IoT' started by sherazi, Jul 7, 2010.

  1. sherazi

    sherazi

    91
    0
    Mar 8, 2010
    hi friends!
    i wanted to implement rs485 communication b/w an avr and a pc.. the reason being distance up to 50 m b/w the two.. i am using the max485 ic, being the first time for such project wanted help and guidence.. i have a rs232-485 converter that i am going to use.
    as far as my thw knowledge i have upto now is that aprt from being half duplex, there is no other big difference in coding.

    i have the following code for initialization,
    Code:
    char ReceivedByte; 
    
    
       UCSR0B |= (1 << RXEN0) | (1 << TXEN0);   // Turn on the transmission and reception circuitry 
       UCSR0C |=  (1 << UCSZ00) | (1 << UCSZ01); // Use 8-bit character sizes 
    
       UBRR0L = 0x0c; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register 
       UBRR0H = 00; // Load upper 8-bits of the baud rate value into the high byte of the UBRR register
    
     
    well i am using 4800 baud rate because the other functions best suit 1mhz freq.

    for a test program that just listens for a byte and echos it back to pc



    Code:
    for (;;) // Loop forever 
       { 
          //enable_rx;
         PORTD|=0x01;
    while ((UCSR0A & (1 << RXC0)) == 0) {}; // Do nothing until data have been recieved and is ready to be read from UDR 
          ReceivedByte = UDR0; // Fetch the recieved byte value into the variable ByteReceived" 
    
    
          //enable_tx 
           PORTD&=0xFE
          while ((UCSR0A & (1 << UDRE0)) == 0) {}; // Do nothing until UDR is ready for more data to be written to it 
          UDR0 = ReceivedByte; // Echo back the received byte back to the computer 
       }    
    is this all ok, plz comment on code and give a better one if available....
    thanx in advance


    if someone can guide me to a tutorial on hyper terminal that would be really a great help...
     
  2. blocka

    blocka

    18
    0
    Jul 7, 2010
    UDR0 = ReceivedByte; // Echo back the received byte back to the computer

    This line loads the byte into the register, you will need to wait until it finishes transmitting out the uart before continuing as the next instruction disables the tx, enables rx.
     
  3. sherazi

    sherazi

    91
    0
    Mar 8, 2010
    would you just correct my code?

    Code:
    #include <avr/io.h> 
    #include <util/delay.h> 
    
    
    
    int main (void) 
    { 
       char ReceivedByte=0; 
    
       UCSR0B |= (1 << RXEN0) | (1 << TXEN0);   // Turn on the transmission and reception circuitry 
       UCSR0C |=  (1 << UCSZ00) | (1 << UCSZ01); // Use 8-bit character sizes 
    
       UBRR0L = 0x0c; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register 
       UBRR0H = 00; // Load upper 8-bits of the baud rate value into the high byte of the UBRR register 
    
      for (;;) // Loop forever 
       { 
         
    	PORTC &=0xDF;// Make PC5 low
    	
    	while ((UCSR0A & (1 << RXC0)) == 0) {}; // Do nothing until data have been recieved and is ready to be read from UDR 
          ReceivedByte = UDR0; // Fetch the recieved byte value into the variable ByteReceived" 
    
    	
     	PORTC |=0x20;// Make PC5 high
    
          while ((UCSR0A & (1 << UDRE0)) == 0) {}; // Do nothing until UDR is ready for more data to be written to it 
          UDR0 = ReceivedByte ; // Echo back the received byte back to the computer 
    	  ????????????? what should be here???
       }   
    }
    
     
  4. blocka

    blocka

    18
    0
    Jul 7, 2010
    Sorry i don't know which micro you are using but you need to look at the UART registers and there will be a bit that you need to check to see the Transmitter is idle and do a while loop like your other while loops. It possibly is a bit in the UCSROA register like the other status bits you have.
     
  5. sherazi

    sherazi

    91
    0
    Mar 8, 2010
    now another request plz can someone share a circuit /schematic for rs485 using max485 keeping in mind distances upto 70 meters

    [

    should this circuit work if r39,r36 & r40 are not used? also the zener and the led section also be removed [​IMG]
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-