⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clock.c

📁 一个简单的单片机教程
💻 C
字号:
#include "reg51.h"
#include"intrins.h"
#define uchar unsigned char
#define uint unsigned int
#define  nop  _nop_()       						/* 定义空操作指令 */
sbit en		=P1^7;							/*DTLED-6显示芯片使能端*/	
sbit dout	=P1^6;							/*DTLED-6显示芯片数据读入端*/
sbit clk	=P1^3;							/*DTLED-6显示芯片/时钟芯片DS1302时钟输入端*/
uchar MScond=0;                                                         //ms
uchar Scond=0;                                                          //s
uchar Minute=0;                                                         //min
uchar Hour=0;                                                           //h
uchar dispbuf[9]={3,0,0,0,0,0,0,1,0};					/*显示值存放阵列*/
void delay (uint value)							/*延时副程式*/
{
      while (value!=0) value--;						/*10US延时*/
}
void tsled(void)							/*串行发送DTLED-6显示副程式*/	
{   
	char ts0,i,j;
	clk=0;
	en=0;
	delay(10);
	for(j=0;j<9;j++)
  	{	ts0=dispbuf[j];
		for(i=0;i<4;i++)					/*发小数点码8,4,2,1*/
		{
 		        clk=1;
		        ts0=ts0<<1;	
		        if(AC==1) 
		        dout=1;
		        else dout=0;
		        clk=0;
		        nop;
		        delay(3);
		}
   	} 	
	en=1;
}									/*返回主程式*/
void BCD(void)
{
	dispbuf[1]=Scond%10;
	dispbuf[2]=Scond/10;
	dispbuf[3]=Minute%10;
	dispbuf[4]=Minute/10;
	dispbuf[5]=Hour%10;
	dispbuf[6]=Hour/10;
 }
/********************************************************/
void timer1(void) interrupt 3 using 2
{
	TH1=0xb1;								/*定时器定时20ms*/	
	TL1=0xeb;
	MScond=MScond+1;
	if(MScond==50)								/*秒处理1s=50*20*/		
	{
        	MScond=0;
	  	Scond=Scond+1;
	  	if(Scond==60)							/*分处理*/
	    	{
	       		Scond=0;
		   	Minute=Minute+1;
		   	if(Minute==60)						/*时处理*/
		      	{
		        	Minute=0;
			    	Hour=Hour+1;
			    	if(Hour==24)Hour=0;
			}
	      	}
      	}
}	  
void main(void)
{
	uchar TMP=0;
	tsled();
	P0=0xff;					/*P0初始化*/
  	TMOD=0x10;					/*定时器工作方式*/
  	TH1=0xb1;
  	TL1=0xeb;
  	TR1=1;						/*允许T1工作*/
  	ET1=1;						/*允许T1中断*/
  	EA=1;						/*允许全局中断*/
  	while(1)
  	{
  		TMP=Scond;
  		while(TMP==Scond);
  		BCD();
  		tsled();
  	}
 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -