STC15W204S drive TM1650
Keil 51下STC15平台的TM1650 IIC驱动

MCU: STC15W204S-35I-SOP8 @ 11.0592 Mhz pic

TM1650.c

驱动:

#include "STC15.h"
#include "TM1650.h"
#include <intrins.h>

sbit CLK = P3^3;
sbit DIO = P3^2;

unsigned char code CODE[10] = {0xEB, 0x88, 0xB3, 0xBA, 0xD8, 0x7E, 0x7F, 0xA8, 0xFB, 0xFA};

void TM1650_Init(void)
{
	P3M0 = 0x0C; //P3.2 P3.3 open drain
	P3M1 = 0x0C;
	P5M0 = 0x10; //P5.4 open drain
	P5M0 = 0x10;
}

void Delay_us(unsigned int i) //delay in us
{
	for(;i>0;i--)
	{
		_nop_();
		_nop_();
		_nop_();
		_nop_();
		_nop_();
	}
}

void TM1650_Set(unsigned char add, unsigned char dat)
{
	TM1650_Start();
	TM1650_Write(add);
	TM1650_Ack();
	TM1650_Write(dat);
	TM1650_Ack();
	TM1650_Stop();
}

void TM1650_Start(void)
{
	CLK_H;
	DIO_H;
	Delay_us(5);
	DIO_L;
}

void TM1650_Ack(void)
{
	unsigned char timeout = 1;
	CLK_H;
	Delay_us(5);
	CLK_L;
	while((DIO)&&(timeout<=100))
	{
		timeout++;
	}
	Delay_us(5);
	CLK_L;
}

void TM1650_Stop(void)
{
	CLK_H;
	DIO_L;
	Delay_us(5);
	DIO_H;
}

void TM1650_Write(unsigned char oneByte)
{
	unsigned char i;
	CLK_L;
	Delay_us(1);
	for(i=0;i<8;i++)
	{
		if(oneByte&0x80)
		{
			DIO = 1;
		}
		else
		{
			DIO = 0;
		}
		CLK_L;
		Delay_us(5);
		CLK_H;
		Delay_us(5);
		CLK_L;
		oneByte = oneByte<<1;
	}
}

以上为驱动,在主程序中可直接使用TM1650_Set()来设置数码管的显示内容. pic

TM1650.h

头文件:

#ifndef __TM1650_H_
#define __TM1650_H_

#define CLK_H  CLK = 1
#define CLK_L  CLK = 0

#define DIO_H  DIO = 1
#define DIO_L  DIO = 0

void TM1650_Init(void);
void Delay_us(unsigned int i);
void TM1650_Set(unsigned char add, unsigned char dat);
void TM1650_Start(void);
void TM1650_Ack(void);
void TM1650_Stop(void);
void TM1650_Write(unsigned char oneByte);

#endif

最后修改于 Fri, 29 Nov 2019