本文目录一览:
- 1、求一个关于用C语言编写的电子时钟的程序,内容要有闹钟,可以调整时间
- 2、用51单片机设计3位led数码显示管作秒表00.0-99.0求设计电路与c语言程序
- 3、用C语言编一个数码管时钟显示程序
- 4、本人在用C语言编一段三位一体数码管动态显示程序,求高手指点
- 5、C语言编程,数码管显示时钟跟设置闹钟的程序?
- 6、用C语言编一个数字电子时钟的程序
求一个关于用C语言编写的电子时钟的程序,内容要有闹钟,可以调整时间
#includereg52.h
#includeabsacc.h
#includeintrins.h
#define unit unsigned int
#define uchar unsigned char
//#define HZ 12
sbit key0=P0^0; // 分钟调整
sbit key1=P0^1; // 小时调整
sbit P2_0=P2^7; //秒 指示灯
sbit MN_RXD=P3^6;
sbit MN_TXD=P3^7;
uchar data CLOCK[4]={0,0,0,12};//存放时钟时间(百分秒,秒,分,和时位)
//数码管显示表0-f 灭
uchar code TABLE[]={0xBE,0x06,0xEA,0x6E,0x56,0x7C,0xFC,0x0E,0xFE,0x7E,0x00};
//**********************************
//模拟串口发送一个字节数据 函数
//**********************************
void SendData(unsigned char senddata)
{
unsigned char i;
for(i=0;i8;i++)
{
if((senddata0x01)==0)
MN_RXD=0;
else
MN_RXD=1;
_nop_();
MN_TXD=0;
_nop_();
MN_TXD=1;
senddata=senddata1;
}
}
//**********************************
//显示程序函数
//**********************************
void display(void)
{
// unsigned int n;
uchar temp;
temp=CLOCK[1]; temp=temp%10; SendData(TABLE[temp]);
temp=CLOCK[1]; temp=temp/10; SendData(TABLE[temp]);
temp=CLOCK[2]; temp=temp%10; SendData(TABLE[temp]);
temp=CLOCK[2]; temp=temp/10; SendData(TABLE[temp]);
temp=CLOCK[3]; temp=temp%10; SendData(TABLE[temp]);
temp=CLOCK[3]; temp=temp/10; SendData(TABLE[temp]);
/*
for(n=0;n5000;n++);
for(n=0;n6;n++)
{
SendData(TABLE[10]);
}
*/
}
//**********************************
//按键控制函数
//**********************************
void keycan()
{
unsigned int n;
EA=0;
if(key0==0) // 分钟调整
{
for(n=0;n10000;n++); //延时去抖动
while(key0==0);
CLOCK[2]=CLOCK[2]+1;
if(CLOCK[2]==60) //到一时
{
CLOCK[2]=0;
}
display();
}
if(key1==0) // 小时调整
{
for(n=0;n10000;n++); //延时去抖动
while(key1==0);
CLOCK[3]=CLOCK[3]+1;
if(CLOCK[3]==24)
{
CLOCK[3]=0;
}
display();
}
EA=1;
}
//**********************************
//T0中断服务函数
//**********************************
void time0() interrupt 1 //using 1
{
TH0=0xD8; TL0=0xF0; //重置初值
// TH0=0xB1; TL0=0xE0;
//时钟处理
CLOCK[0]=CLOCK[0]+1;
}
//**********************************
//主函数
//**********************************
void main()
{
EA=1;
ET0=1;
TMOD=0x01; //T0方式1定时
TH0=0xD8; TL0=0xF0; //D8F0 定时10ms
// TH0=0xB1; TL0=0xE0; //定时 20ms
TR0=1;
for(;;)
{
if(CLOCK[0]==100) //到一秒 10ms*100
{
CLOCK[0]=0;
P2_0=~P2_0;
CLOCK[1]=CLOCK[1]+1;
if(CLOCK[1]==60) //到一分
{
CLOCK[1]=0;
CLOCK[2]=CLOCK[2]+1;
if(CLOCK[2]==60) //到一时
{
CLOCK[2]=0;
CLOCK[3]=CLOCK[3]+1;
if(CLOCK[3]==24)
{
CLOCK[3]=0;
}
}
}
display();
}
keycan();
}
}
用51单片机设计3位led数码显示管作秒表00.0-99.0求设计电路与c语言程序
#include "STC12C54.H"
#define uchar unsigned char
#define uint unsigned int
#define dataport P1//P1直接驱动数码管的8个阳极
sbit wei1=P3^3;//再有三个管脚接3个三极管驱动数码管的阴极
sbit wei2=P3^4;
sbit wei3=P3^5;
uint a=0;//中断次数计数,一次50ms,20次为1 秒
//共阴数码管七段码
uchar TABLE[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
//延时子程序
void delay1ms(uint t)
{
uint i,j,k;
for(i=t;i0;i--)
for(j=15;j0;j--)
for(k=27;k0;k--);
}
//显示子程序
void display(uint n)
{
uchar a1,a2,a3;
a1=n/100%10;
a2=n/10%10;
a3=n%10;
wei1=1;
dataport=TABLE[a1];
delay1ms(1);
wei1=0;
dataport=0x80|TABLE[a2];//这里多个小数点
delay1ms(1);
wei3=1;
dataport=TABLE[a3];
delay1ms(1);
wei3=0;
}
void init(void)
{
TMOD=0X01;
TH0=(655536-50000)/256;//定时50ms
TL0=(655536-50000)%256;
EA=1;
ET0=1;
TR0=1;
P1M0=0;
P1M1=0xff;//P1口设为推挽输出,驱动能力强
P3M0=0;
P3M1=0x38;//P3.3P3.4 P3.5口设为推挽输出
}
void main(void)//主程序
{
init();//初始状态设定
while(1)
{
display(a/2);//显示计数值,以十分之一秒为单位
}
}
void timer0(void)interrupt 1//中断子程序
{
TH0=(655536-50000)/256;
TL0=(655536-50000)%256;
a++;
if(a==1980)a=0;//1980次是99秒
}
用C语言编一个数码管时钟显示程序
问题不太清楚,想帮也无从下手呀!单片机用的是哪款?时间数据哪里来?是程序的一部分还是整个程序就这么点功能?10S报时是语音报时还是“滴滴滴”声报时?“最后一位是10MS,也就是说整个循环是100S”何意?
本人在用C语言编一段三位一体数码管动态显示程序,求高手指点
如果你的硬件已连接完善,那编程需:
1、动态扫描:在已个 t 时间内(可以是几个ms)选中 1 位数码管,以亮点该位数码管对应的码段,其他关断(不亮);而下一个 t 时间需点亮下一位数码管......以此类推,循环起来。这个 t 可以用定时器准确控制。当然,如果你只是为了实现单纯的3个数码管显示,可以不精确时间,循环起来就是了(只要保证耽搁的显示时间少于肉眼识别时间,就是看起来不会一闪一闪的就行);
2、显示内容:多数情况下,待显示的字符/数字先按数码定义编码,存放在表格中。按自己的编程需要,在显示的时候从表格中提取。当然,如果只是简单实验,显示的内容是固定的,也可以程序里用立即数。
多试一试吧。
C语言编程,数码管显示时钟跟设置闹钟的程序?
给个我以前写的程序,你参考参考,我这个使用1602液晶写的,显示方式需要改改
#include "1602yejing.h"
sbit key1=P3^2; //选择调整位置按钮
sbit key2=P3^3; //加一按钮
sbit key3=P3^4; //减一按钮
sbit key4=P3^5; //定时按钮
char shi,fen,miao,T_shi,T_fen,T_miao;
uchar count,keynum,flag;
uchar code table1[]=" 2011-12-26 1";
uchar code table2[]=" 10:30:30";
uchar code table3[]="input the timing";
uchar code table4[]=" 00:00:00";
void write_sfm(uchar add,uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0xc0+add);
write_date(shi+'0');
write_date(ge+'0');
}
void init()
{
uchar num;
shi=10;
fen=30;
miao=30;
P1=0xfe;
write_com(0x38);//显示模式
write_com(0x0c);//不显示光标,光标不闪烁
write_com(0x06);//地址指针加一,光标加一
write_com(0x01);//清屏
write_com(0x80);
for(num=0;num16;num++)
{
write_date(table1[num]);
delay(5);
}
write_com(0xc0);
for(num=0;num12;num++)
{
write_date(table2[num]);
delay(5);
}
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void keyscan()
{
uchar i;
if(key4==0)
{
delay(5);
if(key4==0)
{
while(!key4);
flag=1;
write_com(0x80);
for(i=0;i16;i++)
{
write_date(table3[i]);
delay(5);
}
write_com(0xc0);
for(i=0;i12;i++)
{
write_date(table4[i]);
delay(5);
}
}
}
if(key1==0)
{
delay(5);
if(key1==0)
{
while(!key1);
keynum++;
if(keynum==1)
{
if(flag==0) TR0=0;
write_com(0xc0+11);
write_com(0x0f);
}
if(keynum==2)
{
write_com(0xc0+8);
write_com(0x0f);
}
if(keynum==3)
{
write_com(0xc0+5);
write_com(0x0f);
}
if(keynum==4)
{
write_com(0x0c);
TR0=1;
if(flag==0)
{
keynum=0;
}
if(flag==1)
{
flag=0;
write_com(0x80);
for(i=0;i16;i++)
{
write_date(table1[i]);
delay(5);
}
write_sfm(4,shi);
write_sfm(7,fen);
write_sfm(10,miao);
}
}
}
}
if(keynum!=0)
{
if(key2==0)
{
delay(5);
if(key2==0)
{
// while(!key2);
if(keynum==1)
{
if(flag==0)
{
miao++;
if(miao==60) miao=0;
write_sfm(10,miao);
write_com(0xc0+10);
delay(300);
}
if(flag==1)
{
T_miao++;
if(T_miao==60) T_miao=0;
write_sfm(10,T_miao);
write_com(0xc0+10);
delay(300);
}
}
if(keynum==2)
{
if(flag==0)
{
fen++;
if(fen==60) fen=0;
write_sfm(7,fen);
write_com(0xc0+7);
delay(300);
}
if(flag==1)
{
T_fen++;
if(T_fen==60) T_fen=0;
write_sfm(7,T_fen);
write_com(0xc0+7);
delay(300);
}
}
if(keynum==3)
{
if(flag==0)
{
shi++;
if(shi==24) shi=0;
write_sfm(4,shi);
write_com(0xc0+4);
delay(300);
}
if(flag==1)
{
T_shi++;
if(T_shi==24) T_shi=0;
write_sfm(4,T_shi);
write_com(0xc0+4);
delay(300);
}
}
}
}
if(key3==0)
{
delay(5);
if(key3==0)
{
// while(!key3);
if(keynum==1)
{
if(flag==0)
{
miao--;
if(miao==-1) miao=59;
write_sfm(10,miao);
write_com(0xc0+10);
delay(300);
}
if(flag==1)
{
T_miao--;
if(T_miao==-1) T_miao=59;
write_sfm(10,T_miao);
write_com(0xc0+10);
delay(300);
}
}
if(keynum==2)
{
if(flag==0)
{
fen--;
if(fen==-1) fen=59;
write_sfm(7,fen);
write_com(0xc0+7);
delay(300);
}
if(flag==1)
{
T_fen--;
if(T_fen==-1) T_fen=59;
write_sfm(7,T_fen);
write_com(0xc0+7);
delay(300);
}
}
if(keynum==3)
{
if(flag==0)
{
shi--;
if(shi==-1) shi=23;
write_sfm(4,shi);
write_com(0xc0+4);
delay(300);
}
if(flag==1)
{
T_shi--;
if(T_shi==-1) T_shi=23;
write_sfm(4,T_shi);
write_com(0xc0+4);
delay(300);
}
}
}
}
}
}
void main()
{
init();
while(1)
{
keyscan();
if(miao==T_miaofen==T_fenshi==T_shi)
{
P1=0xff;
}
}
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==16)
{
count=0;
P1=0xfe;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
}
if(flag==0) write_sfm(4,shi);
}
if(flag==0) write_sfm(7,fen);
}
if(flag==0) write_sfm(10,miao);
}
}
头文件
#ifndef _1602yejing_H_
#define _1602yejing_H_
#includereg52.h
#define uchar unsigned char
#define uint unsigned int
sbit E=P2^7;
sbit RW=P2^6;
sbit RS=P2^5;
void delay(uint z)
{
uint x,y;
for(x=z;x0;x--)
for(y=148;y0;y--);
}
void write_com(uchar com)//写命令函数
{
P0=com;
RS=0;
RW=0;
E=0;
delay(5);
E=1;
delay(5);
}
void write_date(uchar date)//写数据函数
{
P0=date;
RS=1;
RW=0;
E=0;
delay(5);
E=1;
delay(5);
}
#endif
用C语言编一个数字电子时钟的程序
1.这是用windows api写的程序。所以要求是纯c的话就没有办法了
2.其中定时用了两种方法。一种是用取消息。另一种是延时队列。这里只使用了取消息的方法。延时队列由于我机器上是vc6.0,CreateTimerQueue在本人机器上无法使用,需要新的sdk,所以没有加以验证,但取消息的方式是可行的。
3.稍稍验证了下,基本满足要求。
-------------------------------------------
程序如下:
// DigitalClock.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include windows.h
#include winbase.h
typedef struct _st_time{
int hour;
int min;
int sec;
}ST_TIME;
ST_TIME g_Time; // The struct contain the hour,min and sec.
HANDLE g_hStdout; //
WORD g_cxCenter, g_cyCenter; // Center of the screen.
HANDLE g_DoneEvent; // The program could be over.
BOOL g_ThreadTerminated; // The Thread should be terminated.
#define SECOND_CIRCLE 60
#define MINUTE_CIRCLE 60
#define HOUR_CIRCLE 24
void TimeIncreaseSecond(ST_TIME st)
{
st.sec ++;
if (st.sec = SECOND_CIRCLE)
{
st.sec -= SECOND_CIRCLE;
st.min++;
if (st.min = MINUTE_CIRCLE)
{
st.min -= MINUTE_CIRCLE;
st.hour++;
if (st.hour = HOUR_CIRCLE)
{
st.hour -= HOUR_CIRCLE;
}
}
}
}
void PrintTimeToScreen(HANDLE hStdout, short cxCenter, short cyCenter, ST_TIME st)
{
char buf[64] = {0};
COORD crdPos;
// make it format to output.
sprintf (buf, "%02d:%02d:%02d", st.hour, st.min, st.sec);
crdPos.X = cxCenter - 4;
crdPos.Y = cyCenter;
SetConsoleCursorPosition(hStdout, crdPos);
printf(buf);
}
#ifdef USE_TIMERQUEUE
// if we use the timer queue function.
// Its procdure is in this.
void CALLBACK TimerRoutine (LPVOID lpParam, BOOL TimerOrWaitFired)
{
if (lpParam == NULL)
{
printf ("NULL parameters.\n");
}
else
{
ST_TIME *st = (ST_TIME *)lpParam;
TimeIncreaseSecond(st);
PrintTimeToScreen(g_hStdout, g_cxCenter, g_cyCenter, *st);
}
}
#else
DWORD WINAPI TimerThreadProc(LPVOID lpParam)
{
#define ID_TIMER_SECOND 1
MSG msg;
BOOL ret;
ST_TIME *st = (ST_TIME *)lpParam;
SetTimer(NULL, ID_TIMER_SECOND, 1000, NULL);
PeekMessage(msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
while (!g_ThreadTerminated (ret = GetMessage (msg, NULL, 0, 0)) != 0)
{
if (ret == -1)
{
//process fatal event.
}
else if (msg.message == WM_TIMER)
{
TimeIncreaseSecond(*st);
PrintTimeToScreen(g_hStdout, g_cxCenter, g_cyCenter, *st);
}
else
{
TranslateMessage (msg);
DispatchMessage (msg);
}
}
return 1;
}
#endif
// If the ctrl+break combined key pressed. call this function.
// It set the g_DoneEvent. this terminate the program.
BOOL WINAPI CtrlHandler(DWORD fdwCtrlType)
{
switch (fdwCtrlType)
{
case CTRL_BREAK_EVENT:
// Terminate the program.
printf ("Terminate.\n");
SetEvent(g_DoneEvent);
return TRUE;
default:
return FALSE;
}
}
BOOL InitApplication()
{
// Get the stdin and stdout handle.
HANDLE hStdIn;
hStdIn = GetStdHandle(STD_INPUT_HANDLE);
if (hStdIn == INVALID_HANDLE_VALUE)
return FALSE;
g_hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
// Set the mode, make the input echo.
DWORD fOldMode;
GetConsoleMode(hStdIn, fOldMode);
fOldMode |= ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT;
SetConsoleMode(hStdIn, fOldMode);
// Set the window buffer.
// make a line 40 columns.
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
GetConsoleScreenBufferInfo(g_hStdout, csbiInfo);
csbiInfo.srWindow.Right = 40;
// get the center point.
g_cxCenter = csbiInfo.srWindow.Right / 2;
g_cyCenter = csbiInfo.srWindow.Bottom / 2;
// Set the window.
SetConsoleWindowInfo(g_hStdout, TRUE, csbiInfo.srWindow);
return TRUE;
}
BOOL PrintTheInitalStateAndGetInput(HANDLE hStdout, WORD cxCenter, WORD cyCenter, ST_TIME time)
{
#define GAPS_LEFT_COLON (-2)
#define GAPS_RIGHT_COLON (1)
#define GAPS_LEFT_UNDERLINE_START (-4)
#define GAPS_MIDDLE_UNDERLINE_START (-1)
#define GAPS_RIGHT_UNDERLINE_START (2)
// __:__:__
// So the left ":" center -2
// so the right ":" center + 1
// so the left "_" center - 4;
// so the lfet "_" center - 1;
// so the right "_" center + 2;
COORD crdPos;
crdPos.X = cxCenter + GAPS_LEFT_COLON;
crdPos.Y = cyCenter;
SetConsoleCursorPosition(hStdout, crdPos);
printf (":");
crdPos.X = cxCenter + GAPS_RIGHT_COLON;
SetConsoleCursorPosition(hStdout, crdPos);
printf (":");
crdPos.X = cxCenter + GAPS_LEFT_UNDERLINE_START;
SetConsoleCursorPosition(hStdout, crdPos);
scanf ("%d", time.hour);
crdPos.X = cxCenter + GAPS_MIDDLE_UNDERLINE_START;
SetConsoleCursorPosition(hStdout, crdPos);
scanf ("%d", time.min);
crdPos.X = cxCenter + GAPS_RIGHT_UNDERLINE_START;
SetConsoleCursorPosition(hStdout, crdPos);
scanf ("%d", time.sec);
if (time.hour 0 || time.hour HOUR_CIRCLE ||
time.min 0 || time.min MINUTE_CIRCLE ||
time.sec 0 || time.sec SECOND_CIRCLE)
return FALSE;
return TRUE;
}
int main(int argc, char* argv[])
{
InitApplication();
PrintTheInitalStateAndGetInput(g_hStdout, g_cxCenter, g_cyCenter, g_Time);
// create a event to tell the program to terminate.
g_DoneEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
#ifdef USE_TIMERQUEUE
HANDLE hTimerQueue, hTimer;
hTimerQueue = CreateTimerQueue();
if (!CreateTimerQueueTimer(hTimer,
hTimerQueue, TimerRoutine, g_Time, 1000, 0, 0))
{
printf("CreateTimerQueueTimer failed (%d)\\n", GetLastError());
return 3;
}
#else
// create the thread.
HANDLE hThreadTimer;
DWORD dwThreadId;
g_ThreadTerminated = FALSE;
hThreadTimer = CreateThread(NULL, 0,
TimerThreadProc, g_Time, 0, dwThreadId);
if (hThreadTimer == NULL)
{
}
#endif
SetConsoleCtrlHandler(CtrlHandler, TRUE);
if (WaitForSingleObject(g_DoneEvent, INFINITE) != WAIT_OBJECT_0)
printf("WaitForSingleObject failed (%d)\\n", GetLastError());
#ifdef USE_TIMERQUEUE
if (!DeleteTimerQueue(hTimerQueue))
printf("DeleteTimerQueue failed(%d) \\n", GetLastError());
#else
g_ThreadTerminated = TRUE;
if (WaitForSingleObject(hThreadTimer, INFINITE) != WAIT_OBJECT_0)
printf("WaitForSingleObject failed (%d)\\n", GetLastError());
#endif
return 0;
}
--------------------------------------------
下面是纯c的。
有几个问题:
1.textmode函数在turboc中没有办法使用,不知道是什么问题,而borland c就可以。
2.无论怎么设置,自己的ctrlbreak函数在上述两个环境中都不能被调用,非常遗憾。所以不能够优雅的退出。只能按两次ctrlbreak。
下面是程序。
------------------------------------------
#include stdio.h
#include stdlib.h
#include stdio.h
#include conio.h
#include dos.h
#define ABORT 0
int jump_out_loop = -1;
int jump_out(void)
{
jump_out_loop = 1;
printf("Abort ..\n");
return ABORT;
}
int main(void)
{
struct text_info ti;
int center_x, center_y;
int hour, min, sec;
char str_out[64] = {0};
clrscr();
/*textmode(BW40);*/
/*textmode在turbo c下设置会出问题*/
gettextinfo(ti);
center_x = ti.winright / 2;
center_y = ti.winbottom / 2;
gotoxy(center_x - 4, center_y);
cprintf(" : : ");
gotoxy(center_x - 4, center_y);
cscanf("%d", hour);
gotoxy(center_x - 1, center_y);
cscanf("%d", min);
gotoxy(center_x + 2, center_y);
cscanf("%d", sec);
/* check input valid or not */
{}
setcbrk(1);
ctrlbrk(jump_out);
/*jump_out没有起到作用,实在不好意思.*/
/*
if (getcbrk())
printf("crtl break is on\n");
else
printf("is off\n");
*/
while (1)
{
delay(1000);
sec++;
if (sec = 60)
{
sec -= 60;
min++;
if (min = 60)
{
min -= 60;
hour++;
if (hour = 24)
{
hour -= 24;
}
}
}
sprintf(str_out, "%02d:%02d:%02d", hour, min, sec);
gotoxy(center_x - 4, center_y);
cprintf(str_out);
}
/* getch();*/
return 0;
}