找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3133|回复: 0
打印 上一主题 下一主题
收起左侧

stm32 SCCB读写时序

[复制链接]
跳转到指定楼层
楼主
ID:82781 发表于 2015-6-14 01:26 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "stm32f10x.h"


  3. GPIO_InitTypeDef GPIO_InitStructure;
  4. void SCCB_IIC_INIT(void);//IIC初始化函数
  5. uint8_t SCCB_IIC_READ(uint8_t add);//读一个字节数据
  6. void mysysinit(void);//系统时钟初始
  7. void my_USART_init(void);//初始化
  8. void my_send_byte(unsigned char send_date); //发送一个字节
  9. void delay(uint32_t a);
  10. void SCCB_IIC_WRITE(uint8_t add,uint8_t date);
  11. int main(void)
  12. {   

  13. uint8_t a;
  14.           mysysinit();
  15.   /* GPIOD Periph clock enable */
  16.    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
  17.    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  18.    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//使能APB2的GPIO_A时钟
  19.    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//使能APB1的USART2时钟
  20.    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//使能APB1的USART2时钟
  21.    /* Enable BKP and PWR clocks */
  22.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);

  23.   /* Configure PD0 and PD2 in output pushpull mode */
  24.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
  25.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  26.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  27.   GPIO_Init(GPIOD, &GPIO_InitStructure);

  28.    /* Configure PB10 and PB11 in 开楼复用输出 mode */
  29.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  30.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  31.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
  32.   GPIO_Init(GPIOB, &GPIO_InitStructure);
  33.    my_USART_init();//初始化




  34.    my_send_byte(0x00); //发送一个字节
  35.    my_send_byte(0x01); //发送一个字节
  36.    my_send_byte(0x02); //发送一个字节
  37.    my_send_byte(0x03); //发送一个字节


  38. delay(5000);

  39. SCCB_IIC_INIT();//IIC初始化函数

  40. SCCB_IIC_WRITE(0x15,0x20);
  41. delay(10);
  42. a=SCCB_IIC_READ(0x15);

  43. my_send_byte(a);
  44. while(1);

  45. }

  46. /**************************************


  47. SCCB底层操作函数,写一个寄存器地址的数据
  48.   无返回值
  49.   形参:寄存器地址&寄存器数据!!!



  50. *****************************************/
  51. void SCCB_IIC_WRITE(uint8_t add,uint8_t date)
  52. {  uint8_t a;
  53.    I2C1->CR1=1;//开启IIC

  54. //my_send_byte(0x80);
  55.   I2C1->CR1=0x0101;//发送起始位

  56.    while(1)
  57.   { a=I2C1->SR1;
  58.     a=(a&0x0001);
  59.   delay(1);
  60. //my_send_byte(0x11);
  61.    if(a==0x0001)
  62.    break;
  63.   
  64.       
  65.   }//EV5是向下

  66.   I2C1->DR=0x42;//写入地址
  67.   delay(10);

  68.   while(1)
  69.   { a=I2C1->SR1;
  70.     a=(a&0x0002);
  71.   delay(1);
  72. // my_send_byte(0x22);
  73.    if(a==0x0002)
  74.    break;
  75.   
  76.       
  77.   }//EV6ADDR发送?是向下,否等待
  78. a=I2C1->SR1;
  79.   a=I2C1->SR2;
  80. //my_send_byte((I2C1->SR1));
  81. //my_send_byte((I2C1->SR2)); //必须读SR1

  82.   I2C1->DR=add;//写入寄存地址

  83.    while(1)
  84.   { a=I2C1->SR1;
  85.     a=(a&0x0080);
  86.   delay(1);
  87. //my_send_byte(0x33);
  88.    if(a==0x0080)
  89.    break;
  90.   
  91.       
  92.   }//EV8寄存器为空?是向下,否等待
  93.   a=I2C1->SR1;
  94.   a=I2C1->SR2;
  95. //my_send_byte((I2C1->SR1));
  96. //my_send_byte((I2C1->SR2)); //必须读SR1

  97.   I2C1->DR=date;//写数据

  98.    while(1)
  99.   { a=I2C1->SR1;
  100.     a=(a&0x0080);
  101.   delay(1);
  102. // my_send_byte(0x44);
  103.    if(a==0x0080)
  104.    break;
  105.   
  106.       
  107.   }//EV8寄存器为空?是向下,否等待

  108.     delay(1);
  109.    while(1)
  110.   {a=I2C1->SR1;
  111.    a=(a&0x0084);
  112.     delay(1);
  113. // my_send_byte(0x55);
  114.    if(a==0x0084)
  115.    break;
  116.   
  117.       
  118.   }//EV8-2发送完成,寄存器为空?是向下,否等待


  119.    
  120.   
  121. I2C1->CR1=0x0201;//STOP



  122. }

  123. /******************************

  124. SCCB底层操作函数利用硬件模块IIC读取一个指定地址的数据

  125. 返回值:为读取的数据
  126. 形参:为将要读取的寄存器地址

  127. *****************************8*/
  128. uint8_t SCCB_IIC_READ(uint8_t add)//读一个字节数据
  129. {
  130. uint8_t date;
  131. uint16_t a;

  132. delay(3000);
  133.   I2C1->CR1=1;//开启IIC

  134. //my_send_byte(0x80);
  135.   I2C1->CR1=0x0101;//发送起始位

  136.    while(1)
  137.   { a=I2C1->SR1;
  138.     a=(a&0x0001);
  139.   delay(2);
  140. // my_send_byte(0x11);
  141.    if(a==0x0001)
  142.    break;
  143.   
  144.       
  145.   }//EV5是向下

  146.   I2C1->DR=0x42;//写入地址


  147.   delay(10);

  148.   while(1)
  149.   { a=I2C1->SR1;
  150.     a=(a&0x0002);
  151.   delay(1);
  152. // my_send_byte(0x22);
  153.    if(a==0x0002)
  154.    break;
  155.   
  156.       
  157.   }//EV6ADDR发送?是向下,否等待

  158.   a=I2C1->SR1;
  159.   a=I2C1->SR2;
  160. //my_send_byte((I2C1->SR1));
  161. //my_send_byte((I2C1->SR2)); //必须读SR1

  162.   I2C1->DR=add;//写入寄存地址

  163.    while(1)
  164.   { a=I2C1->SR1;
  165.     a=(a&0x0080);
  166.   delay(1);
  167. // my_send_byte(0x33);
  168.    if(a==0x0080)
  169.    break;
  170.   
  171.       
  172.   }//EV8寄存器为空?是向下,否等待


  173.   delay(1);
  174.    while(1)
  175.   {a=I2C1->SR1;
  176.    a=(a&0x0084);
  177.     delay(1);
  178. // my_send_byte(0x44);
  179.    if(a==0x0084)
  180.    break;
  181.   
  182.       
  183.   }//EV8-2发送完成,寄存器为空?是向下,否等待


  184.    
  185.   
  186. I2C1->CR1=0x0201;//STOP
  187.   //delay(1000000);
  188. ///delay(10000);
  189.   delay(10);
  190.   //my_send_byte(0x88);

  191. I2C1->CR1=0x0101;//发送起始位

  192.    while(1)
  193.   { a=I2C1->SR1;
  194.     a=(a&0x0001);
  195. delay(2);
  196. //my_send_byte(0x55);
  197.    if(a==0x0001)
  198.    break;
  199.   
  200.       
  201.   }//EV5是向下

  202.   I2C1->DR=0x43;//写入地址


  203.   delay(1);

  204.   while(1)
  205.   { a=I2C1->SR1;
  206.     a=(a&0x0002);
  207.   delay(1);
  208. // my_send_byte(0x66);
  209.    if(a==0x0002)
  210.    break;
  211.   
  212.       
  213.   }//EV6ADDR发送?是向下,否等待

  214.   a=I2C1->SR1;
  215.   a=I2C1->SR2;
  216.    delay(1);
  217.    // my_send_byte(I2C1->SR1);
  218.   // my_send_byte(I2C1->SR2);

  219.    
  220.   while(1)
  221.   { a=I2C1->SR1;
  222.     a=(a&0x0040);
  223.   delay(1);
  224. //my_send_byte(0x77);
  225.    if(a==0x0040)
  226.    break;
  227.   
  228.       
  229.   }//EV7ARXEN发送?是向下,否等待
  230.   date=I2C1->DR;

  231.   I2C1->CR1=0x0201;//STOP

  232.    return date;

  233. }
  234. void SCCB_IIC_INIT()//IIC初始化函数
  235. {

  236.      I2C1->CR1=0;//关闭I2C
  237.   I2C1->CR2=8;//IIC的时钟源设为8MHZ,但是这不是总线上的时钟,他是由CCR1,分频后作为SCL时钟 的,见手册,
  238.      I2C1->CCR=400;//标准IIC模式,对IIC时钟源的分频系数是40,用来产生10KHZ的SCL时钟
  239.   I2C1->TRISE=3;//SCL的上升沿时间宽度为300NS
  240.   I2C1->CR1=1;//开启IIC;

  241. }
  242. void delay(uint32_t a)
  243. {
  244. for(a=23980;a;a--);

  245. }
  246. /***********************************

  247. 发送一个字节函数通过串口

  248. ************************************/
  249. void my_send_byte(unsigned char send_date )
  250. {

  251.   while( (USART1->SR&0x00000080)!=0x80);//发送寄存器为空
  252.    USART1->DR=send_date;


  253. }
  254. /**********************************
  255.           初始化串口

  256. **********************************/
  257. void my_USART_init()
  258. {

  259. /*USART2的优先级设为5*/
  260. NVIC->IP[37]=5;
  261. /*开启38号中断即USART2,关闭其他所有外部的中断*/
  262. NVIC->ISER[1]=0x00000020;   
  263. /*设置复用模式下的引脚模式为全双工:TX输出推挽复用,RX为输入上拉模式,速度50MHZ*/
  264.             GPIOA->CRH=0x000008b0;
  265.   /* 1.开启USART,
  266. *
  267. */
  268. USART1->CR1=0x2000;
  269. /* 1.关闭局域网模式
  270. * 2.1个停止位
  271. * 3.CK引脚禁能
  272. */
  273. USART1->CR2=0;
  274. /* 1.关闭调制解调模式
  275. * 2.关闭DMA模式
  276. * 3.关闭智能卡、红外模式
  277. *   4.关闭错误中断

  278. */
  279. USART1->CR3=0;
  280. /*     波特率设置

  281.      2011年8月11日
  282.         王均伟
  283.          天津第四项目部宿舍

  284.     BRR中的第四位(DIV_Fraction)作为小数,高12位(DIV_MANtissa)作为整数部分,
  285.   
  286.     1,根据公式:波特率=fck/16*usardiv,其中usardivBRR寄存器的值,所以变形得:USARDIV=fck/16*波特率
  287.     2.算出来BRR寄存器的值后就要把这个值变成16进制数据写入BRR寄存器中,
  288.       遵循以下规则:
  289.       小数部分*16=DIV_Fraction或者取近似的值
  290.       整数部分直接=DIV_MANtissa
  291.     3.把这个16进制值写入BRR寄存器
  292.     例如我要算波特率设成9600bps的BRR寄存器值,
  293.     1.先求USARDIV=36000000/16*9600=234.375
  294.     2.换成十六进制:DIV_Fraction=16*0.375=0x6
  295.                     DIV_MANtissa=234=0xea
  296.     3.组合并写入寄存器
  297.                      USART2->BRR=0x0ea6;值得注意的是这里是16位半字操作,所以不要以为是32位。

  298. */
  299. USART1->BRR=0x0ea6;

  300. /* 1.开启USART
  301. * 2.开启接收完毕中断
  302. * 3.开启发送功能
  303. *   4.开启接收功能
  304. */
  305. USART1->CR1=0x202c;


  306. }


  307. void mysysinit()//系统初始化程序
  308. {
  309. ErrorStatus HSEStartUpStatus;//说明标志位
  310. RCC_DeInit();//所有外设全部缺省设置

  311. /* Enable HSE */
  312. RCC_HSEConfig(RCC_HSE_ON);
  313. /* Wait till HSE is ready and if Time out is reached exit */
  314. HSEStartUpStatus = RCC_WaitForHSEStartUp();
  315. if(HSEStartUpStatus == SUCCESS)//启动成功
  316. {
  317. /*这两条FLASH指令必须加上,不知为啥?不加上就运行几秒后出错,参照系统初始化*/
  318. /* Enable The Prefetch Buffer */
  319. FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);//FLASH缓存开启
  320. /* Configure the Latency cycle: Set 2 Latency cycles */
  321.   FLASH_SetLatency(FLASH_Latency_2);  //设置FLASH这些位表示SYSCLK(系统时钟)周期与闪存访问时间的比例,为010:两个等待状态,当 48MHz < SYSCLK ≤ 72MHz
  322. /* Set PLL clock output to 72MHz using HSE (8MHz) as entry clock */
  323. RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);//外部时钟为8M,PLL的输入时钟=8MHZ,倍频系数9,

  324. /* Configure HCLK such as HCLK = SYSCLK */
  325. RCC_HCLKConfig(RCC_SYSCLK_Div1);//设置了啦AHB分频器的分频系数=1,即HCLK=SYSCLK=72MHZ
  326. /* Configure PCLK1 such as PCLK1 = HCLK/2 */
  327. RCC_PCLK1Config(RCC_HCLK_Div2);//设置了APB1外设的时钟频率最大是36M这里是APB1的分频器设为2,PCLK1=HCLK/2=72/2=36MHZ正好是最大值
  328. /* Configure PCLK2 such as PCLK2 = HCLK */
  329. RCC_PCLK2Config(RCC_HCLK_Div1);//设置PLCK2=HCLK=72MHZ,的APB2分频器=1
  330. /* Select the PLL as system clock source */
  331. RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);//设置了SYSCLK的提供者为PLL,频率由上面算出=72MHZ
  332. /* disable PLL Ready interrupt */
  333. RCC_ITConfig(RCC_IT_PLLRDY, DISABLE);//PLL中断关闭
  334. /* disable PLL Ready interrupt */
  335. RCC_ITConfig(RCC_IT_HSERDY,DISABLE);//HSE中断关闭
  336. /* disable PLL Ready interrupt */
  337. RCC_ITConfig(RCC_IT_HSIRDY, DISABLE); //HSI中断关闭
  338. /* disable PLL Ready interrupt */
  339. RCC_ITConfig(RCC_IT_LSERDY, DISABLE); //LSE中断关闭
  340. /* disable PLL Ready interrupt */
  341. RCC_ITConfig(RCC_IT_LSIRDY, DISABLE); //LSI中断关闭

  342. /* PLL clock divided by 1.5 used as USB clock source */
  343. RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);//设置USB的时钟为=72、1.5=48mhz
  344. /* Configure ADCCLK such as ADCCLK = PCLK2/2 */
  345. RCC_ADCCLKConfig(RCC_PCLK2_Div2);//设置ADC时钟=PCLK2/2= 36MHZ
  346. /* disable the LSE */
  347. RCC_LSEConfig(RCC_LSE_OFF);//外部低速晶振关闭

  348. /*DISable the RTC clock */
  349. RCC_RTCCLKCmd(DISABLE);
  350. /* DISable the Clock Security System */
  351. RCC_ClockSecuritySystemCmd(DISABLE);
  352. /* Enable the PLL */
  353. RCC_PLLCmd(ENABLE);//使能PLL







  354. /* PLL ans system clock config */
  355. }
  356. else
  357. {
  358. /* Add here some code to deal with this error */
  359. }





  360. }
复制代码


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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