|
- /* Includes ------------------------------------------------------------------*/
- #include "stm32f10x.h"
- GPIO_InitTypeDef GPIO_InitStructure;
- void SCCB_IIC_INIT(void);//IIC初始化函數(shù)
- uint8_t SCCB_IIC_READ(uint8_t add);//讀一個(gè)字節(jié)數(shù)據(jù)
- void mysysinit(void);//系統(tǒng)時(shí)鐘初始
- void my_USART_init(void);//初始化
- void my_send_byte(unsigned char send_date); //發(fā)送一個(gè)字節(jié)
- 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時(shí)鐘
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//使能APB1的USART2時(shí)鐘
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//使能APB1的USART2時(shí)鐘
- /* 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 開樓復(fù)用輸出 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); //發(fā)送一個(gè)字節(jié)
- my_send_byte(0x01); //發(fā)送一個(gè)字節(jié)
- my_send_byte(0x02); //發(fā)送一個(gè)字節(jié)
- my_send_byte(0x03); //發(fā)送一個(gè)字節(jié)
-
- delay(5000);
- SCCB_IIC_INIT();//IIC初始化函數(shù)
- SCCB_IIC_WRITE(0x15,0x20);
- delay(10);
- a=SCCB_IIC_READ(0x15);
- my_send_byte(a);
- while(1);
- }
- /**************************************
- SCCB底層操作函數(shù),寫一個(gè)寄存器地址的數(shù)據(jù)
- 無返回值
- 形參:寄存器地址&寄存器數(shù)據(jù)!。
-
- *****************************************/
- void SCCB_IIC_WRITE(uint8_t add,uint8_t date)
- { uint8_t a;
- I2C1->CR1=1;//開啟IIC
- //my_send_byte(0x80);
- I2C1->CR1=0x0101;//發(fā)送起始位
- 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發(fā)送?是向下,否等待
- 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;//寫數(shù)據(jù)
- 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發(fā)送完成,寄存器為空?是向下,否等待
-
-
-
-
- I2C1->CR1=0x0201;//STOP
-
- }
- /******************************
- SCCB底層操作函數(shù)利用硬件模塊IIC讀取一個(gè)指定地址的數(shù)據(jù)
- 返回值:為讀取的數(shù)據(jù)
- 形參:為將要讀取的寄存器地址
- *****************************8*/
- uint8_t SCCB_IIC_READ(uint8_t add)//讀一個(gè)字節(jié)數(shù)據(jù)
- {
- uint8_t date;
- uint16_t a;
- delay(3000);
- I2C1->CR1=1;//開啟IIC
- //my_send_byte(0x80);
- I2C1->CR1=0x0101;//發(fā)送起始位
- 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發(fā)送?是向下,否等待
- 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發(fā)送完成,寄存器為空?是向下,否等待
-
-
-
-
- I2C1->CR1=0x0201;//STOP
- //delay(1000000);
- ///delay(10000);
- delay(10);
- //my_send_byte(0x88);
- I2C1->CR1=0x0101;//發(fā)送起始位
- 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發(fā)送?是向下,否等待
- 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發(fā)送?是向下,否等待
- date=I2C1->DR;
- I2C1->CR1=0x0201;//STOP
- return date;
- }
- void SCCB_IIC_INIT()//IIC初始化函數(shù)
- {
- I2C1->CR1=0;//關(guān)閉I2C
- I2C1->CR2=8;//IIC的時(shí)鐘源設(shè)為8MHZ,但是這不是總線上的時(shí)鐘,他是由CCR1,分頻后作為SCL時(shí)鐘 的,見手冊(cè),
- I2C1->CCR=400;//標(biāo)準(zhǔn)IIC模式,對(duì)IIC時(shí)鐘源的分頻系數(shù)是40,用來產(chǎn)生10KHZ的SCL時(shí)鐘
- I2C1->TRISE=3;//SCL的上升沿時(shí)間寬度為300NS
- I2C1->CR1=1;//開啟IIC;
- }
- void delay(uint32_t a)
- {
- for(a=23980;a;a--);
- }
- /***********************************
- 發(fā)送一個(gè)字節(jié)函數(shù)通過串口
- ************************************/
- void my_send_byte(unsigned char send_date )
- {
- while( (USART1->SR&0x00000080)!=0x80);//發(fā)送寄存器為空
- USART1->DR=send_date;
- }
- /**********************************
- 初始化串口
- **********************************/
- void my_USART_init()
- {
- /*USART2的優(yōu)先級(jí)設(shè)為5*/
- NVIC->IP[37]=5;
- /*開啟38號(hào)中斷即USART2,關(guān)閉其他所有外部的中斷*/
- NVIC->ISER[1]=0x00000020;
- /*設(shè)置復(fù)用模式下的引腳模式為全雙工:TX輸出推挽復(fù)用,RX為輸入上拉模式,速度50MHZ*/
- GPIOA->CRH=0x000008b0;
- /* 1.開啟USART,
- *
- */
- USART1->CR1=0x2000;
- /* 1.關(guān)閉局域網(wǎng)模式
- * 2.1個(gè)停止位
- * 3.CK引腳禁能
- */
- USART1->CR2=0;
- /* 1.關(guān)閉調(diào)制解調(diào)模式
- * 2.關(guān)閉DMA模式
- * 3.關(guān)閉智能卡、紅外模式
- * 4.關(guān)閉錯(cuò)誤中斷
- */
- USART1->CR3=0;
- /* 波特率設(shè)置
- 2011年8月11日
- 王均偉
- 天津第四項(xiàng)目部宿舍
- BRR中的第四位(DIV_Fraction)作為小數(shù),高12位(DIV_MANtissa)作為整數(shù)部分,
-
- 1,根據(jù)公式:波特率=fck/16*usardiv,其中usardivBRR寄存器的值,所以變形得:USARDIV=fck/16*波特率
- 2.算出來BRR寄存器的值后就要把這個(gè)值變成16進(jìn)制數(shù)據(jù)寫入BRR寄存器中,
- 遵循以下規(guī)則:
- 小數(shù)部分*16=DIV_Fraction或者取近似的值
- 整數(shù)部分直接=DIV_MANtissa
- 3.把這個(gè)16進(jìn)制值寫入BRR寄存器
- 例如我要算波特率設(shè)成9600bps的BRR寄存器值,
- 1.先求USARDIV=36000000/16*9600=234.375
- 2.換成十六進(jìn)制:DIV_Fraction=16*0.375=0x6
- DIV_MANtissa=234=0xea
- 3.組合并寫入寄存器
- USART2->BRR=0x0ea6;值得注意的是這里是16位半字操作,所以不要以為是32位。
- */
- USART1->BRR=0x0ea6;
- /* 1.開啟USART
- * 2.開啟接收完畢中斷
- * 3.開啟發(fā)送功能
- * 4.開啟接收功能
- */
- USART1->CR1=0x202c;
-
- }
- void mysysinit()//系統(tǒng)初始化程序
- {
- ErrorStatus HSEStartUpStatus;//說明標(biāo)志位
- RCC_DeInit();//所有外設(shè)全部缺省設(shè)置
- /* 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)//啟動(dòng)成功
- {
- /*這兩條FLASH指令必須加上,不知為啥?不加上就運(yùn)行幾秒后出錯(cuò),參照系統(tǒng)初始化*/
- /* Enable The Prefetch Buffer */
- FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);//FLASH緩存開啟
- /* Configure the Latency cycle: Set 2 Latency cycles */
- FLASH_SetLatency(FLASH_Latency_2); //設(shè)置FLASH這些位表示SYSCLK(系統(tǒng)時(shí)鐘)周期與閃存訪問時(shí)間的比例,為010:兩個(gè)等待狀態(tài),當(dāng) 48MHz < SYSCLK ≤ 72MHz
- /* Set PLL clock output to 72MHz using HSE (8MHz) as entry clock */
- RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);//外部時(shí)鐘為8M,PLL的輸入時(shí)鐘=8MHZ,倍頻系數(shù)9,
- /* Configure HCLK such as HCLK = SYSCLK */
- RCC_HCLKConfig(RCC_SYSCLK_Div1);//設(shè)置了啦AHB分頻器的分頻系數(shù)=1,即HCLK=SYSCLK=72MHZ
- /* Configure PCLK1 such as PCLK1 = HCLK/2 */
- RCC_PCLK1Config(RCC_HCLK_Div2);//設(shè)置了APB1外設(shè)的時(shí)鐘頻率最大是36M這里是APB1的分頻器設(shè)為2,PCLK1=HCLK/2=72/2=36MHZ正好是最大值
- /* Configure PCLK2 such as PCLK2 = HCLK */
- RCC_PCLK2Config(RCC_HCLK_Div1);//設(shè)置PLCK2=HCLK=72MHZ,的APB2分頻器=1
- /* Select the PLL as system clock source */
- RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);//設(shè)置了SYSCLK的提供者為PLL,頻率由上面算出=72MHZ
- /* disable PLL Ready interrupt */
- RCC_ITConfig(RCC_IT_PLLRDY, DISABLE);//PLL中斷關(guān)閉
- /* disable PLL Ready interrupt */
- RCC_ITConfig(RCC_IT_HSERDY,DISABLE);//HSE中斷關(guān)閉
- /* disable PLL Ready interrupt */
- RCC_ITConfig(RCC_IT_HSIRDY, DISABLE); //HSI中斷關(guān)閉
- /* disable PLL Ready interrupt */
- RCC_ITConfig(RCC_IT_LSERDY, DISABLE); //LSE中斷關(guān)閉
- /* disable PLL Ready interrupt */
- RCC_ITConfig(RCC_IT_LSIRDY, DISABLE); //LSI中斷關(guān)閉
- /* PLL clock divided by 1.5 used as USB clock source */
- RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);//設(shè)置USB的時(shí)鐘為=72、1.5=48mhz
- /* Configure ADCCLK such as ADCCLK = PCLK2/2 */
- RCC_ADCCLKConfig(RCC_PCLK2_Div2);//設(shè)置ADC時(shí)鐘=PCLK2/2= 36MHZ
- /* disable the LSE */
- RCC_LSEConfig(RCC_LSE_OFF);//外部低速晶振關(guān)閉
- /*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 */
- }
-
-
- }
復(fù)制代碼
|
|