#include"stm32f10x_lib.h"
#include<stdio.h>
u8 dat[6];
I2C_InitTypeDef I2C_InitStructure;
#define BitBand(Addr,BitNum) *((volatile u32 *)((Addr&0XF0000000)+0X2000000+(((Addr&0XFFFFF))<<5)+(BitNum<<2)))
#define PBout(n) BitBand(GPIOB_BASE+0X0C,n)
#define PBin(m) BitBand(GPIOB_BASE+0X08,m)
//***************************************************************************延时*************************************
void delay(u16 x)
{
u16 k,l;
for(k=x;k>0;k--)
for(l=1200;l>0;l--);
}
//***************************************************************************时钟
void RCC_SET()
{
//FUCTION:RCC
//RCC INIT
RCC_DeInit();
//开启外部晶振
RCC_HSEConfig(RCC_HSE_ON);
//检测外部晶振是否开启
while(RCC_WaitForHSEStartUp()!=SUCCESS);
//设置AHB分频器
RCC_HCLKConfig(RCC_SYSCLK_Div1);
//设置APB1
RCC_PCLK1Config(RCC_HCLK_Div2);
//设置APB2
RCC_PCLK2Config(RCC_HCLK_Div1);
//设置PLL
RCC_PLLConfig(RCC_PLLSource_HSE_Div2, RCC_PLLMul_9);
//pll使能
RCC_PLLCmd(ENABLE);
//等待PLL工作
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==SET);
//设置系统时钟
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
//判断PLL是否为系统时钟
while(RCC_GetSYSCLKSource() != 0X08);
//开启GPIOB时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_USART1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2 , ENABLE);
//************************************************************************************************************************
}
//*********************************************************************************usart1 设置****************************
void usart1_config()
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
//*************************************************************************************防守函数***************************
void wait_start()
{
do
{
I2C2 -> CR1 &= 0XFEFF;
I2C_GenerateSTART(I2C2, ENABLE);
}while( !(I2C2 -> SR1 & 0X0001));
}
//************************************************************************************iic 设置****************************
void iic2_config()
{ //init
// I2C_DeInit(I2C2);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x03A2;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 100000;
I2C_Init(I2C2, &I2C_InitStructure);
// I2C2-> CR2 |= 0X0008;
// I2C2->CCR = 0X0FFF;
I2C_AcknowledgeConfig(I2C2, ENABLE);
//DMA
//关闭双地址模式
// I2C_DualAdressCmd(I2C2, DISABLE);
//关闭广播呼叫模式
I2C_GeneralCallCmd(I2C2, DISABLE);
I2C_ITConfig(I2C2, I2C_IT_BUF | I2C_IT_EVT | I2C_IT_ERR, DISABLE);
I2C_TransmitPEC(I2C2, DISABLE);
I2C_ARPCmd(I2C2, DISABLE);
I2C_StretchClockCmd(I2C2, DISABLE);
I2C_Cmd(I2C2, ENABLE);
}
//**********************************************************IIC START*********************************************************
void iic_start(u8 ADD)
{
u16 X;
I2C2->CR1 |= 0X0100;
while(!(I2C2->SR1 & 0X0001));
I2C2->DR = ADD;
while(!(I2C2->SR1 & 0X0002));
X=I2C2->SR2;
}
//*********************************************************检测busy***********************************************************
//*******************************************************************************************单写*****************************
void single_write(u8 add,u8 dat ,u8 dat1 ,u8 dat2)
{
iic_start(0X3C);
I2C2->DR = add;
while(!(I2C2->SR1 & 0X0080));
I2C2->DR = dat;
while(!(I2C2->SR1 & 0X0080));
I2C2->DR = dat1;
while(!(I2C2->SR1 & 0X0080));
I2C2->DR = dat2;
while(!(I2C2->SR1 & 0X0080));
I2C2->CR1 |= 0X0200;
delay(1);
}
//********************************************************************************************连续读***************************
void multiple_read()
{
u8 i;
iic_start(0x3c);
I2C2->DR = 0x03;
while(!(I2C2->SR1 & 0X0080));
iic_start(0x3d);
for(i=0;i<6;i++)
{
while(!(I2C2->SR1 & 0X0040));
if(i==5)
{
I2C2->CR1 &= 0XFBFF;
I2C2->CR1 |= 0X0200;
while(!(I2C2->SR1 & 0X0040));
delay(1);
}
dat[i] = I2C2->DR;
}
}
//**********************************************************************************************初始化 电子罗盘*****************
void init_hmc()
{
single_write(0x00,0x78 ,0xE0 ,0x00 );
}
//*******************************************************************************************************************************
//******************************************************************************************GPIO*********************
void gpio()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitTypeDef GPIO_InitStructur;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructur.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructur.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructur.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructur);
GPIO_InitStructur.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructur.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructur.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructur);
}
//**********************************************************重启hmc*************************************************
//void reset_hmc()
//{
//// PBout(5) = 0;
//// delay(20);
// PBout(5) = 1;
//}
int main(void)
{
u8 j;
// u16 RegisterValue;
RCC_SET();
gpio();
usart1_config();
iic2_config();
while(1)
{
// reset_hmc();
I2C2->CR1 |= 0X0400;;
while(I2C2->SR2 & 0X0002);
init_hmc();
multiple_read();
I2C2->CR1 &= 0XFDFF;
for(j=0;j<6;j++)
{
delay(5);
USART_SendData(USART1,dat[j]);
while(~(USART1->SR|(~0X00000040)));
USART1->SR &= (~0X40);
}
// while(1);
}
}
//******************************************************************************************************************************
|