STM32软件模拟串口协议控制TM1652

TM1652.c

#include "tm1652.h"
#include "delay.h"
	

void tm1652_init()
{
	GPIO_InitTypeDef  GPIO_InitStructure;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;				 
	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_10);	

}

void tm1652_send_data(u8 sdat)
{
	unsigned char i=0, sfalg=0;
	
	//Æðʼλ
	TX=1;   TX=0;   delay_us(52);    //±£³Ö52us
	
   //·¢ËÍ8λÊý¾Ý
	for(i=0; i<8; i++)
	{
		if(sdat & 0x01)
		{
			TX=1;
			sfalg++;
		}else
		{
			TX=0;
		}
		delay_us(52); 
		sdat >>=1;
	}

	//УÑéλ,°´ÕÕ·¢ËÍÊý¾ÝÖÐ1µÄ¸öÊýÀ´ÅжÏ
	if(sfalg%2==0)			         
	{
		TX=1;
	}
	else			        
	{
		TX=0;
	}
	delay_us(52); 
	
	//ֹͣλ
	TX=1;  delay_us(52);   
}

main.c

		tm1652_send_data(0x08);
		tm1652_send_data(0xff);
		tm1652_send_data(0xff);
		tm1652_send_data(0xff);
		tm1652_send_data(0x00);
		tm1652_send_data(0x00);
		tm1652_send_data(0x00);
		tm1652_send_data(0x18);
		tm1652_send_data(0x4f);
		delay_ms(1000);
原创文章 81 获赞 48 访问量 3万+

猜你喜欢

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