user32173: I need to send 6 bytes value from Arduino UNO to the actuator using serial port in order to make it extends. I have added a button to send the data. For example, 0 20 1 1 0 0. The communication settings are 9600 baud rate, 8 data bits, no parity and 1 stop bit. I have used MAX 232 to build a TTL to RS232 circuit. The actuator does not move. Are there any problems on my program? I am new to create USART code. I think I have some problems on transmit function. I am using the build in USART with c code. Here is my code:
#define BAUD_PRESCALER 0x67
int buttonPin1 = 4;
int buttonState1 = 0;

void setup() {
    // put your setup code here, to run once:
    pinMode(buttonPin1, INPUT_PULLUP);
}

unsigned char data1 = 0x00; // 0
unsigned char data2 = 0x14; // 20
unsigned char data3 = 0x01; // 1
unsigned char data4 = 0x01; // 1
unsigned char data5 = 0x00; // 0
unsigned char data6 = 0x00; // 0

void USART_Init(void) {
    /*Set baud rate */
    UBRR0 = BAUD_PRESCALER;
    UCSR0A = 0x00;
    /*Enable receiver and transmitter */
    UCSR0B = 0x08;
    /* Set frame format: 8data, 1stop bit */
    UCSR0C = 0x06;
}

void USART_Transmit( unsigned char data ) {
    /* Wait for empty transmit buffer */
    while ( !( UCSR0A & (1<<UDRE0)) )
    {
        ;
    }
    /* Put data into buffer, sends the data */
    UDR0 = data;
}

void loop() {
    // put your main code here, to run repeatedly:
    buttonState1 = digitalRead(buttonPin1);
    if (buttonState1 == LOW)
    {
        USART_Init();
        USART_Transmit(data1);
        USART_Transmit(data2);
        USART_Transmit(data3);
        USART_Transmit(data4);
        USART_Transmit(data5);
        USART_Transmit(data6);
    }
}


user32463: I would recommend you to use the arduino's built in UART library. Try this code:
int buttonPin1 = 4;
int buttonState1 = 0;

unsigned char data1 = 0; 
unsigned char data2 = 20;
unsigned char data3 = 1;
unsigned char data4 = 1;
unsigned char data5 = 0;
unsigned char data6 = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);           // set up Serial library at 9600 bps
  while (!Serial);              // wait till UART initialized, for arduino UNO

  pinMode(buttonPin1, INPUT_PULLUP);
}


void loop() {
    // put your main code here, to run repeatedly:
    buttonState1 = digitalRead(buttonPin1);
    if (buttonState1 == LOW)
    {
        Serial.write(data1);
        Serial.write(data2);
        Serial.write(data3);
        Serial.write(data4);
        Serial.write(data5);
        Serial.write(data6);
    }
}

You should know that as long as the button is pressed data is being sent to the actuator. With that being said, with one button press you may have the commands sent 10 times. Thus, you have to deal with that in your code.

user29202: I think that your BAUD rate setting is wrong. The UBRR0 register is a 16 bit wide one thus should be accessed in two 8 bit part, via UBRR0H and UBRR0L. You can check it in the datasheet.
If you further check the datasheet there is an example code in addition to the USART description which shows a correct way of initialization.

#define FOSC 1843200 // Clock Speed
#define BAUD 9600
#define MYUBRR FOSC/16/BAUD-1
void main( void )
{
    ...

    USART_Init(MYUBRR)
    ...
}

void USART_Init( unsigned int ubrr)
{
    /*Set baud rate */
    UBRR0H = (unsigned char)(ubrr>>8);
    UBRR0L = (unsigned char)ubrr;
    /* Enable receiver and transmitter */
    UCSR0B = (1<<RXEN0)|(1<<TXEN0);
    ...
}


As you can see the UBRR's value is calculated using the system clock speed FOSC, with MYUBRR macro which is an equation from the datasheet. Later this value is being set in two part upper 8 bit:
UBRR0H = (unsigned char)(ubrr>>8);

lower 8 bit:
UBRR0L = (unsigned char)ubrr;

(Your transmit function seems to be correct. And as I commented it is enough to call the USART init once in the setup().)

user13327: The first thing I do when I have a transceiver that doesn't work is a loopback test. This rules out a lot of potential issues like the wrong pin being used or MAX 232 not being connected correctly.
Receiving your transmission on a PC using USB to RS232 adapter (if you have one) would also be a helpful test.

32173: <p>I need to send 6 bytes value from Arduino UNO to the <a href="http://www.zaber.com/wiki/Manuals/T-LA#Initial_Setup_and_Testing" rel="nofollow noreferrer">actuator</a> using serial port in order to make it extends. I have added a button to send the data. For example, 0 20 1 1 0 0. The communication settings are 9600 baud rate, 8 data bits, no parity and 1 stop bit. I have used MAX 232 to build a TTL to RS232 circuit. The actuator does not move. Are there any problems on my program? I am new to create USART code. I think I have some problems on transmit function. I am using the build in USART with c code. Here is my code:</p>

<pre><code>#define BAUD_PRESCALER 0x67
int buttonPin1 = 4;
int buttonState1 = 0;

void setup() {
    // put your setup code here, to run once:
    pinMode(buttonPin1, INPUT_PULLUP);
}

unsigned char data1 = 0x00; // 0
unsigned char data2 = 0x14; // 20
unsigned char data3 = 0x01; // 1
unsigned char data4 = 0x01; // 1
unsigned char data5 = 0x00; // 0
unsigned char data6 = 0x00; // 0

void USART_Init(void) {
    /*Set baud rate */
    UBRR0 = BAUD_PRESCALER;
    UCSR0A = 0x00;
    /*Enable receiver and transmitter */
    UCSR0B = 0x08;
    /* Set frame format: 8data, 1stop bit */
    UCSR0C = 0x06;
}

void USART_Transmit( unsigned char data ) {
    /* Wait for empty transmit buffer */
    while ( !( UCSR0A &amp; (1&lt;&lt;UDRE0)) )
    {
        ;
    }
    /* Put data into buffer, sends the data */
    UDR0 = data;
}

void loop() {
    // put your main code here, to run repeatedly:
    buttonState1 = digitalRead(buttonPin1);
    if (buttonState1 == LOW)
    {
        USART_Init();
        USART_Transmit(data1);
        USART_Transmit(data2);
        USART_Transmit(data3);
        USART_Transmit(data4);
        USART_Transmit(data5);
        USART_Transmit(data6);
    }
}
</code></pre>
 32463: <p>I would recommend you to use the arduino's built in UART library. Try this code:</p>

<pre><code>int buttonPin1 = 4;
int buttonState1 = 0;

unsigned char data1 = 0; 
unsigned char data2 = 20;
unsigned char data3 = 1;
unsigned char data4 = 1;
unsigned char data5 = 0;
unsigned char data6 = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);           // set up Serial library at 9600 bps
  while (!Serial);              // wait till UART initialized, for arduino UNO

  pinMode(buttonPin1, INPUT_PULLUP);
}


void loop() {
    // put your main code here, to run repeatedly:
    buttonState1 = digitalRead(buttonPin1);
    if (buttonState1 == LOW)
    {
        Serial.write(data1);
        Serial.write(data2);
        Serial.write(data3);
        Serial.write(data4);
        Serial.write(data5);
        Serial.write(data6);
    }
}
</code></pre>

<p>You should know that as long as the button is pressed data is being sent to the actuator. With that being said, with one button press you may have the commands sent 10 times. Thus, you have to deal with that in your code.</p>
 29202: <p>I think that your BAUD rate setting is wrong. The <code>UBRR0</code> register is a 16 bit wide one thus should be accessed in two 8 bit part, via <code>UBRR0H</code> and <code>UBRR0L</code>. You can check it in the <a href="http://www.atmel.com/images/Atmel-8271-8-bit-AVR-Microcontroller-ATmega48A-48PA-88A-88PA-168A-168PA-328-328P_datasheet_Complete.pdf" rel="nofollow noreferrer">datasheet</a>.</p>

<p>If you further check the datasheet there is an example code in addition to the USART description which shows a correct way of initialization.</p>

<blockquote>
<pre><code>#define FOSC 1843200 // Clock Speed
#define BAUD 9600
#define MYUBRR FOSC/16/BAUD-1
void main( void )
{
    ...

    USART_Init(MYUBRR)
    ...
}

void USART_Init( unsigned int ubrr)
{
    /*Set baud rate */
    UBRR0H = (unsigned char)(ubrr&gt;&gt;8);
    UBRR0L = (unsigned char)ubrr;
    /* Enable receiver and transmitter */
    UCSR0B = (1&lt;&lt;RXEN0)|(1&lt;&lt;TXEN0);
    ...
}
</code></pre>
</blockquote>

<p>As you can see the UBRR's value is calculated using the system clock speed <code>FOSC</code>, with <code>MYUBRR</code> macro which is an equation from the datasheet. Later this value is being set in two part upper 8 bit:</p>

<pre><code>UBRR0H = (unsigned char)(ubrr&gt;&gt;8);
</code></pre>

<p>lower 8 bit:</p>

<pre><code>UBRR0L = (unsigned char)ubrr;
</code></pre>

<p>(Your transmit function seems to be correct. And as I commented it is enough to call the USART init once in the <code>setup()</code>.)</p>
 13327: <p>The first thing I do when I have a transceiver that doesn't work is a loopback test. This rules out a lot of potential issues like the wrong pin being used or MAX 232 not being connected correctly.</p>

<p>Receiving your transmission on a PC using USB to RS232 adapter (if you have one) would also be a helpful test.</p>
