The microcontroller uses the key to increase the two digits on the digital tube

Publisher:心有所属Latest update time:2019-08-15 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere


#include

#include "./delay/delay.h"

 

sbit seg_select = P2^1;

sbit bit_select = P2^0;

sbit key1 = P1^0;

 

 

#define SEGPORT  P0

 

 

unsigned char segdata[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

unsigned char bitdata[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};

unsigned char TempData[2];

 

 

void display(void)

{

/*eliminate ghosting*/

  static unsigned char i = 0;

  SEGPORT = 0x0;            

  seg_select = 1;

  seg_select = 0;

  

  SEGPORT = 0xff;

  bit_select = 1;

  bit_select = 0;

  SEGPORT = TempData[i];

  seg_select = 1;

  seg_select = 0;

  

  SEGPORT = bitdata[i];

  bit_select = 1;

  bit_select = 0;

  

    i++;

    if(2 == i)

    {

i = 0;

}

}

 

void timer0_init(void)

{

EA = 1; // Enable general interrupt

  TMOD |= 0x01; //Select interrupt mode

  TH0 = (65536 - 2000) / 256;

  TL0 = (65536 - 2000) % 256;

  ET0 = 1;

  TR0 = 1;

}

 

void timer0_isr(void) interrupt 1

{

TH0 = (65536 - 2000) / 256;

  TL0 = (65536 - 2000) % 256;

  display();

  

}

 

void main()

{

unsigned char disdata = 0;

timer0_init();

while(1)

{

  /*Debounce*/

    if(0 == key1) //A key is pressed

  { 

    delay_ms(10);

    if(0 == key1) //A key is pressed

  {

  while(!key1); //A key is lifted

  delay_ms(10);

  while(!key1); //A key is actually lifted

  }

  disdata++;

  if(100 == disdata)

  {

  disdata = 0;

  }

}

TempData[0] = segdata[disdata / 10];

TempData[1] = segdata[disdata % 10];

    }

 

}


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

/*delay.h*/

#ifndef _DELAY_H_

#define _DELAY_H_

 

extern void delay_us(unsigned char t);

extern void delay_ms(unsigned char t);

extern void delay_s(unsigned char t);

 

#endif


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

/*delay.c*/

void delay_us(unsigned char t)

{

while(--t);

}

 

void delay_ms(unsigned char t)

{

while(t--)

{

delay_us(245);

delay_us(245);

}

}

 

void delay_s(unsigned char t)

{

    while(t--)

{

        delay_ms(200);

delay_ms(200);

  delay_ms(200);

delay_ms(200);

  delay_ms(200);

}

}

Reference address:The microcontroller uses the key to increase the two digits on the digital tube

Previous article:Matrix keyboard driver for single chip microcomputer
Next article:Using the digital tube of the single chip computer to simulate the clock to display time

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号