STM8S TM1650 chip control program

Publisher:EtherealGazeLatest update time:2019-11-08 Source: 51heiKeywords:STM8S Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The following is the .h file:


 

#ifndef __TM1650_I2C_H

#define __TM1650_I2C_H

#include "stm8s.h"

#include "stm8s_gpio.h"

#include "tim1.h"

#include "usart2.h"

#include

#include

 

 

/**********************************

TM1650 chip controls 20 street lights function

PB7 is SCL port

PB6 is the SDA port

**********************************/

 

#define SCL_TM1650              PC_ODR_ODR7

#define SDA_TM1650               PC_ODR_ODR6

#define SDAM_TM1650              PC_IDR_IDR6

#define SET_SCL_OUT_TM1650()    {PC_DDR_DDR7=1; PC_CR1_C17 = 1; PC_CR2_C27 = 0;}

#define SET_SDA_OUT_TM1650()    {PC_DDR_DDR6=1; PC_CR1_C16 = 1; PC_CR2_C26 = 0;}

#define SET_SDA_IN_TM1650()     {PC_DDR_DDR6=0; PC_CR1_C16 = 0; PC_CR2_C26 = 0;}

 

 

void IIC_Init_TM1650(void);

void TDelay_us(u8 z);

void I2C_Start_TM1650(void);

void I2C_Stop_TM1650(void);

void IIC_Ack_TM1650(void);

void IIC_NAck_TM1650(void);

uint8_t IIC_Wait_Ack_TM1650(void);

void IIC_WrByte_TM1650(uint8_t txd);

 

//u8 Scan_Key(void);

void TM1650_Set(u8 add,u8 dat);

void Init_Tm1650(void);

 

#endif

 


The following is the .c file:


#include "TM1650_I2C.h"

 

//--------------------------------------------------------------

// Prototype      : void IIC_Init_TM1650(void)

// Calls          : 

// Description    : 

//--------------------------------------------------------------

void IIC_Init_TM1650(void)

{

   SET_SCL_OUT_TM1650();

   SET_SDA_OUT_TM1650(); 

   SCL_TM1650  = 1;

   SDA_TM1650 = 1;

}

//--------------------------------------------------------------

// Prototype      : void Delay_us(void)

// Description: Approximate delay z us

//--------------------------------------------------------------

void TDelay_us(u8 z)

{

   //u8 i; //fcpu 8MHz

   //for (i=50; i>0; i--);

while(z--)

  {

    nop();nop();nop();nop();

  }

}

//--------------------------------------------------------------

// Prototype      : void I2C_Start(void)

// Calls          : Delay_5us()

// Description    : Start Singnal

//--------------------------------------------------------------

void I2C_Start_TM1650(void)

{

    // SDA 1->0 while SCL High

  //During the high level of SCL, a falling edge appears on SDA to indicate the start signal

  SET_SDA_OUT_TM1650();

    SDA_TM1650 = 1; //The data line is kept high first, and the start signal needs the falling edge of the port 

TDelay_us(4);

    SCL_TM1650 = 1; //The clock line remains high            

    TDelay_us(40); //There is a delay of about 5us, which depends on the device            

    SDA_TM1650 = 0; //The data line is pulled low and a falling edge appears           

    TDelay_us(4); //Delay for a short while to ensure a reliable falling edge            

    SCL_TM1650 = 0; //Pull the clock line low to ensure that the data line is allowed to change next            

}

 

 

//--------------------------------------------------------------

// Prototype      : void I2C_Stop(void)

// Calls          : Delay_5us()

// Description    : Stop Singnal

//-------------------------------------------------------------- 

void I2C_Stop_TM1650(void)

{

    // SDA 0->1 while SCL High

    //During the SCL high level period, SDA generates a rising edge to indicate stop

  SET_SDA_OUT_TM1650();

SCL_TM1650 = 0;

TDelay_us(2);

SDA_TM1650 = 0; // Ensure the data line is low level

TDelay_us(40);

    SCL_TM1650 = 1; //First ensure that the clock line is high

    TDelay_us(10); //Delay to get a reliable level signal            

    SDA_TM1650 = 1; // Rising edge on data line           

    TDelay_us(40); //Delay to ensure a reliable high level           

}

 

 

//Response function

void IIC_Ack_TM1650(void)

{

    //The data line remains at a low level, and a rising edge on the clock line indicates a response

 

SET_SDA_OUT_TM1650();

TDelay_us(10);

    SDA_TM1650 = 0;

    TDelay_us(10);

    SCL_TM1650 = 0;

    TDelay_us(40);

SCL_TM1650 = 1;

TDelay_us(40);

    //After the response is completed, pull the clock line low to allow data modification

    SCL_TM1650 = 0;

}

//Non-response

void IIC_NAck_TM1650(void)

{

    //Non-response is the opposite of response. The difference is that the data line remains at a high level.

SET_SDA_OUT_TM1650();

TDelay_us(10);

    SDA_TM1650 = 1;

    TDelay_us(10);

SCL_TM1650 = 0;

TDelay_us(40);

    SCL_TM1650 = 1;

    TDelay_us(40);

    //Finally, pull the clock line low to allow data to change

    SCL_TM1650 = 0;

}

//Wait for response

uint8_t IIC_Wait_Ack_TM1650(void) //0 means there is a response, 1 means there is no response

{

    //Response waiting count

    uint8_t ackTime = 0;

    //First set the data line to input mode. This program does not reflect this. If there is a response, a falling edge will appear.

SCL_TM1650 = 0;

SET_SDA_OUT_TM1650();

    TDelay_us(10);

SDA_TM1650 = 1;//

TDelay_us(30);

SET_SDA_IN_TM1650(); //Switch to input mode

    //Pull the clock line high

    SCL_TM1650 = 1;

    TDelay_us(30);

    //Wait for the data line to pull low for response

    while(SDAM_TM1650){

        //If it is not pulled low within this time

        ackTime ++;

        if(ackTime > 250)

        {

            // Consider it a non-response stop signal

            I2C_Stop_TM1650();

            return 1;

        }

    }

    SCL_TM1650 = 0;

    return 0 ;

}

 

void IIC_WrByte_TM1650(uint8_t txd)

{

    //Define a counting variable

    uint8_t i;

SET_SDA_OUT_TM1650();

    //Pull the clock line low to allow data to change

//    SCL = 0;

    //Send data bit by bit

    for(i = 0;i < 8; i ++)

    {

  TDelay_us(2);

        if((txd&0x80)>>7) //0x80  1000 0000

SDA_TM1650=1;

else

SDA_TM1650=0;

        txd<<=1;  

TDelay_us(20);   

SCL_TM1650=1;

TDelay_us(20);  

SCL_TM1650=0;

TDelay_us(20); 

    }

}

 

/***************************

u8 Scan_Key(void) //Key scan

{

u8 i;

u8 rekey;

I2C_Start_TM1650();

IIC_WrByte_TM1650(0x49); //Read key command

IIC_Ack_TM1650();

//DIO_H;

SET_SDA_IN_TM1650(); //Switch to input mode

for(i=0;i<8;i++)

{

  SCL_TM1650=1;

  rekey = rekey<<1;

  

  if(SDAM_TM1650)

  {

  rekey++;

  } 

  TDelay_us(5);

  SCL_TM1650=0;

}

IIC_Ack_TM1650();

I2C_Stop_TM1650();

return(rekey);

}

****************************/

 

void TM1650_Set(u8 add,u8 dat) //digital tube display

{

//Writing video memory must start from a high address

I2C_Start_TM1650();

IIC_WrByte_TM1650(add); //The first video memory address

IIC_Ack_TM1650();

IIC_WrByte_TM1650(dat);

IIC_Ack_TM1650();

I2C_Stop_TM1650();

}

void Init_Tm1650(void)

{

IIC_Init_TM1650();

delay_ms(50); //A short delay is required, otherwise the display will be unresponsive

TM1650_Set(0x48,0x31); //Initialize to 5 gray levels, turn on display

}


Note: It may take some time for the TM1650 chip to start up, so you should delay for a short time before sending the display, otherwise it will not be received.

Keywords:STM8S Reference address:STM8S TM1650 chip control program

Previous article:STM8S103K3 I2C
Next article:STM8S MAX7219 dot matrix module driver chip program

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号