초보의 아웅다웅 설계하기/Cypress_PSOC

Cypress-PSoc Designer - 모듈 설정하고 간단 프로그램 짜기 2

로망와니 2011. 10. 14. 16:19


/* Code begins here */

#include <m8c.h>        // part specific constants and macros
#include "PSoCAPI.h"    // PSoC API definitions for all User Modules

void TransmitStatus (void);
void TX8_Wait(void);
void TX8_MultiChart(void);
void TX8_Init_Display(void);
void SetBaudRate (void);
int abs(int a);


WORD Detect_Count;

/*******************************************************************************
* Name     :    Delay
* Input    :    Number of loop cycles
* Output   :    None
*
* Function :    This routine generates software delay
*******************************************************************************/
void Delay(int iLoopCount)
{
     int iLoopIndex;
 
     /* at 12 MHZ operation, stop bits need to be high for 8us,
   * (25*4/12MHZ) = 8.33us each instruction is 4 clock cycles */
     for(iLoopIndex = 0; iLoopIndex <iLoopCount; iLoopIndex++);  
}

void main(void)
{
 LED_1_On();
    Delay(10000);    /* mark after break signal */
 LED_2_On();
    Delay(10000);    /* mark after break signal */
 LED_1_Off();
    Delay(10000);    /* mark after break signal */
 LED_2_Off();
 
 TX8_Init_Display();

 //Enable Global Interrupts.
 M8C_EnableGInt;

// TX8_Init_Display();//

 //Start CSD User Module.
 CSDADC_Start();
    //Sets the finger Thresholds for all the Sensors.
 CSDADC_SetDefaultFingerThresholds(); 
 //Initializes BaseLines for all the Sensors.
 CSDADC_InitializeBaselines(); 

 TX8_Init_Display();

 //Do continuously.
 while(1)
 {
  //Scan sensors.
  CSDADC_ScanSensor(0);
  
  //Update Baselines.
  CSDADC_UpdateSensorBaseline(0);


  //Send CapSense buttonsdata to the MultiChart
  TX8_MultiChart();

  //Send CapSense buttonsdata to the HyperTerminal
//  TransmitStatus();
 }
}

void SetBaudRate (void)
{
 //Now, set OSC_CR1 & OSC_CR3 registers to get desired baudrate (9600 bits/second).
 OSC_CR1 = 0xCB;  //Set VC1 & VC2 values
 OSC_CR3 = 0x01;  //Set VC3 value

// -- SETTING
// CPU CLOCK : SysClk/2
// VC1 : 1
// VC2 : 1
// VC3 SOURCE: VC2
// VC3 DIVIDER : 1
// SYSCLK SOURCE : INTERNAL 24MHZ
// 
// -- TX SET
// CLOCK : VC3
}

void TransmitStatus (void)
{
 BYTE bCounter, bTemp1, bTemp2;
 WORD wCentroid;
 
 bTemp1 = OSC_CR1;
 bTemp2 = OSC_CR3;
 
 SetBaudRate();
 
 //Start the TX8 user module
 TX8_Start(TX8_PARITY_NONE);
 
 //Come to beginning of the line & give space
 TX8_CPutString("\n\r");
 
 //Wait until last byte is transmitted
 TX8_Wait();

 wCentroid = CSDADC_waSnsDiff[0];

 TX8_PutSHexInt(wCentroid);   //Send the Centroid postion value to HyperTerminal
 
 //Wait until last byte is transmitted
 TX8_Wait();
 
 //Stop TX8 user module
 TX8_Stop();

 if(wCentroid >= 10)
    {
       Detect_Count++;
    }
 else
 {
       Detect_Count = 0;
  LED_1_Off();
  LED_2_Off();
 }
 if( Detect_Count >= 5)
    {
      Detect_Count = 0;
  LED_1_On();
  LED_2_On();
    }
 
 //Restore back the settings of OSC_CR1 & OSC_CR3
 OSC_CR1 = bTemp1;
 OSC_CR3 = bTemp2;
}

// When TX8_Wait() function is called, the execution stays in this function
// until the byte in TX Buffer is loaded into shift register & then transmitted completely.
void TX8_Wait(void)
{
 //wait until last byte is loaded into shift register
 while( !( TX8_bReadTxStatus() & TX8_TX_BUFFER_EMPTY ) );
 //wait for last byte of the above string to be transmitted
 while( !( TX8_bReadTxStatus() & TX8_TX_COMPLETE ) );
}

void TX8_MultiChart(void)//Cypress 에서 제공하는 MultiChart 사용시
{
 BYTE bCounter, bTemp1, bTemp2;
 WORD wCentroid;

// BYTE bTemp1, bTemp2;
 
 //CSD user module sets VC1, VC2 & VC3 dividers to the required values depending on Resolution
 //& Scanning speed of CSD (for details refer to parameters section of CSD datasheet).
 //But, for TX8 we may need different values of VC1, VC2 & VC3 to generate desired baud rate.
 //Hence, backup the OSC_CR1 (contains VC1 & VC2 setting) & OSC_CR3 (contains VC3 setting)
 //registers into bTemp1 & bTemp2 variables.
 bTemp1 = OSC_CR1;
 bTemp2 = OSC_CR3;
 
 SetBaudRate();
 
 //Start the TX8 user module
 TX8_Start(TX8_PARITY_NONE);
 
 TX8_PutCRLF(); // Send Header

 TX8_Write((char *) (CSDADC_waSnsResult), CSDADC_TotalSensorCount*2);
 TX8_Write((char *) (CSDADC_waSnsBaseline), CSDADC_TotalSensorCount*2);
 TX8_Write((char *) (CSDADC_waSnsDiff), CSDADC_TotalSensorCount*2);
 
 TX8_PutChar(0); // Send Tail
 TX8_PutChar((CHAR)0xFF);
 TX8_PutChar((CHAR)0xFF);

 //Wait until last byte is transmitted
 TX8_Wait();
 //Stop TX8 UM before changing the VC1, VC2 or VC3.
 TX8_Stop();
 
 wCentroid = CSDADC_waSnsDiff[0];

 if(wCentroid >= 10)
    {
       Detect_Count++;
    }
 else
 {
       Detect_Count = 0;
       LED_1_Off();
  LED_2_Off();
 }
 if( Detect_Count >= 5)
    {
      Detect_Count = 0;
  LED_1_On();
  LED_2_On();
    }
 
 //Restore back the settings of OSC_CR1 & OSC_CR3
 OSC_CR1 = bTemp1;
 OSC_CR3 = bTemp2;
}

void TX8_Init_Display(void)
{
 BYTE bTemp1, bTemp2;
 
 //CSD user module sets VC1, VC2 & VC3 dividers to the required values depending on Resolution
 //& Scanning speed of CSD (for details refer to parameters section of CSD datasheet).
 //But, for TX8 we may need different values of VC1, VC2 & VC3 to generate desired baud rate.
 //Hence, backup the OSC_CR1 (contains VC1 & VC2 setting) & OSC_CR3 (contains VC3 setting)
 //registers into bTemp1 & bTemp2 variables.
 bTemp1 = OSC_CR1;
 bTemp2 = OSC_CR3;
 
 SetBaudRate();
 
 //Start the TX8 user module
 TX8_Start(TX8_PARITY_NONE);

 //Display Title
 TX8_CPutString("\n\n\r  Cypress Semiconductor\n\r  CapSense with TX8");
 
 //Display the Table Header
 TX8_CPutString("\n\n\rBUTTON#   0    1    2    3    4      Slider Centroid Position \n\r");
 //Wait until last byte is transmitted
 TX8_Wait();
 //Stop TX8 UM before changing the VC1, VC2 or VC3.
 TX8_Stop();

 //Restore back the settings of OSC_CR1 & OSC_CR3
 OSC_CR1 = bTemp1;
 OSC_CR3 = bTemp2;
}

int abs(int a)
{
  if(a < 0)
  {
    return -a;
  }
  return a;
}