视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
单片机定时器C代码
2025-10-05 05:17:22 责编:小OO
文档
#include

#include

#include

#include

//#define 0xbf-上点 0xef-下点 0xaf-双点 0xf7-特殊点 0xfe-小数点

//#define zero 0x21\\/*0*/one 0xf9\\/*1*/ two 0x0b\\/*2*/ three 0x49\\/*3*/ four 0xd1\\/*4*/

//#define five 0x45\\/*5*/ six 0x05\\/*6*/ seven 0x79\\/*7*/ eight 0x01\\/*8*/ nine 0x41\\/*9*/

//顺序常数

#define sxzero 0xf7\\/*0*/sxone 0xef\\/*1*/sxtwo 0xdf\\/*2*/sxthree 0xbf\\/*3*/sxfour 0x7f\\/*4*/

#define maxout 2//最大输出

#define maxway 6//输出线路总数

//设置键常数

#define left 3

#define right 6

#define enter 5

#define cance 4

#define up 1

#define down 2

//设置时间常数

#define sunday 0

#define month 1

#define day 2

#define hour 3

#define min 4

#define sec 5

#define tick 6

unsigned char code overday[12]={31,29,31,30,31,30,31,31,30,31,30,31}; //1到12月天数

unsigned char code line[]={0,0,1,1};//显示位置对应的数据位

unsigned char code step[]={10,1,10,1};//每按一次按键变化数值

unsigned char code overhigh[]={7,12,31,23,59,59};//溢出指示,星期,月,日,时,分,秒

unsigned char code overlow[]={1,1,1,0,0,0};//溢出指示,星期,月,日,时,分,秒

unsigned char code bitcheck[]={1,2,4,8,16,32,,128,255};

unsigned char code xswz[]={0x7F,0xBF,0xEF,0xF7,0xDF,100,0,100,0,0};//数码管数码位xswz:显示位置

unsigned char code dottime[]={0xF7,0xBF,0xEF,0xaf,0xaF,0xaF};//小数点显示内容列表

unsigned char code dotflash[]={0,0,0,255,0,0};//小数点显示内容列表

unsigned char code numbcode[]={

33, 249, 11, 73, 209, 69, 5, 121, 1, 65,

33, 249, 11, 73, 209, 69, 5, 121, 1, 65,

33, 249, 11, 73, 209, 69, 5, 121, 1, 65,

33, 249, 11, 73, 209, 69, 5, 121, 1, 65,

33, 249, 11, 73, 209, 69, 5, 121, 1, 65,

33, 249, 11, 73, 209, 69, 5, 121, 1, 65,

33, 249, 11, 73, 209, 69, 5, 121, 1, 65,

33, 249, 11, 73, 209, 69, 5, 121, 1, 65,

33, 249, 11, 73, 209, 69, 5, 121, 1, 65,

33, 249, 11, 73, 209, 69, 5, 121, 1, 65,

33, 33, 33, 33, 33, 33, 33, 33, 33, 33,

249,249,249,249,249,249,249,249,249,249,

11, 11, 11, 11, 11, 11, 11, 11, 11, 11,

73, 73, 73, 73, 73, 73, 73, 73, 73, 73,

209,209,209,209,209,209,209,209,209,209,

69, 69, 69, 69, 69, 69, 69, 69, 69, 69,

5, 5, 5, 5, 5, 5, 5, 5, 5, 5,

121,121,121,121,121,121,121,121,121,121,

1, 1, 1, 1, 1, 1, 1, 1, 1, 1,

65, 65, 65, 65, 65, 65, 65, 65, 65, 65,

191,239,175,247,254

};

unsigned char data outnumb,zzq;//实际输出数,总周期数

unsigned char data power,high,low;//总功率,高,低电平

unsigned char data port[]={254,253,251,247,239,223,191,127};//输出端口

unsigned char data time[]={1,1,1,0,0,0,0,0};//星期1,1月1日 1时0分 0秒0脉冲

unsigned char data temp[]={0,50};

unsigned char data *pshow=&time;//数码管显示指

针,初始化指向时间数组

unsigned char data cursor=hour;//光标,游标-->小时

unsigned char data able[]={255,255,255,255,255};//显示(与)高为允许

unsigned char data disable[]={0,0,0,0,0};//遮掩(或)高为遮掩

unsigned char data flash[]={0,0,0,0,255};//闪烁(或)高为闪烁

unsigned char data dot=0xaf; //DOT:点,原点,小数点

unsigned char idata config[120]={0,1,3,30,96},powlist[maxway];//设定内容存在这里

unsigned char data *pconfig=&config;//定时指针

//=================主函数=====================================

void main (void)

{

//设置定时器

T2CON = 0X00;//大写才能被识别

RCAP2H = 0xe0;//等于65536-(1000tf)/12说明:t的单位为毫秒,f的单位为mhz

RCAP2L = 0x02;//24.576mz

TR2 = 1;//大写才能被识别

ET2 = 1;//大写才能被识别

PT2=1;//大写才能被识别

EA=1;//大写才能被识别

EX1=1;//外部中断1允许

IT1=1;//跳变触发方式

PX1=0;//优先权低

//进入休眠

while(1){

PCON=0X01;

}

}

//=================显示中断子程序======================================

void display(void)

{

staticunsigned char data n=0;//数码位计数

P2=P2|0xF8; //关闭显示,0xf8过滤掉按键端口

if(n==4)

P0=dot;

else

P0=numbcode//显示数据

[

pshow[cursor+line[n]] + xswz[n+5]//cursor指向显示的首位置,即最高数码位

];

P0=able[n]&P0;//显示滤镜

P0=disable[n]|P0;//遮掩滤镜

if(time[tick]>100) P0=flash[n]|P0;//闪烁滤镜

P2=P2&xswz[n];//打开显示 xswz:显示位置数组

if(++n>4) n=0 ;

}

//=================设置函数=====================================

void setup (void)

{

static unsigned char model,delaytick,delaysec,n=0; //gb;光标 ycgb:溢出光标,n为调整位置

static unsigned char data x;

if(

((time[tick]-delaytick)<50) && (delaysec==time[sec])

) return;//防止按键抖动1

if(

((delaytick)-time[tick]>250) && (delaysec+1==time[sec]) //一共是205个tick

) return;//防止按键抖动2

delaysec=time[sec];

delaytick=time[tick];

x=P2&7;//按键检测

//----------时间显示模式-------------------------------------------

if(model==0)

{

if( (x==left) && (cursor>0) ){cursor--;dot=dottime[cursor];flash[4]=dotflash[cursor];return;}

if( (x==right)&&(cursor+1if(x==enter) {model=1;flash[0]=255;return;}//光标处闪烁

}

//----------时间设置模式--------------

else if(model==1)

{

//----------进入下层菜单--------------------------------------------

if(x==enter)

{

model=2;

pshow=&temp;

n=0;

flash[n]=0;

flash[1]=255;//光标处闪烁

return;

}

//----------左移调整位置--------------------------------------------

if(x==left)

{

if(n>0){fla

sh[n]=0;n--;flash[n]=255;return;}//光标左移,新光标处闪烁

if(cursor>0)

{

cursor--;

flash[n]=0;

n+=3;

flash[n]=255;

dot=dottime[cursor];

flash[4]=dotflash[cursor];

return;

}

}

//----------右移调整位置------------

if(x==right)

{

if(n<3){flash[n]=0;n++;flash[n]=255;return;}//光标左移,新光标处闪烁

if(cursor+1{

cursor++;

flash[n]=0;

n-=3;

flash[n]=255;

dot=dottime[cursor];

flash[4]=dotflash[cursor];

return;

}

}

//-----------增加数值--------------------------------------------------

if((x==up) && ( (pshow[ cursor+ line[n]] +step[n]) <=overhigh[cursor+line[n]] ))

{

if( (cursor+ line[n])==day )

{

if( (pshow[ cursor+ line[n]] +step[n])>overday[time[month]-1] ) return;

}

pshow[cursor+line[n]]+=step[n];

return;

}//按键步长

//-----------减少数值---------------------------------------------------

if((x==down) && ( pshow[ cursor+ line[n]] >= (overlow[cursor+line[n]] +step[n]) ))

{

pshow[cursor+line[n]]-=step[n];

return;

}//按键步长

}

else if(model==2)

{

//-----------增减数值---------------------------------------------------

if(n==0)

{

if(x==left) return;

if(x==right){n++;return;}

if((x==up)&&(temp[n]<5)) {temp[n]++;return;}

if((x==down)&&(temp[n]>0)) {temp[n]--;return;}

}

else if(n==1)

{

if(x==left) {n--;return;}

if(x==right) return;

if((x==up)&&(temp[n]<=100)) {temp[n]++;return;}

if((x==down)&&(temp[n]>0)) {temp[n]--;return;}

}

}

}

//===================输出=======================================

void powerout(void)

{

static unsigned char data way=0,m=0,x=0;

static unsigned char data sjgl[]={0,0,0,0,0};

unsigned char data n;

//-------初始化---

n=maxout;

x++;

if(x>zzq)

{

x=0;

m=0;

way=0;

sjgl[0]=0;sjgl[1]=0;sjgl[2]=0;

sjgl[3]=0;sjgl[4]=0;sjgl[5]=0;

}

//--------间歇输出部分----

if(high!=0)

{

m++;

if(m>(high+low)) m=1;

if(m>high) n--;

}

//-------真正输出部分-----

P0=255;//关断输出;

if(n==0) return;

while(n{

if(way>maxway) way=1;

sjgl[way]++;

if(sjgl[way]<=powlist[way]) {n--; P0=P0&port[way];}//输出

way++;

}

}

//===================输出计算=======================================

void outjs(void)

{

unsigned char data zs,ys,jg,m,n;

//-------------输出路数和功率初步计算-----------

outnumb=0;

power=0;

pconfig=pconfig+2;

for(n=0;n<8;n++)

{

if(*(pconfig)&bitcheck[n])

{

outnumb++;//输出路数加1

powlist[n]=*(pconfig+outnumb);//到功率开始部分

power=power+powlist[n];//总功率

}

}

//---------分配计算---------------------------------

zs=power/100;//求整数

ys=power%100;//求余数

high=0;

low=0;

zzq=power;

if(zs>=maxout) {outnumb=maxou

t;return;}

if(ys==0) {outnumb=zs;return;}

outnumb=zs+1;

jg=100-ys;

zzq=zzq+jg;

//---------计算比例(碾展法)----------

if(ys>jg)

{m=ys;n=jg;}

else

{m=jg;n=ys;}

while(zs!=0)/*利用辗除法,直到zs为0为止*/

{

zs=m%n;

m=n;

n=zs;

}

high=ys/m;

low=jg/m;

}

//============时钟中断子程序===========================================

void timer(void) interrupt 5 using 0

{

TF2=0;

display();

//outjs();

//powerout();

time[tick]++;

if(time[tick]<250) return;

time[tick]=0;

time[sec]++;

if(time[sec]<60) return;

time[sec]=0;

time[min]++;

if(time[min]<60)

{//时间检测

if(*pconfig!=time[hour]) return;

if(*(pconfig+1)!=time[min]) return;

outjs();

return;

}

time[min]=0;

time[hour]++;

if(time[hour]<24)

{//时间检测

if(*pconfig!=time[hour]) return;

if(*(pconfig+1)!=time[min]) return;

outjs();

return;

}

time[hour]=0;

time[day]++;

if(time[day]<=overday[time[month]-1]) return;

time[day]=1;

if(time[sunday]<7)

time[sunday]++;

else

time[sunday]=1;

time[month]++;

if(time[month]<13) return;

time[month]=1;

}

//============按键中断子程序===========================================

void set(void) interrupt 2 using 0

{

setup();

}下载本文

显示全文
专题