这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 高校专区 » 漓东e学堂 » 【1.24更新】36 嵌入式的学习

共11条 1/2 1 2 跳转至

【1.24更新】36 嵌入式的学习

菜鸟
2015-11-15 21:50:14     打赏

第一次作业:http://forum.eepw.com.cn/thread/277765/1#2

第二次流水灯作业:http://forum.eepw.com.cn/thread/277765/1#3

第三次作业数码管0~99:http://forum.eepw.com.cn/thread/277765/1#7

第四次作业串口输出0~1000:http://forum.eepw.com.cn/thread/277765/1#8

第五次作业定时器控制流水灯:http://forum.eepw.com.cn/thread/277765/1#9

第六次作业开机计数:http://forum.eepw.com.cn/thread/277765/1#10

二选一改变灯的颜色:http://forum.eepw.com.cn/thread/277765/2#11



菜鸟
2015-11-15 22:08:11     打赏
2楼

1、先双击这图


2、在出现的窗口点击next,


3、之后出现的对话框点击打钩后点击next。


4、选择安装路径后。点击next


5、出现这个窗口,填好之后点击next


6、之后等待。。。。。



7、出现这个窗口,点击next


8、出现窗口该窗口忘记截图了,点击finish
9、后出现的窗口点击安装。


10、之后打开在桌面Keil图标,出现窗口点击File下拉选择License Management…后出现以下窗口


11、去打开压缩包,打开和谐器Keil,出现窗口如下


12、把之前出现License Management…的窗口CID里的内容对应复制到和谐器中如下图

13、多次点击和谐器左下角按键后出现的内容复制相应到License Management…的窗口


14、点击ADDLIC后出现如图,就完成了。



菜鸟
2015-11-15 22:14:45     打赏
3楼
/**第二次作业流水灯的学习
******************************************************************************
* @file    GPIO/IOToggle/main.c 
* @author  MCD Application Team
* @version V3.5.0
* @date    08-April-2011
* @brief   Main program body.
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
*
© COPYRIGHT 2011 STMicroelectronics


******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "stm32_eval.h"
GPIO_InitTypeDef GPIO_InitStructure;
void RCC_Configuration(void)
{
RCC_DeInit();

RCC_HSICmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);

RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);

RCC_HSEConfig(RCC_HSE_OFF);
RCC_LSEConfig(RCC_LSE_OFF);
RCC_PLLConfig(RCC_PLLSource_HSI_Div2,RCC_PLLMul_9); //  72HMz
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
RCC_ADCCLKConfig(RCC_PCLK2_Div4);
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while(RCC_GetSYSCLKSource() != 0x08);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_ResetBits(GPIOD,GPIO_Pin_2);
}
void delay_us(u32 n)
{
u8 j;
while(n--)
for(j=0;j<10;j++);
}
void delay_ms(u32 n)
{
while(n--)
delay_us(1000);
}
/**
* @brief  Main program.
* @param  None
* @retval None
*/
int main(void)
{ 
int i; 
unsigned	int a[8]={GPIO_Pin_0,GPIO_Pin_1,GPIO_Pin_2,GPIO_Pin_3,GPIO_Pin_4,GPIO_Pin_5,GPIO_Pin_6,GPIO_Pin_7}; 
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
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;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
while(1)
{
for(i=0;i<=7;i++) 
{ 
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); 
delay_ms(50); 
GPIO_ResetBits(GPIOC,a[i]); 
delay_ms(50); 
GPIO_SetBits(GPIOC,a[i]); 
delay_ms(50); 
} 

}
}

 



院士
2015-11-15 22:31:38     打赏
4楼

楼主 Keil的安装图解在论坛里已经有很多了。

所以希望楼主能分享一些特别的,分享别人没有注意的东西,这样楼主的帖子才会成为精华!



院士
2015-11-15 22:32:52     打赏
5楼

像第二次作业里,

这样的程序示例很多地方都有,IO翻转里的延迟函数是使用了停机阻塞的方式,楼主是否尝试一下,使用MCU硬件定时器去实现——这可就是一个突破了。


菜鸟
2015-11-20 11:09:17     打赏
6楼
第一次作业因为之前在上个学期那个贴发贴过,然后又把它剪切过来的。。。。。

菜鸟
2015-11-20 11:22:09     打赏
7楼

//第三次作业:
#include "stm32f10x.h"
GPIO_InitTypeDef GPIO_InitStructure;
void RCC_Configuration(void);
void GPIO_INIT(void) ;
void Function(void) ;
int main(void)
{
RCC_Configuration();
GPIO_INIT();
Function();
}
void RCC_Configuration(void) //复位所有的RCC外围设备寄存器
{
ErrorStatus HSEStartUpStatus;
RCC_DeInit(); //*将外设寄存器RCC重设为初始值
RCC_HSEConfig(RCC_HSE_ON);
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus==SUCCESS)
{
RCC_HCLKConfig(RCC_SYSCLK_Div1);//*AHB使用系统时钟,负责外部存储器的时钟
RCC_PCLK1Config(RCC_HCLK_Div2);
RCC_PCLK2Config(RCC_HCLK_Div1);//
RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);
RCC_PLLCmd(ENABLE);//*打开PLL:
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);//*设置系统时钟
while(RCC_GetSYSCLKSource() != 0x08);//* 判断PLL是否是系统时钟:
}
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE); //#
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //#管脚号  关闭蜂鸣器
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
void delay_us(u32 n)
{
u8 j;
while(n--)
for(j=0;j<10;j++);
}
void delay_ms(u32 n)
{
while(n--)
delay_us(1000);
}
void GPIO_INIT(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_1|GPIO_Pin_15|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void Number(int a)
{
switch(a)
{
case 0 : GPIO_ResetBits(GPIOB,GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14);break;
case 1 : GPIO_ResetBits(GPIOB,GPIO_Pin_9|GPIO_Pin_12);break;
case 2 : GPIO_ResetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_9|GPIO_Pin_13|GPIO_Pin_14);break;
case 3 : GPIO_ResetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_14);break;
case 4 : GPIO_ResetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12);break;
case 5 : GPIO_ResetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_12|GPIO_Pin_14);break;
case 6 : GPIO_ResetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14);break;
case 7 : GPIO_ResetBits(GPIOB,GPIO_Pin_7|GPIO_Pin_9|GPIO_Pin_12);break;
case 8 : GPIO_ResetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14);break;
case 9 : GPIO_ResetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_14); break;
}
}
void Function(void)
{
int i=0,j=0;
GPIO_SetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14);
delay_ms(50);
while(1)
{
GPIO_SetBits(GPIOB,GPIO_Pin_1);
Number(i);
delay_ms(t);
GPIO_ResetBits(GPIOB,GPIO_Pin_1);
GPIO_SetBits(GPIOB,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_SetBits(GPIOB,GPIO_Pin_15);
Number(j);
delay_ms(t);
GPIO_ResetBits(GPIOB,GPIO_Pin_15);
GPIO_SetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14);
if(!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_8))
{
delay_ms(50);
if(!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_8))
{
i++;
if(i==10)
{
j++;
i=0;
}
if(j==10)
{
j=0;
i=0;
}
}
}

 


菜鸟
2015-11-20 11:31:10     打赏
8楼

//第四次作业
#include "stm32f10x.h"
#include    "stdio.h"
GPIO_InitTypeDef GPIO_InitStructure; // 声明结构体
NVIC_InitTypeDef  NVIC_InitStructure;
USART_InitTypeDef  USART_InitStructure;
USART_ClockInitTypeDef  USART_ClockInitStructure;
#define buff_size  16;
char rx_buff[],rx_buff_count=0;
void RCC_Configuration(void) ;
void GPIO_INIT(void)  ;
void USART_int(long BaudRate);
void USART_SendStr(char *str);
void delay_ms(u32 n)  ;
void delay_us(u32 n);
/****主函数 ****/
int main()
{
RCC_Configuration();
GPIO_INIT();
USART_int(9600);
GPIO_ResetBits(GPIOC,0xffff);//led全亮,提示程序开始
delay_ms(200);
GPIO_SetBits(GPIOC,0xffff);//led全灭
USART_SendStr("USART Led Speed\r\n");//
USART_SendStr("\n>");//
while(1);
}
/****   配置RCC ****/
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus; //  设置错误标志量
RCC_DeInit(); //*将外设寄存器RCC重设为初始值
RCC_HSEConfig(RCC_HSE_ON);//*打开外部高速时钟晶振HSE:
HSEStartUpStatus = RCC_WaitForHSEStartUp(); //*等待外部高速时钟晶振工作:
if(HSEStartUpStatus==SUCCESS)
{
RCC_HCLKConfig(RCC_SYSCLK_Div1);//*AHB使用系统时钟,负责外部存储器的时钟
RCC_PCLK1Config(RCC_HCLK_Div2);//*APB1 负责DA,USB,SPI,I2C,CAN,串口2345,普通TIM
RCC_PCLK2Config(RCC_HCLK_Div1);//*APB2负责AD,I/O,高级TIM,串口1
RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);//*设置PLL输入时钟源72M
RCC_PLLCmd(ENABLE);//*打开PLL:
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);//*等待PLL工作:
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);//*设置系统时钟
while(RCC_GetSYSCLKSource() != 0x08);//* 判断PLL是否是系统时钟:
}
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);
/*蜂鸣器*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_ResetBits(GPIOD,GPIO_Pin_2);
}
/****延时 ****/
void delay_us(u32 n)
{
u8 j;
while(n--)
for(j=0;j<10;j++);
}
void delay_ms(u32 n)
{
while(n--)
delay_us(1000);
}
/**** 初始化GPIO ****/
void GPIO_INIT(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
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;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
/**** USART传输的波特率 ****/
void USART_int(long BaudRate)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);//使能PA和 usart1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 PA9作为US1的TX端,打开复用,负责发送数据
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //RX位于PA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入,PA10作为US1的RX端,负责接收数据
//RX初始化PA10
USART_InitStructure.USART_BaudRate = BaudRate;//波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//USART_WordLength 在一个帧中传输8位数据(字节)
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_Rx | USART_Mode_Tx;//收发模式
USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;//时钟低电平活动,开启发送接受数据功能(usart 使能)
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low; //SCLK输出时钟低电平
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;//时钟第二个边沿开始数据捕获
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;//最后一位数据不从SCLK发出
USART_ClockInit(USART1, &USART_ClockInitStructure);//初始化USART1时钟
USART_Init(USART1, &USART_InitStructure);//初始化USART1
USART_Cmd(USART1, ENABLE);//USART1使能
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//使能接收中断,在接受移位寄存器中有数据时产生
USART_Cmd(USART1, ENABLE);
//配置NVIC ,设置优先级分组,使用NVIC_Init对NVIC进行初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);//选择中断分组4(先占优先级四位,从优先级0)
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //选择中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//抢占优先级1-15
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//响应优先级1-15
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能中断
NVIC_Init(&NVIC_InitStructure); //初始化中断
}
/**** usart发送数据 ****/
void USART_SendStr(char *str) //USART发送数据
{
while((*str)!='\0')
{
USART_SendData(USART1,*str++); //通过外设 USARTx 发送单个数据
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); //检查指定的 USART 标志位设置与否
}
}
/**** 取字符串中的数字 ****/
unsigned int translate(char* pstr)
{
int s = 0;
while(*pstr != '\0') //如果没扫到字符串的最后不停止
{
if(*pstr >= '0' && *pstr <= '9') //如果字符串中存在0-9的数字
{
s = s * 10 + *pstr - '0';
}
pstr++;
}
return s;
}
/****  点亮led****/
void LED(char *S,char LEN) //(字符串,字符串长度)
{
int m,i;
int a[8]={GPIO_Pin_0,GPIO_Pin_1,GPIO_Pin_2,GPIO_Pin_3,GPIO_Pin_4,GPIO_Pin_5,GPIO_Pin_6,GPIO_Pin_7};
m=translate(S);
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);
delay_ms(100);
for(i=0;i<8;i++)
{
GPIO_ResetBits(GPIOC,a[i]); // 点亮LED
delay_ms(m);
GPIO_SetBits(GPIOC,a[i]); //熄灭LED
delay_ms(m);
}
}
void input_ASK()
{
char j;
LED(rx_buff,rx_buff_count);
rx_buff_count=0;
for (j=0;j
{
rx_buff[j]='\0';
}//判断读入信息是否结束,如果结束则准备下一次读入
USART_SendStr("\n>");
}
/****接收中断****/
void USART1_IRQHandler(void)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)  { } //判断接收中断是否打开
if(USART_ReceiveData(USART1)==0x0d)
{
input_ASK();
}
else
{
USART_SendData(USART1,USART_ReceiveData(USART1)); //将数据传回给电脑
rx_buff[rx_buff_count]= USART_ReceiveData(USART1);
rx_buff_count++;
}
USART_ClearFlag(USART1, USART_FLAG_RXNE); //清除中断接受完成标志位
}

 



菜鸟
2015-12-21 11:21:38     打赏
9楼

作业五:定时器控制流水灯

/**
  ******************************************************************************
  * @file    EXTI/EXTI_Config/main.c 
  * @author  MCD Application Team
  * @version V3.5.0
  * @date    08-April-2011
  * @brief   Main program body
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  ******************************************************************************
  */
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "stm32_eval.h"
#include <stdio.h>
/** @addtogroup STM32F10x_StdPeriph_Examples
  * @{
  */
/** @addtogroup EXTI_Config
  * @{
  */
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
EXTI_InitTypeDef   EXTI_InitStructure;
GPIO_InitTypeDef   GPIO_InitStructure;
NVIC_InitTypeDef   NVIC_InitStructure;
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_OCInitTypeDef  TIM_OCInitStructure;
unsigned int flag;
unsigned char led;
void RCC_Configuration(void)
{/*
  RCC_DeInit();
    
  RCC_HSICmd(ENABLE);
  while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);
  
  RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);
  
  RCC_HSEConfig(RCC_HSE_OFF);
  RCC_LSEConfig(RCC_LSE_OFF);
  RCC_PLLConfig(RCC_PLLSource_HSI_Div2,RCC_PLLMul_9); //  72HMz
  RCC_PLLCmd(ENABLE);
  while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
  RCC_ADCCLKConfig(RCC_PCLK2_Div4);
  RCC_PCLK2Config(RCC_HCLK_Div1);
  RCC_PCLK1Config(RCC_HCLK_Div2);
  RCC_HCLKConfig(RCC_SYSCLK_Div1);
  RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
  while(RCC_GetSYSCLKSource() != 0x08);
*/
 SystemInit();
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);
 GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);
 GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOD, &GPIO_InitStructure);
 GPIO_ResetBits(GPIOD,GPIO_Pin_2);
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE);
 GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG
  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;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
 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);
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
}
void USART_int(long BaudRate)
{
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);
       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; 
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    /* PA10 USART1_Rx  */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
  /* USARTx configured as follow:
        - BaudRate = 115200 baud  
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled
  */
  USART_InitStructure.USART_BaudRate = BaudRate;//??????
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;//???????8bit
  USART_InitStructure.USART_StopBits = USART_StopBits_1;//????1
  USART_InitStructure.USART_Parity = USART_Parity_No;//????
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//??????none
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//??????????
 USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;     
    USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;      
    USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;      
    USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
    USART_ClockInit(USART1, &USART_ClockInitStructure);
  USART_Init(USART1, &USART_InitStructure);
  USART_Cmd(USART1, ENABLE);
 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
 USART_Cmd(USART1, ENABLE);
}
void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
  /* Enable the TIM2 global Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}
/* Private functions ---------------------------------------------------------*/
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured, 
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f10x_xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f10x.c file
     */     
       
  /* System Clocks Configuration */
  RCC_Configuration();
NVIC_Configuration();
//USART_int(115200);
// printf("config done...\r\n");
  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 36000;
  TIM_TimeBaseStructure.TIM_Prescaler = 100;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE ); 
  
  /* TIM2 enable counter */
  TIM_Cmd(TIM2, ENABLE);
  while (1){
  //if(flag == 1)
 // {printf("TIM2 interrupt......\r\n");
//flag = 0;
}
//else{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);}
//}
}
void TIM2_IRQHandler(void) //TIM3 ??
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) //?? TIM3 ????????
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update ); //?? TIM3 ??????
//  flag = ~flag; 
// if(flag){
// GPIO_SetBits(GPIOC,GPIO_Pin_1);}
// else {GPIO_ResetBits(GPIOC,GPIO_Pin_1);}
 flag++;
 if(flag == 1)
  {
   GPIO_ResetBits(GPIOC,GPIO_Pin_0);
}
   else if(flag == 2)
  {
   GPIO_SetBits(GPIOC,GPIO_Pin_0);}
   else if(flag == 3)
  {
   GPIO_ResetBits(GPIOC,GPIO_Pin_1);}
   else if(flag == 4)
  {
   GPIO_SetBits(GPIOC,GPIO_Pin_1);}
   else if(flag == 5)
  {
   GPIO_ResetBits(GPIOC,GPIO_Pin_2);}
   else if(flag == 6)
  {
   GPIO_SetBits(GPIOC,GPIO_Pin_2);}
   else if(flag == 7)
  {
   GPIO_ResetBits(GPIOC,GPIO_Pin_3);}
   else if(flag == 8)
  {
   GPIO_SetBits(GPIOC,GPIO_Pin_3);}
   else if(flag == 9)
  {
   GPIO_ResetBits(GPIOC,GPIO_Pin_4);}
   else if(flag == 10)
  {
   GPIO_SetBits(GPIOC,GPIO_Pin_4);}
   else if(flag == 11)
  {
   GPIO_ResetBits(GPIOC,GPIO_Pin_5);}
   else if(flag == 12)
  {
   GPIO_SetBits(GPIOC,GPIO_Pin_5);}
   else if(flag == 13)
  {
   GPIO_ResetBits(GPIOC,GPIO_Pin_6);}
   else if(flag == 14)
  {
   GPIO_SetBits(GPIOC,GPIO_Pin_6);}
   else if(flag == 15)
  {
   GPIO_ResetBits(GPIOC,GPIO_Pin_7);}
   else if(flag == 16)
  {
   GPIO_SetBits(GPIOC,GPIO_Pin_7);
      flag = 0;
}
   
}
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* Infinite loop */
  while (1)
  {
  }
}
#endif
/**
  * @}
  */
/**
  * @}
  */
#ifdef __GNUC__
  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
     set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
 

/**
  * @brief  Retargets the C library printf function to the USART.
  * @param  None
  * @retval None
  */
PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
  USART_SendData(EVAL_COM1, (uint8_t) ch);
  /* Loop until the end of transmission */
  while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)
  {}
  return ch;
}
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* Infinite loop */
  while (1)
  {
  }
}
#endif

 


菜鸟
2016-01-17 12:47:31     打赏
10楼

第六次作业:

view plaincopy to clipboardprint?
#include "stm32f10x.h"        
#include "stm32_eval.h"        
#include "STM32_I2C.h"      
#include <stdio.h>        
      
GPIO_InitTypeDef   GPIO_InitStructure;      
USART_InitTypeDef USART_InitStructure;      
USART_ClockInitTypeDef USART_ClockInitStructure;      
EXTI_InitTypeDef   EXTI_InitStructure;      
NVIC_InitTypeDef   NVIC_InitStructure;      
unsigned char i=0;      
void EXTIkeyS1_Config(void);      
      
/*delay_us*/       
void delay_us(u32 n)      
{      
    u8 j;      
    while(n--)      
    for(j=0;j<10;j++);      
}      
/*delay_ms*/       
      
void  delay_ms(u32 n)      
{      
    while(n--)      
    delay_us(1000);      
}      
      
void RCC_Configuration(void)      
{      
  RCC_DeInit();      
  RCC_HSICmd(ENABLE);      
  while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);      
  RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);      
  RCC_HSEConfig(RCC_HSE_OFF);      
  RCC_LSEConfig(RCC_LSE_OFF);      
  RCC_PLLConfig(RCC_PLLSource_HSI_Div2,RCC_PLLMul_9); //  72HMz      
  RCC_PLLCmd(ENABLE);      
  while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);      
  RCC_ADCCLKConfig(RCC_PCLK2_Div4);      
  RCC_PCLK2Config(RCC_HCLK_Div1);      
  RCC_PCLK1Config(RCC_HCLK_Div2);      
  RCC_HCLKConfig(RCC_SYSCLK_Div1);      
  RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);      
  while(RCC_GetSYSCLKSource() != 0x08)    
    //SystemInit();      
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);      
  GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG      
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);      
  GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG      
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;      
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;      
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;      
  GPIO_Init(GPIOD, &GPIO_InitStructure);      
    GPIO_ResetBits(GPIOD,GPIO_Pin_2);      
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE);      
  GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG      
  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;      
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;      
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;      
  GPIO_Init(GPIOC, &GPIO_InitStructure);      
    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);      
      RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);      
}       
      
void USART_int(long BaudRate)      
{      
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);      
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;      
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;      
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;       
    GPIO_Init(GPIOA, &GPIO_InitStructure);      
    /* PA10 USART1_Rx  */      
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;      
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;      
    GPIO_Init(GPIOA, &GPIO_InitStructure);      
  /* USARTx configured as follow:   
        - BaudRate = 115200 baud     
        - Word Length = 8 Bits   
        - One Stop Bit   
        - No parity   
        - Hardware flow control disabled (RTS and CTS signals)   
        - Receive and transmit enabled   
  */      
  USART_InitStructure.USART_BaudRate = BaudRate;//传输速率      
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长8bit      
  USART_InitStructure.USART_StopBits = USART_StopBits_1;//停止位1     
  USART_InitStructure.USART_Parity = USART_Parity_No;    
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;      
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//接受和发送          
    USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;           
    USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;            
    USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;            
    USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;      
    USART_ClockInit(USART1, &USART_ClockInitStructure);      
  USART_Init(USART1, &USART_InitStructure);      
  USART_Cmd(USART1, ENABLE);      
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);      
  USART_Cmd(USART1, ENABLE);      
      /* Configure four bit for preemption priority */      
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);      
  /* Enable the USART1 Interrupt */      
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //      
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 15;      
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;      
  NVIC_Init(&NVIC_InitStructure);      
}      
      
void Iic1_Init(void)    
{    
        GPIO_InitTypeDef GPIO_InitStructure;    
        I2C_InitTypeDef I2C_InitStructure;    
            
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);    
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);    
        //PB6-I2C2_SCL PB7-I2C2_SDA PB10-I2C2_SCL PB11-I2C2_SDA    
        /* Configure IO connected to IIC*********************/    
        GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_10 | GPIO_Pin_11;    
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;    
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;    
        GPIO_Init(GPIOB, &GPIO_InitStructure);    
            
        I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;    
        I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;//Tlov/Thigh=2    
        I2C_InitStructure.I2C_OwnAddress1 = 0xA0;//作为从机时的地址    
        I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;//使能I2C的应答    
        I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;//应答7位的地址    
        I2C_InitStructure.I2C_ClockSpeed = 400000;//I2C速率为400KHz(快速模式)           
        I2C_Cmd(I2C2, ENABLE);       
        I2C_Init(I2C2, &I2C_InitStructure);    
        I2C_AcknowledgeConfig(I2C2, ENABLE);        
}     
      
void I2C2_WriteByte(unsigned char id,unsigned char write_address,unsigned char byte)      
{      
        while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY));//等待I2C    
        I2C_GenerateSTART(I2C2,ENABLE);//产生起始信号    
        while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));//确定主机模式是否成功(检测EV5)EV5    
        I2C_Send7bitAddress(I2C2,id,I2C_Direction_Transmitter);//发送器件地址(写)    
        while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));//检测主机传输模式是否成功(检测EV6)EV6    
        I2C_SendData(I2C2, write_address);//通过外设I2C发送地址    
        while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));//检测主机发送的字节是否成功(检测EV8)EV8    
        I2C_SendData(I2C2, byte);//通过外设T2C发送数据    
        while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));//检测主机发送的字节是否成功(检测EV8)    
        I2C_GenerateSTOP(I2C2, ENABLE);//产生I2C2传输STOP条件      
        do      
        {                      
           /* Send START condition */      
           I2C_GenerateSTART(I2C2, ENABLE); //产生I2C2传输的START条件     
          /* Read I2C2 SR1 register */      
            /* Send EEPROM address for write */      
            I2C_Send7bitAddress(I2C2, 0xA0, I2C_Direction_Transmitter);      
        }while(!(I2C_ReadRegister(I2C2, I2C_Register_SR1) & 0x0002));  //地址发送结束    
      
        /* Clear AF flag */      
        I2C_ClearFlag(I2C2, I2C_FLAG_AF);//清除I2Cx的应答错误标志位      
        /* STOP condition */          
        I2C_GenerateSTOP(I2C2, ENABLE);//产生I2C2传输START条件          
}      
      
unsigned char I2C2_ReadByte(unsigned char  id, unsigned char read_address)      
{        
     unsigned char temp;               
     while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY))//等待I2C空闲    
             {}      
     I2C_GenerateSTART(I2C2, ENABLE);//写模式    
     while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));      
     I2C_Send7bitAddress(I2C2, id, I2C_Direction_Transmitter);      
     while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));      
     I2C_Cmd(I2C2, ENABLE);      
     I2C_SendData(I2C2, read_address);        
     while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));      
     I2C_GenerateSTART(I2C2, ENABLE);      
     while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));      
     I2C_Send7bitAddress(I2C2, id, I2C_Direction_Receiver);//读模式      
     while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));//等待读取事件,在读取数据      
    I2C_AcknowledgeConfig(I2C2, DISABLE); //关闭应答和停止条件产生     
    I2C_GenerateSTOP(I2C2, ENABLE);      
    while(!(I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_RECEIVED)));      
    temp = I2C_ReceiveData(I2C2);      
    I2C_AcknowledgeConfig(I2C2, ENABLE);      
     return temp;      
}      
      
    
void EXTIkeyS1_Config(void)//S1     
{      
  /* Enable GPIOA clock */      
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);      
  /* Configure PA.00 pin as input floating */      
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;//S1      
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;      
  GPIO_Init(GPIOC, &GPIO_InitStructure);      
  /* Enable AFIO clock */      
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);      
  GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource8);      
  EXTI_InitStructure.EXTI_Line = EXTI_Line8;      
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;      
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;        
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;      
  EXTI_Init(&EXTI_InitStructure);      
  NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;      
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;      
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;      
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;      
  NVIC_Init(&NVIC_InitStructure);      
}      
void EXTI9_5_IRQHandler(void)      
{      
  if(EXTI_GetITStatus(EXTI_Line8) != RESET)      
  {      
        i=0;      
        I2C2_WriteByte(0xA0,0,0);      
        printf(" 按键清%d\r\n",i);      
    /* Clear the  EXTI line 8 pending bit */      
    EXTI_ClearITPendingBit(EXTI_Line8);      
  }      
}      
    
int main(void)      
{      
  RCC_Configuration();      
  EXTIkeyS1_Config();      
  USART_int(115200);      
  Iic1_Init();      
    printf(" config done...\r\n");      
    i = I2C2_ReadByte(0xA0,0);//    
    printf("write data to address:%d\r\n",i);      
    i++;      
    I2C2_WriteByte(0xA0,0,i);//     
    printf("Reading data from address:%d\r\n",i);      
    while(1)      
    {      
     delay_ms(2000);      
     printf(" 上电计数:%d\r\n",i);      
    }      
}      
      
#ifdef  USE_FULL_ASSERT      
      
void assert_failed(uint8_t* file, uint32_t line)      
{       
  while (1)      
  {      
  }      
}      
      
#endif      
      
#ifdef __GNUC__      
      
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)      
#else      
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)      
#endif /* __GNUC__ */      
        
      
PUTCHAR_PROTOTYPE      
{      
      
  USART_SendData(EVAL_COM1, (uint8_t) ch);      
  while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)      
  {}      
      
  return ch;      
}      
      
#ifdef  USE_FULL_ASSERT      
      
void assert_failed(uint8_t* file, uint32_t line)      
{       
  while (1)      
  {      
  }      
}      
      
#endif      
 

 


共11条 1/2 1 2 跳转至

回复

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