I recently succeeded in communicating with the ATMEL 328P using USART. However, because of my lack of programming experience, I have no idea how to take the commands I am sending it from the PC, and make them form a useful string. Also the general format of the data I am sending it is very inconsistent when the Arduino relays it back to the PC. It can change substantially by changing the code in the smallest way.
I'm very inexperienced but enrolled in an embedded systems class, with the only programming experience I have being a semester of Matlab, 2.5 years ago. We are not to use the included Arduino library functions, instead we have to make our own. So if my main program is right, maybe it's in one of my functions.
In this screenshot I sent the command "PC0=0" and the data comes back very inconsistent, I also included some of the code that I'm sure is not right:
For some reason, that I can't figure out, it keeps exiting the loop when there should still be received bytes to be read. This is causing it to keep repeating "Enter Input" over and over. I don't know what's wrong.
Like I mentioned earlier, changing the code even slightly, makes the data come back entirely different.
Here I changed the index in a way so it would not repeat the code. I then sent the command PC2=1
Main Program Code:
int main()
{
init();
set_BAUDRATE(9600); //9600 or 115200
InitUsart0Module();
enable_TransmitBuffer();
enable_ReceiveBuffer();
EnableGlobalInterrupts();
TIMER2_init();
PORTC_DDR = ( BIT0 | BIT1 | BIT2 );
PORTC_DATA = ~PORTC_DATA; //Turn all on
PORTB_DDR = ( BIT0 | BIT1 );
index = 0;
printhis( "...Initialized\n");
while(1)
{
char dataByte;
uint8_t count = 0;
if ( index == 0 )
{
printhis( "\nEnter Input: ");
index = 1;
}
if ( index == 2 )
{
while (( ReceivedBytes() != 0) && (count < 6) )
{
dataByte = GetNextReceivedByte();
The_Command[count] = dataByte;
count++;
}
PORTB_DATA = ~(BIT0);
index = 3;
}
if ( index == 3 )
{
The_Command[6] = 0;
snprintf((char *)message, SERIAL_MESSAGE,"\nENTERED: %s\n", The_Command);
mySerialWrite(message);
index = 0;
}
}
return 0;
}
The commands, after I figure out how to make the arduino interpret them properly, will be used to toggle LED's on Port C pins 0, 1, and 2 from the PC. Port B LED's are just there for debugging purposes, because many times I'm not sure if a function was even being called like it was supposed to.
My Interrupt service routines for this program:
//USART RECEIVE INTERRUPT ON DATA RECEIVED
ISR(USART_RX_vect)
{
PORTB_DATA |= BIT0;
index = 2;
if (( UCSR0A_REG & UCSR0A_RX_MASK ) != UCSR0A_RX_EMPTY )
{
g_receiveBuffer[g_receiveHead++] = UDR0_REG;
if ( g_receiveHead > USART_RECEIVE_BUFFER_SIZE )
{
g_receiveHead = 0;
}
}
}
//USART TRANSMIT INTERRUPT ON DATA REGISTER EMPTY
ISR(USART_UDRE_vect)
{
// The data register is empty so we need to load it with the next ASCII character
// to transmit or disable interrupts
if (g_transmitTail == g_transmitHead)
{
DisableUsartTxInterrupt();
}
else
{
// load register, initiate transfer
UDR0_REG = g_transmitBuffer[g_transmitTail];
// increment and wrap tail
g_transmitTail++;
if (g_transmitTail == USART_BUFFER_SIZE)
{
g_transmitTail = 0;
}
}
}
//TIMER/COUNTER2 INTERRUPT ON COMPARE MATCH A
// for my custom delay function
ISR(TIMER2_COMPA_vect, ISR_NOBLOCK)
{
g_msecCount = g_msecCount + 1;
}
My custom USART Receive Functions:
//Get next Byte Received
uint8_t GetNextReceivedByte(void)
{
uint8_t receivedByte = 0;
if ( ReceivedBytes() > 0 )
{
receivedByte = g_receiveBuffer[g_receiveTail++];
if ( g_receiveTail >= USART_RECEIVE_BUFFER_SIZE )
{
g_receiveTail = 0;
}
}
return receivedByte;
}
//Check for bytes waiting to be read
uint8_t ReceivedBytes (void)
{
uint8_t receiveHead;
uint8_t receivedBytes = 0;
DisableGlobalInterrupts();
receiveHead = g_receiveHead;
EnableGlobalInterrupts();
if ( receiveHead > g_receiveTail )
{
receivedBytes = receiveHead - g_receiveTail;
}
if ( receiveHead < g_receiveTail )
{
// Check to see if the head has bit the tail
receivedBytes = receiveHead + ( USART_RECEIVE_BUFFER_SIZE - g_receiveTail );
}
return receivedBytes;
}
Of course, there's many more functions I didn't post because I'm pretty sure they are right and this post would get really long.