应朋友要求,设计了一个游戏机的功夫宝宝的控制台板子,用一个SH88F516驱动两片TM1640的芯片,数码管用LN5461AS,驱动代码如下:
#include "SH88F516.h"
#include <stdio.h> /* prototype declarations for I/O functions */
#define U2DIN P1_5
#define U2SCLK P1_6
#define U1DIN P3_7
#define U1SCLK P3_6
#define TM1640_DELAY 1
//0 1 2 3 4 5 6 7 8 9 A B C D E F
const unsigned char code TM1640_CODE[16]={0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x77, 0X7F, 0x39, 0x3F, 0x79, 0x71};
const unsigned char code TM1640_CODEA[16] _at_ 0xe000;
void tm1640_display(unsigned char *led_data);
void tm1640_delay(unsigned int delay)
{
unsigned int i;
for(i=0;i<delay;i++);
}
void tm1640_start(void)
{
U1DIN = U2DIN = 1;
U1SCLK = U2SCLK = 1; tm1640_delay(TM1640_DELAY);
U1DIN = U2DIN = 0; tm1640_delay(TM1640_DELAY);
U1SCLK = U2SCLK = 0; tm1640_delay(TM1640_DELAY);
}
void tm1640_end(void)
{
U1SCLK = U2SCLK = 0;
U1DIN = U2DIN = 0; tm1640_delay(TM1640_DELAY);
U1SCLK = U2SCLK = 1; tm1640_delay(TM1640_DELAY);
U1DIN = U2DIN = 1; tm1640_delay(TM1640_DELAY);
}
void tm1640_byte(unsigned char data1,unsigned char data2)
{
unsigned int i;
for(i=0;i<8;i++)
{
data1 = data1 >>1; U1DIN =CY;
data2 = data2 >>1;
U2DIN =CY; tm1640_delay(TM1640_DELAY);
U1SCLK = U2SCLK = 1; tm1640_delay(TM1640_DELAY);
U1SCLK = U2SCLK = 0; tm1640_delay(TM1640_DELAY);
}
}
void tm1640_display(unsigned char *led_data)
{
unsigned int i;
P1M0 &= ~(1<<6 ); P1M1 |= 1<<6; //
P1M0 &= ~(1<<5 ); P1M1 |= 1<<5; //
P3M0 &= ~(1<<6 ); P3M1 |= 1<<6; //
P3M0 &= ~(1<<7 ); P3M1 |= 1<<7; //
tm1640_end();
tm1640_start();
tm1640_byte(0x40,0x40); // command1
tm1640_end();
tm1640_start();
tm1640_byte(0xc0,0xc0); // command2
for(i=0;i<16;i++)
{
tm1640_byte(TM1640_CODE[led_data[i]],TM1640_CODE[led_data[16+i]]);
}
tm1640_end();
tm1640_start();
tm1640_byte(0x8b,0x8b); // command3
tm1640_end();
}
离线