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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
| #include "stm32f10x.h" #include "usart.h" #include "ultrasonic.h" #include "stm32f10x_gpio.h" #include "stm32f10x_i2c.h" #include "delay.h"
#include <stdio.h> #include <math.h> #include "bsp_i2c_gpio.h"
int fputc(int ch, FILE *f) { USART_SendData(USART1, (unsigned char) ch); while (!(USART1->SR & USART_FLAG_TXE)); return (ch); }
void main_init(void) { Usart_Init(); bsp_InitI2C(); delay_init(72); }
extern u8 max30102_Bus_Read(u8 Register_Address); extern void max30102_init(void);
int main(void) { u8 temp_num=0; main_init(); max30102_init(); printf("\r\n MAX30102 init \r\n");
while(1) { delay_ms(1000); max30102_init(); temp_num = max30102_Bus_Read(0x1f); printf("当前温度 = %d\r\n",temp_num); }
}
#define max30102_WR_address 0xAE
u8 max30102_Bus_Write(u8 Register_Address, u8 Word_Data) {
i2c_Start();
i2c_SendByte(max30102_WR_address | I2C_WR);
if (i2c_WaitAck() != 0) { goto cmd_fail; }
i2c_SendByte(Register_Address); if (i2c_WaitAck() != 0) { goto cmd_fail; } i2c_SendByte(Word_Data);
if (i2c_WaitAck() != 0) { goto cmd_fail; }
i2c_Stop(); return 1;
cmd_fail: i2c_Stop(); return 0; }
u8 max30102_Bus_Read(u8 Register_Address) { u8 data;
i2c_Start();
i2c_SendByte(max30102_WR_address | I2C_WR);
if (i2c_WaitAck() != 0) { goto cmd_fail; }
i2c_SendByte((uint8_t)Register_Address); if (i2c_WaitAck() != 0) { goto cmd_fail; }
i2c_Start();
i2c_SendByte(max30102_WR_address | I2C_RD);
if (i2c_WaitAck() != 0) { goto cmd_fail; }
{ data = i2c_ReadByte();
i2c_NAck(); } i2c_Stop(); return data;
cmd_fail: i2c_Stop(); return 0; }
void max30102_FIFO_Read(u8 Register_Address,u16 Word_Data[][2],u8 count) { u8 i=0; u8 no = count; u8 data1, data2; i2c_Start();
i2c_SendByte(max30102_WR_address | I2C_WR);
if (i2c_WaitAck() != 0) { goto cmd_fail; }
i2c_SendByte((uint8_t)Register_Address); if (i2c_WaitAck() != 0) { goto cmd_fail; }
i2c_Start();
i2c_SendByte(max30102_WR_address | I2C_RD);
if (i2c_WaitAck() != 0) { goto cmd_fail; }
while (no) { data1 = i2c_ReadByte(); i2c_Ack(); data2 = i2c_ReadByte(); i2c_Ack(); Word_Data[i][0] = (((u16)data1 << 8) | data2);
data1 = i2c_ReadByte(); i2c_Ack(); data2 = i2c_ReadByte(); if(1==no) i2c_NAck(); else i2c_Ack(); Word_Data[i][1] = (((u16)data1 << 8) | data2);
no--; i++; } i2c_Stop();
cmd_fail: i2c_Stop(); }
#define INTERRUPT_REG 0X00
void max30102_init() { max30102_Bus_Write(0x09, 0x0b); max30102_Bus_Write(0x01, 0xF0); max30102_Bus_Write(INTERRUPT_REG, 0x00); max30102_Bus_Write(0x03, 0x02); max30102_Bus_Write(0x21, 0x01);
max30102_Bus_Write(0x0a, 0x47);
max30102_Bus_Write(0x0c, 0x47); max30102_Bus_Write(0x0d, 0x47); }
|