可以帮我设计一个c语言闹钟程序么,我课程设计用的。。。最好在每个重要的语句后面附上说明
//T0 中断函数
void Timer0() interrupt 1
{
uchar i;
TH0=(65536-50000)/256; //恢复初值
TL0=(65536-50000)%256;
if (laba==1)
P1.0=~P1.0;//闹钟时间到了 就让P1.0输出 P1.0接喇叭
if(++Count!=2) return;
Count=0;
Digits_of_6DSY[0]++; //0.1s位累加
for(i=0;i<=5;i++) //进位处理
{
if(Digits_of_6DSY[i]==10)
{
Digits_of_6DSY[i]=0;
shijian++;
if(i!=5) Digits_of_6DSY[i+1]++; //如果0~4 位则分别向高一位进位
}
else break; //若某低位没有进位,怎循环提前结束
}
}
// 主程序
void main()
{
uchar i,j;
long shijian=0;
laba=0;
Count=0;
TMOD=0x01; //计数器T0方式1
TH0=(65536-50000)/256; //50ms定时
TL0=(65536-50000)%256;
IE=0x82;
TR0=1; //启动T0
while(1)
{
if (shijian==???)//这里设定闹钟时间多久 按秒计算 如果你要设定小时分什么的自己在改下
laba=1;
}
单片机:闹钟程序
#include"reg52.h"
#define uchar unsigned char
#define uint unsigned int
sbit wela=P2^7;
sbit dula=P2^6;
sbit s1=P3^4;//模式的选择
sbit s2=P3^5;//调高
sbit s3=P3^6;//调低
sbit s4=P3^7;//选择是否开启闹铃
sbit beep=P2^3;
uchar tt,num,t1,settime,bi;
uchar flag; //至位
int fen,miao,fen1=0,miao1=10;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uchar dis[]={0x40,0x40,0x40,0x40,0x40,0x40};
void timedisplay();
void display();
void keydisplay();
void setdisplay();
void display1();
void delay(uchar z)
{
uchar i,j;
for(i=125;i>0;i--)
for(j=z;j>0;j--);
}
void warm()
{
if(bi==1)
{
beep=0;
TR1=1;
}
}
void init()
{
TMOD=0x11;
TH0=(65536-50000)/256;
TL0=(65536-50000)%6;
TH1=(65536-50000)/256;
TL1=(65536-50000)%6;
EA=1;
ET0=1;
ET1=1;
TR0=1;
}
void keyscan()
{
if(s4==0)
{
delay(5);
{
if (s4==0)
{
while(!s4);
bi++;
if(bi==2)
{
bi=0;
}
}
}
}
if(s1==0)
{
delay(5);
if(s1==0)
{
while(!s1);
num++;
if(num==3)
{
num=0;
}
}
}
if(num!=0)
{
if(s2==0)
{
delay(5);
if(s2==0)
{
if(num==1)
{
miao1++;
if(miao1==60)
miao1=0;
}
if(num==2)
{
fen1++;
if(fen1==60)
fen1=0;
}
while(!s2)
{
keydisplay();
}
}
}
if(s3==0)
{
delay(5);
if(s3==0)
{
if(num==1)
{
miao1--;
if(miao1==-1)
miao1=59;
}
if(num==2)
{
fen1--;
if(fen1==-1)
fen1=59;
}
while(!s3)
{
keydisplay();
}
}
}
}
}
void setdisplay() //设置模式下的显示
{
dis[0]=0x40;
dis[1]=0x40;
dis[2]=table[0];
dis[3]=table[num];
dis[4]=0x40;
dis[5]=0x40;
display();
}
void keydisplay() //通过按键加减时的显示
{
if((flag==1)&&(num==2))
{
dis[0]=table[fen1/10]&0x00;
dis[1]=table[fen1]&0x00;
}
else
{
dis[0]=table[fen1/10];
dis[1]=table[fen1];
}
dis[2]=0x40;
if((flag==1)&&(num==1))
{
dis[3]=table[miao1/10]&0x00;
dis[4]=table[miao1]&0x00;
}
else
{
dis[3]=table[miao1/10];
dis[4]=table[miao1];
}
dis[5]=0x40;
display();
}
void display() //基本的动态显示
{
dula=1; //the one
P0=dis[0];
dula=0;
wela=1;
P0=0xfe;
wela=0;
delay(1);
dula=1; //the two
P0=dis[1];
dula=0;
wela=1;
P0=0xfd;
wela=0;
delay(1);
dula=1; //the three
P0=dis[2];
dula=0;
wela=1;
P0=0xfb;
wela=0;
delay(1);
dula=1; //the four
P0=dis[3];
dula=0;
wela=1;
P0=0xf7;
wela=0;
delay(1);
dula=1; //the five
P0=dis[4];
dula=0;
wela=1;
P0=0xef;
wela=0;
delay(1);
dula=1; //the six
P0=dis[5];
dula=0;
wela=1;
P0=0xdf;
wela=0;
delay(1);
}
void timedisplay()
{
dis[0]=table[fen/10];
dis[1]=table[fen];
dis[2]=0x40;
dis[3]=table[miao/10];
dis[4]=table[miao];
dis[5]=0x40;
display();
}
void main()
{
init();
while(1)
{
keyscan();
if(num!=0)
{
keydisplay();
}
if(num==0)
{
timedisplay();
}
}
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%6;
tt++;
if(tt==20)
{
tt=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
}
}
}
if(!num&&fen1==fen&&miao1==miao)
{
warm();
}
settime++;
if(settime==10)
{
flag=1;
}
if(settime==20)
{
settime=0;
flag=0;
}
}
void timer1() interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65536-50000)%6;
t1++;
if(t1==60)
{
t1=0;
beep=1;
}
}
JAVA 闹钟程序
//OK 写好了...怕你不懂 帮你加了注释
package 娱乐;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import .MalformedURLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;
public class Alarm extends JFrame implements Runnable {
JLabel ri ,shi, fen, miao, dangqian;
JButton queding, dakai;
JTextField music,RI, SHI, FEN, MIAO;
int h=0,f=0,m=0,r=0;
boolean fo=false;
public AudioClip soumd1;
public Alarm() {
Container c = getContentPane();
c.setLayout(new GridLayout(3, 1));
JPanel jp = new JPanel();
dangqian = new JLabel();
jp.add(dangqian);
c.add(jp);
JPanel jp1 = new JPanel();
music = new JTextField(20);
dakai = new JButton("选择闹铃音乐");
jp1.add(music);
jp1.add(dakai);
c.add(jp1);
ri = new JLabel("日");
RI = new JTextField(4);
shi = new JLabel("时");
SHI = new JTextField(4);
fen = new JLabel("分");
FEN = new JTextField(4);
miao = new JLabel("秒");
MIAO = new JTextField(4);
JPanel jp2 = new JPanel();
jp2.add(ri);
jp2.add(RI);
jp2.add(shi);
jp2.add(SHI);
jp2.add(fen);
jp2.add(FEN);
jp2.add(miao);
jp2.add(MIAO);
queding = new JButton("确定");
jp2.add(queding);
c.add(jp2);
setSize(400, 130);
setVisible(true);
dakai.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JFileChooser fileChooser = new JFileChooser(); // 实例化文件选择器
fileChooser
.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); // 设置文件选择模式,此处为文件和目录均可
fileChooser.setCurrentDirectory(new File(".")); // 设置文件选择器当前目录
fileChooser
.setFileFilter(new javax.swing.filechooser.FileFilter() {
public boolean ept(File file) { // 可接受的文件类型
String name = file.getName().toLowerCase();
return name.endsWith(".wav")
|| name.endsWith(".au")
|| file.isDirectory();
}
public String getDescription() { // 文件描述
return "音乐文件(*.wav,*.au)";
}
});
if (fileChooser.showOpenDialog(Alarm.this) == JFileChooser.APPROVE_OPTION) { // 弹出文件选择器,并判断是否点击了打开按钮
String fileName = fileChooser.getSelectedFile().getAbsolutePath(); // 得到选择文件或目录的绝对路径
music.setText(fileName);
}
}
});
queding.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
if(queding.getText().equals("确定")){
try{
r=Integer.parseInt(RI.getText());
h=Integer.parseInt(SHI.getText());
f=Integer.parseInt(FEN.getText());
m=Integer.parseInt(MIAO.getText());
if(1<=h&&h<=31&&0<=h&&h<=23&&0<=f&&f<=59&&0<=m&&m<=59)
{
fo=true;
}
else
JOptionPane.showMessageDialog(null, "输入时间错误");
}
catch(Exception e){
JOptionPane.showMessageDialog(null, "请输入正确的时间");
}
}
else
{
fo=false;
RI.setEditable(true);
SHI.setEditable(true);
FEN.setEditable(true);
MIAO.setEditable(true);
queding.setText("确定");
();
}
}
});
}
public static void main(String agrs[]) {
Alarm s = new Alarm();
Thread t1 = new Thread(s);
t1.start();
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void run() {
while (true) {
Date now = new Date();
dangqian.setText("当前时间: " + now.toString());
if(fo)
{
RI.setEditable(false);
SHI.setEditable(false);
FEN.setEditable(false);
MIAO.setEditable(false);
queding.setText("关闭");
SimpleDateFormat ri=new SimpleDateFormat("dd"); //封装 为了获取日期
SimpleDateFormat shi=new SimpleDateFormat("kk"); //封装 为了获取小时
SimpleDateFormat fen=new SimpleDateFormat("mm"); //封装 为了获取分钟
SimpleDateFormat miao=new SimpleDateFormat("ss"); //封装 为了获取秒钟
int riqi=Integer.parseInt(ri.format(now)); //获取日期
int shizhong=Integer.parseInt(shi.format(now)); //获取小时
int fenzhong=Integer.parseInt(fen.format(now)); //获取分钟
int miaozhong=Integer.parseInt(miao.format(now)); //获取秒钟
if(riqi==r&&shizhong==h&&fenzhong==f&&miaozhong==m) //判断条件
{
try {
soumd1=Applet.newAudioClip(new File(music.getText()).toURL()); //播放音乐
soumd1.loop(); //我设置的是循环播放..这样不起床都不行..
fo=false;
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
}
}
}
}