原创 stm32基于硬件i2c的基本程序已完成

2011-10-26 21:48 2630 18 18 分类: MCU/ 嵌入式

硬件平台: Skater-STM + Skater-IMU9-DI1U(adxl345+mpu3050+mag3110)
IDE平台:IAR 5.30
STM32固件库:V3.40
仿真工具:jlink v8

完成功能:基于STM32的硬件I2C1接口,读取传感器ID,并通过串口1批量输出所有寄存器的数据,数据采集和输出同时led亮;
                 串口设置 9600,N,8,1

完整程序详见:http://www.flyarm.cn/read.php?tid=20

主程序部分代码:

if( Uart1_Get_Flag )
        {
          
          GPIO_WriteBit(GPIOD, GPIO_Pin_2, (BitAction)0x00); //设置led灯亮
                  
          switch (Uart1_Get_Data)
          {
          case '1':
            printf("\r\n 您选择需要输出的数据是:%c---加速度\r\n",Uart1_Get_Data);
            AID=SKATER_I2C_ByteRead(ADXL345_ADDRESS,0x00);
            printf("\r\n加速度设备ID=%x,\r\n",AID);
            SKATER_I2C_ByteWrite(ADXL345_ADDRESS, 0x2D,0x08);               //简单初始化
            printf("\r\n加速度原始数据输出(00H~40H连续读取)\r\n");
            SKATER_I2C_MutiRead(Acc_Data, ADXL345_ADDRESS, 0x00,64);
  
            int i1,j1,k1;
            k1=0;
             while( k1<64 )
             {
             for(i1=0;i1<4;i1++)
              {
                for(j1=0;j1<16;j1++)
                {
                                  
                  printf(" %x ",Acc_Data[k1++]);
                }
                 printf("\r\n");            
              }              
             }

            break;
          case '2':
            printf("\r\n 您选择需要输出的数据是:%c---陀螺仪\r\n",Uart1_Get_Data);
            GID=SKATER_I2C_ByteRead(MPU3050_ADDRESS,0x00);
            printf("\r\n陀螺仪设备ID=%x,\r\n",GID);
            SKATER_I2C_ByteWrite(MPU3050_ADDRESS, 0x16,0x10);          //简单初始化
            printf("\r\n陀螺仪原始数据输出(00H~40H连续读取)\r\n");
            SKATER_I2C_MutiRead(G_Data, MPU3050_ADDRESS, 0x00,64);
            int i2,j2,k2;
                  k2=0;
                  while( k2<64 )
             {
             for(i2=0;i2<4;i2++)
              {
                for(j2=0;j2<16;j2++ )
                {
                  printf(" %x ",G_Data[k2++]);
                }
                 printf("\r\n");            
              }              
             }
            break;
          case '3':
            printf("\r\n 您选择需要输出的数据是:%c---磁阻\r\n",Uart1_Get_Data);
            MID=SKATER_I2C_ByteRead(MAG3110_ADDRESS,0x07);
            printf("\r\n 磁阻传感器设备ID=%x,",MID);
            SKATER_I2C_ByteWrite(MAG3110_ADDRESS, 0x10,0x01);
            printf("\r\n磁阻传感器原始数据输出(00H~14H连续读取)\r\n");
            SKATER_I2C_MutiRead(M_Data,MAG3110_ADDRESS, 0x00,18);
            int k3;
                 k3=0;
             while( k3<18 )
             {
        
                  printf(" %x ",M_Data[k3++]);
              }
                 printf("\r\n");            
                            
            
            break;
          case '4':
            printf("\r\n 您选择需要输出的数据是:%c---压力\r\n",Uart1_Get_Data);
//           PID=SKATER_I2C_ByteRead(ADXL345_ADDRESS,0x00);
            printf("\r\n PID=%x, Px=      Py=     Pz=",PID);
            break;
          default:
            printf("\r\n 输入无效,请重新输入:\r\n");
            break;
          }
          Uart1_Get_Flag=0;  
          welcome( );
        GPIO_WriteBit(GPIOD, GPIO_Pin_2, (BitAction)0x01); //设置led灯灭
        }        
      }
测试效果切图:
20111026214722211.jpg?65

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
18
关闭 站长推荐上一条 /3 下一条