當前位置:編程學習大全網 - 編程語言 - 電路如下圖請問單片機動態數碼管顯示C語言程序怎麽寫呢?

電路如下圖請問單片機動態數碼管顯示C語言程序怎麽寫呢?

#include<reg52.h> //包含頭文件,壹般情況不需要改動,頭文件包含特殊功能寄存器的定義

code unsigned char tab[]=

{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

//***陰數碼管 0-9

unsigned char Dis_Shiwei;//定義十位

unsigned char Dis_Gewei; //定義個位

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

/* 延時函數 */

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

void delay(unsigned int cnt)

{

while(--cnt);

}

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

/* 主函數 */

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

main()

{

TMOD |=0x01;//定時器設置 10ms in 12M crystal,工作在模式1,16位定時

TH0=0xd8;

TL0=0xf0;

IE= 0x82; //打開中斷

TR0=1; //打開定時開關

while(1)

{

P0=Dis_Shiwei;//顯示十位

P2=0;

delay(300); //短暫延時

P0=Dis_Gewei; //顯示個位

P2=1;

delay(300);

}

}

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

/* 定時器中斷函數 */

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

void tim(void) interrupt 1 using 1

{

static unsigned char second,count;

TH0=0xd8; //重新賦值

TL0=0xf0;

count++;

if (count==100) //100x10ms=1S,大致延時時間

{

count=0;

second++; //秒加1

if(second==100)

second=0;

Dis_Shiwei=tab[second/10];//十位顯示值處理

Dis_Gewei=tab[second%10]; //個位顯示處理

}

  • 上一篇:感悟人生的經典語錄10條
  • 下一篇:四平職業大學的各系介紹
  • copyright 2024編程學習大全網