HC12 Read String or Char

Use this to read a string on the HC12. What this function does is check to see if there is an available character in the HC12’s Serial Communication Interface Port 0 Data Register. If you need to use this function with 9bit values the function needs to be adjusted to incorporate SC0DRH. Similarity if you would like to use Port 1 change SC0 to SC1.

UPDATE: I added the single char version because thats more basic.

/*******************************************
    Get Character
         Usage:    Get a character from the serial port
          Exit:    Returns the char from the input
*******************************************/
char Read(void)
{
    //Wait for the Receive Data Register Full Flag of SC0SR1
    while((SC0SR1 & 0x20) == 0)
    {
        //Yield or Wait        
    }
 
    //Read the Data Register
    char input = SC0DRL;
 
    //Echo
    Print(input);
 
    //Return value
    return input;
}
 
 
/*******************************************
  Get String
    Usage: Gets a string from the serial
    Enter: Buffer address and max buffer size.
     Exit: True on Overflow, False on Return Key
*******************************************/
bool Read(char* buffer, unsigned int size)
{
    char input = '\0';
    unsigned int i = 0;
    while ( i < size )
    {        
        //Wait for the Receive Data Register Full Flag of SC0SR1
        while((SC0SR1 & 0x20) == 0)
        {
            //Yield or Wait        
        }
 
        //Read the Data Register
        input = SC0DRL;
 
        if ( input == 0x08 && i > 0 )
        {
            //Delete
            buffer[i--] = '\0';
        }
        else if ( input == 0x0D )
        {
            //Enter
            buffer[i] = '\0';
            return false;
        }
        else
        {
            buffer[i++] = input;
        }
        //Echo
        Print(input);
    }
    return true;
}

2 Responses to “HC12 Read String or Char”

  1. Tristan says:

    Hey have you gotten the mon12 commands to work within your code? I got the address from your blog entry, but I can’t seem to get it to respond with anything but “bad argument”.

  2. Shawn says:

    I am assuming you want to use something like an “MD” Memory Display in your code. I haven’t done this myself but I would venture to guess you could acheve this in one of two ways.

    typedef void(*protoMemoryDisplay)(void*,void* = 0);
    protoMemoryDisplay MemoryDisplay = (protoMemoryDisplay)(0xC9B6);
    MemoryDisplay((void*)0x4000,(void*)0x4400);

Leave a Reply