/*DS18B20部分*/
#include <STC15F2K60S2.H>
#include "Delay.h"
sbit DQ = P1^4;
#define DS18B20_SKIP_ROM 0XCC
#define DS18B20_CONVERT_T 0X44
#define DS18B20_READ_SCRATCHPAD 0XBE
//
void Delay_OneWire(unsigned int t)
{
unsigned char i;
while(t--)
{
for(i=0;i<12;i++);
}
}
//
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat&0x01;
Delay_OneWire(5);
DQ = 1;
dat >>= 1;
}
Delay_OneWire(5);
}
//
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(5);
}
return dat;
}
//
bit init_ds18b20(void)
{
bit initflag = 0;
DQ = 1;
Delay_OneWire(12);
DQ = 0;
Delay_OneWire(80);
DQ = 1;
Delay_OneWire(10);
initflag = DQ;
Delay_OneWire(5);
return initflag;
}
void DS18B20_Convert_Temperature(void)
{
init_ds18b20();
Write_DS18B20(DS18B20_SKIP_ROM);
Write_DS18B20(DS18B20_CONVERT_T);
}
float DS18B20_Read_Temperature(void)
{
unsigned char TLSB , THSB ;
int Temp;
float Temperature;
init_ds18b20();
Write_DS18B20(DS18B20_SKIP_ROM);
Write_DS18B20(DS18B20_READ_SCRATCHPAD);
TLSB = Read_DS18B20();
THSB = Read_DS18B20();
Temp = (THSB<<8)|TLSB;
Temperature = Temp/16.0;
return Temperature;
}
/*DS1302*部分/
#include <STC15F2K60S2.H>
#include "intrins.h"
sbit SCK = P1^7;
sbit SDA = P2^3;
sbit RST = P1^3;
#define DS1302_Year 0X8C
#define DS1302_Month 0X88
#define DS1302_Date 0X86
#define DS1302_Hour 0X84
#define DS1302_Minute 0X82
#define DS1302_Second 0X80
#define DS1302_Day 0X8A
#define DS1302_WP 0X8E
extern unsigned char Time[]={23,1,8,23,59,58,7};
void Init_Ds1302(void)
{
SCK = 0;
RST = 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;
address |= 0x01;
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 DS1302_SetTime(void)
{
Write_Ds1302_Byte( DS1302_WP , 0X00 );
Write_Ds1302_Byte( DS1302_Year , Time[0]/10*16+Time[0]%10 );
Write_Ds1302_Byte( DS1302_Month , Time[1]/10*16+Time[1]%10 );
Write_Ds1302_Byte( DS1302_Date , Time[2]/10*16+Time[2]%10 );
Write_Ds1302_Byte( DS1302_Hour , Time[3]/10*16+Time[3]%10 );
Write_Ds1302_Byte( DS1302_Minute , Time[4]/10*16+Time[4]%10 );
Write_Ds1302_Byte( DS1302_Second , Time[5]/10*16+Time[5]%10 );
Write_Ds1302_Byte( DS1302_Day , Time[6]/10*16+Time[6]%10 );
Write_Ds1302_Byte( DS1302_WP , 0X80 );
}
void DS1302_ReadTime(void)
{
unsigned char Temp;
Temp = Read_Ds1302_Byte(DS1302_Year);
Time[0]= Temp/16*10+Temp%16;
Temp = Read_Ds1302_Byte(DS1302_Month);
Time[1]= Temp/16*10+Temp%16;
Temp = Read_Ds1302_Byte(DS1302_Date);
Time[2]= Temp/16*10+Temp%16;
Temp = Read_Ds1302_Byte(DS1302_Hour);
Time[3]= Temp/16*10+Temp%16;
Temp = Read_Ds1302_Byte(DS1302_Minute);
Time[4]= Temp/16*10+Temp%16;
Temp = Read_Ds1302_Byte(DS1302_Second);
Time[5]= Temp/16*10+Temp%16;
Temp = Read_Ds1302_Byte(DS1302_Day);
Time[6] = Temp/16*10+Temp%16;
}
使用该模块时只需修改 Time[]数组中参数即可更改初始值
/*独立按键模块*/
#include <STC15F2K60S2.H>
#include "Delay.h"
unsigned char Key(void)
{
unsigned char KeyNum;
if(P30==0){Delay(20);while(P30==0);Delay(20);KeyNum=1;}
if(P31==0){Delay(20);while(P31==0);Delay(20);KeyNum=2;}
if(P32==0){Delay(20);while(P32==0);Delay(20);KeyNum=3;}
if(P33==0){Delay(20);while(P33==0);Delay(20);KeyNum=4;}
return KeyNum;
}
使用该模块时需加入延时函数,可在spc下载器中获取
/*矩阵键盘模块*/
#include <STC15F2K60S2.H>
unsigned char MarixKey(void)
{
unsigned char KeyNum;
P44=0;P42=1;P3=0X7F;
if(P30==0){KeyNum=7;}
if(P31==0){KeyNum=6;}
if(P32==0){KeyNum=5;}
if(P33==0){KeyNum=4;}
P44=1;P42=0;P3=0XBF;
if(P30==0){KeyNum=11;}
if(P31==0){KeyNum=10;}
if(P32==0){KeyNum=9;}
if(P33==0){KeyNum=8;}
P44=1;P42=1;P3=0XDF;
if(P30==0){KeyNum=15;}
if(P31==0){KeyNum=14;}
if(P32==0){KeyNum=13;}
if(P33==0){KeyNum=12;}
P44=1;P42=1;P3=0XEF;
if(P30==0){KeyNum=19;}
if(P31==0){KeyNum=18;}
if(P32==0){KeyNum=17;}
if(P33==0){KeyNum=16;}
return KeyNum;
}
/*动态数码管模块*/
#include <STC15F2K60S2.H>
#include "Delay.h"
unsigned char tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff/*不显示*/,0x7f/*.*/,0xBF/*-*/};
void Seg(unsigned char Location , unsigned char Num)
{
switch(Location)
{
case 1 : P2 = 0XC0;P0 = 0X01;break;
case 2 : P2 = 0XC0;P0 = 0X02;break;
case 3 : P2 = 0XC0;P0 = 0X04;break;
case 4 : P2 = 0XC0;P0 = 0X08;break;
case 5 : P2 = 0XC0;P0 = 0X10;break;
case 6 : P2 = 0XC0;P0 = 0X20;break;
case 7 : P2 = 0XC0;P0 = 0X40;break;
case 8 : P2 = 0XC0;P0 = 0X80;break;
}
P2 = 0XE0;
P0 = tab[Num];
Delay(3);
}
利用视觉暂存的原理,使8个数码管依次点亮,实现8个数码管同时点亮,若有闪烁现象,可更改Delay函数的延时值进行调整。
/*LCD1602模块*/
#include <STC15F2K60S2.H>
//引脚配置:
sbit LCD_RS=P2^0;
sbit LCD_RW=P2^1;
sbit LCD_EN=P1^2;
#define LCD_DataPort P0
//函数定义:
/**
* @brief LCD1602延时函数,12MHz调用可延时1ms
* @param 无
* @retval 无
*/
void LCD_Delay()
{
unsigned char i, j;
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
/**
* @brief LCD1602写命令
* @param Command 要写入的命令
* @retval 无
*/
void LCD_WriteCommand(unsigned char Command)
{
LCD_RS=0;
LCD_RW=0;
LCD_DataPort=Command;
LCD_EN=1;
LCD_Delay();
LCD_EN=0;
LCD_Delay();
}
/**
* @brief LCD1602写数据
* @param Data 要写入的数据
* @retval 无
*/
void LCD_WriteData(unsigned char Data)
{
LCD_RS=1;
LCD_RW=0;
LCD_DataPort=Data;
LCD_EN=1;
LCD_Delay();
LCD_EN=0;
LCD_Delay();
}
/**
* @brief LCD1602设置光标位置
* @param Line 行位置,范围:1~2
* @param Column 列位置,范围:1~16
* @retval 无
*/
void LCD_SetCursor(unsigned char Line,unsigned char Column)
{
if(Line==1)
{
LCD_WriteCommand(0x80|(Column-1));
}
else if(Line==2)
{
LCD_WriteCommand(0x80|(Column-1+0x40));
}
}
/**
* @brief LCD1602初始化函数
* @param 无
* @retval 无
*/
void LCD_Init()
{
LCD_WriteCommand(0x38);//八位数据接口,两行显示,5*7点阵
LCD_WriteCommand(0x0c);//显示开,光标关,闪烁关
LCD_WriteCommand(0x06);//数据读写操作后,光标自动加一,画面不动
LCD_WriteCommand(0x01);//光标复位,清屏
}
/**
* @brief 在LCD1602指定位置上显示一个字符
* @param Line 行位置,范围:1~2
* @param Column 列位置,范围:1~16
* @param Char 要显示的字符
* @retval 无
*/
void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char)
{
LCD_SetCursor(Line,Column);
LCD_WriteData(Char);
}
/**
* @brief 在LCD1602指定位置开始显示所给字符串
* @param Line 起始行位置,范围:1~2
* @param Column 起始列位置,范围:1~16
* @param String 要显示的字符串
* @retval 无
*/
void LCD_ShowString(unsigned char Line,unsigned char Column,char *String)
{
unsigned char i;
LCD_SetCursor(Line,Column);
for(i=0;String[i]!='\0';i++)
{
LCD_WriteData(String[i]);
}
}
/**
* @brief 返回值=X的Y次方
*/
int LCD_Pow(int X,int Y)
{
unsigned char i;
int Result=1;
for(i=0;i<Y;i++)
{
Result*=X;
}
return Result;
}
/**
* @brief 在LCD1602指定位置开始显示所给数字
* @param Line 起始行位置,范围:1~2
* @param Column 起始列位置,范围:1~16
* @param Number 要显示的数字,范围:0~65535
* @param Length 要显示数字的长度,范围:1~5
* @retval 无
*/
void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
unsigned char i;
LCD_SetCursor(Line,Column);
for(i=Length;i>0;i--)
{
LCD_WriteData(Number/LCD_Pow(10,i-1)%10+'0');
}
}
/**
* @brief 在LCD1602指定位置开始以有符号十进制显示所给数字
* @param Line 起始行位置,范围:1~2
* @param Column 起始列位置,范围:1~16
* @param Number 要显示的数字,范围:-32768~32767
* @param Length 要显示数字的长度,范围:1~5
* @retval 无
*/
void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length)
{
unsigned char i;
unsigned int Number1;
LCD_SetCursor(Line,Column);
if(Number>=0)
{
LCD_WriteData('+');
Number1=Number;
}
else
{
LCD_WriteData('-');
Number1=-Number;
}
for(i=Length;i>0;i--)
{
LCD_WriteData(Number1/LCD_Pow(10,i-1)%10+'0');
}
}
/**
* @brief 在LCD1602指定位置开始以十六进制显示所给数字
* @param Line 起始行位置,范围:1~2
* @param Column 起始列位置,范围:1~16
* @param Number 要显示的数字,范围:0~0xFFFF
* @param Length 要显示数字的长度,范围:1~4
* @retval 无
*/
void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
unsigned char i,SingleNumber;
LCD_SetCursor(Line,Column);
for(i=Length;i>0;i--)
{
SingleNumber=Number/LCD_Pow(16,i-1)%16;
if(SingleNumber<10)
{
LCD_WriteData(SingleNumber+'0');
}
else
{
LCD_WriteData(SingleNumber-10+'A');
}
}
}
/**
* @brief 在LCD1602指定位置开始以二进制显示所给数字
* @param Line 起始行位置,范围:1~2
* @param Column 起始列位置,范围:1~16
* @param Number 要显示的数字,范围:0~1111 1111 1111 1111
* @param Length 要显示数字的长度,范围:1~16
* @retval 无
*/
void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
unsigned char i;
LCD_SetCursor(Line,Column);
for(i=Length;i>0;i--)
{
LCD_WriteData(Number/LCD_Pow(2,i-1)%2+'0');
}
}
所有代码是在学习蓝桥杯竞赛中练习写,如有错误请大家指正。