專注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機(jī)教程網(wǎng) >> MCU設(shè)計(jì)實(shí)例 >> 瀏覽文章

AVR單片機(jī)4位數(shù)碼管計(jì)數(shù)C程序

作者:佚名   來源:本站原創(chuàng)   點(diǎn)擊數(shù):  更新時(shí)間:2012年06月13日   【字體:

AVR單片機(jī)4位數(shù)碼管計(jì)數(shù),從0000-5000一直循環(huán)。位選端接在PC的低4位,而段選端接在PA口。程序如下:

#include<iom16v.h>//頭文件
#include<macros.h>//頭文件
#define uchar unsigned char//宏定義
#define uint unsigned int//宏定義
uchar i;//定義變量
uint num;//定義計(jì)數(shù)變量
#pragma data:code//數(shù)組存放位置
const table[]={
0xc0,0xf9,0xa4,0xb0,0x99,
0x92,0x82,0xf8,0x80,0x90,
0x88,0x83,0xc6,0xa1,0x86,
0x8e
};//共陽(yáng)數(shù)碼管0--9代碼
/*************************************************************/
/*********************延時(shí)子程序******************************/
/*************************************************************/
void delay(uint z)//一個(gè)帶參數(shù),不帶返回值的子函數(shù)
{
 uint x,y;//定義兩個(gè)變量
 for(x=0;x<55;x++)//外循環(huán)
 for(y=z;y>0;y--);//內(nèi)循環(huán)
}
/*************************************************************/
/*********************顯示子程序******************************/
/*************************************************************/
void xian()
{  
      for(i=0;i<20;i++)//顯示20次,使整體看起來不閃爍
 {
         PORTA=table[num/1000];//將千位數(shù)給PA口
   PORTC&=~BIT(3);//打開千位位選端
   delay(2);//延時(shí)一會(huì)兒
   PORTC|=BIT(3);//關(guān)掉千位
   PORTA=table[num%1000/100];//將百位數(shù)給PA口
   PORTC&=~BIT(2);
   delay(2);
   PORTC|=BIT(2);
   PORTA=table[num%100/10];//將十位數(shù)給PA口
   PORTC&=~BIT(1);
   delay(2);
   PORTC|=BIT(1);
   PORTA=table[num%10];//將個(gè)位數(shù)給PA口
   PORTC&=~BIT(0);
   delay(2);
   PORTC|=BIT(0);
 }
 num++;//將它加1,形成動(dòng)態(tài)計(jì)數(shù)
 if(num>5000)//如果它大于5000就將計(jì)數(shù)器清0
 num=0;//清0
}
/*************************************************************/
/*************************主程序******************************/
/*************************************************************/
void main()
{
     DDRA=0xff;//設(shè)置PA口為輸出
     DDRC=0xff;//設(shè)置PC口為輸出
     while(1)
     {
        xian(); //調(diào)用顯示子函數(shù) 
     }
}

關(guān)閉窗口

相關(guān)文章