基于stm32的智能照明设备

芯片:stm32f103zet6
开发工具:MDK5
功能:这里写图片描述

介绍:此作品用到了ILI9325,HCSR04,DS18B20等外设模块。

体会:第一次用stm32做作品,难点莫过于各个寄存器之间的协调,GPIO的配置及各个引脚时钟的控制。刚开始自己写完一个模块的代码后,机器不工作,找不出什么原因,经过一行一行和例程对比,才发现是没有开启时钟这么令人无奈的错误。

使用的开发板

下面是一些模块的代码:
主函数:

int main(void)
{
	
	LED_Init();					//LED初始化
	TFTLCD_Init();			//LCD初始化
	FRONT_COLOR=RED; 	//设置颜色为红色
	LCD_ShowFontHZ(20, 100,"file name"); //显示图片
	LCD_ShowString(30,250,tftlcd_data.width,tftlcd_data.height,16,"-designed by traceur yuan");	
	FRONT_COLOR=BRED;
	LCD_Draw_Circle(40,165,25);
	LCD_Draw_Circle(80,165,25);
	LCD_Draw_Circle(120,165,25);
	LCD_Draw_Circle(160,165,25);
	LCD_Draw_Circle(200,165,25);
//	while(1);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
 	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));   //设置GPIO端口C引脚0~7为高电平

	GPIO_Configuration();// 配置GPIO
	USART1_Config();	 //配置串口
   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //配置中断
   LED_Init(); //LED初始化
	 Hcsr04Init(); //超声波初始化
	 BEEP_Init(); //蜂鸣器初始化
	
  unsigned int  w = 0, b = 0;
	TIM3_CH1_PWM_Init(500,72-1);
	//GPIO_ResetBits(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));
  while (1)
      {	
		   	
			if ( Ds18b20ReadTemp2() < 30 )	
			{
				if (Hcsr04GetLength() <= 10)  w=450;
				if ( (Hcsr04GetLength() > 10) && (Hcsr04GetLength() <= 20) ) 
				{	
				   w=300;
	      }
				if ( (Hcsr04GetLength() > 20) && (Hcsr04GetLength() <= 30) ) 
				{
					 w=200;
				}
				if ( (Hcsr04GetLength() > 30) && (Hcsr04GetLength() <= 40) ) 
				{
					 w=100;
				}
				if ( (Hcsr04GetLength() > 40) && (Hcsr04GetLength() <= 50) ) 
				{
					 w=50;
				}
			  if (Hcsr04GetLength() > 50)  w=0;
			}
			
			else
					
					  w = 0;  
     					
				
		TIM_SetCompare1(TIM3,w);  //输出PWM
	
					
					if ( Ds18b20ReadTemp2() > 30 )
					{
					for ( b = 0 ; b < 80000 ; b++ )	
						{
		          if(b%10==0)
		          {
		              	beep=!beep;
	           	}

	         	delay_us(10);  
	}
						
					}        
								
	    }   
		
}

DS18B20:

//配置GPIOG11
void GPA15_Output(unsigned int d)
{
	GPIO_InitTypeDef GPIO_InitStructure;

	
	GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_11;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOG, &GPIO_InitStructure);

	GPIO_WriteBit(GPIOG, GPIO_Pin_11, (BitAction)d);
}


uint8_t GPA15_Input(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
	GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_11;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOG, &GPIO_InitStructure);

	return GPIO_ReadInputDataBit(GPIOG, GPIO_Pin_11);	
}



// ·初始化DS18b20
unsigned char Ds18b20Init(void)
{
	unsigned char i = 0;

	//DATA = 0;			  
	GPA15_Output(0);
	//delay750us();		
	delay_us(750);
	//DATA = 1;			  
	GPA15_Output(1);

	i = 0;
	while (GPA15_Input())	
	{
		i++;
		if(i>5)
		{
			return 1;
		}
		//delay15us();
		delay_us(15);	
	}
	return 0;
}


void Ds18b20WriteByte(unsigned char dat)
{
	unsigned int j = 0;

	for (j=0; j<8; j++)
	{
		//DATA = 0;	     	  
		GPA15_Output(0);
		//i++;
		delay_us(3);
		//DATA = dat & 0x01;  	
		GPA15_Output(dat & 0x01);
		//delay70us();			   
		delay_us(70);
		//DATA = 1;				
		GPA15_Output(1);
		dat >>= 1;
	}
}


unsigned char Ds18b20ReadByte()
{
	unsigned char byte = 0, bi = 0;
	unsigned int  j = 0;
		
	for (j=8; j>0; j--)
	{
		//DATA = 0;	
		GPA15_Output(0);
		//i++;
		delay_us(3);
		//DATA = 1;	
		GPA15_Output(1);
	//	i++;
		delay_us(6);
		//bi = DATA;	 
		bi = GPA15_Input();

		byte = (byte >> 1) | (bi << 7);	

		delay_us(45);
	}				
	return byte;
}



void Ds18b20ChangTemp(void)
{
	Ds18b20Init();
	//delay1ms();
	delay_ms(1);
	Ds18b20WriteByte(0xcc);	 
	Ds18b20WriteByte(0x44);	  
	delay_ms(750);
}

void Ds18b20ReadTempCom(void)
{	
	Ds18b20Init();
//	delay1ms();
	delay_ms(1);
	Ds18b20WriteByte(0xcc);
	Ds18b20WriteByte(0xbe);	
}


u32 Ds18b20ReadTemp2(void)
{
	unsigned int temp = 0;
	unsigned char tmh, tml;
	double t = 0;

	Ds18b20ChangTemp();			 
	Ds18b20ReadTempCom();		
	tml = Ds18b20ReadByte();	
	tmh = Ds18b20ReadByte();		
	temp = tml | (tmh << 8);

	t = temp * 0.0625;	
	return t;
}
 
 比价麻烦的一个模块就是ILI9325的使用,配置时参考了网上的代码,代码量也很大,这里就不展示了

这个作品,主要是为了熟悉stm32的内部资源,所以很少用一些自己之前没有用过的模块,缺点十分明显,作品不人性化,识别不准确。当然,就连核心的照明设备也是一颗小小的led,接下来几周,我会继续完善这个作品,让它变得更加智能实用。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值