/*******************************************************************************
* File Name: main.c 
*
* Version: 1.2
*
* Description:
*  This file provides source code for the ADC to LCD example project.  The  
*  firmware takes a voltage output from a potentiometer and displays the raw 
*  counts on an LCD. 
* 
* Code Tested With:
*  1. PSoC Creator 1.0 Beta 4 Build 5810
*  2. PSoC Programmer 3.10 Beta 4 Build 616
*  3. DP8051 Keil Generic Compiler
*  4. MiniProg3 Rev *A  FW version 2.05 [2.88/1.12]
*
********************************************************************************
* Copyright (2010), Cypress Semiconductor Corporation.
********************************************************************************
* This software is owned by Cypress Semiconductor Corporation (Cypress)
* and is protected by and subject to worldwide patent protection (United
* States and foreign), United States copyright laws and international treaty
* provisions. Cypress hereby grants to licensee a personal, non-exclusive,
* non-transferable license to copy, use, modify, create derivative works of,
* and compile the Cypress Source Code and derivative works for the sole
* purpose of creating custom software in support of licensee product to be
* used only in conjunction with a Cypress integrated circuit as specified in
* the applicable agreement. Any reproduction, modification, translation,
* compilation, or representation of this software except as specified above 
* is prohibited without the express written permission of Cypress.
*
* Disclaimer: CYPRESS MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, WITH 
* REGARD TO THIS MATERIAL, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
* Cypress reserves the right to make changes without further notice to the 
* materials described herein. Cypress does not assume any liability arising out 
* of the application or use of any product or circuit described herein. Cypress 
* does not authorize its products for use as critical components in life-support 
* systems where a malfunction or failure may reasonably be expected to result in 
* significant injury to the user. The inclusion of Cypress' product in a life-
* support systems application implies that the manufacturer assumes all risk of 
* such use and in doing so indemnifies Cypress against all charges. Use may be 
* limited by and subject to the applicable Cypress software license agreement. 
*******************************************************************************/

#include <device.h>

/* LCD specific */
#define ROW_0       0  /* LCD row 0     */
#define COLUMN_0    0  /* LCD column 0  */
#define COLUMN_9    9  /* LCD column 9  */
#define COLUMN_10  10  /* LCD column 10 */
#define COLUMN_11  11  /* LCD column 11 */
/* For clearing Tens and Hundreds place */
#define CLEAR_TENS_HUNDREDS "    "
/* For clearing Hundreds place */
#define CLEAR_HUNDREDS      "   "

void UpdateDisplay(uint16 voltageRawCount);


/*******************************************************************************
* Function Name: main
********************************************************************************
*
* Summary:
*   The main function initializes both the ADC and LCD, starts and waits for an 
*   ADC conversion, then it displays the raw counts to the LCD.
*
* Parameters:
*  void
*
* Return:
*  void
*
*******************************************************************************/
void main()
{
    uint16 voltageRawCount;
    
    ADC_DelSig_1_Start();  /* Configure and power up ADC */
    LCD_Char_1_Start(); /* Initialize and clear the LCD */

    LCD_Char_1_Position(ROW_0,COLUMN_0); /* Move the cursor to Row 0 Column 0 */
    
    /* Print Label for the pot voltage raw count */
    LCD_Char_1_PrintString("V Count: ");

    ADC_DelSig_1_StartConvert(); /* Force ADC to initiate a conversion */

    while(1)
    {
        /* Wait for end of conversion */
        ADC_DelSig_1_IsEndConversion(ADC_DelSig_1_WAIT_FOR_RESULT); 
        voltageRawCount = ADC_DelSig_1_GetResult16(); /* Get converted result */

        /* Set range limit */
        if (voltageRawCount > 0x7FFF)
        {
            voltageRawCount = 0;
        }
        else 
        {
            /* Continue on */
        }
        
        UpdateDisplay(voltageRawCount); /* Print result on LCD */
    }
}

/*******************************************************************************
* Function Name: UpdateDisplay
********************************************************************************
*
* Summary:
*   Print voltage raw count result to the LCD.  Clears some characters if
*   necessary.
*
* Parameters:
*   voltageRawCount: The voltage raw counts being received from the ADC 
*
* Return:
*   void
*
*******************************************************************************/
void UpdateDisplay (uint16 voltageRawCount)
{
    /* Move the cursor to Row 0, Column 9 */
    LCD_Char_1_Position(ROW_0,COLUMN_9); 
    LCD_Char_1_PrintNumber(voltageRawCount); /* Print the result */
    
    if (voltageRawCount < 10)
    {
        /* Move the cursor to Row 0, Column 10 */
        LCD_Char_1_Position(ROW_0,COLUMN_10); 
        LCD_Char_1_PrintString(CLEAR_TENS_HUNDREDS); /* Clear last characters */
    }
    else if (voltageRawCount < 100)
    {
        /* Move the cursor to Row 0, Column 11 */
        LCD_Char_1_Position(ROW_0,COLUMN_11); 
        LCD_Char_1_PrintString(CLEAR_HUNDREDS); /* Clear last characters */
    }
    else
    {
        /* Continue on */
    }
}


/* [] END OF FILE */
