CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

TM1640 Multi-Seven segment Display Driver

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
sshahryiar



Joined: 05 May 2010
Posts: 94
Location: Dhaka, Bangladesh

View user's profile Send private message Send e-mail Visit poster's website

TM1640 Multi-Seven segment Display Driver
PostPosted: Wed Jun 11, 2014 8:31 am     Reply with quote

Font.h

Code:

const unsigned char font[95] =
{                       
  0x00, // (32) <space>
  0x86, // (33) !
  0x22, // (34) "     
  0x7E, // (35) # 
  0x6D, // (36) $
  0x00, // (37) %
  0x00, // (38) &   
  0x02, // (39) '
  0x30, // (40) (
  0x06, // (41) )                         
  0x63, // (42) *
  0x00, // (43) +
  0x04, // (44) ,                                                   
  0x40, // (45) -
  0x80, // (46) .                                   
  0x52, // (47) /
  0x3F, // (48) 0
  0x06, // (49) 1
  0x5B, // (50) 2
  0x4F, // (51) 3
  0x66, // (52) 4 
  0x6D, // (53) 5 
  0x7D, // (54) 6 
  0x27, // (55) 7
  0x7F, // (56) 8 
  0x6F, // (57) 9 
  0x00, // (58) :
  0x00, // (59) ; 
  0x00, // (60) < 
  0x48, // (61) =
  0x00, // (62) > 
  0x53, // (63) ?
  0x5E, // (64) @ 
  0x77, // (65) A 
  0x7E, // (66) B 
  0x39, // (67) C 
  0x3E, // (68) D 
  0x79, // (69) E 
  0x71, // (70) F 
  0x3D, // (71) G 
  0x76, // (72) H 
  0x06, // (73) I 
  0x1F, // (74) J
  0x69, // (75) K
  0x38, // (76) L
  0x15, // (77) M
  0x37, // (78) N
  0x3F, // (79) O
  0x73, // (80) P
  0x67, // (81) Q
  0x31, // (82) R 
  0x6D, // (83) S
  0x78, // (84) T
  0x3E, // (85) U
  0x2A, // (86) V
  0x1D, // (87) W
  0x76, // (88) X
  0x6E, // (89) Y
  0x5B, // (90) Z
  0x39, // (91) [
  0x64, // (92) \
  0x0F, // (93) ]
  0x00, // (94) ^
  0x08, // (95) _
  0x20, // (96) `
  0x5F, // (97) a                                                     
  0x7C, // (98) b
  0x58, // (99) c
  0x5E, // (100) d
  0x7B, // (101) e
  0x31, // (102) f
  0x6F, // (103) g
  0x74, // (104) h
  0x04, // (105) i
  0x0E, // (106) j
  0x75, // (107) k
  0x30, // (108) l
  0x55, // (109) m
  0x54, // (110) n
  0x5C, // (111) o
  0x73, // (112) p
  0x67, // (113) q
  0x50, // (114) r
  0x6D, // (115) s
  0x78, // (116) t
  0x1C, // (117) u
  0x2A, // (118) v
  0x1D, // (119) w
  0x76, // (120) x
  0x6E, // (121) y
  0x47, // (122) z       
  0x46, // (123) {
  0x06, // (124) |
  0x70, // (125) }
  0x01  // (126) ~
}; 



TM1640.h

Code:

#include "font.h" 


#define DIN                       pin_A4
#define SCLK                      pin_A5 
                                 
#define no_of_segments            16
                                 
#define auto_address              0x40                                           
#define   fixed_address           0x44     
#define normal_mode               0x40                 
#define test_mode                 0x48 
             
#define start_address             0xC0 
                                                             
#define brightness_5_pc           0x88
#define brightness_10_pc          0x89
#define brightness_25_pc          0x8A                                           
#define brightness_60_pc          0x8B                                               
#define brightness_70_pc          0x8C 
#define brightness_75_pc          0x8D                                                 
#define brightness_80_pc          0x8E   
#define brightness_100_pc         0x8F
#define display_off               0x80
#define display_on                0x8F                                               
                                   

void TM1640_init(unsigned char brightness_level);     
void TM1640_write(unsigned char value);       
void TM1640_send_command(unsigned char value);
void TM1640_send_data(unsigned char address, unsigned char value);
void TM1640_clear_display(); 



TM1640.c

Code:

#include "TM1640.h"
             
                 
void TM1640_init(unsigned char brightness_level)
{               
   output_high(DIN);
   output_high(SCLK);
                          
   TM1640_send_command(auto_address);
   TM1640_send_command(brightness_level);                             
   TM1640_clear_display();                   
}                               
                           
                                                                             
void TM1640_write(unsigned char value) 
{                                                       
   unsigned char s = 0x00; 
                        
   for(s = 0x00; s < 0x08; s += 0x01)                                                                   
   {                                               
      output_low(SCLK);
      
      if((value & 0x01))
      {                                                                                 
          output_high(DIN);
      }                                 
      else                       
      {                     
         output_low(DIN);   
      }                                             
                         
      output_high(SCLK); 
                                 
      value >>= 0x01;
   };         
}                                 


void TM1640_send_command(unsigned char value)   
{                           
   output_low(DIN);
   TM1640_write(value);                 
   output_high(DIN); 
}               


void TM1640_send_data(unsigned char address, unsigned char value)
{                 
    TM1640_send_command(fixed_address);
    output_low(DIN);                           
   TM1640_write((0xC0 | (0x0F & address)));   
   TM1640_write(value);
   output_high(DIN); 



void TM1640_clear_display() 
{
   unsigned char s = 0x00;   
   
   output_low(DIN); 
   TM1640_write(start_address);
   for(s = 0x00; s < no_of_segments; s += 0x01) 
   {          
      TM1640_write(font[0]);         
   };             
   output_high(DIN); 
}
                       



DHT11.h

Code:

#define DHT11_pin PIN_A1


unsigned char values[5];


void DHT11_init();
unsigned char get_byte();
unsigned char get_data();     



DHT11.c

Code:

#include "DHT11.h"


void DHT11_init()
{
   output_float(DHT11_pin);
   delay_ms(1000);
}


unsigned char get_byte()
{
   unsigned char s = 0;                                   
   unsigned char value = 0;
   for(s = 0; s < 8; s += 1)
   {
      value <<= 1;
      while(!input(DHT11_pin));
      delay_us(30);
      if(input(DHT11_pin))
      {
          value |= 1;
      }
      while(input(DHT11_pin));
   }
   return value;
}


unsigned char get_data()
{
   short chk = 0;
   unsigned char s = 0;
   unsigned char check_sum = 0;

   output_high(DHT11_pin);
   output_low(DHT11_pin);
   delay_ms(18);
   output_high(DHT11_pin);
   delay_us(26);
   
   chk = input(DHT11_pin);
   if(chk)
   {
      return 1;
   }
   delay_us(80);
   
   chk = input(DHT11_pin);
   if(!chk)
   {
      return 1;
   }
   delay_us(80);

   for(s = 0; s <= 4; s += 1)
   {
       values[s] = get_byte();
   }
 
   output_high(DHT11_pin);
   
   for(s = 0; s < 4; s += 1)
   {
       check_sum += values[s];
   }
   
   if(check_sum != values[4])
   {
      return 1;
   }
   else
   {
      return 0;
   }
}



Example:

Code:

#include <12F675.h> 


#device *= 16
#device ADC = 8
                       
                                                           
#fuses INTRC_IO, NOWDT, PROTECT, NOMCLR, BROWNOUT, PUT, CPD   
                                                                     

#use delay (internal = 4MHz) 
                                                           
                                                                                                   
#include "DHT11.c"                   
#include "TM1640.c"                     
                                             
                         
void setup();           
                                           
                                                                                                                                                                   
void main()                                                                                           
{                                         
   short state = 0; 
   unsigned char tmp = 0;
   unsigned char brightness = 0;
                         
   setup();     
   
   while(TRUE)                                                                         
   {                                         
     state = get_data();
     
      read_adc(adc_start_only);
      while(!adc_done());
      brightness = read_adc(adc_read_only);
      brightness >>= 5;
     
      if(brightness > 7)
      {
         brightness = 7;
      }
             
      TM1640_send_command(0x88 | brightness);
     
      switch(state)
      {
            case 1:                                                         
            {                 
                TM1640_send_data(5, (font[37]));
                TM1640_send_data(6, (font[82]));
                TM1640_send_data(7, (font[82])); 
                TM1640_send_data(8, (font[79]));
                TM1640_send_data(9, (font[82] | font[14]));       
                delay_ms(600);
                TM1640_clear_display(); 
               delay_ms(900); 
               break;
            }                           
            default:                                     
            {                         
               TM1640_clear_display(); 
               TM1640_send_data(0, (font[82] | font[14]));
            TM1640_send_data(1, (font[72] | font[14]));     
            TM1640_send_data(2, (font[0]));         
                                    
            tmp = (values[0] / 100);   
            if(tmp >= 1) 
            {
               TM1640_send_data(3, (font[16 + tmp]));
            }
            else
            {   
               TM1640_send_data(3, (font[0]));   
            }   
            
            tmp = ((values[0] / 10) % 10);     
            if(tmp >= 1) 
            {
               TM1640_send_data(4, (font[16 + tmp]));
            }
            else
            {                     
               TM1640_send_data(4, (font[0]));   
            }   
            
            tmp = (values[0] % 10);   
            TM1640_send_data(5, (font[16 + tmp] | font[14]));   
            tmp = (values[1] / 100);             
            TM1640_send_data(6, (font[16 + tmp]));     
                                                                    
            TM1640_send_data(7, (font[0]));     
            TM1640_send_data(8, (font[0]));     
            TM1640_send_data(9, (font[0]));   
            TM1640_send_data(10, (font[84] | font[14]));
            TM1640_send_data(11, (font[0]));                       
            
            tmp = (values[2] / 100);
            if(tmp >= 1) 
            {
               TM1640_send_data(12, (font[16 + tmp]));
            }
            else
            {   
               TM1640_send_data(12, (font[0]));   
            }   
                                                        
            tmp = ((values[2] / 10) % 10); 
            if(tmp >= 1) 
            {
               TM1640_send_data(13, (font[16 + tmp]));
            }
            else
            {   
               TM1640_send_data(13, (font[0]));   
            }   
            
            tmp = (values[2] % 10);                                             
            TM1640_send_data(14, (font[16 + tmp] | font[14]));   
            tmp = (values[3] / 100);             
            TM1640_send_data(15, (font[16 + tmp]));   
            
               break;                   
            }                   
      }     
      delay_ms(1000);
   }                                                         
}                 

                                                                                                           
void setup()
{                                     
   disable_interrupts(global);                   
   setup_comparator(NC_NC_NC_NC);           
   setup_ADC(ADC_clock_div_16);
   setup_ADC_ports(sAN0 | VSS_VDD); 
   set_ADC_channel(0);
   setup_timer_0(T0_internal | T0_8_bit);
   setup_timer_1(T1_disabled);     
   set_timer0(0);                                     
   set_timer1(0);   
   DHT11_init();   
   TM1640_init(brightness_100_pc);     
}                       


https://www.facebook.com/MicroArena/photos/a.113629448737180.12954.113572212076237/512591835507604/?type=1&theater
_________________
https://www.facebook.com/MicroArena

SShahryiar
Victor



Joined: 27 Oct 2003
Posts: 12

View user's profile Send private message

PostPosted: Fri Oct 07, 2016 9:58 pm     Reply with quote

Thanks for the code. Very Happy
Recently I am struggling with TM1640, it was very unstable.
Until, I connected two 100pf ceramic caps on Data & Clk pins and 0.1uF bus cap on power, then the TM1640 become very stable.

However, some buddy feedback TM1640 will hang (eg. interference, voltage spike). The only way to reset is power off and power up again, since it don‘t have reset line. Although I have not seen this happen, but during design time, just take note of this issue.

P/s: From my experiments, the cap must be about 100pf (101) , 1000pf (102) may not work well. 10K pull up resistor may not be needed.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group