//---------------------------------------------------------------------------- // Designed by A. Kitagawa, MeRL, Kanazawa Univ. 2012.9.11 //---------------------------------------------------------------------------- #include // part specific constants and macros #include "PSoCAPI.h" // PSoC API definitions for all User Modules #include "string.h" #include "stdlib.h" #include "ctype.h" int setGain(char *); int calGain(char *); void reset_dev(void); void main(void) { #define Npar 2 // Number of valied command parameters #define skip 4000 // Data transmission skip cycles in slow mode #define DataCount 201 // Number of sampling data in continuous mode #define RBTopen 0x02690C07 // RBT-001 Bluetooth Linked #define RBTclose 0x02690E02 // RBT-001 Bluetooth Link released #define RBTnoeventH 0x02524E01 // RBT-001 event filter = No report upper 32bit #define RBTnoeventL 0x00A10303 // RBT-001 event filter = No report lower 32bet #define RBTname1 0x0252040B // RBT-001 device name: Tobacco-D #define RBTname2 0x00610A54 #define RBTname3 0x6F626163 #define RBTname4 0x636F2D44 #define RBTname5 0x0003 // Variables char * strCmd; // Command: RX char * strPar; // Parameter: RX char * para[Npar]; // Command parameters int dac_val = 0; // DAC input value int dac_ref = 0; // DAC initial value int adcResult[1]; // ADC output value int i, j, k; // Loop counters int Nth = skip; // Thined-out rate of transmission rate int gstate; // PGA setting status int Nmax = DataCount; // Number of sampling data // Command list char meas[] = "meas"; // continuous measurement char sample[] = "sample"; // one-shot measurement char set[] = "set"; // configuration char stop[] = "stop"; // stop aquisition char reset[] = "reset"; // initiallization // Parameter list char gain[] = "gain"; // Set gain char dac[] = "dac"; // Set DAC // Command for Bluetooth module long rbt_filterH = RBTnoeventH; // No event report long rbt_filterL = RBTnoeventL; // No event report long rbt_name1 = RBTname1; // Device name long rbt_name2 = RBTname2; // Device name long rbt_name3 = RBTname3; // Device name long rbt_name4 = RBTname4; // Device name short rbt_name5 = RBTname5; // Device name // Enable the global and local interrupts M8C_EnableGInt; // Start LED LED_1_Start(); LED_1_On(); // Start UART UART_1_CmdReset(); // Initialize receiver/cmd buf UART_1_IntCntl(UART_1_ENABLE_RX_INT); // Enable RX interrupts UART_1_Start(UART_1_PARITY_NONE); // Enable UART // AGND level output RefMux_1_Start(RefMux_1_LOWPOWER); // Initiallization of DAC DAC9_1_Start(DAC9_1_MEDPOWER); // Counter start Counter8_1_Start(); // DAC start dac_val = dac_ref; // Initiallize DAC value DAC9_1_WriteBlind(dac_val); // Update DAC1 output immediately // Initiallization of PGA PGA_1_SetGain(PGA_1_G1_00); // Sst initial gain PGA_1_Start(PGA_1_HIGHPOWER); // Start PGA // Initiallization of ADC DelSigPlus_1_Start(DelSigPlus_1_HIGHPOWER); // Turn on Analog section DelSigPlus_1_ClearFlag(); // Clear data ready flag DelSigPlus_1_StopAD(); // Stop sampling // Wait for a start of RBT-001 for 128ms (by using no user module) SleepTimer_1_Start(); SleepTimer_1_SetInterval(SleepTimer_1_64_HZ); // Set interrupt to a 64Hz SleepTimer_1_EnableInt(); SleepTimer_1_TickWait(8); // wait for 125ms SleepTimer_1_DisableInt(); // Supress evevts reports by RBT-001 UART_1_Write((char *)rbt_filterH, 4); UART_1_Write((char *)rbt_filterL, 4); // Set device name for RBT-001 UART_1_Write((char *)rbt_name1, 4); UART_1_Write((char *)rbt_name2, 4); UART_1_Write((char *)rbt_name3, 4); UART_1_Write((char *)rbt_name4, 4); UART_1_Write((char *)rbt_name5, 2); // Main loop while (1) { // Reciver if (UART_1_bCmdCheck()) { // Wait for command if (strCmd = UART_1_szGetParam()) { // More than delimiter // Command echo without event report if ( isalpha(strCmd[0])) { UART_1_CPutString("ACommand: ["); UART_1_PutString(strCmd); // Echo out command UART_1_CPutString("] Param: "); // Check parameters i = 0; while (strPar = UART_1_szGetParam()) { // Loop on each parameter if (i < Npar) para[i] = strPar; // Save valied parameters i++; UART_1_CPutString("["); UART_1_PutString(strPar); // Echo each parameter UART_1_CPutString("] "); } UART_1_CPutString("\r\n"); } // Excute received command if (strcmp(strCmd, meas) == 0) { // Contunuous measuremnt LED_1_On(); // Cycle of skipped ADC and DAC data transmission Nth = skip; // Number of skip cycles j = 0; // Skip couter // ADC start DelSigPlus_1_StopAD(); // Stop sampling DelSigPlus_1_ClearFlag(); // Clear data ready flag DelSigPlus_1_StartAD(); // Start sampling } else if (strcmp(strCmd, sample) == 0) { // One-shot measurement LED_1_On(); // ADC buffer reset and restart DelSigPlus_1_StopAD(); // Stop sampling DelSigPlus_1_ClearFlag(); // Clear data ready flag DelSigPlus_1_StartAD(); // Start sampling // Wait for sampling while (1) { if (DelSigPlus_1_fIsDataAvailable()) { // Get Data from ADC buffer adcResult[0] = DelSigPlus_1_iGetDataClearFlag(); break; } } // Stop ADC DelSigPlus_1_StopAD(); // Stop sampling DelSigPlus_1_ClearFlag(); // Clear data ready flag // Send one-shot sampling data UART_1_CPutString("B"); // Start Delimitor UART_1_PutSHexInt(adcResult[0] + 0x4000); // ADC result (ASCII Hex) UART_1_CPutString("\r\n"); LED_1_Off(); } else if (strcmp(strCmd, stop) == 0) { // STOP LED_1_Off(); // Stop ADC DelSigPlus_1_StopAD(); // Stop sampling DelSigPlus_1_ClearFlag(); // Clear data ready flag } else if (strcmp(strCmd, reset) == 0) { // RESET LED_1_Off(); // Stop ADC DelSigPlus_1_StopAD(); // Stop sampling DelSigPlus_1_ClearFlag(); // Clear data ready flag // Reset PGA gain PGA_1_SetGain(PGA_1_G1_00); // Reset DAC value and update dac_val = dac_ref; DAC9_1_WriteBlind(dac_val); // Reset command message UART_1_CPutString("ADAC = "); UART_1_PutSHexInt(dac_val); UART_1_CPutString(", PGA gain = 1.00"); UART_1_CPutString("\r\n"); } else if (strcmp(strCmd, set) == 0) { // SET LED_1_Off(); // Stop ADC DelSigPlus_1_StopAD(); // Stop sampling DelSigPlus_1_ClearFlag(); // Clear data ready flag // Set parameter and send message if (strcmp(para[0], gain) == 0) { gstate = setGain(para[1]); if (gstate != -1) { UART_1_CPutString("APGA gain = "); UART_1_PutSHexInt(gstate); UART_1_CPutString("\r\n"); } else { UART_1_CPutString("AUnresolved parameter"); UART_1_CPutString("\r\n"); } } // Set DAC else if (strcmp(para[0], dac) == 0) { dac_val = atoi(para[1]); if (dac_val < -255) { dac_val = -255; } else if (dac_val > 255) { dac_val = 255; } DAC9_1_WriteBlind(dac_val); // Update DAC output immediately UART_1_CPutString("ADAC input = "); UART_1_PutString(para[1]); UART_1_CPutString("\r\n"); } else { UART_1_CPutString("AUnresolved parameter"); UART_1_CPutString("\r\n"); } } else { // UNINTERPRETED // No effective command UART_1_CPutString("AUninterpreted command - Ignored"); UART_1_CPutString("\r\n"); } } UART_1_CmdReset(); // Clear command buffer of UART } // Read data and reset flag of ADC 1-3, and output from Multiplying DAC if (DelSigPlus_1_fIsDataAvailable()) { // Check data to be ready adcResult[0] = DelSigPlus_1_iGetDataClearFlag(); // Get Data from ADC buffer // Transmission of measurement results in master device j++; if (j >= Nth) { // Transmission with binary format UART_1_CPutString("B"); UART_1_PutSHexInt(adcResult[0] + 0x4000); // ADC result (14bit offset binary) UART_1_CPutString("\r\n"); j = 0; if (k > Nmax) { // Stop ADC DelSigPlus_1_StopAD(); // Stop sampling DelSigPlus_1_ClearFlag(); // Clear data ready flag LED_1_Off(); k = 0; } else { k++; } } } } } int setGain(char * par0) { double gain; gain = atoi(par0); if (gain >= 48.0) { PGA_1_SetGain(PGA_1_G48_0); return 4800; } else if (gain >= 24.00) { PGA_1_SetGain(PGA_1_G24_0); return 2400; } else if (gain >= 16.00) { PGA_1_SetGain(PGA_1_G16_0); return 1600; } else if (gain >= 8.00) { PGA_1_SetGain(PGA_1_G8_00); return 800; } else if (gain >= 4.00) { PGA_1_SetGain(PGA_1_G4_00); return 400; } else if (gain >= 2.00) { PGA_1_SetGain(PGA_1_G2_00); return 200; } else if (gain >= 1.00) { PGA_1_SetGain(PGA_1_G1_00); return 100; } else if (gain >= 0.50) { PGA_1_SetGain(PGA_1_G0_50); return 50; } else if (gain >= 0.25) { PGA_1_SetGain(PGA_1_G0_25); return 25; } else if (gain >= 0.12) { PGA_1_SetGain(PGA_1_G0_12); return 12; } else if (gain < 0.12) { PGA_1_SetGain(PGA_1_G0_06); return 6; } else { return -1; } }