UNIT 26 SPI Test 로봇 SW 교육원 조용수
학습 목표 SPI Sample SPI Read/Write Function SPI loop back Test MPL115A1 Pressure and Temperature Sensor
SPI Sample SPI Loop Back Source \M051_Series_BSP_CMSIS_Rev3.00.001\SampleCode\StdDriver\SPI_Loopback\KEIL
System Init void SYS_Init(void) { SYS_UnlockReg(); CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HIRC, CLK_CLKDIV_UART(1)); CLK_SetModuleClock(SPI0_MODULE, CLK_CLKSEL1_SPI0_S_HCLK, MODULE_NoMsk); CLK_EnableModuleClock(UART0_MODULE); CLK_EnableModuleClock(SPI0_MODULE); SYS->P3_MFP = SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0; SYS->P1_MFP = SYS_MFP_P14_SPISS0 | SYS_MFP_P15_MOSI_0 | SYS_MFP_P16_MISO_0 | SYS_MFP_P17_SPICLK0; SYS_LockReg(); SystemCoreClockUpdate(); }
System Init void SPI_Init(void) { /* Configure as a master, clock idle low, 32-bit transaction, drive output on falling clock edge and latch input on rising edge. */ /* Set IP clock divider. SPI clock rate = 2MHz */ SPI_Open(SPI0, SPI_MASTER, SPI_MODE_0, 32, 2000000); /* Enable the automatic hardware slave select function. Select the SS pin and configure as low-active. */ SPI_EnableAutoSS(SPI0, SPI_SS, SPI_SS_ACTIVE_LOW); }
System Init while(1) { /* Write to TX register */ SPI_WRITE_TX0(SPI0, g_au32SourceData[u32DataCount]); /* Trigger SPI data transfer */ SPI_TRIGGER(SPI0); /* Check SPI0 bust status */ while(SPI_IS_BUSY(SPI0)); /* Read received data */ g_au32DestinationData[u32DataCount] = SPI_READ_RX0(SPI0); u32DataCount++; if(u32DataCount > TEST_COUNT) break; }
System Init while(1) { /* Write to TX register */ SPI_WRITE_TX0(SPI0, g_au32SourceData[u32DataCount]); /* Trigger SPI data transfer */ SPI_TRIGGER(SPI0); /* Check SPI0 bust status */ while(SPI_IS_BUSY(SPI0)); /* Read received data */ g_au32DestinationData[u32DataCount] = SPI_READ_RX0(SPI0); u32DataCount++; if(u32DataCount > TEST_COUNT) break; }
MPL115A1 Digitized pressure and temperature information together with programmed calibration coefficients for host micro use. Factory calibrated 50 kPa to 115 kPa absolute pressure ±1 kPa accuracy 2.375V to 5.5V supply Integrated ADC SPI Interface Monotonic pressure and temperature data outputs Surface mount RoHS compliant package
MPL115A1
MPL115A1
MPL115A1 SPI Command
System Init void SPI_Init(void) { /* Configure as a master, clock idle low, 32-bit transaction, drive output on falling clock edge and latch input on rising edge. */ /* Set IP clock divider. SPI clock rate = 2MHz */ SPI_Open(SPI0, SPI_MASTER, SPI_MODE_0, 8, 2000000); /* Enable the automatic hardware slave select function. Select the SS pin and configure as low-active. */ SPI_EnableAutoSS(SPI0, SPI_SS, SPI_SS_ACTIVE_LOW); }
MPL115A1 void writeSPI(char address , char data ) { int count = 1000; SPI_SET_SS_LOW(SPI0); while(count--); /* Write to TX register */ SPI_WRITE_TX0(SPI0, address); /* Trigger SPI data transfer */ SPI_TRIGGER(SPI0); /* Check SPI0 bust status */ while(SPI_IS_BUSY(SPI0)); SPI_WRITE_TX0(SPI0, data); SPI_SET_SS_HIGH(SPI0); }
MPL115A1 char readSPI(char address) { char read; int count = 1000; SPI_SET_SS_LOW(SPI0); while(count--); SPI_WRITE_TX0(SPI0, address); SPI_TRIGGER(SPI0); while(SPI_IS_BUSY(SPI0)); SPI_WRITE_TX0(SPI0, 0x00); read = SPI_READ_RX0(SPI0); SPI_SET_SS_HIGH(SPI0); return read; }
MPL115A1 #define NWS_BARO 30.04 #define PRESH 0x80 // 80 #define PRESL 0x82 // 82 #define TEMPH 0x84 // 84 #define TEMPL 0x86 // 86 #define A0MSB 0x88 // 88 #define A0LSB 0x8A // 8A #define B1MSB 0x8C // 8C #define B1LSB 0x8E // 8E #define B2MSB 0x90 // 90 #define B2LSB 0x92 // 92 #define C12MSB 0x94 // 94 #define C12LSB 0x96 // 96 #define C11MSB 0x98 // 98 #define C11LSB 0x9A // 9A #define C22MSB 0x9C // 9C #define C22LSB 0x9E // 9E
MPL115A1 float A0; float B1; float B2; float C12; void readMPL115A1_Coefficients() { int A0H, A0L, B1H, B1L, B2H, B2L, C12H, C12L; A0H = readSPI(A0MSB); A0L = readSPI(A0LSB); A0 = (A0H << 5) + (A0L >> 3) + (A0L & 0x07) / 8.0; B1H = readSPI(B1MSB); B1L = readSPI(B1LSB); B1 = ((((B1H & 0x1F ) * 0x100) + B1L) / 8192.0) - 3; B2H = readSPI(B2MSB); B2L = readSPI(B2LSB); B2 = ((((B2H - 0x80 ) << 8) + B2L) / 16384.0) - 2; C12H = readSPI(C12MSB); C12L = readSPI(C12LSB); C12 = (((C12H * 0x100 ) + C12L) / 16777216.0) ; }
MPL115A1 float readMPL115A1() { int count = 30000; unsigned char uiPH, uiPL; float press = 0.0; float pressure = 0.0; float presKPa = 0.0; float temp = 0.0; unsigned int uiTadc; unsigned char uiTH, uiTL; unsigned int temperature_counts = 0; writeSPI(0x24, 0x00); // Start temperature conversion while(count--); // Read pressure uiTH = readSPI(TEMPH); uiTL = readSPI(TEMPL); uiPH = readSPI(PRESH); uiPL = readSPI(PRESL); printf("Read MPL115A1 0x%x, 0x%x \n", uiTH, uiTL); printf("Read MPL115A1 0x%x, 0x%x \n", uiPH, uiPL); uiTadc = (unsigned int) uiTH << 8; uiTadc += (unsigned int) uiTL & 0x00FF; // Temperature is a 10bit value uiTadc = uiTadc >> 6; // -5.35 counts per °C, 472 counts is 25°C temp = 25 + (uiTadc - 472) / -5.35; printf("Temperature %f \n", temp); press = ((uiPH * 256 ) + uiPL) / 64; temp = ((uiTH * 256) + uiTH) / 64; pressure = A0 + (B1 + C12 * temp) * press + B2 * temp; presKPa = pressure * (65.0/1023.0) + 50.0; printf("Pressure %f , Pressure(KPa) %f \n", pressure, presKPa); }
MPL115A1 uiTadc = (unsigned int) uiTH << 8; uiTadc += (unsigned int) uiTL & 0x00FF; // Temperature is a 10bit value uiTadc = uiTadc >> 6; // -5.35 counts per °C, 472 counts is 25°C temp = 25 + (uiTadc - 472) / -5.35; printf("Temperature %f \n", temp); press = ((uiPH * 256 ) + uiPL) / 64; temp = ((uiTH * 256) + uiTH) / 64; pressure = A0 + (B1 + C12 * temp) * press + B2 * temp; presKPa = pressure * (65.0/1023.0) + 50.0; printf("Pressure %f , Pressure(KPa) %f \n", pressure, presKPa); }