芯片手册请使用半导体小芯进行查询:http://semiee.com/ 半导体小芯
根据任务所需 需要用到TM1620芯片驱动RGB灯
原理图部分: main.c
#include <STC8.H>
#include <stdio.h>
#include <intrins.h>
#include "TM1620.h"
sbit LED1=P2^6;
unsigned char i,j;
void delay_ms(unsigned int t) ;
void main()
{
P0M1 =0x00; P0M0 =0x00;
P1M1 =0x00; P1M0 =0x00;
P2M1 =0x00; P2M0 =0x00;
P3M1 =0x00; P3M0 =0x00;
P4M1 =0x00; P4M0 =0x00;
P5M1 =0x00; P5M0 =0x00;
RSTCFG|=0x10;
for(i=0;i<12;i++)
{
STB=0;
TM1620_WriteByte(0xc0+i);
TM1620_WriteByte(0x00);
STB=1;
}
while(1)
{
#if 0
for(i=1;i<=6;i++)
{
for(j=1;j<=6;j++)
{
TM1620_Dsplay(i,j);
delay_ms(500);
}
}
#endif
#if 1
DIN=1;
CLK=1;
STB=1;
TM1620_WriteByte(0x02);
STB=1;
TM1620_WriteByte(0x44);
STB=1;
for(i=0;i<6;i++)
{
TM1620_WriteByte(0xC0+i*2);
TM1620_WriteByte(0x3f);
STB=1;
}
TM1620_WriteByte(0x8a);
STB=1;
#endif
}
}
void delay_ms(unsigned int t)
{
unsigned char i, j;
while(t--){
_nop_();
i = 32;
j = 40;
do
{
while (--j);
} while (--i);}
}
TM1620.c
#include <STC8.H>
#include <stdio.h>
#include <intrins.h>
#include "TM1620.h"
unsigned long TmData;
void TM1620_WriteByte(unsigned char dat)
{
unsigned char i;
STB=0;
_nop_();_nop_();
for(i=0;i<8;i++)
{
CLK=0;
_nop_();_nop_();
if((dat&0x01)!=0)
{
DIN=1;
}
else
{
DIN=0;
}
_nop_();_nop_();
CLK=1;
dat=dat>>1;
}
_nop_();_nop_();
}
void TM1620_Dsplay(unsigned char ch,unsigned dat)
{
DIN=1;
CLK=1;
STB=1;
TM1620_WriteByte(0x02);
STB=1;
TM1620_WriteByte(0x44);
STB=1;
TM1620_WriteByte(startaddress+(ch-1)*2);
TM1620_WriteByte(1<<(dat-1));
STB=1;
TM1620_WriteByte(disconmode);
STB=1;
}
TM1620.h
#ifndef __TM1620_H__
#define __TM1620_H__
sbit STB =P2^0;
sbit CLK =P4^2;
sbit DIN =P4^1;
#define dissetmode 0x02
#define writedatamode_z 0x40
#define writeledmode_z 0x41
#define readkeymode 0x42
#define readswmode 0x43
#define writedatamode_g 0x44
#define writeledmote_g 0x45
#define startaddress 0xc0
#define disconmode 0x8f
#define uchar unsigned char
void TM1620_WriteByte(unsigned char dat);
void TM1620_Dsplay(unsigned char ch,unsigned dat);
#endif
全亮测试(全亮为白色,设置脉冲宽度为 4/16)
|