TM1642.C
#include "tm1642.h"
//#include <math.h>
#include "stm8s.h"
#include "stm8s_type.h"
#include "stm8s_spi.h"
#include "stm8s_clk.h"
#include "stm8s_gpio.h"
#include <iostm8s103f3.h>
u8 seg_code_cc[10] =
{
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07, 0x7f,0x6f
};
/*0 1 2 3 4 5 6 7 8 9 O E F n r */
//u8 rdBuffer[5] = {0};
u8 wrBuffer[12] = { 0,0,0,0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,0};
/********************************************************
Function: segBufCha() 把需要显示的数转换为LED显示码
*********************************************************/
void segBufCha (u16 Idat)
{
u8 i,j;
u16 temp = Idat;
if ( Idat < 10 )
j = 1;
else if ( Idat < 100 )
j = 2;
else
j = 3;
// j = 1 + (int)log10(dat);
for ( i = 3; i < 11; i++ )
{
wrBuffer = 0;
}
for ( i = 0; i < j; i++ )
{
if (seg_code_cc[ temp % 10+3 ]<0x40)
wrBuffer [ i * 2+3 ] = seg_code_cc[ temp % 10 ];
else
wrBuffer [ i * 2+3 ] = seg_code_cc[ temp % 10 ]-0x40;
wrBuffer [ i * 2 + 4 ] = 0x01;
temp = temp / 10;
}
}
/******************************************************
Function: displayLED() read key-cmd of TM1642
*******************************************************/
void displayLED (void)
{
wrBuffer[0] = BIT7_SEG10;
wrBuffer[1] = DAT_WR | ADD_INC;
wrBuffer[2] = 0xC0;
wrBuffer[11] = CUR_14_16|DIS_ON;
wrBufferData ( wrBuffer, 12 );
}
/**********************************************************************/
void wrBufferData (u8 *idata,u8 len)
{
u8 i;
SPICS = LOW;
nop();
nop();
for ( i = 0; i < len; i++ )
{
SPI_SendData(wrBuffer);
}
SPICS = HIGH;
}
/**************************************************************
Function: tm1642Init() initialize TM1642
***************************************************************/
void tm1642Init (void)
{
SPICS = HIGH;
SPICLK = HIGH;
SPIOUT = HIGH;
}
/**********************************************************************/
/**********************************************************************/
void Delay(u16 nCount)
{
while (nCount != 0)
{
nCount--;
}
}
/**********************************************************************/
void SPI_SendData(unsigned char DataToSend){
while(!SPI_SR_TXE);
SPI_DR = DataToSend;
}
void GPIO_Config(void)
{
GPIO_DeInit(GPIOC);
GPIO_Init(GPIOC, (GPIO_Pin_TypeDef)GPIO_PIN_5|GPIO_PIN_6, GPIO_MODE_OUT_PP_HIGH_SLOW);
GPIO_Init(GPIOC, (GPIO_Pin_TypeDef)GPIO_PIN_7, GPIO_MODE_OUT_PP_LOW_FAST);
}
/**********************************************************************/
void CLK_Config(void)
{
CLK_DeInit();
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV8);
}
/**********************************************************************/
void SPI_Config(void)
{
/* Enable SPI clock */
CLK_PeripheralClockConfig(CLK_PERIPHERAL_SPI, ENABLE);
/* SD_SPI Configuration */
SPI_Init(SPI_FIRSTBIT_MSB, SPI_BAUDRATEPRESCALER_256, SPI_MODE_MASTER,SPI_CLOCKPOLARITY_HIGH,
SPI_CLOCKPHASE_2EDGE, SPI_DATADIRECTION_2LINES_RXONLY, SPI_NSS_SOFT, 0x07);
/* SPI enable */
SPI_Cmd( ENABLE);
}
/****************************************************************************
Function: wrBufferData() write data&cmd to TM1642
*****************************************************************************/
/*
void wrBufferData (u8 *dat,u8 len)
{
u8 i, j, temp;
SPICS = LOW;
nop();
nop();
for ( i = 0; i < len; i++ )
{
temp = dat;
for ( j=0; j<8; j++)
{
SPICLK = LOW;
nop();
nop();
//SPIOUT = bool(temp & 1) ;
if((temp & 0x01)==0x01)
SPIOUT = HIGH;
else SPIOUT = LOW;
temp>>=1;
SPICLK = HIGH;
nop();
}
}
SPICS = LOW;
//if ( KEY_RD != dat[0] ) SPICS = HIGH;
}*******/
|