基于stc15f2k60s2芯片单片机编程(可调时钟)
生活随笔
收集整理的這篇文章主要介紹了
基于stc15f2k60s2芯片单片机编程(可调时钟)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Ds1302可調(diào)時鐘,按下S7調(diào)節(jié)時分秒,按下S6加1,按下S5減1
mian.c
#include <stc15f2k60s2.h> #include <intrins.h> #include "ds1302.h"void Timer0Init(void); void xianshi(); void Delay10ms(); void anjian();unsigned char display[]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}; unsigned char code shuzu[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; unsigned char wei;sbit s7=P3^0; sbit s6=P3^1; sbit s5=P3^2;void main() {Timer0Init();EA=1;ET0=1;P2=(P2&0X1F)|0XA0;P0=0X00;P2&=0X1F;P2=(P2&0X1F)|0X80;P0=0XFF;P2&=0X1F; write_ds();while(1){anjian();xianshi();}}void xianshi() {if(wei==0){read_ds();display[0]=shuzu[time[2]/16];display[1]=shuzu[time[2]%16];// display[2]=shuzu[3];display[3]=shuzu[time[1]/16];display[4]=shuzu[time[1]%16];// display[5]=shuzu[6];display[6]=shuzu[time[0]/16];display[7]=shuzu[time[0]%16];// if((time[0]%16)%2==0) // { // display[2]=display[5]=0xbf; // } // else // { // display[2]=display[5]=0xff; // } }}void Timer0Init(void) //2毫秒@11.0592MHz {AUXR |= 0x80; //定時器時鐘1T模式TMOD &= 0xF0; //設(shè)置定時器模式TL0 = 0x9A; //設(shè)置定時初值TH0 = 0xA9; //設(shè)置定時初值TF0 = 0; //清除TF0標(biāo)志TR0 = 1; //定時器0開始計時 } void timer0() interrupt 1 {unsigned char i;unsigned int shan;P2=(P2&0X1F)|0XE0;P0=0XFF;P2&=0X1F;P2=(P2&0X1F)|0XC0;P0=1<<i;P2&=0X1F;P2=(P2&0X1F)|0XE0;P0=display[i];P2&=0X1F;i++;if(i>=8)i=0;shan++;if(shan==500){display[2]=display[5]=0xbf;if(wei==1){display[0]=display[1]=0xff;}if(wei==2){display[3]=display[4]=0xff;}if(wei==3){display[6]=display[7]=0xff;} }else if(shan==1000){shan=0;display[2]=display[5]=0xff;display[0]=shuzu[time[2]/16];display[1]=shuzu[time[2]%16];// display[2]=shuzu[3];display[3]=shuzu[time[1]/16];display[4]=shuzu[time[1]%16];// display[5]=shuzu[6];display[6]=shuzu[time[0]/16];display[7]=shuzu[time[0]%16];}}void anjian() {if(s7==0){Delay10ms();if(s7==0){wei++;if(wei>=4){write_ds();wei=0;}}while(!s7); }if(s6==0){Delay10ms();if(s6==0){if(wei==1){time[2]++; if(time[2]>=0x24)time[2]=0x00;}if(wei==2){time[1]++; if(time[1]>0x59)time[1]=0x00;}if(wei==3){time[0]++; if(time[0]>0x59)time[0]=0x00;}if(time[2]%16>9)time[2]=time[2]+6;if(time[1]%16>9)time[1]=time[1]+6;if(time[0]%16>9)time[0]=time[0]+6;}while(!s6); }if(s5==0){Delay10ms();if(s5==0){if(wei==1){time[2]--; if(time[2]<0x00)time[2]=0x23;}if(wei==2){time[1]--; if(time[1]<0x00)time[1]=0x59;}if(wei==3){time[0]--; if(time[0]<0x00)time[0]=0x59;}if(time[2]%16>9)time[2]=time[2]-6;if(time[1]%16>9)time[1]=time[2]-6;if(time[0]%16>9)time[0]=time[2]-6;}while(!s5); }}void Delay10ms() //@11.0592MHz {unsigned char i, j;i = 108;j = 145;do{while (--j);} while (--i); }ds1302.c
/*程序說明: DS1302驅(qū)動程序軟件環(huán)境: Keil uVision 4.10 硬件環(huán)境: CT107單片機綜合實訓(xùn)平臺 8051,12MHz日 期: 2011-8-9 */#include <stc15f2k60s2.h> #include <intrins.h>sbit SCK=P1^7; sbit SDA=P2^3; sbit RST = P1^3; // DS1302復(fù)位 char time[]={0x59,0x50,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 write_ds() {Write_Ds1302_Byte(0xbe,0x00);Write_Ds1302_Byte(0x80,time[0]);Write_Ds1302_Byte(0x82,time[1]);Write_Ds1302_Byte(0x84,time[2]);Write_Ds1302_Byte(0xbe,0x80); }void read_ds() { time[0]=Read_Ds1302_Byte(0x81); time[1]=Read_Ds1302_Byte(0x83); time[2]=Read_Ds1302_Byte(0x85); }ds1302.h
#ifndef __DS1302_H #define __DS1302_Hvoid Write_Ds1302(unsigned char temp); void Write_Ds1302_Byte( unsigned char address,unsigned char dat ); unsigned char Read_Ds1302_Byte( unsigned char address );void write_ds(); void read_ds(); extern char time[];#endif總結(jié)
以上是生活随笔為你收集整理的基于stc15f2k60s2芯片单片机编程(可调时钟)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 前端学习(3096):vue+eleme
- 下一篇: 前端学习(3244):react的生命周