- /* Includes ------------------------------------------------------------------*/
- #include "stm32f10x.h"
- GPIO_InitTypeDef GPIO_InitStructure;
- void SCCB_IIC_INIT(void);//IIC初始化函数
- uint8_t SCCB_IIC_READ(uint8_t add);//读一个字节数据
- void mysysinit(void);//系统时钟初始
- void my_USART_init(void);//初始化
- void my_send_byte(unsigned char send_date); //发送一个字节
- void delay(uint32_t a);
- void SCCB_IIC_WRITE(uint8_t add,uint8_t date);
- int main(void)
- {
- uint8_t a;
- mysysinit();
- /* GPIOD Periph clock enable */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//使能APB2的GPIO_A时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//使能APB1的USART2时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//使能APB1的USART2时钟
- /* Enable BKP and PWR clocks */
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
- /* Configure PD0 and PD2 in output pushpull mode */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- /* Configure PB10 and PB11 in 开楼复用输出 mode */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- my_USART_init();//初始化
-
- my_send_byte(0x00); //发送一个字节
- my_send_byte(0x01); //发送一个字节
- my_send_byte(0x02); //发送一个字节
- my_send_byte(0x03); //发送一个字节
-
- delay(5000);
- SCCB_IIC_INIT();//IIC初始化函数
- SCCB_IIC_WRITE(0x15,0x20);
- delay(10);
- a=SCCB_IIC_READ(0x15);
- my_send_byte(a);
- while(1);
- }
- /**************************************
- SCCB底层操作函数,写一个寄存器地址的数据
- 无返回值
- 形参:寄存器地址&寄存器数据!!!
-
- *****************************************/
- void SCCB_IIC_WRITE(uint8_t add,uint8_t date)
- { uint8_t a;
- I2C1->CR1=1;//开启IIC
- //my_send_byte(0x80);
- I2C1->CR1=0x0101;//发送起始位
- while(1)
- { a=I2C1->SR1;
- a=(a&0x0001);
- delay(1);
- //my_send_byte(0x11);
- if(a==0x0001)
- break;
-
-
- }//EV5是向下
- I2C1->DR=0x42;//写入地址
- delay(10);
- while(1)
- { a=I2C1->SR1;
- a=(a&0x0002);
- delay(1);
- // my_send_byte(0x22);
- if(a==0x0002)
- break;
-
-
- }//EV6ADDR发送?是向下,否等待
- a=I2C1->SR1;
- a=I2C1->SR2;
- //my_send_byte((I2C1->SR1));
- //my_send_byte((I2C1->SR2)); //必须读SR1
-
- I2C1->DR=add;//写入寄存地址
- while(1)
- { a=I2C1->SR1;
- a=(a&0x0080);
- delay(1);
- //my_send_byte(0x33);
- if(a==0x0080)
- break;
-
-
- }//EV8寄存器为空?是向下,否等待
- a=I2C1->SR1;
- a=I2C1->SR2;
- //my_send_byte((I2C1->SR1));
- //my_send_byte((I2C1->SR2)); //必须读SR1
-
- I2C1->DR=date;//写数据
- while(1)
- { a=I2C1->SR1;
- a=(a&0x0080);
- delay(1);
- // my_send_byte(0x44);
- if(a==0x0080)
- break;
-
-
- }//EV8寄存器为空?是向下,否等待
- delay(1);
- while(1)
- {a=I2C1->SR1;
- a=(a&0x0084);
- delay(1);
- // my_send_byte(0x55);
- if(a==0x0084)
- break;
-
-
- }//EV8-2发送完成,寄存器为空?是向下,否等待
-
-
-
-
- I2C1->CR1=0x0201;//STOP
-
- }
- /******************************
- SCCB底层操作函数利用硬件模块IIC读取一个指定地址的数据
- 返回值:为读取的数据
- 形参:为将要读取的寄存器地址
- *****************************8*/
- uint8_t SCCB_IIC_READ(uint8_t add)//读一个字节数据
- {
- uint8_t date;
- uint16_t a;
- delay(3000);
- I2C1->CR1=1;//开启IIC
- //my_send_byte(0x80);
- I2C1->CR1=0x0101;//发送起始位
- while(1)
- { a=I2C1->SR1;
- a=(a&0x0001);
- delay(2);
- // my_send_byte(0x11);
- if(a==0x0001)
- break;
-
-
- }//EV5是向下
- I2C1->DR=0x42;//写入地址
-
- delay(10);
- while(1)
- { a=I2C1->SR1;
- a=(a&0x0002);
- delay(1);
- // my_send_byte(0x22);
- if(a==0x0002)
- break;
-
-
- }//EV6ADDR发送?是向下,否等待
- a=I2C1->SR1;
- a=I2C1->SR2;
- //my_send_byte((I2C1->SR1));
- //my_send_byte((I2C1->SR2)); //必须读SR1
-
- I2C1->DR=add;//写入寄存地址
- while(1)
- { a=I2C1->SR1;
- a=(a&0x0080);
- delay(1);
- // my_send_byte(0x33);
- if(a==0x0080)
- break;
-
-
- }//EV8寄存器为空?是向下,否等待
-
- delay(1);
- while(1)
- {a=I2C1->SR1;
- a=(a&0x0084);
- delay(1);
- // my_send_byte(0x44);
- if(a==0x0084)
- break;
-
-
- }//EV8-2发送完成,寄存器为空?是向下,否等待
-
-
-
-
- I2C1->CR1=0x0201;//STOP
- //delay(1000000);
- ///delay(10000);
- delay(10);
- //my_send_byte(0x88);
- I2C1->CR1=0x0101;//发送起始位
- while(1)
- { a=I2C1->SR1;
- a=(a&0x0001);
- delay(2);
- //my_send_byte(0x55);
- if(a==0x0001)
- break;
-
-
- }//EV5是向下
- I2C1->DR=0x43;//写入地址
-
- delay(1);
- while(1)
- { a=I2C1->SR1;
- a=(a&0x0002);
- delay(1);
- // my_send_byte(0x66);
- if(a==0x0002)
- break;
-
-
- }//EV6ADDR发送?是向下,否等待
- a=I2C1->SR1;
- a=I2C1->SR2;
- delay(1);
- // my_send_byte(I2C1->SR1);
- // my_send_byte(I2C1->SR2);
-
- while(1)
- { a=I2C1->SR1;
- a=(a&0x0040);
- delay(1);
- //my_send_byte(0x77);
- if(a==0x0040)
- break;
-
-
- }//EV7ARXEN发送?是向下,否等待
- date=I2C1->DR;
- I2C1->CR1=0x0201;//STOP
- return date;
- }
- void SCCB_IIC_INIT()//IIC初始化函数
- {
- I2C1->CR1=0;//关闭I2C
- I2C1->CR2=8;//IIC的时钟源设为8MHZ,但是这不是总线上的时钟,他是由CCR1,分频后作为SCL时钟 的,见手册,
- I2C1->CCR=400;//标准IIC模式,对IIC时钟源的分频系数是40,用来产生10KHZ的SCL时钟
- I2C1->TRISE=3;//SCL的上升沿时间宽度为300NS
- I2C1->CR1=1;//开启IIC;
- }
- void delay(uint32_t a)
- {
- for(a=23980;a;a--);
- }
- /***********************************
- 发送一个字节函数通过串口
- ************************************/
- void my_send_byte(unsigned char send_date )
- {
- while( (USART1->SR&0x00000080)!=0x80);//发送寄存器为空
- USART1->DR=send_date;
- }
- /**********************************
- 初始化串口
- **********************************/
- void my_USART_init()
- {
- /*USART2的优先级设为5*/
- NVIC->IP[37]=5;
- /*开启38号中断即USART2,关闭其他所有外部的中断*/
- NVIC->ISER[1]=0x00000020;
- /*设置复用模式下的引脚模式为全双工:TX输出推挽复用,RX为输入上拉模式,速度50MHZ*/
- GPIOA->CRH=0x000008b0;
- /* 1.开启USART,
- *
- */
- USART1->CR1=0x2000;
- /* 1.关闭局域网模式
- * 2.1个停止位
- * 3.CK引脚禁能
- */
- USART1->CR2=0;
- /* 1.关闭调制解调模式
- * 2.关闭DMA模式
- * 3.关闭智能卡、红外模式
- * 4.关闭错误中断
- */
- USART1->CR3=0;
- /* 波特率设置
- 2011年8月11日
- 王均伟
- 天津第四项目部宿舍
- BRR中的第四位(DIV_Fraction)作为小数,高12位(DIV_MANtissa)作为整数部分,
-
- 1,根据公式:波特率=fck/16*usardiv,其中usardivBRR寄存器的值,所以变形得:USARDIV=fck/16*波特率
- 2.算出来BRR寄存器的值后就要把这个值变成16进制数据写入BRR寄存器中,
- 遵循以下规则:
- 小数部分*16=DIV_Fraction或者取近似的值
- 整数部分直接=DIV_MANtissa
- 3.把这个16进制值写入BRR寄存器
- 例如我要算波特率设成9600bps的BRR寄存器值,
- 1.先求USARDIV=36000000/16*9600=234.375
- 2.换成十六进制:DIV_Fraction=16*0.375=0x6
- DIV_MANtissa=234=0xea
- 3.组合并写入寄存器
- USART2->BRR=0x0ea6;值得注意的是这里是16位半字操作,所以不要以为是32位。
- */
- USART1->BRR=0x0ea6;
- /* 1.开启USART
- * 2.开启接收完毕中断
- * 3.开启发送功能
- * 4.开启接收功能
- */
- USART1->CR1=0x202c;
-
- }
- void mysysinit()//系统初始化程序
- {
- ErrorStatus HSEStartUpStatus;//说明标志位
- RCC_DeInit();//所有外设全部缺省设置
- /* Enable HSE */
- RCC_HSEConfig(RCC_HSE_ON);
- /* Wait till HSE is ready and if Time out is reached exit */
- HSEStartUpStatus = RCC_WaitForHSEStartUp();
- if(HSEStartUpStatus == SUCCESS)//启动成功
- {
- /*这两条FLASH指令必须加上,不知为啥?不加上就运行几秒后出错,参照系统初始化*/
- /* Enable The Prefetch Buffer */
- FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);//FLASH缓存开启
- /* Configure the Latency cycle: Set 2 Latency cycles */
- FLASH_SetLatency(FLASH_Latency_2); //设置FLASH这些位表示SYSCLK(系统时钟)周期与闪存访问时间的比例,为010:两个等待状态,当 48MHz < SYSCLK ≤ 72MHz
- /* Set PLL clock output to 72MHz using HSE (8MHz) as entry clock */
- RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);//外部时钟为8M,PLL的输入时钟=8MHZ,倍频系数9,
- /* Configure HCLK such as HCLK = SYSCLK */
- RCC_HCLKConfig(RCC_SYSCLK_Div1);//设置了啦AHB分频器的分频系数=1,即HCLK=SYSCLK=72MHZ
- /* Configure PCLK1 such as PCLK1 = HCLK/2 */
- RCC_PCLK1Config(RCC_HCLK_Div2);//设置了APB1外设的时钟频率最大是36M这里是APB1的分频器设为2,PCLK1=HCLK/2=72/2=36MHZ正好是最大值
- /* Configure PCLK2 such as PCLK2 = HCLK */
- RCC_PCLK2Config(RCC_HCLK_Div1);//设置PLCK2=HCLK=72MHZ,的APB2分频器=1
- /* Select the PLL as system clock source */
- RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);//设置了SYSCLK的提供者为PLL,频率由上面算出=72MHZ
- /* disable PLL Ready interrupt */
- RCC_ITConfig(RCC_IT_PLLRDY, DISABLE);//PLL中断关闭
- /* disable PLL Ready interrupt */
- RCC_ITConfig(RCC_IT_HSERDY,DISABLE);//HSE中断关闭
- /* disable PLL Ready interrupt */
- RCC_ITConfig(RCC_IT_HSIRDY, DISABLE); //HSI中断关闭
- /* disable PLL Ready interrupt */
- RCC_ITConfig(RCC_IT_LSERDY, DISABLE); //LSE中断关闭
- /* disable PLL Ready interrupt */
- RCC_ITConfig(RCC_IT_LSIRDY, DISABLE); //LSI中断关闭
- /* PLL clock divided by 1.5 used as USB clock source */
- RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);//设置USB的时钟为=72、1.5=48mhz
- /* Configure ADCCLK such as ADCCLK = PCLK2/2 */
- RCC_ADCCLKConfig(RCC_PCLK2_Div2);//设置ADC时钟=PCLK2/2= 36MHZ
- /* disable the LSE */
- RCC_LSEConfig(RCC_LSE_OFF);//外部低速晶振关闭
- /*DISable the RTC clock */
- RCC_RTCCLKCmd(DISABLE);
- /* DISable the Clock Security System */
- RCC_ClockSecuritySystemCmd(DISABLE);
- /* Enable the PLL */
- RCC_PLLCmd(ENABLE);//使能PLL
-
-
-
- /* PLL ans system clock config */
- }
- else
- {
- /* Add here some code to deal with this error */
- }
-
-
- }
复制代码
|