Polprog's papers mirror

Contact polprog at Esper/DALnet/Libera for info

Back to homepage

Icon  Name                    Last modified      Size  Description
[PARENTDIR] Parent Directory - [DIR] progpc/ 2019-12-20 22:47 - [TXT] HD44780.c 2007-03-25 11:16 5.6K [   ] Makefile 2019-04-10 23:29 444 [TXT] HD44780.h 2019-04-10 23:30 3.2K [   ] HD44780.o 2019-04-10 23:30 1.7K [TXT] main.c 2019-04-11 13:25 3.0K [   ] main.hex 2019-04-11 13:25 2.0K [   ] main.o 2019-04-11 13:25 2.3K [TXT] readme.txt 2019-10-03 00:10 4.8K [   ] 485slave.tar.gz 2019-10-03 00:14 11K
                  ................                    .
  ____           |-   ATmega8L    |--[103]-*-        /|\
  \  /  /\        '|||''''''''''''                   [_] 680
  |\/o /__\  ___  ___       ___  ___              .-- |
  |  '-o-+--/_ / /__  /__/ /__/ /__------.----.---'  | | 120
  '------'-/  \  __/    / /__/  ___\--.--+-.--+---.  |_|
                 //|                  o__| |  |   '---|
       o--. .---'/ o                  \  / o/\|      [ ] 680
         .|-|   /                      \/  /__\      _|_
	|   -|-'    MAX485  
         [+x]                                       ~plpg

       RS485 example code for AVR and Linux/NetBSD
       http://polprog.net/         #avrs on Freenode IRC network
       Released on the terms of the 3-clause BSD license.
       Hardware demo as seen here: https://www.youtube.com/watch?v=BB3f8T9BxUs

1. Directory tree
2. Short intro to RS485
3. Useful functions
4. Notes on software
5. Notes on hardware
6. Links

  1. Directory tree

 485slave/ - root directory for the project
 |- 485slave.tar.gz - this whole directory as a tar.gz file
 |- hd44780.{c,h} - HD44780 library for AVR by Radosław Kwiecień [1]- http://radzio.dxp.pl
 |- main.c - AVR slave program
 |- main.hex - ihex file for atmega8
 |- Makefile - AVR program Makefile
 '- progpc/ - root directory for the PC programs
    |- linux.c - Sources for the smple transmitter program (send lines from stdin to display)
    |- zegarek.c - Display current time on the slave LCD
    |- util.{c,h} - Various serial port related utilities, as seen in [2]
    |- Makefile - simple makefile for the PC programs (targets: linux, zegarek; default linux)
    '- *.txt - test txt files. strzalki.txt displays a cool animation.

  2. Short intro to RS485
  RS485 is a half-duplex, differential, multi-drop serial bus that is the basis of many popular 
  systems and protocols including Modbus RTU (used for cotrolling industrial automation systems)
  and DMX512 (stage lighting control). It can carry data over long ranges (up to 1200 m at 10MBaud)
  and support up to 32 receivers. Typically, a bus topology is used.

  RS485 only defines the electrical specification of the bus, that is voltages, timing, typical impedances,
  and termination resistors. It does not specify any addressing, packets, or checksums - that is
  specified by higher level protocols.

  3. Useful functions
  Disclaimer: not all of these are perfect. Many may overflow when mistreated. This code
  was written to in major part demonstrate the ideas and teach the basics.
  There is a uart_print(volatile uint8_t *) function that is very reudimentary,
  but that's what contains the logic for sending strings over 485 (switch to TX, send,
  switch to RX) on the wire. It expects a null terminated string. Demonstrates the basic 485
  transceiver operation.

  This file contains an xxd(char *) function that hexdumps a null terminated string. I've written
  many a those functions, this one is very simple - you can easily improve it for longer payloads
  by doing the following things:
   1) change it so that it takes a len argument (for dumpbing binary strings with zeros in them)
   2) then chenge the loop code to make it more xxd(1) style (print offsets and break the line
      every 16 bytes):
      for(int i = 0; i < len; i++){
        if(i % 16 == 0) printf("\n%04x: " + i);
        printf("%02x ", d[i]);

  This file is full of useful string-oriented functions for interfacing with the serial port
  int open_port(char *devname) - open a serial port (wrapper)
  bool write_chars(int fd, char *data) 
  char *read_line(int fd, char *buf)  - write and read a string to the port
  set_rts, clr_rts, set_cts, clr_cts - modem lines control (ioctl wrappers)

  4. Notes on software
  The AVR program is pretty much the result of the Linux-AVR serial tutorial [2] with the
  init code modified and some 485 logic added (mostly in the uart_print function). It implements
  an interrupt based state machine for listening and uses the UART peripheral along with a GPIO
  pin to control the 485 transceiver. The pin number can be changed in the main.c file (it's
  a macro). LCD GPIO assignments may be changed in the hd44780.h header file. The firmare checks
  the address assigned by the dip switches, and stores it for display. It's not used when
  receiving packets. Modifying the ISR is one of, and the easiest way, to implement this.

  5. Notes on hardware
  As mentioned before, the RS485 transceiver is switched between Tx and Rx mode by a GPIO pin.
  The HD44780 display is being run in 4-bit mode.

  6. Links

  http://polprog.net - my blog
  http://avrs.fi - AVR programming related links
  1 - http://radzio.dxp.pl/hd44780/ HD44780 library by Radosław Kwiecień
  2 - https://polprog.net/blog/serial/ Linux-AVR serial communication tutorial