Simple serial communication between pc and arduino

Hi all,

I have been working on a project now in which I want to send character to my arduino through the arduino serial terminal, and have it echo back the character. I am programming this without using any serial library functions for a school project. So far so good, but for some reason I can only get the "default" case from my switch statement. Ive tried several different things to try and send a "One\n" to the terminal, but no success. My code is attached

Any feedback would be really appreciated!

P.S. Im new to posting so my second post will be the code I have so far....sorry

#define USART_RECEIVE_BUFFER_SIZE 80
unsigned char g_receiveBuffer [ USART_RECEIVE_BUFFER_SIZE ];
unsigned char g_receiveHead ;
unsigned char g_receiveTail ;

#define UCSR0A_ADDR (unsigned char *) 0xC0
#define UDR0_ADDR (unsigned char *) 0xC6
#define UCSR0A_RXC_MASK 0x80
#define UCSR0A_RXC_EMPTY 0x00

#define USART_TRANSMIT_BUFFER_SIZE 80
unsigned char g_transmitBuffer [ USART_TRANSMIT_BUFFER_SIZE ];
unsigned char g_transmitHead ;
unsigned char g_transmitTail ;

#define UCSR0B_ADDR (unsigned char *) 0xC1
#define UCSR0B_TXEN_MASK 0x08
#define UCSR0B_TXEN_ON 0x08
#define UCSR0B_TXEN_OFF 0x00

#define SREG_ADDR (unsigned char *) 0x5F
#define SREG_GLOBAL_INT_ENABLE 0x80

#define FORCE_0_MASK 0x7F;

unsigned char *portDDRD;
unsigned char *portD;

void setup()
{
unsigned char *uControlandStatusRegisterA;
unsigned char *uControlandStatusRegisterB;
unsigned char *uControlandStatusRegisterC;
unsigned char *baudRateRegisterH;
unsigned char *baudRateRegisterL;

uControlandStatusRegisterA = (unsigned char *) 0xC0;
uControlandStatusRegisterB = (unsigned char *) 0xC1;
uControlandStatusRegisterC = (unsigned char *) 0xC2;
baudRateRegisterH = (unsigned char *) 0xC5;
baudRateRegisterL = (unsigned char *) 0xC4;

*uControlandStatusRegisterA &= 0xE0;
*uControlandStatusRegisterA |= 0x40;
*uControlandStatusRegisterB &= 0xBB;
*uControlandStatusRegisterB |= 0xB8;
*uControlandStatusRegisterC = 0x0E;
*baudRateRegisterH &= 0xF0;
*baudRateRegisterL = 0x08;

portDDRD = (unsigned char *) 0x2A;
portD = (unsigned char *) 0x2B;

*portDDRD = 0xFC;
}

void loop ()
{
unsigned char dataByte ;

if ( ReceivedBytes () > 0)
{
dataByte = GetNextReceivedByte ();
switch (dataByte)
{
case '0':
TransmitString("Zero\n", 5);
break;
case '1':
TransmitString("One\n", 4);
break ;
case '2':
TransmitString("Two\n", 4);
break ;
case '3':
TransmitString("Three\n", 6);
break ;
case '4':
TransmitString("Four\n", 5);
break ;
case '5':
TransmitString("Five\n", 5);
break ;
case '6':
TransmitString("Six\n", 4);
break ;
case '7':
TransmitString("Seven\n", 6);
break ;
case '8':
TransmitString("Eight\n", 6);
break ;
case '9':
TransmitString("Nine\n", 5);
break ;
default :
TransmitString("Default\n", 8);
break ;
}
}
}

unsigned char ReceivedBytes (void )
{
unsigned char *statusRegister ;
unsigned char shadow ;
unsigned char receiveHead ;
unsigned char receivedBytes = 0;

statusRegister = SREG_ADDR;
shadow = *statusRegister;

statusRegister &= ~SREG_GLOBAL_INT_ENABLE;
/
Turn off global interrupts here. */
receiveHead = g_receiveHead;

/* Restore global interrupts here. */
if (( shadow & SREG_GLOBAL_INT_ENABLE ) == SREG_GLOBAL_INT_ENABLE )
{
*statusRegister |= SREG_GLOBAL_INT_ENABLE;
}

if ( receiveHead > g_receiveTail )
{
receivedBytes = receiveHead - g_receiveTail;
}
else if ( receiveHead < g_receiveTail )
{
/* Check the case that the head has circled around but the tail
hasn ’t. */
receivedBytes = receiveHead + (USART_RECEIVE_BUFFER_SIZE - g_receiveTail);
}
return receivedBytes ;
}

unsigned char GetNextReceivedByte (void )
{
unsigned char receivedByte = 0;

if ( ReceivedBytes () > 0)
{
receivedByte = g_receiveBuffer[g_receiveTail ++];

if ( g_receiveTail >= USART_RECEIVE_BUFFER_SIZE )
{
g_receiveTail = 0;
}
}
return receivedByte ;
}

ISR(USART_RX_vect)
{
*portD = 0x80;
unsigned char * portUSARTDataRegister ;
unsigned char * portUSARTControlAndStatusRegisterA ;
unsigned char shadow ;
portUSARTDataRegister = UDR0_ADDR;
portUSARTControlAndStatusRegisterA = UCSR0A_ADDR;

while (((* portUSARTControlAndStatusRegisterA ) & UCSR0A_RXC_MASK ) != UCSR0A_RXC_EMPTY)
{
g_receiveBuffer[g_receiveHead ++] = *portUSARTDataRegister;

if ( g_receiveHead >= USART_RECEIVE_BUFFER_SIZE )
{
g_receiveHead = 0;
}
}
}

void TransmitString (const char *bytes , unsigned char numberOfBytes )
{
unsigned char * portUSARTControlAndStatusRegisterB ;
unsigned char shadow ;
unsigned char i;

for (i = 0; i < numberOfBytes ; i++)
{
g_transmitBuffer [ g_transmitHead ++] = (unsigned char ) bytes ;

  • if ( g_transmitHead >= USART_TRANSMIT_BUFFER_SIZE )*

  • {*

  • g_transmitHead = 0;*

  • }*

  • }*
    _ /* Turn on the TX interrupt. */_

  • portUSARTControlAndStatusRegisterB = UCSR0B_ADDR;*
    _ shadow = *portUSARTControlAndStatusRegisterB;_

  • shadow &= ~( UCSR0B_TXEN_MASK);*

  • shadow |= (UCSR0B_TXEN_ON);*
    _ *portUSARTControlAndStatusRegisterB = shadow;_
    }
    ISR(USART_UDRE_vect)
    {
    _ unsigned char * portUSARTDataRegister ;_
    _ unsigned char * portUSARTControlAndStatusRegisterB ;_

  • unsigned char shadow ;*

  • portUSARTDataRegister = UDR0_ADDR;*

  • portUSARTControlAndStatusRegisterB = UCSR0B_ADDR;*
    _ shadow = *portUSARTControlAndStatusRegisterB;_

  • shadow &= ~( UCSR0B_TXEN_MASK);*

  • if ( g_transmitHead != g_transmitTail )*

  • {*
    *portUSARTDataRegister = g_transmitBuffer[g_transmitTail ++];

  • if ( g_transmitTail >= USART_TRANSMIT_BUFFER_SIZE )*

  • {*

  • g_transmitTail = 0;*

  • }*

  • shadow |= (UCSR0B_TXEN_ON);*

  • }*

  • else*

  • {*

  • shadow |= (UCSR0B_TXEN_OFF);*

  • }*
    _ *portUSARTControlAndStatusRegisterB = shadow;_
    }

A few things:

  1. When posting code, please use the # icon on the editor's toolbar (this also avoids the problems of smilies).

2)void TransmitString (const char *bytes , unsigned char numberOfBytes ) - you're sending a string, which the C compiler has kindly terminated with a '\0' - there's no need to send the length, just send characters to the transmitter until you encounter a '0'.

3)Why not print the character code of the character you received for the default? This would help debug, and would add only a half-dozen lines of code.