文章目录
- 零、吐槽
- 一、关于自创模板,和自写模板库的问题
- 二、关于 `详解A/D、D/A、PCF8591` 这篇文章一些小错误
- 三、模板最终版本
- main.c
- ds1302,h
- ds1302.c
- onewire.h
- onewire.c
- iic.h
- iic.c
- 附、第十四届蓝桥杯单片机赛题
零、吐槽
今年是矩阵键盘+三个协议一起调用啊。真是一年比一年难了,去年是超声波。后来者们,加油!
一、关于自创模板,和自写模板库的问题
那两篇文章,代码可以不用看了,直接看最新的,我都总结在里面了
蓝桥杯之单片机学习(三十)——模板罗列、技巧总结与心得
这个由于我之前自己的个人疏忽,从而有很多错误没有更正,并且我之后会在更新用法。保证程序的正确性
并且在这里我也列一下。
二、关于 详解A/D、D/A、PCF8591
这篇文章一些小错误
就是对于AIN1和AIN3的注释的一些错误
我会进行一一更正。
三、模板最终版本
主函数:是注释了矩阵键盘,打开了独立按键(如果需要可以解开矩阵键盘的代码)
ds1302协议:调用万年历时,需要在主函数初始化时,加入ds1302的初始化,之后直接调用,read_ds1302即可,再从shijian
这是数组取出,数组内部顺序是秒、分、时、日、月、周、年
onewire协议:温度读取直接调用tempget()
,即可,但是好像精度达不到小数点之后,函数体有实例
iic协议:
①关于 EEPROM,直接调用:EEPROM_read
、EEPROM_write
就行,但要注意地址
②关于 光敏电阻 数值读取:调用:read_AIN1()
即可,函数体下面有获取实例,可以看看
③关于 Rb2电位 数值读取:调用:read_AIN3()
即可。实例同上
main.c
#include <STC15F2K60S2.h>
#include "onewire.h"
#include "iic.h"
#include "ds1302.h"
#define uchar unsigned char
#define uint unsigned int
extern uchar shijian[7];
uchar SMG_duanma[19] =
{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
0x88,0x80,0xc6,0xc0,0x86,0x8e,
0xbf,0x7f,0XFF};
uchar yi,er,san,si,wu,liu,qi,ba;
void SelectHC138(uchar channel);
void initsys();
void DisplaySMG_Bit(uchar pos, uchar value);
void Delay_one_ms_SMG();
void SMG_Display();
void Alone_Key();
void Delay_five_ms_Key();
void Timer0Init(void);
void main()
{
yi = er = san = si = wu = liu = qi = ba = 18;
initsys();
Timer0Init();
while(1)
{
Alone_Key();
SMG_Display();
}
}
void SelectHC138(uchar channel)
{
switch(channel)
{
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 initsys()
{
SelectHC138(5);
P0 = 0X00;
SelectHC138(4);
P0 = 0XFF;
SelectHC138(6);
P0 = 0XFF;
SelectHC138(7);
P0 = 0XFF;
}
void DisplaySMG_Bit(uchar pos, uchar value)
{
SelectHC138(6);
P0 = 0X01 << pos;
SelectHC138(7);
P0 = value;
}
void Delay_one_ms_SMG()
{
uint j;
for(j = 845; j > 0; j--);
}
void SMG_Display()
{
DisplaySMG_Bit(0, SMG_duanma[yi]);
Delay_one_ms_SMG();
DisplaySMG_Bit(1, SMG_duanma[er]);
Delay_one_ms_SMG();
DisplaySMG_Bit(2, SMG_duanma[san]);
Delay_one_ms_SMG();
DisplaySMG_Bit(3, SMG_duanma[si]);
Delay_one_ms_SMG();
DisplaySMG_Bit(4, SMG_duanma[wu]);
Delay_one_ms_SMG();
DisplaySMG_Bit(5, SMG_duanma[liu]);
Delay_one_ms_SMG();
DisplaySMG_Bit(6, SMG_duanma[qi]);
Delay_one_ms_SMG();
DisplaySMG_Bit(7, SMG_duanma[ba]);
Delay_one_ms_SMG();
}
void Delay_five_ms_Key()
{
uint i,j;
for(i = 0;i < 5;i++)
for(j = 845; j > 0;j--);
}
void Alone_Key()
{
if(P30 == 0)
{
Delay_five_ms_Key();
if(P30 == 0)
{
yi = 1;
}
while(!P30);
}
if(P31 == 0)
{
Delay_five_ms_Key();
if(P31 == 0)
{
er = 1;
}
while(!P31);
}
if(P32 == 0)
{
Delay_five_ms_Key();
if(P32 == 0)
{
san = 1;
}
while(!P32);
}
if(P33 == 0)
{
Delay_five_ms_Key();
if(P33 == 0)
{
si = 1;
}
while(!P33);
}
}
void Timer0Init(void)
{
AUXR |= 0x80;
TMOD &= 0xF0;
TL0 = 0x20;
TH0 = 0xD1;
TF0 = 0;
TR0 = 1;
}
void Timer0Service() interrupt 1
{
}
ds1302,h
#ifndef __DS1302_H__
#define __DS1302_H__
#include <STC15F2K60S2.h>
#include <intrins.h>
sbit SCK = P1^7;
sbit SDA = P2^3;
sbit RST = P1^3;
void Write_Ds1302(unsigned char temp);
void Write_Ds1302_Byte( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302_Byte( unsigned char address );
#endif
ds1302.c
#include "ds1302.h"
#define DecToBCD(dec) (dec/10*16)+(dec%10)
#define BCDToDec(BCD) (BCD/16*10)+(BCD%16)
unsigned char shijian[7]={50,59,23,0,0,0,0};
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 Init_DS1302()
{
unsigned char i,add;
add = 0X80;
Write_Ds1302_Byte(0X8E, 0X00);
for(i = 0; i < 7; i++)
{
Write_Ds1302_Byte(add,DecToBCD(shijian[i]));
add = add + 2;
}
}
void Read_DS1302()
{
unsigned char i,add;
add = 0X81;
for(i = 0; i < 7; i++)
{
shijian[i] = BCDToDec(Read_Ds1302_Byte(add));
add = add + 2;
}
}
onewire.h
#ifndef __ONEWIRE_H__
#define __ONEWIRE_H__
#include <STC15F2K60S2.h>
sbit DQ = P1^4;
unsigned char rd_temperature(void);
unsigned char tempget();
#endif
onewire.c
#include "onewire.h"
void Delay_OneWire(unsigned int t)
{
while(t--);
}
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat&0x01;
Delay_OneWire(50);
DQ = 1;
dat >>= 1;
}
Delay_OneWire(50);
}
unsigned char Read_DS18B20(void)
{
unsigned char i;
unsigned char dat;
for(i=0;i<8;i++)
{
DQ = 0;
dat >>= 1;
DQ = 1;
if(DQ)
{
dat |= 0x80;
}
Delay_OneWire(50);
}
return dat;
}
bit init_ds18b20(void)
{
bit initflag = 0;
DQ = 1;
Delay_OneWire(120);
DQ = 0;
Delay_OneWire(800);
DQ = 1;
Delay_OneWire(100);
initflag = DQ;
Delay_OneWire(50);
return initflag;
}
unsigned char tempget()
{
unsigned char high,low,temp;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
low=Read_DS18B20();
high=Read_DS18B20();
temp=(unsigned char )((high<<8|low)*0.0625f);
return temp;
}
iic.h
#ifndef __IIC_H__
#define __IIC_H__
#include <STC15F2K60S2.h>
#include "intrins.h"
sbit SD = P2^1;
sbit SCL = P2^0;
void 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);
void Read_DAC(unsigned char date);
unsigned char Write_ADC(unsigned char add);
unsigned char read_AIN3();
unsigned char read_AIN1();
unsigned char EEPROM_read(unsigned char add);
void EEPROM_write(unsigned char add,unsigned char date);
#endif
iic.c
#include "iic.h"
#define DELAY_TIME 5
void IIC_Delay(unsigned char i)
{
do{_nop_();}
while(i--);
}
void IIC_Start(void)
{
SD = 1;
SCL = 1;
IIC_Delay(DELAY_TIME);
SD = 0;
IIC_Delay(DELAY_TIME);
SCL = 0;
}
void IIC_Stop(void)
{
SD = 0;
SCL = 1;
IIC_Delay(DELAY_TIME);
SD = 1;
IIC_Delay(DELAY_TIME);
}
void IIC_SendAck(bit ackbit)
{
SCL = 0;
SD = ackbit;
IIC_Delay(DELAY_TIME);
SCL = 1;
IIC_Delay(DELAY_TIME);
SCL = 0;
SD = 1;
IIC_Delay(DELAY_TIME);
}
bit IIC_WaitAck(void)
{
bit ackbit;
SCL = 1;
IIC_Delay(DELAY_TIME);
ackbit = SD;
SCL = 0;
IIC_Delay(DELAY_TIME);
return ackbit;
}
void IIC_SendByte(unsigned char byt)
{
unsigned char i;
for(i=0; i<8; i++)
{
SCL = 0;
IIC_Delay(DELAY_TIME);
if(byt & 0x80) SD = 1;
else SD = 0;
IIC_Delay(DELAY_TIME);
SCL = 1;
byt <<= 1;
IIC_Delay(DELAY_TIME);
}
SCL = 0;
}
unsigned char IIC_RecByte(void)
{
unsigned char i, da;
for(i=0; i<8; i++)
{
SCL = 1;
IIC_Delay(DELAY_TIME);
da <<= 1;
if(SD) da |= 1;
SCL = 0;
IIC_Delay(DELAY_TIME);
}
return da;
}
unsigned char Write_ADC(unsigned char add)
{
unsigned char dat;
IIC_Start();
IIC_SendByte(0X90);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0X91);
IIC_WaitAck();
dat = IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
return dat;
}
void Read_DAC(unsigned char date)
{
IIC_Start();
IIC_SendByte(0X90);
IIC_WaitAck();
IIC_SendByte(0X40);
IIC_WaitAck();
IIC_SendByte(date);
IIC_WaitAck();
IIC_Stop();
}
void EEPROM_write(unsigned char add,unsigned char date)
{
IIC_Start();
IIC_SendByte(0XA0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_SendByte(date);
IIC_WaitAck();
IIC_Stop();
IIC_Delay(5);
}
unsigned char EEPROM_read(unsigned char add)
{
unsigned char dat;
IIC_Start();
IIC_SendByte(0XA0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0XA1);
IIC_WaitAck();
dat = IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
return dat;
}
unsigned char read_AIN1()
{
unsigned char dat1;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x01);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
dat1 = IIC_RecByte();
IIC_Stop();
return dat1;
}
unsigned char read_AIN3()
{
unsigned char dat3;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x03);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
dat3 = IIC_RecByte();
IIC_Stop();
return dat3;
}
附、第十四届蓝桥杯单片机赛题
#include <STC15F2K60S2.h>
#include "onewire.h"
#include "iic.h"
#include "ds1302.h"
#define uchar unsigned char
#define uint unsigned int
extern uchar shijian[7];
sbit LED_0 = P0^0;
sbit LED_1 = P0^1;
sbit LED_2 = P0^2;
sbit LED_3 = P0^3;
sbit LED_4 = P0^4;
uchar SMG_duanma[21] =
{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
0x88,0x80,0xc6,0xc0,0x86,0x8e,
0xbf,0x7f,0XFF,0X8C,0X89};
uchar yi,er,san,si,wu,liu,qi,ba;
void keycan16();
void delayms(int b);
void SelectHC138(uchar channel);
void initsys();
void DisplaySMG_Bit(uchar pos, uchar value);
void Delay_one_ms_SMG();
void SMG_Display();
void Timer0Init(void);
void S4_chocie(uchar S4_st);
void MODE_back(uchar S5_st);
void Check_light();
uchar Two_win = 0;
uchar S4_st = 0;
uchar S5_st = 0;
uchar gather = 0;
uchar wen_can = 30;
uchar old_light = 0;
uchar shijian_new[7];
uchar shidu = 0;
uchar wendu_table[10] = 0;
void main()
{
uchar temp;
yi = er = san = si = wu = liu = qi = ba = 18;
initsys();
Timer0Init();
Init_DS1302();
while(1)
{
keycan16();
S4_chocie(S4_st);
SMG_Display();
}
}
void MODE_back(uchar S5_st)
{
uchar wendu_show_max=0;
uchar wendu_show_avr=0;
uchar i;
if(S5_st==0)
{
{
wendu_show_max = tempget();
yi = 12;
er=18;
san = wendu_show_max / 10;
si = wendu_show_max % 10;
wu = 16;
liu = wendu_show_max / 10;
qi = wendu_show_max % 10;
ba = (wendu_show_max * 10) % 10;
}
}
else if(S5_st==1)
{
yi = 20;
wu = 16;
er = 18 ;
san = si = liu = ba = qi = 10;
}
else if(S5_st==2)
{
yi = 15;
er = san = 0;
si = shijian[2] / 10;
wu = shijian[2] % 10;
liu = 16;
qi = shijian[1] / 10;
ba = shijian[1] % 10;
}
}
void S4_chocie(uchar S4_st)
{
if(Two_win==0)
{
P0 &=0xFF;
if(S4_st==0)
{
Read_DS1302();
SelectHC138(4);
LED_0 = 0;LED_1 = 1;LED_2 = 1;
yi = shijian[2] / 10;er = shijian[2] % 10;
san = 16;
si = shijian[1] / 10;wu = shijian[1] % 10;
liu = 16;
qi = shijian[0] / 10;ba = shijian[0] % 10;
}
else if(S4_st==1)
{
SelectHC138(4);
LED_0 = 1;LED_1 = 0;LED_2 = 1;
MODE_back(S5_st);
}
else if(S4_st==2)
{
SelectHC138(4);
LED_0 = 1;LED_1 = 1;LED_2 = 0;
yi = 19;er=san=si=wu=liu=18;
qi = wen_can / 10;
ba = wen_can % 10;
}
}
}
void Check_light()
{
uchar light_temp;
uchar wendu;
uchar i;
light_temp = read_AIN1();
liu = light_temp / 100;
qi = light_temp % 100 /10;
ba = light_temp % 10;
if(old_light - light_temp > 50)
{
Two_win = 1;
}
else if(old_light - light_temp < 50)
{
Two_win = 0;
}
old_light = light_temp;
delayms(10000);
if(Two_win == 1)
{
gather++;
wendu = tempget();
shidu = 0;
for(i=0;i<3;i++)
{
shijian_new[i] = shijian[i];
}
for(i=0;i=10;i++)
{
if(wendu_table[i]==0)
{
wendu_table[i]=wendu;
}
}
}
}
void SelectHC138(uchar channel)
{
switch(channel)
{
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 initsys()
{
SelectHC138(5);
P0 = 0X00;
SelectHC138(4);
P0 = 0XFF;
SelectHC138(6);
P0 = 0XFF;
SelectHC138(7);
P0 = 0XFF;
}
void DisplaySMG_Bit(uchar pos, uchar value)
{
SelectHC138(6);
P0 = 0X01 << pos;
SelectHC138(7);
P0 = value;
}
void Delay_one_ms_SMG()
{
uint j;
for(j = 845; j > 0; j--);
}
void SMG_Display()
{
DisplaySMG_Bit(0, SMG_duanma[yi]);
Delay_one_ms_SMG();
DisplaySMG_Bit(1, SMG_duanma[er]);
Delay_one_ms_SMG();
DisplaySMG_Bit(2, SMG_duanma[san]);
Delay_one_ms_SMG();
DisplaySMG_Bit(3, SMG_duanma[si]);
Delay_one_ms_SMG();
DisplaySMG_Bit(4, SMG_duanma[wu]);
Delay_one_ms_SMG();
DisplaySMG_Bit(5, SMG_duanma[liu]);
Delay_one_ms_SMG();
DisplaySMG_Bit(6, SMG_duanma[qi]);
Delay_one_ms_SMG();
DisplaySMG_Bit(7, SMG_duanma[ba]);
Delay_one_ms_SMG();
SelectHC138(6);
P0 = 0XFF;
SelectHC138(7);
P0 = 0XFF;
}
void Timer0Init(void)
{
AUXR |= 0x80;
TMOD &= 0xF0;
TL0 = 0xCD;
TH0 = 0xD4;
TF0 = 0;
TR0 = 1;
}
void Timer0Service() interrupt 1
{
TL0 = 0xCD;
TH0 = 0xD4;
}
void delayms(int b)
{
int i,a;
for(i=b;i>0;i--)
for(a=110;a>0;a--);
}
void keycan16()
{
int temp;
P3=0x7f;P44=0;P42=1;
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
delayms(5);
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{temp=P3;
switch(temp)
{
case 0x7b:
if(Two_win==0 && S4_st==1)
{
S5_st++;
if(S5_st==3)
{
S5_st=0;
}
}
;break;
case 0x77:
if(Two_win==0)
{
S4_st++;
if(S4_st==3)
{
S4_st=0;
}
}
;break;
}
while(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f;
}
}
}
P3=0xbf;P44=1;P42=0;
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
delayms(5);
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{temp=P3;
switch(temp)
{
case 0xbb:
if(Two_win==0 && S4_st==2)
{
wen_can -= 1;
}
;break;
case 0xb7:
if(Two_win==0 && S4_st==2)
{
wen_can += 1;
}
;break;
}
while(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f;
}
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)