OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 2558|回复: 4

L3G4200驱动问题。

[复制链接]

1

主题

2

帖子

0

精华

新手入门

积分
26
金钱
26
注册时间
2014-11-22
在线时间
0 小时
发表于 2014-11-22 14:53:02 | 显示全部楼层 |阅读模式
5金钱
[mw_shl_code=c,true] /******************************************************************************* // GY-50 L3G4200D IIC???????ò // ?????????úSTM32F103C8T6 // ?§????8.00M // ??????pc?®???ú?? ?¨??????115200 // ±à???·?? Keil uVision4 // ?±????2011?ê9??1?? // QQ??531389319 // ?????é???? GPIOB6->SCL GPIOB7->SDA // ???? STM32F103C8T6?®??1 // ±à???÷Keil uVision4 *******************************************************************************/ #include "stm32f10x.h" #include <math.h> //Keil library #include "stm32f10x_flash.h" GPIO_InitTypeDef GPIO_InitStructure; ErrorStatus HSEStartUpStatus; #define uchar unsigned char #define uint unsigned int #define L3G4200_Addr 0xD2 //?¨???÷????IIC×????????????·,?ù??ALT ADDRESS???·???????????? //**********L3G4200D?????????÷???·********* #define WHO_AM_I 0x0F #define CTRL_REG1 0x20 #define CTRL_REG2 0x21 #define CTRL_REG3 0x22 #define CTRL_REG4 0x23 #define CTRL_REG5 0x24 #define REFERENCE 0x25 #define OUT_TEMP 0x26 #define STATUS_REG 0x27 #define OUT_X_L 0x28 #define OUT_X_H 0x29 #define OUT_Y_L 0x2A #define OUT_Y_H 0x2B #define OUT_Z_L 0x2C #define OUT_Z_H 0x2D #define FIFO_CTRL_REG 0x2E #define FIFO_SRC_REG 0x2F #define INT1_CFG 0x30 #define INT1_SRC 0x31 #define INT1_TSH_XH 0x32 #define INT1_TSH_XL 0x33 #define INT1_TSH_YH 0x34 #define INT1_TSH_YL 0x35 #define INT1_TSH_ZH 0x36 #define INT1_TSH_ZL 0x37 #define INT1_DURATION 0x38 unsigned char TX_DATA[4]; unsigned char BUF[8]; //?????????????? char test=0; short T_X,T_Y,T_Z; //************************************ /*????IIC?????????????¨??*/ #define SCL_H GPIOB->BSRR = GPIO_Pin_6 #define SCL_L GPIOB->BRR = GPIO_Pin_6 #define SDA_H GPIOB->BSRR = GPIO_Pin_7 #define SDA_L GPIOB->BRR = GPIO_Pin_7 #define SCL_read GPIOB->IDR & GPIO_Pin_6 #define SDA_read GPIOB->IDR & GPIO_Pin_7 /* ?????ê?÷ -----------------------------------------------*/ void RCC_Configuration(void); void GPIO_Configuration(void); void NVIC_Configuration(void); void USART1_Configuration(void); void WWDG_Configuration(void); void Delay(u32 nTime); void Delayms(vu32 m); /* ±????¨?? ----------------------------------------------*/ /*******************************/ void DATA_printf(uchar *s,short temp_data) { float temp_dis; temp_dis=(float)temp_data*0.07; //×??????????? ??/?? temp_data=(int)temp_dis; if(temp_data<0){ temp_data=-temp_data; *s='-'; } else *s=' '; *++s =temp_data/100+0x30; temp_data=temp_data%100; //???à???? *++s =temp_data/10+0x30; temp_data=temp_data%10; //???à???? *++s =temp_data+0x30; } /******************************************************************************* * Function Name : I2C_GPIO_Config * Description : Configration Simulation IIC GPIO * Input : None * Output : None * Return : None ****************************************************************************** */ void I2C_GPIO_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; GPIO_Init(GPIOB, &GPIO_InitStructure); } /******************************************************************************* * Function Name : I2C_delay * Description : Simulation IIC Timing series delay * Input : None * Output : None * Return : None ****************************************************************************** */ void I2C_delay(void) { u8 i=30; //???????????????? ????????×?????5???????? while(i) { i--; } } void delay5ms(void) { int i=5000; while(i) { i--; } } /******************************************************************************* * Function Name : I2C_Start * Description : Master Start Simulation IIC Communication * Input : None * Output : None * Return : Wheather Start ****************************************************************************** */ bool I2C_Start(void) { SDA_H; SCL_H; I2C_delay(); if(!SDA_read)return FALSE; //SDA???????????ò×?????,???? SDA_L; I2C_delay(); if(SDA_read) return FALSE; //SDA???????????ò×??????í,???? SDA_L; I2C_delay(); return TRUE; } /******************************************************************************* * Function Name : I2C_Stop * Description : Master Stop Simulation IIC Communication * Input : None * Output : None * Return : None ****************************************************************************** */ void I2C_Stop(void) { SCL_L; I2C_delay(); SDA_L; I2C_delay(); SCL_H; I2C_delay(); SDA_H; I2C_delay(); } /******************************************************************************* * Function Name : I2C_Ack * Description : Master Send Acknowledge Single * Input : None * Output : None * Return : None ****************************************************************************** */ void I2C_Ack(void) { SCL_L; I2C_delay(); SDA_L; I2C_delay(); SCL_H; I2C_delay(); SCL_L; I2C_delay(); } /******************************************************************************* * Function Name : I2C_NoAck * Description : Master Send No Acknowledge Single * Input : None * Output : None * Return : None ****************************************************************************** */ void I2C_NoAck(void) { SCL_L; I2C_delay(); SDA_H; I2C_delay(); SCL_H; I2C_delay(); SCL_L; I2C_delay(); } /******************************************************************************* * Function Name : I2C_WaitAck * Description : Master Reserive Slave Acknowledge Single * Input : None * Output : None * Return : Wheather Reserive Slave Acknowledge Single ****************************************************************************** */ bool I2C_WaitAck(void) //·?????:=1??ACK,=0??ACK { SCL_L; I2C_delay(); SDA_H; I2C_delay(); SCL_H; I2C_delay(); if(SDA_read) { SCL_L; I2C_delay(); return FALSE; } SCL_L; I2C_delay(); return TRUE; } /******************************************************************************* * Function Name : I2C_SendByte * Description : Master Send a Byte to Slave * Input : Will Send Date * Output : None * Return : None ****************************************************************************** */ void I2C_SendByte(u8 SendByte) //????????????????// { u8 i=8; while(i--) { SCL_L; I2C_delay(); if(SendByte&0x80) SDA_H; else SDA_L; SendByte<<=1; I2C_delay(); SCL_H; I2C_delay(); } SCL_L; } /******************************************************************************* * Function Name : I2C_RadeByte * Description : Master Reserive a Byte From Slave * Input : None * Output : None * Return : Date From Slave ****************************************************************************** */ unsigned char I2C_RadeByte(void) //????????????????// { u8 i=8; u8 ReceiveByte=0; SDA_H; while(i--) { ReceiveByte<<=1; SCL_L; I2C_delay(); SCL_H; I2C_delay(); if(SDA_read) { ReceiveByte|=0x01; } } SCL_L; return ReceiveByte; } //ZRX //??×???????******************************************* bool Single_Write(unsigned char SlaveAddress,unsigned char REG_Address,unsigned char REG_data) //void { if(!I2C_Start())return FALSE; I2C_SendByte(SlaveAddress); //·????è±????·+??????//I2C_SendByte(((REG_Address & 0x0700) >>7) | SlaveAddress & 0xFFFE);//?è???????????·+?÷?????· if(!I2C_WaitAck()){I2C_Stop(); return FALSE;} I2C_SendByte(REG_Address ); //?è???????????· I2C_WaitAck(); I2C_SendByte(REG_data); I2C_WaitAck(); I2C_Stop(); delay5ms(); return TRUE; } //??×???????***************************************** unsigned char Single_Read(unsigned char SlaveAddress,unsigned char REG_Address) { unsigned char REG_data; if(!I2C_Start())return FALSE; I2C_SendByte(SlaveAddress); //I2C_SendByte(((REG_Address & 0x0700) >>7) | REG_Address & 0xFFFE);//?è???????????·+?÷?????· if(!I2C_WaitAck()){I2C_Stop();test=1; return FALSE;} I2C_SendByte((u8) REG_Address); //?è???????????· I2C_WaitAck(); I2C_Start(); I2C_SendByte(SlaveAddress+1); I2C_WaitAck(); REG_data= I2C_RadeByte(); I2C_NoAck(); I2C_Stop(); //return TRUE; return REG_data; } /* ******************************************************************************** ** ???????? ?? RCC_Configuration(void) ** ???????? ?? ?±???????? ** ?? ?? ?? ?? ** ?? ?? ?? ?? ** ·? ?? ?? ?? ******************************************************************************** */ void RCC_Configuration(void) { /* RCC system reset(for debug purpose) */ RCC_DeInit(); /* Enable HSE */ RCC_HSEConfig(RCC_HSE_ON); /* Wait till HSE is ready */ HSEStartUpStatus = RCC_WaitForHSEStartUp(); if(HSEStartUpStatus == SUCCESS) { /* HCLK = SYSCLK */ RCC_HCLKConfig(RCC_SYSCLK_Div1); /* PCLK2 = HCLK */ RCC_PCLK2Config(RCC_HCLK_Div1); /* PCLK1 = HCLK/2 */ RCC_PCLK1Config(RCC_HCLK_Div2); /* Flash 2 wait state */ FLASH_SetLatency(FLASH_Latency_2); //?è?????? /* Enable Prefetch Buffer */ FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); /* PLLCLK = 8MHz * 9 = 72 MHz */ RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); /* Enable PLL */ RCC_PLLCmd(ENABLE); /* Wait till PLL is ready */ while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) { } /* Select PLL as system clock source */ RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); /* Wait till PLL is used as system clock source */ while(RCC_GetSYSCLKSource() != 0x08) { } } /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG and AFIO clocks */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB , ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD , ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE | RCC_APB2Periph_GPIOF , ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG | RCC_APB2Periph_AFIO , ENABLE); } /* ******************************************************************************** ** ???????? ?? GPIO_Configuration(void) ** ???????? ?? ?????????? ** ?? ?? ?? ?? ** ?? ?? ?? ?? ** ·? ?? ?? ?? ******************************************************************************** */ void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD, ENABLE ); /* Configure USART1 Tx (PA.09) as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; // ????????9 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // ???????ì???? GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // ×???????????50MHz GPIO_Init(GPIOA, &GPIO_InitStructure); // ????A???? /* Configure USART1 Rx (PA.10) as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //????????10 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //???????? GPIO_Init(GPIOA, &GPIO_InitStructure); //????A???? } /* ******************************************************************************** ** ???????? ?? USART1_Configuration(void) ** ???????? ?? ?®??1?????? ** ?? ?? ?? ?? ** ?? ?? ?? ?? ** ·? ?? ?? ?? ******************************************************************************** */ void USART1_Configuration(void) { USART_InitTypeDef USART_InitStructure; USART_ClockInitTypeDef USART_ClockInitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 |RCC_APB2Periph_USART1, ENABLE ); USART_ClockInitStructure.USART_Clock = USART_Clock_Disable; // ?±???????????? USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low; // ?±???????? USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge; // ?±????????±??????????????? USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable; // ×??ó???????????±??????????SCLK???? /* Configure the USART1 synchronous paramters */ USART_ClockInit(USART1, &USART_ClockInitStructure); // ?±?????????????è?? USART_InitStructure.USART_BaudRate = 115200; // ?¨????????115200 USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 8?????? USART_InitStructure.USART_StopBits = USART_StopBits_1; // ?????á??????1???????? USART_InitStructure.USART_Parity = USART_Parity_No ; // ?????§?? USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // ?????÷?????§?? USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // ·???????+???????? /* Configure USART1 basic and asynchronous paramters */ USART_Init(USART1, &USART_InitStructure); /* Enable USART1 */ USART_ClearFlag(USART1, USART_IT_RXNE); //???????????????????????ó?????ú?ú???? USART_ITConfig(USART1,USART_IT_RXNE, ENABLE); //????USART1?????? USART_Cmd(USART1, ENABLE); //USART1×??????????? } /* ******************************************************************************** ** ???????? ?? NVIC_Configuration(void) ** ???????? ?? ?????????? ** ?? ?? ?? ?? ** ?? ?? ?? ?? ** ·? ?? ?? ?? ******************************************************************************** */ void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); // NVIC_InitStructure.NVIC_IRQChannel = WWDG_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_Init(&NVIC_InitStructure); } /* ******************************************************************************** ** ???????? ?? WWDG_Configuration(void) ** ???????? ?? ?????·?????? ** ?? ?? ?? ?? ** ?? ?? ?? ?? ** ·? ?? ?? ?? ******************************************************************************** */ void WWDG_Configuration(void) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG, ENABLE); WWDG_SetPrescaler(WWDG_Prescaler_8); // WWDG clock counter = (PCLK1/4096)/8 = 244 Hz (~4 ms) WWDG_SetWindowValue(0x41); // Set Window value to 0x41 WWDG_Enable(0x50); // Enable WWDG and set counter value to 0x7F, WWDG timeout = ~4 ms * 64 = 262 ms WWDG_ClearFlag(); // Clear EWI flag WWDG_EnableIT(); // Enable EW interrupt } /* ******************************************************************************** ** ???????? ?? Delay(vu32 nCount) ** ???????? ?? ???±???? ** ?? ?? ?? ?? ** ?? ?? ?? ?? ** ·? ?? ?? ?? ******************************************************************************** */ void Delay(vu32 nCount) { for(; nCount != 0; nCount--); } /* ******************************************************************************** ** ???????? ?? void Delayms(vu32 m) ** ???????? ?? ?¤???±???? m=1,???±1ms ** ?? ?? ?? ?? ** ?? ?? ?? ?? ** ·? ?? ?? ?? ******************************************************************************** */ void Delayms(vu32 m) { u32 i; for(; m != 0; m--) for (i=0; i<50000; i++); } /* ******************************************************************************** ** ???????? ?? WWDG_IRQHandler(void) ** ???????? ?? ?°???á?°???????? ** ?? ?? ?? ?? ** ?? ?? ?? ?? ** ·? ?? ?? ?? ******************************************************************************** */ void WWDG_IRQHandler(void) { /* Update WWDG counter */ WWDG_SetCounter(0x50); /* Clear EWI flag */ WWDG_ClearFlag(); } //************************************************ void USART1_SendData(uchar SendData) { USART_SendData(USART1, SendData); Delayms(1); } //************??????L3G4200D********************************* void Init_L3G4200D(void) { Single_Write(L3G4200_Addr,CTRL_REG1, 0x0f); Single_Write(L3G4200_Addr,CTRL_REG2, 0x00); Single_Write(L3G4200_Addr,CTRL_REG3, 0x08); Single_Write(L3G4200_Addr,CTRL_REG4, 0x30); //+-2000dps Single_Write(L3G4200_Addr,CTRL_REG5, 0x00); } //******????L3G4200D????**************************************** void READ_L3G4200D(void) { BUF[0]=Single_Read(L3G4200_Addr,OUT_X_L); BUF[1]=Single_Read(L3G4200_Addr,OUT_X_H); T_X= (BUF[1]<<8)|BUF[0]; BUF[2]=Single_Read(L3G4200_Addr,OUT_Y_L); BUF[3]=Single_Read(L3G4200_Addr,OUT_Y_H); T_Y= (BUF[3]<<8)|BUF[2]; BUF[4]=Single_Read(L3G4200_Addr,OUT_Z_L); BUF[5]=Single_Read(L3G4200_Addr,OUT_Z_H); T_Z= (BUF[5]<<8)|BUF[4]; } //********?®??·???????*************************************** void Send_data(uchar axis) {uchar i; USART1_SendData(axis); USART1_SendData(':'); for(i=0;i<4;i++)USART1_SendData(TX_DATA); USART1_SendData(' '); USART1_SendData(' '); } /* ******************************************************************************** ** ???????? ?? main(void) ** ???????? ?? ?÷???? ** ?? ?? ?? ?? ** ?? ?? ?? ?? ** ·? ?? ?? ?? ******************************************************************************** */ int main(void) { RCC_Configuration(); GPIO_Configuration(); USART1_Configuration(); I2C_GPIO_Config(); Delayms(10); Init_L3G4200D(); //??????L3G4200D while(1) { READ_L3G4200D(); //????L3G4200D???? DATA_printf(TX_DATA,T_X);//×???X?á????????×é Send_data('X'); //·???X?á?? DATA_printf(TX_DATA,T_Y);//×???Y?á????????×é Send_data('Y'); //·???Y?á?? DATA_printf(TX_DATA,T_Z);//×???Z?á????????×é Send_data('Z'); //·???Z?á?? USART1_SendData(0X0D); //???? USART1_SendData(0X0A); //???? Delayms(5); } } /*************?á??***************/ [/mw_shl_code]

STM32MCU-CODE.rar

1.46 MB, 下载次数: 116

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

88

主题

7377

帖子

5

精华

资深版主

Rank: 8Rank: 8

积分
14980
金钱
14980
注册时间
2013-11-13
在线时间
1823 小时
发表于 2014-11-22 17:07:59 | 显示全部楼层
你这是要分享你写好的驱动呢,还是说你的驱动有问题?
开往春天的手扶拖拉机
回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165540
金钱
165540
注册时间
2010-12-1
在线时间
2117 小时
发表于 2014-11-23 00:28:31 | 显示全部楼层
帮顶....
回复

使用道具 举报

1

主题

2

帖子

0

精华

新手入门

积分
26
金钱
26
注册时间
2014-11-22
在线时间
0 小时
 楼主| 发表于 2014-11-24 23:05:49 | 显示全部楼层
回复【2楼】zuozhongkai:
---------------------------------
驱动有问题,串口输出都是0
回复

使用道具 举报

11

主题

39

帖子

0

精华

初级会员

Rank: 2

积分
107
金钱
107
注册时间
2014-8-22
在线时间
0 小时
发表于 2015-5-19 08:56:43 | 显示全部楼层
我的也是,全是零。请问这个问题你解决了吗?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-6-22 05:31

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表