3.c.01. Command 200: Set Serial Parameters

Effect: Specifies the settings that govern the mega168’s serial port. This command will set the baud (by giving the three lowest bits of UBRRH, the eight-bit UBRRL, and 2x speed bit), the parity (disabled, even, or odd), and the number of stop bits in a packet (one or two). The character size of a packet will always be 8 bits. Parity is a two-bit enumeration as follows: 0 = no parity, 1 = reserved, 2 = even parity, 3 = odd parity. If the 2x-speed bit is set, the baud rate as set by UBRR is doubled. If the stop-bits bit is cleared, one stop bit will be used; if it is set, two stop bits will be used. UBRR relates to baud as follows:

    in normal mode:
    	baud = ( 20 MHz / 16 ) / ( UBRR + 1 ) = 1.25 MHz / ( UBRR + 1 ), so
    	UBRR = ( 1.25 MHz / baud ) – 1

    in double-speed mode:
    	baud = ( 20 MHz / 8 ) / ( UBRR + 1 ) = 2.5 MHz / ( UBRR + 1 ), so 
    	UBRR = ( 2.5 MHz / baud ) – 1

Note: If you are using firmware version 1.00, you must give the mega168 time to set its serial parameters to the desired values before issuing a serial-transmit command. We recommend you wait at least 50 us after sending a Set Serial Parameters command before attempting to use the UART. No such delay is needed if you are running version 1.01 or later.

In addition to the serial setting parameters described above, there is a flag that can put the mega168 into permanent program mode, which means that incoming packets over the UART are treated as an attempt to program the mega644. When set, all other serial parameters sent along with this command are ignored as the UART is now reserved solely for programming. If put into permanent program mode, the mega644 can be programmed without requiring the reset button be held until the yellow LED lights. Using permanent program mode therefore can be more convenient for you if you’re doing a lot of incremental software development for the mega644, but the tradeoff is that you cannot use the serial port for any other purpose (such as debugging, logging values, etc).

In permanent program mode, the mega168 and mega644 will run normally until the mega168 receives a command over the UART that it is to enter programming ISP mode. At this point the mega168 becomes the SPI master and the mega644 is held reset. When the mega168 receives the UART command to leave programming ISP mode at the completion of the ISP action, it will reset itself and the two microcontrollers will once again run normally.

If the save-to-EEPROM bit is one, these serial settings will persist after a hardware reset. Three EEPROM bytes are used to encode these settings (one byte for UBRRH, one byte for UBRRL, and one byte for the rest). The EEPROM encoding differs from the SPI encoding:

    	EEPROM byte 13: (permanent program mode bit << 6) |
    	                (2-bit parity << 4) |
    	                (stop-bits bit << 3) |
    	                (2x-speed bit << 1)
    	EEPROM byte 14: UBRRH
    	EEPROM byte 15: UBRRL

If you want to store the permanent program mode setting so that you will still be in that mode after a hardware reset, you can accomplish this by writing 0x40 to EEPROM byte 13. Warning: the parameters that are stored in EEPROM can be written thousands of times, but not infinitely many. An automated setup that rewrites a parameter over and over could potentially burn out the memory in a few minutes.

Values sent: baud (11 bits — UBRRH and UBRRL), 2x speed (1 bit), parity (2 bits), one or two stop bits (1 bit), permanent program mode (1 bit), save to EEPROM (1 bit)

command byte = 200 | (2-bit parity << 1) | save to EEPROM bit

data byte 1 = (permanent program mode bit << 6) | (stop-bits bit << 5) | (2x-speed bit << 4) | (UBRRH << 1) | MSB of UBRRL

data byte 2 = seven lowest bits of UBRRL

If you want to enable permanent programming mode, you only need to send:

command byte = 200
data byte 1 = 64
data byte 2 = 0