TEST 구성
MPU-9250 Module 에서 WHO AM I 레지스터 값을 SPI 통신을 이용하여STM32F101C8T6 에서 읽어와
PLM-100 모듈로 전송하여 결과를 확인하는 TEST 를 진행하였다.
[ MPU-9250-Refister-Map 44page ]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | #include <stdio.h> #include "stm32f10x.h"
#define CONFIG 0x1A #define GYRO_CONFIG 0x1B #define INT_PIN_CFG 0x37 #define INT_ENABLE 0x38
#define USER_CTRL 0x6A #define PWR_MGMT_1 0x6B #define WHO_AM_I 0x75
#define DLPF_CFG_42HZ 0x03 #define I2C_IF_DIS 0x10 #define BIT_H_RESET 0x80
#define MPU_CLK_SEL_PLLGYROX 0x01 #define MPU_CLK_SEL_PLLGYROZ 0x03
/* 00:01:93 */ volatile unsigned int msTicks;
void LED_Init(void); void UART_Init(void); void SPI1_Init(void); void SPI1_SS_L(void); void SPI1_SS_H(void); void PLM_100_RF_SendData(unsigned char* data, int len); void SPI1_Write_byte(uint8_t addr, uint8_t data); void Delay(unsigned int dlyTicks);
uint8_t mpuData[2] = {0,};
void SysTick_Handler(void){ msTicks++; }
int main(void) { unsigned char data[20] = {0,};
LED_Init(); UART_Init(); SPI1_Init(); SystemCoreClockUpdate(); SysTick_Config(SystemCoreClock / 10000); GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_SET);
while(1) { GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_SET); Delay(1000); SPI1_Write_byte(WHO_AM_I | 0x80, 0); sprintf((char *)data, "0x%x, 0x%x", mpuData[0], mpuData[1]); PLM_100_RF_SendData(data, sizeof(data)-1); GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_RESET); Delay(10000); } }
void PLM_100_RF_SendData(unsigned char* data, int len) { unsigned char atCmd[] = "AT+DATA=000193:"; int i;
/* AT Cmd 전송 */ for(i = 0; i < sizeof(atCmd)-1; i++){ USART_SendData(USART1, atCmd[i]); Delay(10); }
for(i = 0; i < len; i++){ USART_SendData(USART1, data[i]); Delay(10); }
USART_SendData(USART1, '\r'); }
void LED_Init(void) { GPIO_InitTypeDef PORTB;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
PORTB.GPIO_Pin = GPIO_Pin_12; PORTB.GPIO_Speed = GPIO_Speed_50MHz; PORTB.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &PORTB); }
void UART_Init(void) { GPIO_InitTypeDef PORTA; USART_InitTypeDef USART1_struct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
PORTA.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; PORTA.GPIO_Speed = GPIO_Speed_50MHz; PORTA.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &PORTA);
USART_Cmd(USART1, ENABLE); USART1_struct.USART_BaudRate = 115200; USART1_struct.USART_WordLength = USART_WordLength_8b; USART1_struct.USART_Parity = USART_Parity_No; USART1_struct.USART_StopBits = USART_StopBits_1; USART1_struct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART1_struct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_Init(USART1, &USART1_struct); USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); NVIC_EnableIRQ(USART1_IRQn); }
void SPI1_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; SPI_InitTypeDef SPI_InitStructure;
/* Enable GPIO clock for SPI MASTER */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_SPI1 | RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; /* PA6 : SDO */ GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7; /* PA5 : SCLK, PA7 : SDI */ GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; /* PA4 : nCS */ GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOA, &GPIO_InitStructure);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI1, &SPI_InitStructure);
SPI1_SS_H(); SPI_Cmd(SPI1, ENABLE); }
void SPI1_Write_byte(uint8_t addr, uint8_t data) { SPI1_SS_L();
/* 레지스터 주소 전송 */ while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); SPI_I2S_SendData(SPI1, addr);
/* SPI 더미 데이터 수신 */ while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); mpuData[0] = SPI_I2S_ReceiveData(SPI1);
/* SPI 더미 데이터 송신 */ while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); SPI_I2S_SendData(SPI1, data);
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); mpuData[1] = SPI_I2S_ReceiveData(SPI1);
SPI1_SS_H(); }
/* SPI LOW CLOCK */ void SPI1_SS_L(void) { GPIO_ResetBits(GPIOA, GPIO_Pin_4); }
/* SPI HIGH CLOCK */ void SPI1_SS_H(void) { GPIO_SetBits(GPIOA, GPIO_Pin_4); }
void USART1_IRQHandler(void) { if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) { if(USART_ReceiveData(USART1) != 0) { GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_RESET); while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET) { } } } }
void Delay(unsigned int dlyTicks) { unsigned int curTicks;
curTicks = msTicks; while((msTicks - curTicks) < dlyTicks){ __NOP(); } } |
[TEST 결과]
STM32F130C8T6 (feat.TDCR1050M FND) (0) | 2018.11.19 |
---|---|
nRF51422-QFAB(GPIO/ADC) (0) | 2018.11.01 |
PLM-100 RF Module (6) | 2016.12.12 |
STM32F103VCT6 for MPU-9250 자이로 센서 StartUp (0) | 2016.09.26 |
STM32 Clock - 클럭 소스와 클럭 설정 (0) | 2016.09.23 |