|
蓝桥杯单片机组 第七届“电压、频率采集设备”源代码
包括主函数,驱动函数等。
- #include<stc15f2k60s2.h>
- #include<deputy.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar code tab[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0xbf,0XFF};
- uchar yi,er,san,si,wu,liu,qi,ba;
- uchar num,flag;
- uint t = 0;
- uint intr = 0;
- bit s_flag;
- unsigned int zheng=0,fan=0,zheng_now=0,fan_now=0,time,freq;
- uchar t_flag=0;
- uchar low ,high;
- uint num_p;
- uchar jia=0,jian=0;
- uchar shizhong=1,dianya=0,pinlv=0,chaxun=0,gongneng=0;
- uint vol=0;
- uchar shangxian,xiaxian;
- uchar shan=0;
- uchar cun_shi,cun_fen,cun_miao,cun_lei;
- void delayms(uint ms);
- void allinit(void);
- void keyscan(void);
- void display1(uchar yi,uchar er);
- void display2(uchar san,uchar si);
- void display3(uchar wu,uchar liu);
- void display4(uchar qi,uchar ba);
- //void send_wave(void);
- void Timer0Init(void); //5US
- void Timer1Init(void); //2MS
- // extern unsigned char shijian[];
- void main()
- {
- allinit();
- yi=0,er=6,san=10,si=5,wu=9;liu=10,qi=0,ba=0;
- // EEPROM_write(0x00,20);delayms(2);
- // EEPROM_write(0x01,10);delayms(2); //只初始化一次,之后注释掉
- shangxian=EEPROM_Read(0x00);delayms(2);
- xiaxian=EEPROM_Read(0x01);delayms(2);
- DS_init();
- Timer0Init();
- Timer1Init();
- EA=1;
- ET1=1;
- while(1)
- {
- DS_get();
- /****************时钟************************/
- if (shizhong==1)
- {
- if(gongneng==0)
- {
- yi=shijian[2]/10;er=shijian[2]%10;san=10;si=shijian[1]/10;
- wu=shijian[1]%10;liu=10;qi=shijian[0]/10;ba=shijian[0]%10;
- }
- else if(gongneng==1)
- {
- if(shijian[0]%2==0)
- {
- yi=shijian[2]/10;er=shijian[2]%10;san=10;si=shijian[1]/10;
- wu=shijian[1]%10;liu=10;qi=shijian[0]/10;ba=shijian[0]%10;
- }
- else if(shijian[0]%2==1)
- {
- yi=11;er=11;san=10;
- si=shijian[1]/10;wu=shijian[1]%10;liu=10;qi=shijian[0]/10;ba=shijian[0]%10;
- }
- if(jia==1)
- {
- jia=0;
- shijian[2]=shijian[2]+1;
- DS_init();
- }
- if(jian==1)
- {
- jian=0;
- shijian[2]=shijian[2]-1;
- DS_init();
- }
- }
- else if(gongneng==2)
- {
- if(shijian[0]%2==0)
- {
- yi=shijian[2]/10;er=shijian[2]%10;san=10;si=shijian[1]/10;
- wu=shijian[1]%10;liu=10;qi=shijian[0]/10;ba=shijian[0]%10;
- }
- else if(shijian[0]%2==1)
- {
- yi=shijian[2]/10;er=shijian[2]%10;san=10;si=11;
- wu=11;liu=10;qi=shijian[0]/10;ba=shijian[0]%10;
- }
- if(jia==1)
- {
- jia=0;
- shijian[1]=shijian[1]+1;
- DS_init();
- }
- if(jian==1)
- {
- jian=0;
- shijian[1]=shijian[1]-1;
- DS_init();
- }
- }
- else if(gongneng==3)
- {
- if(shijian[0]%2==0)
- {
- yi=shijian[2]/10;er=shijian[2]%10;san=10;si=shijian[1]/10;
- wu=shijian[1]%10;liu=10;qi=shijian[0]/10;ba=shijian[0]%10;
- }
- else if(shijian[0]%2==1)
- {
- yi=shijian[2]/10;er=shijian[2]%10;san=10;si=shijian[1]/10;
- wu=shijian[1]%10;liu=10;qi=11;ba=11;
- }
- if(jia==1)
- {
- jia=0;
- shijian[0]=shijian[0]+1;
- DS_init();
- }
- if(jian==1)
- {
- jian=0;
- shijian[0]=shijian[0]-1;
- DS_init();
- }
- }
- }
- /****************电压)************************/
- else if (dianya==1)
- {
- vol=EEPROM_Read(0x03)*19.6; //1000/51
- if(gongneng==0)
- {
- yi=10;er=1;san=10;si=11;
- wu=vol/1000;liu=vol%1000/100;qi=vol%100/10;ba=vol%10;
- }
- else if(gongneng==1)
- {
- if(shan<100)
- {
- shan++;
- yi=shangxian/10;er=shangxian%10;
- wu=xiaxian/10;liu=xiaxian%10;san=0;si=0;qi=0;ba=0;
- }
- else if(shan>=100)
- {
- shan++;
- if(shan==200) shan=0;
- yi=11;er=11;wu=xiaxian/10;liu=xiaxian%10;san=11;si=11;
- }
- if(jia==1)
- {
- jia=0;
- shangxian=shangxian+5;
- EEPROM_write(0x00,shangxian) ;delayms(2);
- }
- if(jian==1)
- {
- jian=0;
- shangxian=shangxian-5;
- EEPROM_write(0x00,shangxian) ;delayms(2);
- }
- }
- else if(gongneng==2)
- {
- if(shan<100)
- {
- shan++;
- yi=shangxian/10;er=shangxian%10;
- wu=xiaxian/10;liu=xiaxian%10;san=0;si=0;qi=0;ba=0;
- }
- else if(shan>=100)
- {
- shan++;
- if(shan==200) shan=0;
- yi=shangxian/10;er=shangxian%10;
- wu=11;liu=11;qi=11;ba=11;
- }
- if(jia==1)
- {
- jia=0;
- xiaxian=xiaxian+5;
- EEPROM_write(0x01,xiaxian) ;delayms(2);
- }
- if(jian==1)
- {
- jian=0;
- xiaxian=xiaxian-5;
- EEPROM_write(0x01,xiaxian) ;delayms(2);
- }
- }
- if((vol>shangxian)&&(EEPROM_Read(0x13)!=1))
- {
- EEPROM_write(0x10,shijian[2]);delayms(2);
- EEPROM_write(0x11,shijian[1]);delayms(2);
- EEPROM_write(0x12,shijian[0]);delayms(2);
- EEPROM_write(0x13,0);delayms(2);
- }
- if((vol<xiaxian)&&(EEPROM_Read(0x13)!=0))
- {
- EEPROM_write(0x10,shijian[2]);delayms(2);
- EEPROM_write(0x11,shijian[1]);delayms(2);
- EEPROM_write(0x12,shijian[0]);delayms(2);
- EEPROM_write(0x13,0);delayms(2);
- }
- }
- /****************频率************************/
- else if (pinlv==1)
- {
- yi=10;er=2;san=10;
- time=(zheng_now+fan_now)*5;
- freq=1000000/time;
- if(gongneng==0)
- {
- si=freq/10000;wu=freq%10000/1000;
- liu=freq%1000/100;qi=freq%100/10;ba=freq%10;
- }
- else if(gongneng==1)
- {
- si=time/10000;wu=time%10000/1000;
- liu=time%1000/100;qi=time%100/10;ba=time%10;
- }
- }
- /****************查询************************/
- else if (chaxun==1)
- {
- if(gongneng==0)
- {
- yi=11;er=11;san=11;si=11;
- wu=11;liu=11;qi=cun_lei/10;ba=cun_lei%10;
- }
- else if(gongneng==1)
- {
- san=10; liu=10;
- yi=cun_shi/10;er=cun_shi%10;si=cun_fen/10;
- wu=cun_fen%10;qi=cun_miao/10;ba=cun_miao%10;
- }
- }
- keyscan();
- display1(yi,er);
- display2(san,si);
- display3(wu,liu);
- display4(qi,ba);
- }
- }
- /****************矩阵按键************************/
- void keyscan()
- {
- uchar temp;
- P3=0XFF;P4=0XEF;
- //P44=0;P42=1;P30=1;P31=1;P32=1;P33=1;
- temp=P3;
- temp=temp&0x1f;
- while(temp!=0x1f)
- {
- delayms(1);
- temp=P3;
- temp=temp&0x1f;
- while(temp!=0x1f)
- {
- temp=P3;
- switch(temp)
- {
- case 0xfe:shizhong=1,dianya=0,pinlv=0,chaxun=0,gongneng=0;break;
- case 0xfd:shizhong=0,dianya=1,pinlv=0,chaxun=0,gongneng=0;break;
- case 0xfb:shizhong=0,dianya=0,pinlv=1,chaxun=0,gongneng=0;break;
- case 0xf7:
- if (shizhong==1)
- {
- if (gongneng==0) gongneng=1;
- else if(gongneng==1) gongneng=2;
- else if(gongneng==2) gongneng=3;
- else if(gongneng==3) gongneng=0;
- }
- if (dianya==1)
- {
- if (gongneng==0) gongneng=1;
- else if(gongneng==1) gongneng=2;
- else if(gongneng==2) gongneng=1;
- }
- if (pinlv==1)
- {
- if (gongneng==0) gongneng=1;
- else if(gongneng==1) gongneng=0;
-
- }
- if (chaxun==1)
- {
- if (gongneng==0) gongneng=1;
- else if(gongneng==1) gongneng=0;
-
- }
- break;
-
- }
- while(temp!=0x1f)
- {
- temp=P3;
- temp=temp&0x1f;
- }
- }
- }
-
- P3=0XFF;P4=0XFB;
- temp=P3;
- temp=temp&0x1f;
- while(temp!=0x1f)
- {
- delayms(1);
- temp=P3;
- temp=temp&0x1f;
- while(temp!=0x1f)
- {
- temp=P3;
- switch(temp)
- {
- case 0xfe:jia=1;break;
- case 0xfd:jian=1;break;
- case 0xfb:shizhong=0,dianya=0,pinlv=0,chaxun=1,gongneng=0;
- cun_shi=EEPROM_Read(0x10);delayms(2);
- cun_fen=EEPROM_Read(0x11);delayms(2);
- cun_miao=EEPROM_Read(0x12);delayms(2);
- cun_lei=EEPROM_Read(0x13);delayms(2);
- break;
-
-
- }
-
- while(temp!=0x1f)
- {
- temp=P3;
- temp=temp&0x1f;
- }
- }
- }
-
- // P3=0XDF;
- // temp=P3;
- // temp=temp&0x1f;
- // while(temp!=0x0f)
- // {
- // delayms(2);
- // temp=P3;
- // temp=temp&0x0f;
- // while(temp!=0x0f)
- // {
- // temp=P3;
- // switch(temp)
- // {
- // case 0xde:;break;
- // case 0xdd:;break;
- // case 0xdb:break;
- // case 0xd7:break;
- // }
- // while(temp!=0x0f)
- // {
- // temp=P3;
- // temp=temp&0x0f;
- // }
- // }
- // }
- //
- // P3=0XEF;
- // temp=P3;
- // temp=temp&0x0f;
- // while(temp!=0x0f)
- // {
- // delayms(2);
- // temp=P3;
- // temp=temp&0x0f;
- // while(temp!=0x0f)
- // {
- // temp=P3;
- // switch(temp)
- // {
- // case 0xee:break;
- // case 0xed:break;
- // case 0xeb:break;
- // case 0xe7:break;
- // }
- // while(temp!=0x0f)
- // {
- // temp=P3;
- // temp=temp&0x0f;
- // }
- // }
- // }
- }
- /****************初始化************************/
- void allinit(void)
- {
- P2=0XA0;
- P0=0X00; //关闭蜂鸣器
- P2=0X80;
- P0=0XFF;//关闭LED
- P2=0XC0;
- P0=0XFF;//选择数码管
- P2=0XFF;
- P0=0XFF; //关闭数码管
- }
- void display1(uchar yi,uchar er)
- {
- P2=0XC0;
- P0=0X01;//选择数码管
- P2=0XFF;
- P0=tab[yi];
- delayms(1);
- P2=0XC0;
- P0=0X02;//选择数码管
- P2=0XFF;
- P0=tab[er];
- delayms(1);
- }
-
- void display2(uchar san,uchar si)
- {
- P2=0XC0;
- P0=0X04;//选择数码管
- P2=0XFF;
- P0=tab[san];
- delayms(1);
- P2=0XC0;
- P0=0X08;//选择数码管
- P2=0XFF;
- P0=tab[si];
- delayms(1);
- }
-
- void display3(uchar wu,uchar liu)
- {
- P2=0XC0;
- P0=0X10;//选择数码管
- P2=0XFF;
- P0=tab[wu];
- delayms(1);
- P2=0XC0;
- P0=0X20;//选择数码管
- P2=0XFF;
- P0=tab[liu];
- delayms(1);
- }
- void display4(uchar qi,uchar ba)
- {
- P2=0XC0;
- P0=0X40;//选择数码管
- P2=0XFF;
- P0=tab[qi];
- delayms(1);
- P2=0XC0;
- P0=0X80;//选择数码管
- P2=0XFF;
- P0=tab[ba];
- delayms(1);
- }
- //定时器0中断服务函数
- void Timer0Init(void) //5微秒@12.000MHz
- {
- AUXR |= 0x80; //定时器时钟1T模式
- TMOD &= 0xF0; //设置定时器模式
- TL0 = 0xC4; //设置定时初值
- TH0 = 0xFF; //设置定时初值
- TF0 = 0; //清除TF0标志
- }
- void Timer0_Rountine(void) interrupt 1
- {
- /************************方法pwm*****************/
- if(P34==0)
- {
- fan++;
- if(t_flag==1)
- {
- t_flag=0;
- fan_now=fan;
- fan=0;
- }
- }
- else if(P34==1)
- {
- zheng++;
-
- if(t_flag==0)
- {
- t_flag=1;
- zheng_now=zheng;
- zheng=0;
- }
- }
-
- // /************************只显示周期*****************/
- // if(P34==0)
- // {
- // if(high==1)
- // {
- // time=num_p;
- // num_p=0;
- // high=0;
- // }
- // num_p++;
- // }
- //
- // else if(P34==1)
- // {
- //
- // num_p++;
- // high=1;
- // }
-
- }
- void Timer1Init(void) //2毫秒@12.000MHz
- {
- AUXR |= 0x40; //定时器时钟1T模式
- TMOD &= 0x0F; //设置定时器模式
- TL1 = 0x40; //设置定时初值
- TH1 = 0xA2; //设置定时初值
- TF1 = 0; //清除TF1标志
- TR1 = 1; //定时器1开始计时
- }
- void Timer1_Rountine(void) interrupt 3
- {
- if(pinlv==1)
- {
- intr++;
- if(intr==495)
- {
- TR0 = 1; ET0= 1;
- }
- else if(intr==500)
- {
- intr=0;
- TR0 = 0; ET0= 0;
- }
- }
- }
- void delayms(uint ms)
- {
- uint i,j;
- for(i=ms;i>0;i--)
- for(j=845;j>0;j--);
- }
- <deputy.h>
- #include<stc15f2k60s2.h>
- #include<deputy.h>
- #include <intrins.h>
- #define somenop {_nop_();_nop_();_nop_();_nop_();_nop_();}
- #define uchar unsigned char
- #define SlaveAddrW 0xA0
- #define SlaveAddrR 0xA1
- //总线引脚定义
- sbit SD = P2^1; /* 数据线 */
- sbit SCL = P2^0; /* 时钟线 */
- sbit SCK=P1^7;
- sbit SDA=P2^3;
- sbit RST = P1^3; // DS1302复位
- unsigned char shijian[]={55,59,23,0,0,0,0};
- void Write_Ds1302_Byte(unsigned char temp)
- {
- unsigned char i;
- for (i=0;i<8;i++)
- {
- SCK=0;
- SDA=temp&0x01;
- temp>>=1;
- SCK=1;
- }
- }
- void Write_Ds1302( unsigned char address,unsigned char dat )
- {
- RST=0;
- _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
- SCK=0;
- _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
- RST=1;
- _nop_(); _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
- Write_Ds1302_Byte(address);
- Write_Ds1302_Byte((dat/10<<4)|(dat%10));
- RST=0;
- }
- unsigned char Read_Ds1302 ( unsigned char address )
- {
- unsigned char i,temp=0x00,dat1,dat2;
- RST=0;
- _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
- SCK=0;
- _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
- RST=1;
- _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
- Write_Ds1302_Byte(address);
- for (i=0;i<8;i++)
- {
- SCK=0;
- temp>>=1;
- if(SDA)
- temp|=0x80;
- SCK=1;
- }
- RST=0;
- _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
- RST=0;
- SCK=0;
- _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
- SCK=1;
- _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
- SDA=0;
- _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
- SDA=1;
- _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
-
- dat1=temp/16;
- dat2=temp%16;
- temp=dat1*10+dat2;
-
- return (temp);
- }
- void DS_init(void)
- {
- unsigned char i,add;
- add=0x80;
- Write_Ds1302(0x8e,0x00);
- for(i=0;i<7;i++)
- {
- Write_Ds1302(add,shijian[i]);
- add=add+2;
- }
- Write_Ds1302(0x8e,0x80);
- }
- void DS_get(void)
- {
- unsigned char i,add;
- add=0x81;
- Write_Ds1302(0x8e,0x00);
- for(i=0;i<7;i++)
- {
- shijian[i]=Read_Ds1302(add);
- add=add+2;
- }
- Write_Ds1302(0x8e,0x80);
- }
-
- /******************************************IIC********************************/
- //总线启动条件
- void IIC_Start(void)
- {
- SD = 1;
- SCL = 1;
- somenop;
- SD = 0;
- somenop;
- SCL = 0;
- }
- //总线停止条件
- void IIC_Stop(void)
- {
- SD = 0;
- SCL = 1;
- somenop;
- SD = 1;
- }
- //应答位控制
- //void IIC_Ack(bit ackbit)
- //{
- // if(ackbit)
- // {
- // SD = 0;
- // }
- // else
- // {
- // SD = 1;
- // }
- // somenop;
- // SCL = 1;
- // somenop;
- // SCL = 0;
- // SD = 1;
- // somenop;
- //}
- //等待应答
- bit IIC_WaitAck(void)
- {
- SD = 1;
- somenop;
- SCL = 1;
- somenop;
- if(SD)
- {
- SCL = 0;
- IIC_Stop();
- return 0;
- }
- else
- {
- SCL = 0;
- return 1;
- }
- }
- //通过I2C总线发送数据
- void IIC_SendByte(unsigned char byt)
- {
- unsigned char i;
- for(i=0;i<8;i++)
- {
- if(byt&0x80)
- {
- SD = 1;
- }
- else
- {
- SD = 0;
- }
- somenop;
- SCL = 1;
- byt <<= 1;
- somenop;
- SCL = 0;
- }
- }
- //从I2C总线上接收数据
- unsigned char IIC_RecByte(void)
- {
- unsigned char da;
- unsigned char i;
-
- for(i=0;i<8;i++)
- {
- SCL = 1;
- somenop;
- da <<= 1;
- if(SD)
- da |= 0x01;
- SCL = 0;
- somenop;
- }
- return da;
- }
- void EEPROM_write(uchar add,uchar date)
- {
- IIC_Start();
- IIC_SendByte(0xa0);
- IIC_WaitAck();
- IIC_SendByte(0X10);
- IIC_WaitAck();
- IIC_SendByte(date);
- IIC_WaitAck();
- IIC_Stop();
- }
- uchar EEPROM_Read(uchar add)
- {
- uchar date;
- IIC_Start();
- IIC_SendByte(0xa0);
- IIC_WaitAck();
- IIC_SendByte(add);
- IIC_WaitAck();
-
- IIC_Start();
- IIC_SendByte(0xa1);
- IIC_WaitAck();
- date=IIC_RecByte();
- IIC_Stop();
- return date;
- }
- DEPUTY.H
- #ifndef __DEPUTY_H__
- #define __DEPUTY_H__
- #define uchar unsigned char
- #define uint unsigned int
- extern unsigned char shijian[];
- void Write_Ds1302_Byte(unsigned char temp);
- void Write_Ds1302( unsigned char address,unsigned char dat );
- unsigned char Read_Ds1302 ( unsigned char address );
- void DS_init(void);
- void DS_get(void);
- void EEPROM_write(uchar add,uchar date);
- uchar EEPROM_Read(uchar add);
- void IIC_Start(void);
- void IIC_Stop(void);
- void IIC_Ack(bit ackbit);
- void IIC_SendByte(unsigned char byt);
- bit IIC_WaitAck(void);
- unsigned char IIC_RecByte(void);
- #endif
复制代码
|
-
-
2.doc
93.09 KB, 下载次数: 14, 下载积分: 黑币 -5
c文件文档
评分
-
查看全部评分
|