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);
}
}
USART_init
does not have to be called every time, it is enough if you call it insetup()
./*Enable receiver and transmitter */
is incorrect: you are only enabling the transmitter.