Untitled - MARKUP 9.88 KB
                                
                                    /*******************************************************************************
* File Name: main.c
* Version 1.0
*
* Description:
* This c file contains the main function for Temperature Sensing application using FTMF board 
* in CY3270/71 Kit using I2C as Communication.
*
* Note:
*
********************************************************************************
* Copyright (2011), 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.
*******************************************************************************/

/******************************************************************************
                                THEROY OF OPERATION
* This project demonstrates the use of Temperature Sensors.
* The firmware supports 3 LED and Temperature sensor.
* The "main" function completes the initialization of I2C Slave and Capsense
* component.
* After initialization of the User Modules, the "main" enters into a loop
* which consists of,
* Get ADC data.
* Calculate the voltage ratio of thermistor voltage and excitation voltage
* Get the lower and upper temperature values with respect to current voltage ratio and 
* corresponding voltage ratios from table for interpolation
* Interpolate the values to find the temperature value in hundredths of a deg C
* obtain the integer form of the temperature value
* Change the sign (+ve or -ve) of the temperature f required.
* If temperature is between -10 dec C to 16 dec C, Blue LED is turned ON
* If temperature is between 16 dec C to 28 dec C, Green LED is turned ON
* If temperature is between 28 dec C to 55 dec C, Red LED is turned ON
* If temperature exceeds either the lower (-10) or upper limit (55), buzzer is turned ON
* PSoC Designer Version: 5.1 SP1.1 (Build:2110)
*******************************************************************************/

/*----------------------------------------------------------------------------
 C main line
---------------------------------------------------------------------------- */

#include <m8c.h>        /* part specific constants and macros */
#include "PSoCAPI.h"    /* PSoC API definitions for all User Modules */
#define BUFSIZE		1
#define RWBOUNDARY	1
#define	MIN		-101
#define	MAX		551	
#define COUNT_VALUES    23
#define REF_RESISTOR	10000
#define REF_VOLTAGE		0x2FB
#define DUTYCYCLE		50

//Defines for enabling Temperature Input
#define ReadVTEMPInput      (ABF_CR0 = 0x00)
#define ReadVTEMP_EXCInput  (ABF_CR0 = 0x80)

const int arTherm[2][COUNT_VALUES] = 
{   
   {2301, 2505, 2725, 2960, 3211, 3477, 3757, 4051, 4358, 4675, 5000, 5331, 5664, 5998, 6328, 6652, 6967, 7269, 7557, 7829, 8083, 8317,  8462},
   {5500, 5200, 4900, 4600, 4300, 4000, 3700, 3400, 3100, 2800, 2500, 2200, 1900, 1600, 1300, 1000,  700,  400,  100, -200, -500, -800, -1000}   // array of temperature values (in hundredths of a deg C)
//     1           3           5           7           9          11          13          15          17          19          21           23
};

struct I2C_Regs 
{ 
BYTE blueLed;	
BYTE buzzer;
BYTE greenLed;
BYTE redLed;
BYTE pwmPeriod;
BYTE tempRange;
int  temp;
} Data_Regs;

void main(void)
{
	BYTE bPointIndex;
	long lVtherm;
	long ivalue1,ivalue2,itemp1,itemp2;
	int temperature,i, Value;
	int compareValue, period;
	
	EzI2Cs_SetRamBuffer(sizeof(Data_Regs), RWBOUNDARY, (BYTE *) &Data_Regs); 
	M8C_EnableGInt;
 
	EzI2Cs_Start();  /*I2C start */
	LED_RED_Start();
	LED_GREEN_Start();
	LED_BLUE_Start();
	Data_Regs.temp=0;
	while(1)
	{
		ADC10_Start(ADC10_FULLRANGE);     // Start the User Module  
		ADC10_iCal(0x1FF, ADC10_CAL_VBG);  // Calibrate the ADC so 1.3V = 0x01FF  
		ADC10_StartADC();
	
	    ReadVTEMPInput;        // Enable the Analog Column Input Mux to
                               // read Port0.0/VTemp input
		
		while(ADC10_fIsDataAvailable() == 0){};  /*checking for the availability of data */
		lVtherm = ADC10_iGetDataClearFlag();   /* Get the data and clear the flag */
		
        ReadVTEMP_EXCInput;    // Enable the Analog Column Input Mux to
                               // read Port0.7/VTemp_exc input

		for(i=0; i<20000; i++)
		{
			;		// Give a delay before changing Analog input source
		}
		while(ADC10_fIsDataAvailable() == 0){};  /*checking for the availability of data */
		ivalue1 = ADC10_iGetDataClearFlag();   /* Get the data and clear the flag */
		
		ADC10_Stop();
		ADC10_StopADC();
		
		//ivalue1 = REF_VOLTAGE;		// Excitation voltage
		lVtherm *= REF_RESISTOR;	// Calculate the thermistor voltage value
		lVtherm /= ivalue1;			// get voltage ratio of thermistor voltage and excitation voltage

		if ((int)lVtherm < arTherm[0][0])
		{
			// The voltage ratio is too low, so the temperature is greater than what can be measured                
			lVtherm = MAX;
		}
		else if((int)lVtherm > arTherm[0][COUNT_VALUES-1])
		{
			// The voltage ratio is too high, so the temperature is less than what can be measured.
			lVtherm = MIN; 
		}
		else 
		{  
			// Scan through the voltage ratio values in the piecewise linear curve fit data to find
            // the appropriate line to interpolate                
			for(bPointIndex = 0; bPointIndex < (COUNT_VALUES-2); bPointIndex++) 
			{																			
				if (lVtherm < arTherm[0][bPointIndex+1])  break;
			}
	
			// Retrieve the voltage ratios for interpolation
			ivalue1 = arTherm[0][bPointIndex];
			ivalue2 = arTherm[0][bPointIndex + 1];

            // Retrieve the temperatures for interpolation
			itemp1 = arTherm[1][bPointIndex];
			itemp2 = arTherm[1][bPointIndex + 1];


			// Interpolate to find the temperature in hundredths of a deg C
			lVtherm = (((long) ivalue2 - lVtherm) * (itemp1 - itemp2)) / (ivalue2 - ivalue1) + itemp2;

            // Divide the result by 10 in order to get the temperature in tenths of a deg C.                     
            // Round to the nearest tenth rather than truncating
     
            // First, get the temperature value as an integer
            ivalue1 = lVtherm;
     
            // Next, get the sign and absolute value of the temperature
            if (ivalue1 < 0)
            {               
               bPointIndex = 1; 
               ivalue1 = 0 - ivalue1;
            }
            else
            {
               bPointIndex = 0;
            }

            // Calculate the truncated form
            ivalue2 = ivalue1 / 10;  

            // Multiply the truncated form by 10 and add 5
            // If the result is less than or equal to the original undivided number, then the 
            // Truncated value must be incremented by 1.
            if ((ivalue2 * 10 + 5) <= ivalue1)
            {
               ivalue2++;
            }
             
            
            // Change the sign to negative if necessary               
            if (bPointIndex)
            {
               ivalue2 = 0 - ivalue2;
            }

            
            // Store the temperature value
            lVtherm = ivalue2;
		}

		if ((lVtherm >= -100) && (lVtherm < 160))
		{
			PWM8_Stop();
			LED_GREEN_Off();		// Turn ON blue LED if temp is between -10 and 16
			LED_RED_Off();
			LED_BLUE_On();

		}
		else if ((lVtherm >= 160) && (lVtherm < 280))
		{
			PWM8_Stop();
			LED_GREEN_On();			// Turn on Green LED if temp between 16 and 28
			LED_RED_Off();
			LED_BLUE_Off();		

		}
		else if ((lVtherm >= 280) && (lVtherm < 550))
		{
			PWM8_Stop();
			LED_GREEN_Off();		// Turn on Red LED if temp between 28 and 55
			LED_RED_On();
			LED_BLUE_Off();		
		}
		else
		{
			PWM8_Start();
			PWM8_EnableInt();
			period = PWM8_PERIOD;
			compareValue = ((int)(period + 1) * (int)DUTYCYCLE)/100;
    		PWM8_WritePulseWidth(compareValue);
		}
		temperature = (int) lVtherm;
		Data_Regs.temp = temperature ;	
	}
}
                                
                            

Paste Hosted With By Wklejamy.pl