STM32驱动74HC165原理图加程序

一、原理图
在这里插入图片描述
二、时序图
在这里插入图片描述

三、驱动源码

void couple_init(void)
{
    
    
	GPIO_InitTypeDef  GPIO_InitStructure;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE);	 //使能PB,PE端口时钟

	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); 
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;				 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; 		 
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		 
	GPIO_Init(GPIOB, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;				 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		 
	GPIO_Init(GPIOB, &GPIO_InitStructure);	
	GPIO_SetBits(GPIOB,GPIO_Pin_6);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;				 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		 
	GPIO_Init(GPIOC, &GPIO_InitStructure);	
	GPIO_SetBits(GPIOC,GPIO_Pin_0);	
}
void couple_delay(u8 i)
{
    
    
	while(i--);
}
u16 read_couple(void)
{
    
    
	u8 i;
	u16 temp = 0;

	couple_ld = 0; //PL拉低,读取按键状态
	couple_ld = 1; //PL拉高,停止按键读取

	if(couple_dat == 1)        //等于1即并行输入D7引脚的按键被按下
			temp |= 0x01;         //最低位置1表示按键被按下,反之则没按键被按下
	for(i = 0; i < 15; i ++)  //因为最高位不用移位即可读取,故循环(16-1)次,依次把次高位移到最高位,第一级74HC165优先读取
	{
    
    
		temp = temp << 1;

		couple_clk = 0; //HC165_CLK = 0 ——> HC165_CLK = 1 产生上升沿,次高位D(n-1)移到高位D(n)
		couple_delay(10);
		couple_clk = 1;

		if(couple_dat == 1)
			temp |= 0x01;
	}

	return temp;
}

四、完整工程链接
完整工程链接

猜你喜欢

转载自blog.csdn.net/qq_15181569/article/details/107919856