‘壹’ 51单片机按键计数器c语言编程
#include<reg51.h>
#defineucharunsignedchar;
uchardistab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00};//0到f
ucharnumber,dat,dis[4];
voidt0isr()interrupt1
{
TH0=(65536-5000)/256;
TL0=(65536-5000)%256;
number++;
number%=3;
switch(number)
P1=0x20<<number;
P0=distab[dis[number]];
}
voidint0isr()interrupt0
{
dat++;
dat%=1000;
dis[0]=dat%10;
dis[1]=dat%100/10;
dis[2]=dat/100;
}
main()
{
TMOD=0x01;
TH0=(65536-5000)/256;
TL0=(65536-5000)%256;
TR1=1;
ET1=1;
EX0=1;
IT0=1;
EA=1;
while(1);
}
‘贰’ 51单片机设计两位计数器C语言
#include "reg52.h"
#define uchar unsigned char
#define uint unsigned int
#define dataport P1
sbit s1=P2^0;
sbit s2=P2^1;
sbit s3=P2^2;
sbit wei1=P2^4;
sbit wei2=P2^5;
signed char a=0;
uchar TABLE[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
/雹悄/延源耐渣时子程序
void delay1ms(uint t)
{
uint i;
uint j;
for(i=0;i<t;i++)
for(j=0;j<116;j++);
}
//显亩宏示子程序
void display(uchar n)
{
wei1=1;
dataport=TABLE[n/10];
delay1ms(1);
wei1=0;
wei2=1;
dataport=TABLE[n%10];
delay1ms(1);
wei2=0;
}
void main(void)//主程序
{
while(1)
{
if(s1==0)
{
delay1ms(20);
if(s1==0)
{
a++;
}
}
if(a=100)a=0;
if(s2==0)
{
delay1ms(20);
if(s2==0)
{
a--;
}
}
if(a<0)a=99;
if(s3==0)
{
delay1ms(20);
if(s3==0)
{
a=0;
}
}
display(a);
}
}
‘叁’ 介绍计数器 /定时器 程序 的编写步骤 (C语言的)
注意:
多数C语言编译器不支持多线程,而且ANSI C也没有线程库,因此C语言无法实现实际意义上的定时器(即包含触发机制的定时器)。
回到本问题:
1 计数器:
简单的int变量(一般为全局或相对全局)就可以实现。
2 计时器:
包含time.h,使用clock相关函数,通过运行时间差来实现计时功能。示例:
/*@*/ clock_t startstart = clock();
……
/*@*/ clock_t endend = clock();
float start2end = (float)(endend-startstart)/CLOCKS_PER_SEC;
// 这里的start2end就是时间差
3 定时器
使用系统API,比如Windows下的Sleep()函数(注意,是大写),原型如下:
VOID Sleep(
DWORD dwMilliseconds // sleep time in milliseconds
);
‘肆’ 单片机计数器C程序
#include<reg51.h>
#define uchar unsigned char
uchar j,k,i,a,A1,A2,second;
sbit la=P2^6;
sbit wela=P2^7;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void delay(uchar i)
{
for(j=i;j>0;j--)
for(k=125;k>悄嫌0;k--);
}
void display(uchar sh_c,uchar g_c)
{
la=0;
P0=table[sh_c];
la=1;
la=0;
wela=0;
P0=0xfe;
wela=1;
wela=0;
delay(5);
P0=table[g_c];
la=1;
la=0;
P0=0xfd;
wela=1;
wela=0;
delay(5);
}void main()
{
while(1)
{
second++;
if(second==60)
second=0;
A1=second/10;
A2=second%10;
for(a=50;a>0;a--)
{ display(A1,A2);};
} }这是0到59的你让伍改一坦运或下就可以了~!
‘伍’ 用单片机c51,c语言编0-9计数器程序!谢谢!!!
你的硬件电路有问题啊,单片机31号脚要接高电平
还有,你的数码管接法也不对啊
看你的排阻接法,你的数码管要用共阴极的
当加到9之后,再按一下,是不是又恢复到0啦枣神首???
下面是更凳数改的回答
#include<瞎禅reg51.h>
unsignedchara;
unsignedcharcodetable[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x67};
voidinit(void)
{
EA=1;
EX0=1;
IT0=1;
}
voidmain(void)
{
init();
while(1)
{
P0=table[a];
}
}
voidEX_0(void)interrupt0
{
a++;
if(a>9)
a=0;
}
望采纳
‘陆’ c语言计数器
#include <stdlib.h>
#include <math.h>
#include <graphics.h>
#include <stdio.h>
#include <process.h>
#define EXCAPE 27
#define ENTER 13
main(){
int press,i,x,y,x1,y1,ch_z=0;
int dian=0;
char ch='0'; /*input + - * / */
char emp[80],sum[80],*e,*s;
double yuan=0.000000000000;
void init(void);
void clear_z(char *u);
double strtoflt(char *p);
int getkey();
int gd=DETECT, gm;
initgraph(&gd, &gm, "");
e=emp;
s=sum;
init();
x = (getmaxx() / 2) - 120;
y = (getmaxy() / 2) - 150;
x1 = (getmaxx() / 2) + 120;
y1 = (getmaxy() / 2) + 150;
while(1){
press = getkey();
switch(press){
case EXCAPE:
exit(0);
case 47:
bar (x + 10, y + 80 + 10, x + 60 - 10, y + 80 + 60 - 10);
delay(8000);
init();
if (ch!='0'){
switch(ch){
case '/':
if (strtoflt(emp)==0.0){
ch='0';
ch_z=0;
dian=0;
emp[0]='\0';
sum[0]='\0';
e=emp;
s=sum;
outtextxy(x+30,y+40,"error!!!!!");
break;
}
yuan = strtoflt(sum) / strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
break;
case '*':
yuan = strtoflt(sum) * strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
break;
case '+':
yuan = strtoflt(sum) + strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
break;
case '-':
if (strtoflt(sum)>=strtoflt(emp)){
yuan = strtoflt(sum) - strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
}
else{
yuan=strtoflt(emp)-strtoflt(sum);
sprintf(sum,"-%0.10f",yuan);
}
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
}
}
else{
if (ch_z==0){
outtextxy(x+30,y+40,emp);
stpcpy(sum,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
}
ch='/';
ch_z=0;
emp[0]='\0';
e=emp;
dian=0;
break;
case 42:
bar (x + 60 + 10, y + 80 + 10, x + 60 * 2 - 10, y + 80 + 60 - 10);
delay(8000);
init();
if (ch!='0'){
switch(ch){
case '/':
yuan = strtoflt(sum) / strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '*':
yuan = strtoflt(sum) * strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '+':
yuan = strtoflt(sum) + strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '-':
if (strtoflt(sum)>=strtoflt(emp)){
yuan = strtoflt(sum) - strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
}
else{
yuan=strtoflt(emp)-strtoflt(sum);
sprintf(sum,"-%0.10f",yuan);
}
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
}
}
else{
if (ch_z==0){
outtextxy(x+30,y+40,emp);
stpcpy(sum,emp);
e=emp;
}
else
outtextxy(x+30,y+40,sum);
}
ch='*';
ch_z=0;
dian=0;
break;
case 45:
bar (x + 60 * 2 + 10, y + 80 + 10, x + 60 * 3 - 10, y + 80 + 60 - 10);
delay(8000);
init();
if (ch!='0'){
switch(ch){
case '/':
yuan = strtoflt(sum) / strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '*':
yuan = strtoflt(sum) * strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '+':
yuan = strtoflt(sum) + strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '-':
if (strtoflt(sum)>=strtoflt(emp)){
yuan = strtoflt(sum) - strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
}
else{
yuan=strtoflt(emp)-strtoflt(sum);
sprintf(sum,"-%0.10f",yuan);
}
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
}
}
else{
if (ch_z==0){
outtextxy(x+30,y+40,emp);
stpcpy(sum,emp);
e=emp;
}
else
outtextxy(x+30,y+40,sum);
}
ch='-';
ch_z=0;
dian=0;
break;
case 43:
bar (x + 60 * 3 + 10, y + 80 + 10, x + 60 * 4 - 10, y + 80 + 60 - 10);
delay(8000);
init();
if (ch!='0'){
switch(ch){
case '/':
yuan = strtoflt(sum) / strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '*':
yuan = strtoflt(sum) * strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '+':
yuan = strtoflt(sum) + strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '-':
if (strtoflt(sum)>=strtoflt(emp)){
yuan = strtoflt(sum) - strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
}
else{
yuan=strtoflt(emp)-strtoflt(sum);
sprintf(sum,"-%0.10f",yuan);
}
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
}
}
else{
if (ch_z==0){
outtextxy(x+30,y+40,emp);
stpcpy(sum,emp);
e=emp;
}
else
outtextxy(x+30,y+40,sum);
}
ch='+';
ch_z=0;
dian=0;
break;
case 49:
bar (x + 10, y + 80 + 53 + 10, x + 60 - 10, y + 80 + 53 * 2 - 4);
delay(8000);
init();
for (i=0;i<=79;i++){
if (emp[i]=='\0')
break;
}
if (ch_z==0){
*e='1';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case 50:
bar (x + 60 + 10, y + 80 + 53 + 10, x + 60 * 2 - 10, y + 80 + 53 * 2 - 4);
delay(8000);
init();
for (i=0;i<=79;i++){
if (emp[i]=='\0')
break;
}
if (ch_z==0){
*e='2';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case 51:
bar (x + 60 * 2 + 10, y + 80 + 53 + 10, x + 60 * 3 - 10, y + 80 + 53 * 2 - 4);
delay(8000);
init();
for (i=0;i<=79;i++){
if (emp[i]=='\0')
break;
}
if (ch_z==0){
*e='3';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case ENTER:
bar (x + 60 * 3 + 10, y + 80 + 53 + 10, x + 60 * 4 - 10, y + 80 + 53 * 2 - 4);
delay(8000);
init();
if (ch!='0'){
switch(ch){
case '/':
yuan = strtoflt(sum) / strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '*':
yuan = strtoflt(sum) * strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '+':
yuan = strtoflt(sum) + strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
case '-':
if (strtoflt(sum)>=strtoflt(emp)){
yuan = strtoflt(sum) - strtoflt(emp);
sprintf(sum,"%0.10f",yuan);
}
else{
yuan=strtoflt(emp)-strtoflt(sum);
sprintf(sum,"-%0.10f",yuan);
}
clear_z(sum);
outtextxy(x+30,y+40,sum);
emp[0]='\0';
e=emp;
break;
}
}
else{
if (ch_z==0){
outtextxy(x+30,y+40,emp);
stpcpy(sum,emp);
e=emp;
}
else{
outtextxy(x+30,y+40,sum);
}
}
ch='0';
ch_z=1;
dian=0;
break;
case 52:
bar (x + 10, y + 80 + 53 * 2 + 10, x + 60 - 10, y + 80 + 53 * 3 - 4);
delay(8000);
init();
if (ch_z==0){
*e='4';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case 53:
bar (x + 60 + 10, y + 80 + 53 * 2 + 10, x + 60 * 2 - 10, y + 80 + 53 * 3 - 4);
delay(8000);
init();
if (ch_z==0){
*e='5';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case 54:
bar (x + 60 * 2 +10, y + 80 + 53 * 2 + 10, x + 60 * 3 - 10, y + 80 + 53 * 3 - 4);
delay(8000);
init();
if (ch_z==0){
*e='6';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case 46:
bar (x + 60 * 3 + 10, y + 80 + 53 * 2 + 10, x + 60 * 4 - 10, y + 80 + 53 * 3 - 4);
delay(8000);
init();
if (dian==0){
if (ch_z==0){
*e='.';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
}
else{
if (ch_z==0)
outtextxy(x+30,y+40,emp);
else
outtextxy(x+30,y+40,sum);
}
dian=1;
break;
case 55:
bar (x + 10, y + 80 + 53 * 3 + 10, x + 60 - 10, y + 80 + 53 * 4 - 4);
delay(8000);
init();
if (ch_z==0){
*e='7';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case 56:
bar (x + 60 + 10, y + 80 + 53 * 3 + 10, x + 60 * 2 -10, y + 80 + 53 * 4 - 4);
delay(8000);
init();
if (ch_z==0){
*e='8';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case 57:
bar (x + 60 * 2 + 10, y + 80 + 53 * 3 + 10, x + 60 * 3 - 10, y + 80 + 53 * 4 - 4);
delay(8000);
init();
if (ch_z==0){
*e='9';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case 48:
bar (x + 60 * 3 + 10, y + 80 + 53 * 3 + 10, x + 60 * 4 - 10, y + 80 + 53 * 4 - 4);
delay(8000);
init();
if (ch_z==0){
*e='0';e++;*e='\0';
outtextxy(x+30,y+40,emp);
}
else{
outtextxy(x+30,y+40,sum);
}
break;
case 32:
emp[0]='\0';
sum[0]='\0';
e=emp;
s=sum;
ch='0';
ch_z=0;
dian=0;
init();
break;
case 8:
delay(8000);
for(i=0;i<=79;i++){
if (emp[i]=='\0')
break;
}
if (i==0)
break;
if (i!=79&&i!=0){
i--;
emp[i]='\0';
e=&emp[i];
}
init();
outtextxy(x+30,y+40,emp);
break;
}
}
}
/*---------------------------------------------------------------------*/
void init(void){
int x, y, x1, y1, i, j;
char emp;
x = (getmaxx() / 2) - 120;
y = (getmaxy() / 2) - 150;
x1 = (getmaxx() / 2) + 120;
y1 = (getmaxy() / 2) + 150;
cleardevice();
setbkcolor(3);
setfillstyle(1, 15);
setcolor(15);
settextstyle(1,0,1);
rectangle (x, y, x1, y1);
rectangle (x - 7, y - 7, x1 + 7, y1 + 7);
rectangle (x + 10, y + 10, x1 - 10, y + 80 - 10);
line (x, y + 80, x1, y + 80);
y = y + 80;
for (j = 1; j <= 4; j++){
x = (getmaxx() / 2) - 120;
for (i = 1; i <= 4; i++){
/* bar (x + 10, y + 10, x + 60 - 10, y + 60 - 10);*/
rectangle(x + 10, y + 10, x + 60 - 10, y + 60 - 10);
if (j == 1){
if (i == 1)
outtextxy(x + 20, y + 20, "/");
if (i == 2)
outtextxy(x + 25, y + 20, "*");
if (i == 3)
outtextxy(x + 27, y + 20, "-");
if (i == 4)
outtextxy(x + 25, y + 20, "+");
}
if (j == 2){
if (i == 1)
outtextxy(x + 25, y + 20, "1");
if (i == 2)
outtextxy(x + 25, y + 20, "2");
if (i == 3)
outtextxy(x + 25, y + 20, "3");
if (i == 4)
outtextxy(x + 25, y + 20, "=");
}
if (j == 3){
if (i == 1)
outtextxy(x + 25, y + 20, "4");
if (i == 2)
outtextxy(x + 25, y + 20, "5");
if (i == 3)
outtextxy(x + 25, y + 20, "6");
if (i == 4)
outtextxy(x + 25, y + 20, ".");
}
if (j == 4){
if (i == 1)
outtextxy(x + 25, y + 20, "7");
if (i == 2)
outtextxy(x + 25, y + 20, "8");
if (i == 3)
outtextxy(x + 25, y + 20, "9");
if (i == 4)
outtextxy(x + 25, y + 20, "0");
}
x = x + 60;
}
y = y + 53;
}
}
/*---------------------------------------------------------------------*/
int getkey(){
char lowbyte;
int press;
while(bioskey(1)==0);
press = bioskey(0);
press = press&0xff? press&0xff: press>>8;
return(press);
}
double strtoflt(char *p)
{
double rtl=0.000000000000;
double pnt=0.000000000000;
double t = 10;
int ispoint = 0;
while (*p!='\0'||*p!='.'){
if(*p<'0'||*p>'9')
break;
rtl*=10;
rtl+=*p-'0';
p++;
}
if (*p=='.'){
ispoint=1;
p++;
}
while(ispoint&&*p!='\0'){
pnt+=(double)(*p-'0')/t;
t*=10;
p++;
}
rtl+=pnt;
return (rtl);
}
/*-----------------------------------------------------------------------*/
void clear_z(char u[]){
int i;
for(i=strlen(u)-1;i>=0;i--){
if (u[i]!='0')
break;
}
if (u[i]=='.'){
u[i]='\0';
}
else{
i++;
u[i]='\0';
}
}
‘柒’ 单片机计数器编程如何计数脉冲,用C语言
每50ms来了多少脉冲,那定时器就不能50ms中断一次,尽量快的中断会比较好,2个变量计数,一个计算50ms,一个累加脉冲
‘捌’ C语言设计一个加减计数器,通过两个按键来控制。就是按一个键就加一,按另外一个就减一。求大神帮忙设计
如果是windows上程序。单词按键判断ASCII码,然后变量值++,--就可以了。
#include<stdio.h>
#include<conio.h>
#include<windows.h>
#defineKEYA61//按键盘'+'键(非小键盘)
#defineKEYM45//按键盘'-'键(非小键盘)
intmain()
{
intkey,num=0;
while(1)
{
system("cls");
printf("当前值:%d ",num);
key=getch();
switch(key)
{
caseKEYA:num++;break;
caseKEYM:num--;break;
}
}
return0;
}
如果你是要其他平台,比如单片机上运行,只要对应按钮电平对应防抖代码中对变量++,--就可以了。我之前给别人写个一个单片机的简单程序,里面就有按钮+-的,你可以参考。
以前回答记录
‘玖’ 用C语言写两个计数器的小程序。
很多人回家过年了,所以回答少,程序已通过
#include<stdio.h>
/*第一个程序:*/
main()
{
int i=1, m=0;
printf("请输入一个数:\n");
while(i!=0){ //while 循环开始当i等于0时,程序中止;
scanf("%d",&i); //从键盘获取数值,如果i=0,则退出并输出m的值
//printf("\n");
if (i==1) m++; //如果i=1,则m+1;
}
printf("输入 '1' 的次数:%d\n",m);
}
//第二个程序 在第一个基础上稍修改即可
main()
{
int i=1, n,m=0;
printf("请输入N:\n");
scanf("%d",&n);
printf("请输入一个数:\n");
while(i!=0){
scanf("%d",&i); //从键盘获取数值,如果i=0,则退出并输出m的值
if (i>n) m++; //如果i>n,则m+1;
}
printf("大于N次数:%d\n",m);
}
‘拾’ 介绍计数器 /定时器 程序 的编写步骤 (C语言的)
假设你用的晶振为12m,用p1.0口输出周期为2ms的方波。使用定时器工作方式1.
至于计数初值的计算,授之以鱼不如授之以渔!
在定时器模式下,计数器的计数脉冲来自于晶振脉冲的12分频信号,即对机器周期进行计数。若选择12m晶振,则定时器的计数频率为1mhz。假设定时时间为t,机器周期为t1,即12/晶振频率。x为定时器初值。则
x=2^n-t/t1。方式0,n=13,方式1时,n=16,方式2和方式3,n=8
自己算去吧!
#include
void
inittimer0(void)//
{
tmod
=
0x01;
th0
=
0x0fc;
//计数器初值
tl0
=
0x18;
ea
=
1;
et0
=
1;
tr0
=
1;//开启定时器t0
}
void
main(void)
{
inittimer0();
}
void
timer0interrupt(void)
interrupt
1
{
th0
=
0x0fc;//重新赋初值
tl0
=
0x18;
p1.0=~p1.0;
//输出方波
}