IIC协议这里就不多说了;
使用模拟IIC口 定义B14 B15
#define AnalonSDA_Pin GPIO_PIN_12
#define AnalonSDA_GPIO_Port GPIOB
#define AnalonSCL_Pin GPIO_PIN_13
#define AnalonSCL_GPIO_Port GPIOB
IO配置为输出
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, AnalonSDA_Pin|AnalonSCL_Pin|BIG_SDA_Pin|BIG_SCL_Pin, GPIO_PIN_RESET);
/*Configure GPIO pins : PBPin PBPin */
GPIO_InitStruct.Pin = AnalonSDA_Pin|AnalonSCL_Pin|BIG_SDA_Pin|BIG_SCL_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
驱动程序如下
void TM1637_start(void)
{
SCLK_H;
Delay_us(1);
DIO_H;
Delay_us(1);
DIO_L;
Delay_us(1);
SCLK_L;
Delay_us(1);
}
void TM1637_stop(void)
{
SCLK_L;
Delay_us(1);
DIO_L;
Delay_us(1);
SCLK_H;
Delay_us(1);
DIO_H;
Delay_us(1);
}
void TM1637_ack(void)
{
SCLK_L;
DIO_H;
while(HAL_GPIO_ReadPin(GPIOB, BIG_SDA_Pin));
SCLK_H;
DIO_IN;
}
void TM1637_Write(unsigned char DATA) //写数据函数
{
unsigned char i;
for(i=0;i<8;i++)
{
SCLK_L;
if(DATA & 0x01)DIO_H; //SDA
else DIO_L;
DATA >>= 1;
SCLK_H;
}
TM1637_ack();
}
unsigned char READ_KEY(void)
{
unsigned char i,temp=0;
TM1637_start();
TM1637_Write(READ_COMMAND);
DIO_IN;
for(i=0;i<8;i++)
{
SCLK_L;
Delay_us(1);
temp<<=1;
SCLK_H;
if(HAL_GPIO_ReadPin(GPIOB, BIG_SDA_Pin))temp+=1;
Delay_us(1);
}
TM1637_ack();
TM1637_stop();
return temp;
}
void display_fix(unsigned char add,unsigned char DispData) //指定地址写入数据
{
TM1637_start(); //start signal sent to TM1637 from MCU
TM1637_Write(ADD_FIX);//
TM1637_stop(); //
TM1637_start(); //
TM1637_Write(ADDR_COMMAND|add);//
TM1637_Write(DispData);//
TM1637_stop(); //
}
void display_char(unsigned char add,unsigned char dis_char)
{
unsigned char point_flag,char_code;
point_flag=dis_char&0x80;
dis_char&=0x7f;
if(dis_char<16)
char_code=tab[dis_char];
else switch(dis_char)
{
case 'L':char_code=0x38;break;
case 'H':char_code=0x76;break;
case 'U':char_code=0x3e;break;
case 'N':char_code=0x37;break;
case 'P':char_code=0x73;break;
case 'J':char_code=0x0e;break;
case 'O':char_code=0x3f;break;
case 'o':char_code=0x5c;break;
case 'n':char_code=0x54;break;
case 'Y':char_code=0x6e;break;
case ' ':char_code=0;break;
default:char_code=dis_char;break;
}
display_fix(add,char_code|point_flag);
}
void init_TM1637(void)
{
unsigned char i;
TM1637_start();
TM1637_Write(ADD_AUTO);
TM1637_stop();
TM1637_start();
TM1637_Write(ADDR_COMMAND);
for(i=0;i<6;i++)
TM1637_Write(0);
TM1637_stop();
TM1637_start();
TM1637_Write(BRIGHT);
TM1637_stop();
}
void Delay_us(unsigned int i)
{
for(;i>0;i--);
}
最后在任务中调用
void StartDefaultTask(void const * argument)
{
unsigned char i,j;
init_TM1637();
for(;;)
{
for(i=0;i<16;i++)
{
for(j=0;j<4;j++)
{
display_char(j,((j+i)%16)|0x80);
}
HAL_Delay(500);
}
HAL_Delay(1);
}
}
上一篇:冷启动与热启动在STM32基于RTOS编程中需要注意的地方
下一篇:STM32启动文件的理解
推荐阅读最新更新时间:2024-03-16 16:10