您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > 基于PIC16F877A单片机的秒表计时器
基于pic16f877a单片机的秒表计时器内蒙古民族大学草原神鹰工作组电路组王恒舜2012/12/12D714D613D512D411D310D29D18D07E6RW5RS4VSS1VDD2VEE3LCD1LM032L47%RV11kRA0/AN02RA1/AN13RA2/AN2/VREF-/CVREF4RA4/T0CKI/C1OUT6RA5/AN4/SS/C2OUT7RE0/AN5/RD8RE1/AN6/WR9RE2/AN7/CS10OSC1/CLKIN13OSC2/CLKOUT14RC1/T1OSI/CCP216RC2/CCP117RC3/SCK/SCL18RD0/PSP019RD1/PSP120RB7/PGD40RB6/PGC39RB538RB437RB3/PGM36RB235RB134RB0/INT33RD7/PSP730RD6/PSP629RD5/PSP528RD4/PSP427RD3/PSP322RD2/PSP221RC7/RX/DT26RC6/TX/CK25RC5/SDO24RC4/SDI/SDA23RA3/AN3/VREF+5RC0/T1OSO/T1CKI15MCLR/Vpp/THV1U1PIC16F877A+88.8Volts功能实现:用于计时,可精确到秒。其最多可用于三次计时,计时范围00:00s~59:59s。也可把它改成简单的时钟。实现原理:通过pic16f877a内部TMR0计时\定数器计时,利用键盘来控制其开启、定时,通过液晶显示。键盘从左至右依次为:开启计时器,一次计时、二次计时,三次计时。按下左一键开始计时:D714D613D512D411D310D29D18D07E6RW5RS4VSS1VDD2VEE3LCD1LM032L第一次按下左二键第一次计时:D714D613D512D411D310D29D18D07E6RW5RS4VSS1VDD2VEE3LCD1LM032L第二次按下左二键第二次计时:D714D613D512D411D310D29D18D07E6RW5RS4VSS1VDD2VEE3LCD1LM032L按下右一键第三次计时,且计时结束:D714D613D512D411D310D29D18D07E6RW5RS4VSS1VDD2VEE3LCD1LM032L第一、二、三次所计时间分别为02:52s(两分五十二秒),03:54s(三分五十四秒),05:09s(五分零九秒)。其代码如下:#includepic.h#defineucharunsignedchar#defineuintunsignedint#defineRSRC0#defineRWRC1#defineERC2ucharconsttable[]={'0','1','2','3','4','5','6','7','8','9'};intnum,s1,s2,s3,s4,s5,s6,a,b,c,n,i;voidLCD_write_command(uchardat);voidLCD_write_data(uchardat);voidLCD_disp_char(ucharx,uchary,uchardat);voiddelay(uintx);voidinit();voidmain(){TRISB=0;PORTB=0X07;i=0;if(RB0==0){init();delay(10);while(RB0){if(num=58){num=0;n++;c=n/3600;s5=c/10;s6=c%10;a=n%3600/60;s1=a/10;s2=a%10;b=n%60;s3=b/10;s4=b%10;LCD_disp_char(0,1,'3');LCD_disp_char(2,1,table[s1]);LCD_disp_char(3,1,table[s2]);LCD_disp_char(4,1,':');LCD_disp_char(5,1,table[s3]);LCD_disp_char(6,1,table[s4]);LCD_disp_char(7,1,'s');}if(RB1==0&&i==0){i=1;LCD_disp_char(0,0,'1');LCD_disp_char(2,0,table[s1]);LCD_disp_char(3,0,table[s2]);LCD_disp_char(4,0,':');LCD_disp_char(5,0,table[s3]);LCD_disp_char(6,0,table[s4]);LCD_disp_char(7,0,'s');}if(RB1==0&&i==1){i=1;LCD_disp_char(10,0,'2');LCD_disp_char(12,0,table[s1]);LCD_disp_char(13,0,table[s2]);LCD_disp_char(14,0,':');LCD_disp_char(15,0,table[s3]);LCD_disp_char(16,0,table[s4]);LCD_disp_char(17,0,'s');}if(RB2==0)while(RB0);}}}voidinit(){TRISC=0;TRISD=0;PORTD=0;LCD_write_command(0x38);//设置格式LCD_write_command(0x0c);//整体显示关光标不闪烁?LCD_write_command(0x06);//设置输入方式,增量不够移位?LCD_write_command(0x01);//清屏?OPTION_REG=0X07;//设置预分为1:256INTCON=0Xa0;//开启TMR0中断TMR0=57;//TMR0寄存器预存57}voidinterrupttime0()//中断{T0IF=0;TMR0=57;num++;}voidLCD_write_data(uchardat){RS=1;RW=0;PORTD=dat;E=1;E=0;delay(10);}voidLCD_write_command(uchardat){RS=0;RW=0;PORTD=dat;E=1;E=0;delay(10);}voidLCD_disp_char(ucharx,uchary,uchardat){ucharaddress;if(y==1)address=0x80+x;elseaddress=0xc0+x;LCD_write_command(address);LCD_write_data(dat);}voiddelay(uintx){inta,b;for(a=x;a0;a--)for(b=110;b0;b--);}
本文标题:基于PIC16F877A单片机的秒表计时器
链接地址:https://www.777doc.com/doc-2603752 .html