蓝桥杯嵌入式学习————IIC、RTC、Buzz

注:这里指软件IIC,实时时钟,蜂鸣器用的PB4因此是学习端口复用web

iic

这里使用软件模拟IIC是为了实现EEPROM的功能
这儿挺简单的,直接上代码吧svg

//写在main函数中的,用做记录开机次数
    i2c_init();	
	temp = x24c02_read(0xff);
	Delay_Ms(2);
	x24c02_write(0xff,++temp);
	Delay_Ms(2);


/** * @说明 向AT24C02指定地址读入一个字节数据 * @参数 address:AT24C02内部存储地址 * @返回值 val:读出数据 */
uint8_t x24c02_read(uint8_t address)
{
	unsigned char val;
	
	I2CStart(); 
	I2CSendByte(0xa0);
	I2CWaitAck(); 
	
	I2CSendByte(address);
	I2CWaitAck(); 
	
	I2CStart();
	I2CSendByte(0xa1); 
	I2CWaitAck();
	val = I2CReceiveByte(); 
	I2CWaitAck();
	I2CStop();
	
	return(val);
}


/** * @说明 向AT24C02指定地址写入一个字节数据 * @参数 address:AT24C02内部存储地址 * @参数 info:写入的数据 * @返回值 无 */
void x24c02_write(unsigned char address,unsigned char info)
{
	I2CStart(); 
	I2CSendByte(0xa0); 
	I2CWaitAck(); 
	
	I2CSendByte(address);	
	I2CWaitAck(); 
	I2CSendByte(info); 
	I2CWaitAck(); 
	I2CStop();
}

RTC实时时钟

直接从官方库里面的project中example里面相关工程复制代码过来
注:由于蓝桥平台嵌入式开发板没有加外部低速时钟因此把RCC_LSEConfig(RCC_LSE_ON);改成RCC_LSICmd(ENABLE);, 且其它LSE都改成LSI, 把RTC_SetPrescaler(32767); 内数字改成39999函数

void RTC_Configuration(void)
{
	/* Enable PWR and BKP clocks */
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);

	PWR_BackupAccessCmd(ENABLE);
	BKP_DeInit();
	RCC_LSICmd(ENABLE);
	/* Wait till LSE is ready */
	while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET);
	
	RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
	RCC_RTCCLKCmd(ENABLE);

	RTC_WaitForSynchro();
	RTC_WaitForLastTask();
	/* Enable the RTC Second */
	RTC_ITConfig(RTC_IT_SEC, ENABLE);
	RTC_WaitForLastTask();
	/* Set RTC prescaler: set RTC period to 1sec */
	RTC_SetPrescaler(39999); /* RTC period = RTCCLK/RTC_PR = (40 KHz)/(39999+1) */
	/* Wait until last write operation on RTC registers has finished */
	RTC_WaitForLastTask();
	
//这两行本身加的,用来写初值或改变时间值
	RTC_SetCounter(HH*3600+MM*60+SS);
	RTC_WaitForLastTask();
}

void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
  /* Configure one bit for preemption priority */
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

  /* Enable the RTC Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}


//中断处理函数
void RTC_IRQHandler(void)
{
  if (RTC_GetITStatus(RTC_IT_SEC) != RESET)
  {
    RTC_ClearITPendingBit(RTC_IT_SEC);
    TimeDisplay = 1;         //秒中断标志
    RTC_WaitForLastTask();
    
		if(RTC_GetCounter()==3600*24)  //24点时归零
		{
			
			RTC_SetCounter(0);
			RTC_WaitForLastTask();
		}
  }
}


//main中函数段
  while (1)
  {
		if(TimeDisplay==1)
		{
			TimeDisplay=0;
			Time_Display(RTC_GetCounter());
		}	
  }



//时钟转化和显示
u8 Timetext[20];
void Time_Display(u32 Time)
{
	u32 TH,TM,TS;
	TH=Time/3600;
	TM=(Time%3600)/60;
	TS=(Time%3600)%60;
	
	sprintf(Timetext,"Time: %0.2d:%0.2d:%0.2d ",TH,TM,TS);
	LCD_DisplayStringLine(Line8,Timetext);
}

上面配置NVIC分组,这个有点记不住了,放在这里学习

在这里插入图片描述

蜂鸣器

蜂鸣器很简单,但在蓝桥杯开发板上是PB4,默认为JTAG的功能引脚,因此这里考的是复用和重映射ui

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE);  

GPIO_SetBits(GPIOB,GPIO_Pin_4);
Delay_Ms(200);