• AVR Freaks

Hot!PIC24FV32KA302 with TM1637

Author
BdT141
New Member
  • Total Posts : 2
  • Reward points : 0
  • Joined: 2016/04/09 09:59:24
  • Location: 0
  • Status: offline
2016/04/13 01:23:25 (permalink)
0

PIC24FV32KA302 with TM1637

Hi, I want to use my PIC24FV32KA302 to interface with a TM1637 absed 4 digit 7segment module. I tried modifying the Arduino Library to work on the PIC, but I am not having success. Literally all I changed from the Arduino Library was where it used digitalWrite(CLK, HIGH) I used the PIC equivalent and so on. Any help?
 

#include "mcc_generated_files/mcc.h"
/* Used for delay functions */
#define FOSC (3685000ULL)
#define FCY (FOSC/2)
#include "libpic30.h"
//************definitions for TM1637*********************
#define ADDR_AUTO 0x40
#define ADDR_FIXED 0x44
#define STARTADDR 0xc0
/**** definitions for the clock point of the digit tube *******/
#define POINT_ON 1
#define POINT_OFF 0
/**************definitions for brightness***********************/
#define BRIGHT_DARKEST 0
#define BRIGHT_TYPICAL 2
#define BRIGHTEST 7

static int8_t TubeTab[] = {0x3f,0x06,0x5b,0x4f,
    0x66,0x6d,0x7d,0x07,
    0x7f,0x6f,0x77,0x7c,
    0x39,0x5e,0x79,0x71,0x63,0x40};//0~9,A,b,C,d,E,F,°,-
uint8_t Cmd_SetData;
uint8_t Cmd_SetAddr;
uint8_t Cmd_DispCtrl;
bool _PointFlag;
// Functions
void dispInit(void);
void dispClear(void);
void start(void);
void stop(void);
int writeByte(int8_t wr_data);
void displayArray(int8_t DispData[]);
void displayPos(uint8_t BitAddr, int8_t DispData);
void set(uint8_t brightness,uint8_t SetData,uint8_t SetAddr);
void point(bool PointFlag);
void codingArray(int8_t DispData[]);
int8_t coding(int8_t DispData);
void bitDelay(void);

int main(void)
{
    // initialize the device
    SYSTEM_Initialize();
    dispInit();
    while (1)
    {
        // Display 23°C
        point(true);
        displayPos(0x00,0x02);
        displayPos(0x01,0x03);
        displayPos(0x02,0x10);
        displayPos(0x03,0x11);
    }
    return 0;
}

void dispInit(void)
{
    CLK_SetDigitalOutput();
 DIO_SetDigitalOutput();
    set(BRIGHT_TYPICAL,0x40,0xc0);
 dispClear();
}

int writeByte(int8_t wr_data)
{
 uint8_t i;
    for(i=0;i<8;i++) //sent 8bit data
    {
        CLK_SetLow();
        if(wr_data & 0x01)
  {
            DIO_SetHigh(); //LSB first
  }
        else
  {
            DIO_SetLow();
  }
        wr_data >>= 1;
        CLK_SetHigh();
    }
    CLK_SetLow(); //wait for the ACK
    DIO_SetHigh();
    CLK_SetHigh();
    DIO_SetDigitalInput();
    bitDelay();
    uint8_t ack = DIO_GetValue();
    if (ack == 0)
    {
        DIO_SetDigitalOutput();
        DIO_SetLow();
    }
    bitDelay();
    DIO_SetDigitalOutput();
    bitDelay();
    return ack;
}

//send start signal to TM1637
void start(void)
{
    CLK_SetHigh();//send start signal to TM1637
    DIO_SetHigh();
    DIO_SetLow();
    CLK_SetLow();
}
//End of transmission
void stop(void)
{
    CLK_SetLow();
    DIO_SetLow();
    CLK_SetHigh();
    DIO_SetHigh();
}

//display function.Write to full-screen.
void displayArray(int8_t DispData[])
{
    int8_t SegData[4];
    uint8_t i;
    for(i = 0;i < 4;i ++)
    {
        SegData[i] = DispData[i];
    }
    codingArray(SegData);
    start(); //start signal sent to TM1637 from MCU
    writeByte(ADDR_AUTO);//
    stop(); //
    start(); //
    writeByte(Cmd_SetAddr);//
    for(i=0;i < 4;i ++)
    {
        writeByte(SegData[i]); //
    }
    stop(); //
    start(); //
    writeByte(Cmd_DispCtrl);//
    stop(); //
}

//******************************************
void displayPos(uint8_t BitAddr,int8_t DispData)
{
    int8_t SegData;
    SegData = coding(DispData);
    start(); //start signal sent to TM1637 from MCU
    writeByte(ADDR_FIXED);//
    stop(); //
    start(); //
    writeByte(BitAddr|0xc0);//
    writeByte(SegData);//
    stop(); //
    start(); //
    writeByte(Cmd_DispCtrl);//
    stop(); //
}

void dispClear(void)
{
    displayPos(0x00,0x7f);
    displayPos(0x01,0x7f);
    displayPos(0x02,0x7f);
    displayPos(0x03,0x7f);
}

//To take effect the next time it displays.
void set(uint8_t brightness,uint8_t SetData,uint8_t SetAddr)
{
    Cmd_SetData = SetData;
    Cmd_SetAddr = SetAddr;
    Cmd_DispCtrl = 0x88 + brightness;
    //Set the brightness and it takes effect the next time it displays.
}

//Whether to light the clock point ":".
//To take effect the next time it displays.
void point(bool PointFlag)
{
    _PointFlag = PointFlag;
}

void codingArray(int8_t DispData[])
{
    uint8_t PointData;
    uint8_t i;
    if(_PointFlag == POINT_ON)PointData = 0x80;
    else PointData = 0;
    for(i = 0;i < 4;i ++)
    {
        if(DispData[i] == 0x7f)
        {
            DispData[i] = 0x00;
        }
        else
        {
            DispData[i] = TubeTab[DispData[i]] + PointData;
        }
    }
}

int8_t coding(int8_t DispData)
{
    uint8_t PointData;
    if(_PointFlag == POINT_ON)
    {
        PointData = 0x80;
    }
    else
    {
        PointData = 0;
    }
    if(DispData == 0x7f)
    {
        DispData = 0x00 + PointData;//The bit digital tube off
    }
    else
    {
        DispData = TubeTab[DispData] + PointData;
    }
    return DispData;
}

void bitDelay(void)
{
    __delay_us(50);
}

#1

5 Replies Related Threads

    Aussie Susan
    Super Member
    • Total Posts : 3651
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: PIC24FV32KA302 with TM1637 2016/04/13 19:50:12 (permalink)
    3 (1)
    You will need to tell us what you are expecting to happen that is not? We cannot start to help you with the only probelm description being that you are "not havign success"!
    Am I right in that the TM1637 uses an I2C interface? (The data sheet I could find in a quick search was in Chinese.) If so, then you don't need to bit-bang the interface but you can use the I2C peripheral.
    Susan
    #2
    NKurzman
    A Guy on the Net
    • Total Posts : 18191
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: PIC24FV32KA302 with TM1637 2016/04/13 20:29:08 (permalink)
    0
    http://www.mouser.com/catalog/specsheets/Seeed_104030003.pdf
     
    It is SPI or some clock shift Register .
    It has CLK and DIO
     
    So you show just the Known working code?
       CLK_SetHigh();
        DIO_SetHigh();
        DIO_SetLow();
        CLK_SetLow();

    I assum the ports are set as Digital, and outputs. And you are changing the LATch not the Port.

    #3
    BdT141
    New Member
    • Total Posts : 2
    • Reward points : 0
    • Joined: 2016/04/09 09:59:24
    • Location: 0
    • Status: offline
    Re: PIC24FV32KA302 with TM1637 2016/04/16 02:35:50 (permalink)
    0
    I just want it to display 23°C as an example, in the end I want to hook up a RTC and DS18b20 and toggle between showing the time and the temperature. No, my module isn't I2C, though some are. Yes, my ports are outputs and digital. I use the functions defined after setting the ports using CodeConfigurator so I assume they are using LAT and not PORT.
    #4
    MPicSte
    Starting Member
    • Total Posts : 43
    • Reward points : 0
    • Joined: 2012/11/08 05:17:25
    • Location: 0
    • Status: offline
    Re: PIC24FV32KA302 with TM1637 2016/10/19 05:16:39 (permalink)
    0
    Hello!
     
    Did you get it working? I need to do exactly the same thing, so I was looking around for pointers!
     
    Perhaps I could persuade you to help me out! Thanks, Ste.
    #5
    javiscrip
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2019/04/24 16:25:42
    • Location: 0
    • Status: offline
    Re: PIC24FV32KA302 with TM1637 2020/01/11 16:55:59 (permalink)
    #6
    Jump to:
    © 2020 APG vNext Commercial Version 4.5