这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 高校专区 » 漓东e学堂 » IIC上电计数和外部中断清零代码

共3条 1/1 1 跳转至

IIC上电计数和外部中断清零代码

菜鸟
2014-12-11 21:29:48     打赏

  1. #include "stm32f10x.h"    
  2. #include "stm32_eval.h"    
  3. #include     
  4. #define VREF 3.3    
  5. unsigned char i=0;    
  6.   
  7. EXTI_InitTypeDef   EXTI_InitStructure;      
  8. NVIC_InitTypeDef   NVIC_InitStructure;    
  9. GPIO_InitTypeDef   GPIO_InitStructure;    
  10. USART_InitTypeDef USART_InitStructure;    
  11. USART_ClockInitTypeDef USART_ClockInitStructure;    
  12.   
  13.   
  14. void delay_us(u32 n)  
  15. {  
  16.     u8 j;  
  17.     while(n--)  
  18.     for(j=0;j<10;j++);  
  19. }  
  20.   
  21. void  delay_ms(u32 n)  
  22. {  
  23.     while(n--)  
  24.     delay_us(1000);  
  25. }  
  26.   
  27.      
  28.     
  29. void RCC_Configuration(void)    
  30. {    
  31.   RCC_DeInit();    
  32.         
  33.   RCC_HSICmd(ENABLE);    
  34.   while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);    
  35.       
  36.   RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);    
  37.       
  38.   RCC_HSEConfig(RCC_HSE_OFF);    
  39.   RCC_LSEConfig(RCC_LSE_OFF);    
  40.   RCC_PLLConfig(RCC_PLLSource_HSI_Div2,RCC_PLLMul_9); //  72HMz    
  41.   RCC_PLLCmd(ENABLE);    
  42.   while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);    
  43.   RCC_ADCCLKConfig(RCC_PCLK2_Div4);    
  44.   RCC_PCLK2Config(RCC_HCLK_Div1);    
  45.   RCC_PCLK1Config(RCC_HCLK_Div2);    
  46.   RCC_HCLKConfig(RCC_SYSCLK_Div1);    
  47.   RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);    
  48.   while(RCC_GetSYSCLKSource() != 0x08);    
  49.     
  50.         
  51.     //SystemInit();    
  52.         
  53.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);    
  54.  GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);   
  55. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);    
  56.  GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);    
  57.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;    
  58.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;    
  59.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;    
  60.   GPIO_Init(GPIOD, &GPIO_InitStructure);    
  61.     GPIO_ResetBits(GPIOD,GPIO_Pin_2);    
  62.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE);    
  63.  GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);    
  64.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;    
  65.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;    
  66.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;    
  67.   GPIO_Init(GPIOC, &GPIO_InitStructure);    
  68.     GPIO_SetBits(GPIOC,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);    
  69.       RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);    
  70. }     
  71.     
  72. void USART_int(long BaudRate)    
  73. {    
  74.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);    
  75.        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;    
  76.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;    
  77.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;     
  78.     GPIO_Init(GPIOA, &GPIO_InitStructure);    
  79.     /* PA10 USART1_Rx  */    
  80.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;    
  81.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;    
  82.     GPIO_Init(GPIOA, &GPIO_InitStructure);    
  83.   /* USARTx configured as follow:  
  84.         - BaudRate = 115200 baud    
  85.         - Word Length = 8 Bits  
  86.         - One Stop Bit  
  87.         - No parity  
  88.         - Hardware flow control disabled (RTS and CTS signals)  
  89.         - Receive and transmit enabled  
  90.   */    
  91.   USART_InitStructure.USART_BaudRate = BaudRate;  
  92.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;    
  93.   USART_InitStructure.USART_StopBits = USART_StopBits_1;    
  94.   USART_InitStructure.USART_Parity = USART_Parity_No;    
  95.   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;e    
  96.   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;   
  97.     USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;         
  98.     USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;          
  99.     USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;          
  100.     USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;    
  101.     USART_ClockInit(USART1, &USART_ClockInitStructure);    
  102.   USART_Init(USART1, &USART_InitStructure);    
  103.   USART_Cmd(USART1, ENABLE);    
  104.     USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);    
  105.  USART_Cmd(USART1, ENABLE);    
  106. }    
  107. void GPIO_INIT()      
  108. {      
  109.      
  110.   RCC_Configuration();      
  111. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);      
  112.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11;      
  113.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;      
  114.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;      
  115. GPIO_Init(GPIOC, &GPIO_InitStructure);      
  116.      
  117.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);      
  118.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;      
  119.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;      
  120.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;      
  121.   GPIO_Init(GPIOB, &GPIO_InitStructure);      
  122. }      
  123.     
  124. void Iic1_Init(void)    
  125. {    
  126.         GPIO_InitTypeDef GPIO_InitStructure;    
  127.         I2C_InitTypeDef I2C_InitStructure;    
  128.             
  129.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);    
  130.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);    
  131.         //PB6-I2C2_SCL PB7-I2C2_SDA PB10-I2C2_SCL PB11-I2C2_SDA    
  132.         /* Configure IO connected to IIC*********************/    
  133.         GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_10 | GPIO_Pin_11;    
  134.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;    
  135.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;    
  136.         GPIO_Init(GPIOB, &GPIO_InitStructure);    
  137.             
  138.         I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;    
  139.         I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;    
  140.         I2C_InitStructure.I2C_OwnAddress1 = 0xA0;    
  141.         I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;    
  142.         I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;    
  143.         I2C_InitStructure.I2C_ClockSpeed = 400000;           
  144.         I2C_Cmd(I2C2, ENABLE);       
  145.         I2C_Init(I2C2, &I2C_InitStructure);    
  146.         I2C_AcknowledgeConfig(I2C2, ENABLE);        
  147. }    
  148.     
  149.     
  150. /**********************************************************************/    
  151. /*IIC?????                                                                               */    
  152. /*                                                                                                                                          */    
  153. /**********************************************************************/    
  154. void I2C2_WriteByte(unsigned char id,unsigned char write_address,unsigned char byte)    
  155. {    
  156.         while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY));    
  157.         I2C_GenerateSTART(I2C2,ENABLE);    
  158.         while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));  
  159.         I2C_Send7bitAddress(I2C2,id,I2C_Direction_Transmitter);    
  160.         while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));    
  161.         I2C_SendData(I2C2, write_address);    
  162.         while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));    
  163.         I2C_SendData(I2C2, byte);    
  164.         while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));    
  165.         I2C_GenerateSTOP(I2C2, ENABLE);    
  166.     
  167.         do    
  168.         {                    
  169.                 /* Send START condition */    
  170.                 I2C_GenerateSTART(I2C2, ENABLE);    
  171.                 /* Read I2C2 SR1 register */    
  172.                 /* Send EEPROM address for write */    
  173.                 I2C_Send7bitAddress(I2C2, 0xA0, I2C_Direction_Transmitter);    
  174.         }while(!(I2C_ReadRegister(I2C2, I2C_Register_SR1) & 0x0002));   
  175.     
  176.         /* Clear AF flag */    
  177.         I2C_ClearFlag(I2C2, I2C_FLAG_AF);    
  178.         /* STOP condition */        
  179.         I2C_GenerateSTOP(I2C2, ENABLE);      
  180. }    
  181.     
  182.     
  183. unsigned char I2C2_ReadByte(unsigned char  id, unsigned char read_address)    
  184. {      
  185.         unsigned char temp;             
  186.     
  187.         while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY)){}    
  188.           I2C_GenerateSTART(I2C2, ENABLE);    
  189.           while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));    
  190.           I2C_Send7bitAddress(I2C2, id, I2C_Direction_Transmitter);    
  191.           while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));    
  192.           I2C_Cmd(I2C2, ENABLE);    
  193.           I2C_SendData(I2C2, read_address);      
  194.           while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));    
  195.           I2C_GenerateSTART(I2C2, ENABLE);    
  196.           while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));    
  197.           I2C_Send7bitAddress(I2C2, id, I2C_Direction_Receiver);    
  198.           while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));    
  199.     I2C_AcknowledgeConfig(I2C2, DISABLE);    
  200.     I2C_GenerateSTOP(I2C2, ENABLE);    
  201.     while(!(I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_RECEIVED)));    
  202.                   
  203.     temp = I2C_ReceiveData(I2C2);    
  204.        
  205.           I2C_AcknowledgeConfig(I2C2, ENABLE);    
  206.     
  207.         return temp;    
  208. }    
  209.     
  210.   
  211.     
  212.     
  213. void EXTIkeyS1_Config(void)     
  214. {      
  215.   /* Enable GPIOA clock */      
  216.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);      
  217.   /* Configure PA.00 pin as input floating */      
  218.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;     
  219.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;      
  220.   GPIO_Init(GPIOC, &GPIO_InitStructure);      
  221.   /* Enable AFIO clock */      
  222.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);      
  223.   GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource8);      
  224.   EXTI_InitStructure.EXTI_Line = EXTI_Line8;      
  225.   EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;      
  226.   EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;        
  227.   EXTI_InitStructure.EXTI_LineCmd = ENABLE;      
  228.   EXTI_Init(&EXTI_InitStructure);      
  229.   NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;      
  230.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;      
  231.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;      
  232.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;      
  233.   NVIC_Init(&NVIC_InitStructure);      
  234. }      
  235. void EXTI9_5_IRQHandler(void)    
  236. {    
  237.   if(EXTI_GetITStatus(EXTI_Line8) != RESET)    
  238.   {    
  239.         i=0;    
  240.         I2C2_WriteByte(0xA0,0,0);    
  241.        printf("上电清0\r\n",i);    
  242.     /* Clear the  EXTI line 8 pending bit */    
  243.     EXTI_ClearITPendingBit(EXTI_Line8);    
  244.   }    
  245. }    
  246.     
  247.  void IIC_TEST()    
  248. {    
  249.    printf(" config done...\r\n");      
  250.     i = I2C2_ReadByte(0xA0,0);    
  251.     i++;        
  252.     printf("从地址读出数据:%d\r\n",i);      
  253.     I2C2_WriteByte(0xA0,0,i);      
  254.       printf("向地址输入数据:%d\r\n",i);      
  255.     printf("上电次数%d\r\n",i);    
  256.           
  257.       
  258. }     
  259.   
  260. int main(void)      
  261. {      
  262.      /*!< At this stage the microcontroller clock setting is already configured,   
  263.        this is done through SystemInit() function which is called from startup  
  264.        file (startup_stm32f10x_xx.s) before to branch to application main.  
  265.        To reconfigure the default setting of SystemInit() function, refer to  
  266.        system_stm32f10x.c file  
  267.      */         
  268.            
  269.   /* System Clocks Configuration */    
  270.   RCC_Configuration();      
  271.   EXTIkeyS1_Config();    
  272.   GPIO_INIT();      
  273.   USART_int(115200);      
  274.   Iic1_Init();    
  275.   IIC_TEST();    
  276.   
  277.         
  278. }      
  279.     
  280.     
  281. #ifdef  USE_FULL_ASSERT      
  282.       
  283. void assert_failed(uint8_t* file, uint32_t line)      
  284. {       
  285.   while (1)      
  286.   {      
  287.   }      
  288. }      
  289.       
  290. #endif      
  291.       
  292. #ifdef __GNUC__      
  293.       
  294.   #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)      
  295. #else      
  296.   #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)      
  297. #endif /* __GNUC__ */      
  298.         
  299.       
  300. PUTCHAR_PROTOTYPE      
  301. {      
  302.       
  303.   USART_SendData(EVAL_COM1, (uint8_t) ch);      
  304.   while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)      
  305.   {}      
  306.       
  307.   return ch;      
  308. }     

高工
2014-12-12 12:45:31     打赏
2楼
好长的代码啊,哈哈哈把工程打包上传,这样大家看着就舒服了,哈哈哈

高工
2014-12-16 15:28:46     打赏
3楼
真夸张。。。

共3条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]