#define DIO_PORT GPIOC
#define DIO_PIN GPIO_Pins_9
#define CLK_PORT GPIOC
#define CLK_PIN GPIO_Pins_8
#define STB_PORT GPIOC
#define STB_PIN GPIO_Pins_7
#define DIO_L GPIO_ResetBits(DIO_PORT,DIO_PIN)
#define DIO_H GPIO_SetBits(DIO_PORT,DIO_PIN)
#define CLK_L GPIO_ResetBits(CLK_PORT,CLK_PIN)
#define CLK_H GPIO_SetBits(CLK_PORT,CLK_PIN)
#define STB_L GPIO_ResetBits(STB_PORT,STB_PIN)
#define STB_H GPIO_SetBits(STB_PORT,STB_PIN)
#define DATA_READ GPIO_ReadInputDataBit(DIO_PORT,DIO_PIN)
#define V_MDSP1 0x00
#define V_MDSP2 0x01
#define V_MDSP3 0x02
#define V_MDSP4 0x03
#define V_MDAT1 0x40
#define V_MDAT2 0x42
#define V_MDAT3 0x40
#define V_MDAT4 0x44
#define V_ADDR0 0xC0
#define V_ADDR1 0xC1
#define V_ADDR2 0xC2
#define V_ADDR3 0xC3
#define V_ADDR4 0xC4
#define V_ADDR5 0xC5
#define V_ADDR6 0xC6
#define V_ADDR7 0xC7
#define V_ADDR8 0xC8
#define V_ADDR9 0xC9
#define V_ADDR10 0xCA
#define V_ADDR11 0xCB
#define V_ADDR12 0xCC
#define V_ADDR13 0xCD
#define V_DIS16_01 0x80
#define V_DIS16_02 0x81
#define V_DIS16_03 0x82
#define V_DIS16_10 0x83
#define V_DIS16_11 0x84
#define V_DIS16_12 0x85
#define V_DIS16_13 0x86
#define V_DIS16_14 0x87
#define V_DIS16_OFF 0x80
#define V_DIS16_ON 0x88
#define V_LED_LIGHT (V_DIS16_10|V_DIS16_ON)
#define V_NOP 3
#define DATA_NB 14
void gpio_init(void)
{
GPIO_InitType GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT_OD;
GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_10MHz;
GPIO_InitStructure.GPIO_Pins = DIO_PIN;
GPIO_Init(DIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT_PP;
GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_10MHz;
GPIO_InitStructure.GPIO_Pins = STB_PIN;
GPIO_Init(STB_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT_PP;
GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_10MHz;
GPIO_InitStructure.GPIO_Pins = CLK_PIN;
GPIO_Init(CLK_PORT, &GPIO_InitStructure);
}
void me_delay(unsigned char x)
{
unsigned char i;
for(;x>0;x--) for(i=110;i>0;i--);
}
void write_byte_data(unsigned char data)
{
unsigned char i;
me_delay(V_NOP) ;
for(i=8;i>0;i--)
{
CLK_L ;
if((data & 0x01) == 0)
{
DIO_L ;
}
else
{
DIO_H ;
}
data >>= 1 ;
me_delay(V_NOP) ;
CLK_H ;
me_delay(V_NOP) ;
}
}
unsigned char read_byte_data(void)
{
unsigned char i;
unsigned char ret_value = 0 ;
CLK_L ;
for(i=0;i<8;i++)
{
me_delay(V_NOP) ;
CLK_H ;
ret_value >>= 1 ;
if(DATA_READ)
{
ret_value |= 0x80 ;
}
CLK_L ;
}
return (ret_value);
}
void write_command(unsigned char command)
{
STB_L ;
me_delay(V_NOP) ;
write_byte_data(command);
STB_H ;
}
void display_off(void)
{
STB_L ;
me_delay(V_NOP) ;
write_byte_data(V_DIS16_OFF);
STB_H ;
}
void write_addr_data(unsigned char addr,unsigned char data)
{
STB_L ;
write_byte_data(addr);
write_byte_data(data);
STB_H ;
}
void write_led_dat_increase(unsigned char *p, unsigned char n_bytes, unsigned char brightness)
{
unsigned char i = 0;
write_command(V_MDSP4);
write_command(V_MDAT3) ;
STB_L;
me_delay(V_NOP) ;
write_byte_data(V_ADDR0);
for (i = 0; i < n_bytes; i++)
{
write_byte_data(p[i]);
}
STB_H;
write_command(V_LED_LIGHT) ;
}
void write_led_dat_fixed(unsigned char *p, unsigned char n_bytes, unsigned char brightness)
{
unsigned char i = 0;
write_command(V_MDSP4);
write_command(V_MDAT4) ;
for (i = 0; i < n_bytes; i++)
{
STB_L;
write_addr_data(V_ADDR0+i,*(p+i));
STB_H;
}
write_command(V_LED_LIGHT) ;
}
void write_led_dat(unsigned char *in_dat)
{
unsigned char i ;
unsigned char addr,tmp2 ;
write_command(V_MDSP4) ;
write_command(V_MDAT1) ;
addr = V_ADDR2 ;
for(i=0;i<5;i++)
{
if((*in_dat) & 0x01)
{
tmp2 = 0x01 ;
}
else
{
tmp2 = 0x00 ;
}
write_addr_data(addr,tmp2) ;
addr += 2 ;
(*in_dat) >>= 1 ;
}
write_command(V_LED_LIGHT) ;
}
void read_key(unsigned char *out_dat)
{
unsigned char i ;
STB_L ;
me_delay(V_NOP) ;
write_byte_data(V_MDAT2) ;
me_delay(V_NOP) ;
me_delay(V_NOP) ;
DIO_H ;
for(i=5;i>0;i--)
{
*out_dat = read_byte_data() ;
out_dat ++ ;
}
STB_H ;
}
void tm1668_init(void)
{
unsigned char nop_buff[14]={0};
gpio_init();
write_led_dat_increase(nop_buff,14,V_DIS16_OFF);
}
|