我是靠谱客的博主 碧蓝鸡,最近开发中收集的这篇文章主要介绍基于stc15f2k60s2芯片单片机编程(到00点时继电器亮5秒),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

main.c

#include <stc15f2k60s2.h>
#include <ds1302.h>
#include <intrins.h>
sbit s7=P3^0;
void Timer0Init(void);
unsigned char shan=0;
unsigned char code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned char display[]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
void xianshi();
void main()
{
EA=1;ET0=1;Timer0Init();
P2=0XA0;P0=0X00;P2=0X1F;
DS_init();
while(1)
{
xianshi();
DS_get();
}
}
void xianshi()
{
display[0]=tab[TIME[2]/16];
display[1]=tab[TIME[2]%16];
display[3]=tab[TIME[1]/16];
display[4]=tab[TIME[1]%16];
display[6]=tab[TIME[0]/16];
display[7]=tab[TIME[0]%16];
}
void Timer0Init(void)
//2毫秒@11.0592MHz
{
AUXR |= 0x80;
//定时器时钟1T模式
TMOD &= 0xF0;
//设置定时器模式
TL0 = 0x9A;
//设置定时初值
TH0 = 0xA9;
//设置定时初值
TF0 = 0;
//清除TF0标志
TR0 = 1;
//定时器0开始计时
}
void shuma() interrupt 1
{
unsigned char i;
unsigned int p;
unsigned int tt;
P2=0XE0;
P0=0XFF;
P2=0X1F;
P2=0XC0;
P0=1<<i;
P2=0X1F;
P2=0XE0;
P0=display[i];
P2=0X1F;
i++;
if(i>8)i=0;
p++;
if(p==250)
{
display[2]=display[5]=0XfF;
}
else if(p==500)
{
p=0;
display[2]=display[5]=0XbF;
}
if((TIME[2]==0X00)&(shan==0))
{
tt++;
P2=(P2&0X1F)|0XA0;
P0=0X10;
P2&=0X1F;
if(tt==2500)
{
shan++;
P2=(P2&0X1F)|0XA0;
P0=0X00;
P2&=0X1F;
}
}
}

ds1302.c

#include<ds1302.h>
sbit SCK=P1^7;
sbit SDA=P2^3;
sbit RST = P1^3;
// DS1302??

unsigned char code READ_RTCADDR[7]={0X81,0X83,0X85,0X87,0X89,0X8B,0X8D};
unsigned char code WRITE_RTCADDR[7]={0x80,0X82,0X84,0X86,0X88,0X8C,0X8E};
char TIME[7]={0x50,0x59,0x23};
void Write_Ds1302(unsigned
char temp)
{
unsigned char i;
for (i=0;i<8;i++)
{
SCK=0;
SDA=temp&0x01;
temp>>=1;
SCK=1;
}
}
void Write_Ds1302_Byte( unsigned char address,unsigned char dat )
{
RST=0;	_nop_();
SCK=0;	_nop_();
RST=1;
_nop_();
Write_Ds1302(address);
Write_Ds1302(dat);
RST=0;
}
unsigned char Read_Ds1302_Byte ( unsigned char address )
{
unsigned char i,temp=0x00;
RST=0;	_nop_();
SCK=0;	_nop_();
RST=1;	_nop_();
Write_Ds1302(address);
for (i=0;i<8;i++)
{
SCK=0;
temp>>=1;
if(SDA)
temp|=0x80;
SCK=1;
}
RST=0;	_nop_();
SCK=0;	_nop_();
SCK=1;	_nop_();
SDA=0;	_nop_();
SDA=1;	_nop_();
return (temp);
}
void DS_init()
{
unsigned char n;
Write_Ds1302_Byte(0x8e,0x00);//关闭写保护
for(n=0;n<3;n++)
Write_Ds1302_Byte(WRITE_RTCADDR[n],TIME[n]);
Write_Ds1302_Byte(0x8e,0x80);
}
void DS_get()
{
unsigned char n;
for(n=0;n<3;n++)
TIME[n]=Read_Ds1302_Byte (READ_RTCADDR[n]);
}

ds1302.h

#ifndef __DS1302_H_
#define __DS1302_H_
#include<stc15f2k60s2.h>
#include<intrins.h>
void Write_Ds1302(unsigned
char temp) ;
void Write_Ds1302_Byte( unsigned char address,unsigned char dat ) ;
unsigned char Read_Ds1302_Byte ( unsigned char address );
void DS_init();
void DS_get();
extern char TIME[7];
//---??????--//
#endif

最后

以上就是碧蓝鸡为你收集整理的基于stc15f2k60s2芯片单片机编程(到00点时继电器亮5秒)的全部内容,希望文章能够帮你解决基于stc15f2k60s2芯片单片机编程(到00点时继电器亮5秒)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部