/*----------------------------------------------------------------------------
File Name :TM1637.C
New Date :2017-8-17
Author :WangXinJia
Hardware :PW-A-V10
Comments :
------------------------------------------------------------------------------*/
/* Includes ------------------------------------------------------------------*/
//#include "HardWare.h"
#include "stm32f10x.h"
#include "Basic.H"
#include "TM1637.H"
#include "API.H"
#include "OP_Display.H"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define SCL_GPIO GPIOB
#define SCL_CLK RCC_APB2Periph_GPIOB
#define SCL_PIN GPIO_Pin_8
#define SDA_GPIO GPIOB
#define SDA_CLK RCC_APB2Periph_GPIOB
#define SDA_PIN GPIO_Pin_9
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
//-------------------------------------
void TM1637Init (void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(SCL_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin = SCL_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(SCL_GPIO, &GPIO_InitStructure);
// GPIO_ResetBits(SCL_GPIO, SCL_PIN);
GPIO_SetBits(SCL_GPIO, SCL_PIN);
RCC_APB2PeriphClockCmd(SDA_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin = SDA_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(SDA_GPIO, &GPIO_InitStructure);
GPIO_SetBits(SDA_GPIO, SDA_PIN);
GPIO_SetBits(SCL_GPIO, SCL_PIN);
}
//SDA DIO PB9
#define SDA_IN() {GPIOB->CRH&=0xFFFFFF0F;GPIOB->CRH|=(8<<4);}//8/4
#define SDA_OUT() {GPIOB->CRH&=0xFFFFFF0F;GPIOB->CRH|=(3<<4);}
void TM1637_Start(void)
{
SDA_OUT();
GPIO_SetBits(SCL_GPIO, SCL_PIN);
GPIO_SetBits(SDA_GPIO, SDA_PIN);
Delay32(20);
GPIO_ResetBits(SDA_GPIO, SDA_PIN);
}
void TM1637_Ack(void)
{
INT16U i=0;
GPIO_ResetBits(SCL_GPIO, SCL_PIN);
SDA_IN();
Delay32(50);//
// GPIO_ResetBits(SCL_GPIO, SCL_PIN);
Delay32(50);
while(GPIO_ReadInputDataBit(SDA_GPIO, SDA_PIN)){
i++;
if(i>300){
break;
}
};
GPIO_SetBits(SCL_GPIO, SCL_PIN);
Delay32(20);
GPIO_ResetBits(SCL_GPIO, SCL_PIN);
}
void TM1637_Stop(void)
{
SDA_OUT();
GPIO_ResetBits(SCL_GPIO, SCL_PIN);
Delay32(20);
GPIO_ResetBits(SDA_GPIO, SDA_PIN);
Delay32(20);
GPIO_SetBits(SCL_GPIO, SCL_PIN);
Delay32(20);
GPIO_SetBits(SDA_GPIO, SDA_PIN);
// GPIO_SetBits(SDA_GPIO, SDA_PIN);
}
void TM1637_WriteByte(unsigned char oneByte)
{
unsigned char i;
SDA_OUT();
for(i=0;i<8;i++){
GPIO_ResetBits(SCL_GPIO, SCL_PIN);
if(oneByte&0x01){
GPIO_SetBits(SDA_GPIO, SDA_PIN);
}else{
GPIO_ResetBits(SDA_GPIO, SDA_PIN);
}
Delay32(30);
oneByte=oneByte>>1;
GPIO_SetBits(SCL_GPIO, SCL_PIN);
Delay32(30);
}
}
INT8U TM1637_ScanKey(void)
{
unsigned char reKey,i;
TM1637_Start();
TM1637_WriteByte(0x42);
TM1637_Ack();
GPIO_SetBits(SDA_GPIO, SDA_PIN);
SDA_IN();
for(i=0;i<8;i++){
GPIO_ResetBits(SCL_GPIO, SCL_PIN);
reKey=reKey>>1;
Delay32(30);
GPIO_SetBits(SCL_GPIO, SCL_PIN);
if(GPIO_ReadInputDataBit(SDA_GPIO, SDA_PIN)){
reKey=reKey|0x80;
}else{
reKey=reKey|0x00;
}
Delay32(30);
}
TM1637_Ack();
TM1637_Stop();
switch (reKey){
case 255:
return(0x00);
case 247:
return(0x01);
case 246:
return(0x02);
case 245:
return(0x04);
case 239:
return(0x08);
case 238:
return(0x10);
case 237:
return(0x20);
default:
return(0x00);
}
}
void TM1637_SegDisplay(void)
{
unsigned char i;
TM1637_Start();
TM1637_WriteByte(0x40);//0x40µØÖ·¼Ó1ģʽ
TM1637_Ack();
TM1637_Stop();
TM1637_Start();
TM1637_WriteByte(0xC0);//ÉèÊ×µØÖ·
TM1637_Ack();
for(i=0;i<6;i++){
TM1637_WriteByte(SegData[i]);
TM1637_Ack();
}
TM1637_Stop();
TM1637_Start();
TM1637_WriteByte(0x8F);//×î´óÁÁ¶È0x88~0x8F
TM1637_Ack();
TM1637_Stop();
}
/******** (C) COPYRIGHT 2017 LongChang Electronics CO.,ltd *****END OF FILE****/