这届题中我觉得重要的就是eeprom的写入的时候是大于两个字节的,所以要分成两个来写,还有一个就是对于S11和S10的使用要特别注意约束条件。

题目:




代码:

main.c

#include "reg52.h"
#include"intrins.h"
#include"ds1302.h"
#include"iic.h"
sfr AUXR=0x8e;
sfr P4=0xC0;
int code write_add[]={0x80,0x82,0x84};
int code read_add[]={0x81,0x83,0x85};
int time[]={0x55,0x59,0x23};
int judge_time[]={0x00,0x00,0x00};
int code xianshi[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf};
int temp_h,temp_m,temp_s;
int time_mode=0;
int v_mode=0;
int f_mode=0;
int count_f;int num_f;
int num_t;//周期
int num_v;
int smg_mode=0;
int shanshuo=0;//闪烁
int v_h=2000;
int v_l=1000;
int judge_mode;
int judge_temp=0;
sbit R1=P3^0;
sbit R2=P3^1;
sbit R3=P3^2;
sbit R4=P3^3;
sbit C4=P3^4;
sbit C3=P3^5;
sbit C2=P4^2;
sbit C1=P4^4;
void choose_573(int i)
{switch(i){case(0):P2=(P2&0x1f)|0x00;break;case(4):P2=(P2&0x1f)|0x80;break;case(5):P2=(P2&0x1f)|0xa0;break;case(6):P2=(P2&0x1f)|0xc0;break;case(7):P2=(P2&0x1f)|0xe0;break;}
}
void init_system()
{choose_573(4);P0=0xff;choose_573(5);P0=0x00;choose_573(0);
}
//====================================时间
void write_time()
{int i;Write_Ds1302_Byte(0x8e,0x00);for(i=0;i<3;i++){Write_Ds1302_Byte(write_add[i],time[i]);}Write_Ds1302_Byte(0x8e,0x80);
}
void read_time()
{int i;for(i=0;i<3;i++){time[i]=Read_Ds1302_Byte(read_add[i]);}
}
//====================================
//====================================555 and v
void Timer0Init(void)       //100微秒@11.0592MHz
{AUXR |= 0x80;     //定时器时钟1T模式TMOD = 0x04;        //设置定时器模式TL0 = 0xff;       //设置定时初始值TH0 = 0xff;       //设置定时初始值TF0 = 0;      //清除TF0标志TR0 = 1;      //定时器0开始计时ET0=1;EA=1;
}
void time0_service() interrupt 1
{count_f++;
}
void V()
{num_v=get_v();
}
//====================================
//====================================中断
void Timer1Init(void)       //5毫秒@11.0592MHz
{AUXR |= 0x40;     //定时器时钟1T模式TMOD &= 0x0F;       //设置定时器模式TL1 = 0x00;       //设置定时初始值TH1 = 0x28;       //设置定时初始值TF1 = 0;      //清除TF1标志TR1 = 1;      //定时器1开始计时ET1=1;EA=1;
}
int count_1;int count_2;
void time1_server() interrupt 3
{count_1++;count_2++;if(count_1>=100){count_1=0;num_f=count_f;count_f=0;
//      V();num_t=1000000/num_f;}if(count_2>=200){count_2=0;if(shanshuo==0){shanshuo=1;}else{shanshuo=0;}}
}
//====================================
//====================================键盘
void Delay10ms()        //@11.0592MHz
{unsigned char i, j;i = 108;j = 145;do{while (--j);} while (--i);
}
void key_board()
{R1=0;R2=R3=R4=1;C1=C2=C3=C4=1;//S7if(C1==0){Delay10ms();if(C1==0){smg_mode=0;time_mode=0;}while(!C1);}//S11 加if(C2==0){Delay10ms();if(C2==0){if(smg_mode==0)//time{if(time_mode==1){temp_h=(time[2]/16)*10+time[2]%16;temp_h++;if(temp_h>=24){temp_h=0;}time[2]=((temp_h/10)*6)+temp_h;write_time();}if(time_mode==2){temp_m=(time[1]/16)*10+time[1]%16;temp_m++;if(temp_m>=60){temp_m=0;}time[1]=((temp_m/10)*6)+temp_m;write_time();}if(time_mode==3){temp_s=(time[0]/16)*10+time[0]%16;temp_s++;if(temp_s>=60){temp_s=0;}time[0]=((temp_s/10)*6)+temp_s;write_time();}}if(smg_mode==1){if(v_mode==1){v_h=v_h+500;if(v_h>9500){v_h=9500;}}if(v_mode==2){v_l=v_l+500;if(v_l>v_h){v_l=v_l-500;}}}}while(!C2); }R2=0;R1=R3=R4=1;C1=C2=C3=C4=1;//S6if(C1==0){Delay10ms();if(C1==0){smg_mode=1;v_mode=0;}while(!C1);}//S10减if(C2==0){Delay10ms();if(C2==0){if(smg_mode==0)//time{if(time_mode==1){temp_h=(time[2]/16)*10+time[2]%16;temp_h--;if(temp_h<0){temp_h=23;}time[2]=((temp_h/10)*6)+temp_h;write_time();}if(time_mode==2){temp_m=(time[1]/16)*10+time[1]%16;temp_m--;if(temp_m<0){temp_m=59;}time[1]=((temp_m/10)*6)+temp_m;write_time();}if(time_mode==3){temp_s=(time[0]/16)*10+time[0]%16;temp_s--;if(temp_s<0){temp_s=59;}time[0]=((temp_s/10)*6)+temp_s;write_time();}}if(smg_mode==1){if(v_mode==1){v_h=v_h-500;if(v_h<v_l){v_h=v_h+500;}}if(v_mode==2){v_l=v_l-500;if(v_l<0){v_l=0;}}}}while(!C2);}R3=0;R1=R2=R4=1;C1=C2=C3=C4=1;//S5if(C1==0){Delay10ms();if(C1==0){smg_mode=2;f_mode=0;}while(!C1);}//S9 查询if(C2==0){Delay10ms();if(C2==0){smg_mode=3;judge_temp=0;}while(!C2);}R4=0;R1=R2=R3=1;C1=C2=C3=C4=1;//S4if(C1==0){Delay10ms();if(C1==0){if(smg_mode==0){time_mode++;if(time_mode>=4){time_mode=0;}}if(smg_mode==1){v_mode++;if(v_mode>=3){v_mode=0;write_EEPROM(0x01,v_h);Delay10ms();write_EEPROM(0x02,v_h>>8);Delay10ms();write_EEPROM(0x03,v_l);Delay10ms();write_EEPROM(0x04,v_l>>8);}}if(smg_mode==2){f_mode++;if(f_mode>=2){f_mode=0;}}if(smg_mode==3){judge_temp++;if(judge_temp>=2){judge_temp=0;}}}while(!C1);}
}
//====================================
//====================================数码管
void Delay400us()       //@11.0592MHz
{unsigned char i, j;i = 5;j = 74;do{while (--j);} while (--i);
}
void SMG(int wei,int dat)
{choose_573(6);P0=0x80>>(wei-1);choose_573(7);P0=xianshi[dat];choose_573(0);Delay400us();choose_573(7);P0=xianshi[10];choose_573(0);
}
void smg_display()
{if(smg_mode==0)//时间显示{if(time_mode==3&&shanshuo==1){SMG(1,10);SMG(2,10);}else{SMG(1,time[0]%16);SMG(2,time[0]/16);}SMG(3,11);if(time_mode==2&&shanshuo==1){SMG(4,10);SMG(5,10);}else{SMG(4,time[1]%16);SMG(5,time[1]/16);}SMG(6,11);if(time_mode==1&&shanshuo==1){SMG(7,10);SMG(8,10);}else{SMG(7,time[2]%16);SMG(8,time[2]/16);}        }if(smg_mode==1){if(v_mode==0){SMG(1,num_v%10);SMG(2,(num_v%100)/10);SMG(3,(num_v%1000)/100);SMG(4,(num_v%10000)/1000);SMG(5,10);SMG(6,11);SMG(7,smg_mode);SMG(8,11);}if(v_mode==1&&shanshuo==1){SMG(1,v_l%10);SMG(2,(v_l%100)/10);SMG(3,(v_l%1000)/100);SMG(4,(v_l%10000)/1000);SMG(5,10);SMG(6,10);SMG(7,10);SMG(8,10);}else if(v_mode==2&&shanshuo==1){SMG(1,10);SMG(2,10);SMG(3,10);SMG(4,10);SMG(5,v_h%10);SMG(6,(v_h%100)/10);SMG(7,(v_h%1000)/100);SMG(8,(v_h%10000)/1000);}else if((v_mode==1||v_mode==2)&&shanshuo==0){SMG(1,v_l%10);SMG(2,(v_l%100)/10);SMG(3,(v_l%1000)/100);SMG(4,(v_l%10000)/1000);SMG(5,v_h%10);SMG(6,(v_h%100)/10);SMG(7,(v_h%1000)/100);SMG(8,(v_h%10000)/1000);}}if(smg_mode==2){if(f_mode==0){SMG(1,num_f%10);SMG(2,(num_f%100)/10);SMG(3,(num_f%1000)/100);SMG(4,(num_f%10000)/1000);SMG(5,(num_f%100000)/10000);SMG(6,11);SMG(7,smg_mode);SMG(8,11);}if(f_mode==1){SMG(1,num_t%10);SMG(2,(num_t%100)/10);SMG(3,(num_t%1000)/100);SMG(4,(num_t%10000)/1000);SMG(5,(num_t%100000)/10000);SMG(6,11);SMG(7,smg_mode);SMG(8,11);}}if(smg_mode==3){if(judge_temp==0){SMG(1,judge_mode);SMG(2,0);SMG(3,10);SMG(4,10);SMG(5,10);SMG(6,10);SMG(7,10);SMG(8,10);}if(judge_temp==1){SMG(1,judge_time[0]%16);SMG(2,judge_time[0]/16);SMG(3,11);SMG(4,judge_time[1]%16);SMG(5,judge_time[1]/16);SMG(6,11);SMG(7,judge_time[2]%16);SMG(8,judge_time[2]/16);}}
}
//====================================
//====================================判断
void  judge_work()
{//大于if(num_v==v_h){//      Delay10ms();
//      if(num_v<v_h){int i;judge_mode=1;for(i=0;i<3;i++){judge_time[i]=time[i];}}}//小于if(num_v==v_l&&num_v<v_h){Delay10ms();if(num_v>=v_l){int i;judge_mode=0;for(i=0;i<3;i++){judge_time[i]=time[i];}}}
}
//====================================
//====================================eeprom_read
void eeprom_read()
{v_h=read_EEPROM(0x02);v_h=(v_h<<8)|read_EEPROM(0x01);v_l=read_EEPROM(0x04);v_l=(v_l<<8)|read_EEPROM(0x03);
}
//====================================
void main()
{init_system();write_time();Timer0Init();Timer1Init();eeprom_read();while(1){read_time();key_board();smg_display();V();judge_work();}
}

iic.c

/*程序说明: IIC总线驱动程序软件环境: Keil uVision 4.10 硬件环境: CT107单片机综合实训平台 8051,12MHz日    期: 2011-8-9
*/#include "reg52.h"
#include "intrins.h"#define DELAY_TIME 5#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1//总线引脚定义
sbit SDA = P2^1;  /* 数据线 */
sbit SCL = P2^0;  /* 时钟线 */void IIC_Delay(unsigned char i)
{do{_nop_();}while(i--);
}
//总线启动条件
void IIC_Start(void)
{SDA = 1;SCL = 1;IIC_Delay(DELAY_TIME);SDA = 0;IIC_Delay(DELAY_TIME);SCL = 0;
}//总线停止条件
void IIC_Stop(void)
{SDA = 0;SCL = 1;IIC_Delay(DELAY_TIME);SDA = 1;IIC_Delay(DELAY_TIME);
}//发送应答
void IIC_SendAck(bit ackbit)
{SCL = 0;SDA = ackbit;                    // 0:应答,1:非应答IIC_Delay(DELAY_TIME);SCL = 1;IIC_Delay(DELAY_TIME);SCL = 0; SDA = 1;IIC_Delay(DELAY_TIME);
}//等待应答
bit IIC_WaitAck(void)
{bit ackbit;SCL  = 1;IIC_Delay(DELAY_TIME);ackbit = SDA;SCL = 0;IIC_Delay(DELAY_TIME);return ackbit;
}//通过I2C总线发送数据
void IIC_SendByte(unsigned char byt)
{unsigned char i;for(i=0; i<8; i++){SCL  = 0;IIC_Delay(DELAY_TIME);if(byt & 0x80) SDA  = 1;else SDA  = 0;IIC_Delay(DELAY_TIME);SCL = 1;byt <<= 1;IIC_Delay(DELAY_TIME);}SCL  = 0;
}//从I2C总线上接收数据
unsigned char IIC_RecByte(void)
{unsigned char i, da;for(i=0; i<8; i++){   SCL = 1;IIC_Delay(DELAY_TIME);da <<= 1;if(SDA) da |= 1;SCL = 0;IIC_Delay(DELAY_TIME);}return da;
}
int get_v()
{int temp;IIC_Start();IIC_SendByte(0x90);IIC_WaitAck();IIC_SendByte(0x03);IIC_WaitAck();IIC_Stop();IIC_Start();IIC_SendByte(0x91);IIC_WaitAck();temp=IIC_RecByte();IIC_SendAck(1);IIC_Stop();temp=temp*1.96+0.5;temp=temp*10;return temp;
}
void write_EEPROM(int add,int dat)
{IIC_Start(); IIC_SendByte(0xa0);IIC_WaitAck();IIC_SendByte(add);IIC_WaitAck();IIC_SendByte(dat);IIC_WaitAck();IIC_Stop();
}
int read_EEPROM(int add)
{int temp;IIC_Start();IIC_SendByte(0xa0);IIC_WaitAck();IIC_SendByte(add);IIC_WaitAck();IIC_Start();IIC_SendByte(0xa1);IIC_WaitAck();temp=IIC_RecByte();IIC_SendAck(1);IIC_Stop();return temp;
}

iic.h

#ifndef _IIC_H
#define _IIC_Hvoid IIC_Start(void);
void IIC_Stop(void);
bit IIC_WaitAck(void);
void IIC_SendAck(bit ackbit);
void IIC_SendByte(unsigned char byt);
unsigned char IIC_RecByte(void);
int get_v();
void write_EEPROM(int add,int dat);
int read_EEPROM(int add);
#endif

ds1302.c

/*程序说明: DS1302驱动程序软件环境: Keil uVision 4.10 硬件环境: CT107单片机综合实训平台 8051,12MHz日    期: 2011-8-9
*/#include <reg52.h>
#include <intrins.h>sbit SCK=P1^7;
sbit SDA=P2^3;
sbit RST = P1^3;   // DS1302复位                                             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);
}

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 );
#endif

蓝桥杯 单片机 决赛 第7届 电压、频率采集设备相关推荐

  1. 蓝桥杯单片机第十四届省赛题目和程序答案

    目录 1.前言 2.题目 3.程序架构 3.1 display.c 3.2 ds1302.c 3.3 iic.c 3.4 onewire.c 3.5 main.c 主函数文件 3.6 环境配置 4. ...

  2. 蓝桥杯—单片机第十四届底层驱动函数修改

    本文总结了关于14届蓝桥杯单片机底层驱动文件相较13届的变化所需作出的改变,总体区别不大,欢迎批评指正. 1.不再提供.h文件,故要牢记头文件书写格式: 与引用格式: 添加步骤: 2.Onewire. ...

  3. 蓝桥杯单片机比赛学习:11、频率测量的基本原理

    在蓝桥杯单片机比赛中,频率测量一次都没考过,但是也不排除不考的可能.频率测量相对前面的几个模块来说相对简单,只需要两个定时器就可以实现. 频率测量的基本原理 产生脉冲的主要元件是555定时器.555定 ...

  4. 【蓝桥杯单片机组第十届决赛】— 客观试题

    目录 第1题 第2题 第3题 第4题 第5题 第6题 第7题 第8题 第9题

  5. 【蓝桥杯单片机】第十三届蓝桥杯单片机省赛客观题及其题解

        本次的客观题小编觉得还是比较简单的,涉及到单片机组成结构知识的题目直接在官方给的参看文档里面,仔细的找一找就能够找到了. 题目及解析     通过查找芯片手册小编得知IAP15F2K61S2单 ...

  6. 蓝桥杯单片机决赛(国赛)第十一届题目加代码

    一. 前言 马上就要决赛了,总是感觉准备的不是很充足,听说拿到省一的小伙伴有的都保研了,可怜那,学校不好,都没有保研名额. 下面进入正题 二.题目 pdf版链接 链接:https://pan.baid ...

  7. 蓝桥杯单片机练习_第九届彩灯控制器

    一.题目要求 二.程序源码 1 #include "stc15.h" 2 #include "iic.h" 3 #include <stdio.h> ...

  8. 【蓝桥杯单片机组第六届省赛】— “温度记录器”设计任务书

    1.试题 (1)功能简述   设备按照用户通过按键设定的时间间隔自动采集并存储温度数据,并具有采集完成提醒.数码管显示等功能,系统硬件部分主要由按键电路.电源供电电路. RTC 时钟.传感器电路和显示 ...

  9. 蓝桥杯单片机——用NE555定时器来测量频率

    蓝桥杯单片机比赛第十届以及十二届(第二场)中会用到NE555来测量频率以及频率周期,平时也会用到所以这个是必须要掌握的知识,简单来介绍一下芯片和编程用法. 芯片简介 NE555是属于555系列的计时I ...

最新文章

  1. go 通过nginx代理后获取用户ip
  2. opencv 通过网络连接工业相机_单目摄像机测距(python+opencv)
  3. 怎么修改存储路径_Power Query数据位置变了?利用参数轻松解决源文件路径问题...
  4. 《C和指针》读书笔记
  5. 大数据与Hadoop
  6. python标准库os中的方法_python中OS常用方法
  7. C++工作笔记-仿大佬使用枚举类型
  8. 《HTML5+CSS3网页设计入门必读》——1.13 测验
  9. 未找到 arp 项。_高新热力公司抢工期保供暖 42项新建改造项目全部完工
  10. Python No Module name cv2解决方案
  11. listview item里面的控件点击事件
  12. IPMI 接口配置
  13. linux自动登录drcom,Ubuntu用drcom客户端登陆
  14. [Eigen]Eigen的单位矩阵C++
  15. 用proteus实现STM32仿真
  16. 从1900年1月1日(星期一),开始经过的n年当中,没个月的13号这一天是星期一,星期二,星期三...星期日的次数分别是多少
  17. 注册验证码短信收不到是怎么回事
  18. 《如何阅读一本书》——读书方法的整理
  19. NLP——李宏毅课程笔记
  20. Java分别获取指定日期的年月日

热门文章

  1. 用certbot申请https证书
  2. 计算思维-卡内基梅隆大学计算机系主任周以真
  3. VM下Centos7虚拟机无法进入图形界面并提示Started Virtualization daemon...ices..shutdown
  4. sequencer和driver
  5. ubuntu中vim编辑模式退格键无法删除,方向键乱入ABCD解决方法
  6. windows2003 升级sp1,sp2 产品密匙无效 (转)
  7. 飞腾PC机器与奔腾G645的PC机器以及E5-2630V4虚拟机内存性能简单对比
  8. 刚开始做斗音掌握这5点至少让你少走半年弯路
  9. Java开发中各类名词解释大全
  10. oracle sql 分区查询语句_Oracle SQL调优之分区表