#include <STC15F2K60S2.H>
#include "intrins.h"
sbit SDA = P2^1; /* 数据线 */
sbit SCL = P2^0; /* 时钟线 */
sbit SCK=P1^7;
sbit IO=P2^3;
sbit RST = P1^3; // DS1302复位
#define uchar unsigned char
#define uint unsigned int
#define DELAY_TIME 40
#define AT24C02_address 0xA0
bit IIC_WaitAck(void);
void IIC_SendAck(bit ackbit);
void IIC_Stop(void);
void IIC_Start(void);
void IIC_Delay(unsigned char i);
unsigned char IIC_RecByte(void);
void IIC_SendByte(unsigned char byt);
void IIC_read(uchar hw_address,uchar reg_address);
void IIC_write(uchar hw_address,uchar reg_address);
unsigned char Read_Ds1302_Byte ( unsigned char address );
void Write_Ds1302_Byte( unsigned char address,unsigned char dat );
void Write_Ds1302(unsigned char temp);
void get_time(void);
void ds1302_init(void);
void init(void);
void SMG_output(void);
void Delay1ms(void);
uchar tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,\
0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0xff,0xbf};
uchar SMG[8]={20,20,20,20,20,20,20,20};
uint move=0;
uchar time[7]={0,0,0,0,0,0,0};
void main(void)
{
init();
// IIC_write(AT24C02_address,0x00);Delay1ms();Delay1ms();Delay1ms();Delay1ms();Delay1ms();
IIC_read(AT24C02_address,0x00);
ds1302_init();
while(1)
{
get_time();
IIC_write(AT24C02_address,0x00);
SMG[0]=time[2]/10;SMG[1]=time[2]%10;
SMG[3]=time[1]/10;SMG[4]=time[1]%10;
SMG[6]=time[0]/10;SMG[7]=time[0]%10;
SMG_output();
}
}
void IIC_write(uchar hw_address,uchar reg_address)
{
uchar i;
IIC_Start();
IIC_SendByte(hw_address&0xfe);
IIC_WaitAck();
IIC_SendByte(reg_address);
IIC_WaitAck();
for(i=0;i<3;i++)
{
IIC_SendByte(time[i]);
IIC_WaitAck();
}
IIC_Stop();
}
void IIC_read(uchar hw_address,uchar reg_address)
{
uchar i;
IIC_Start();
IIC_SendByte(hw_address&0xfe);
IIC_WaitAck();
IIC_SendByte(reg_address);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(hw_address|0x01);
IIC_WaitAck();
for(i=0;i<3;i++)
{
time[i]=IIC_RecByte();
IIC_SendAck(0);
}
IIC_Stop();
}
void ds1302_init(void)
{
uchar i,j=0x80;
Write_Ds1302_Byte(0x8e,0x00);
for(i=0;i<7;i++)
{
Write_Ds1302_Byte(j,time[i]);
j +=2;
}
Write_Ds1302_Byte(0x8e,0x80);
}
void get_time(void)
{
uchar i,j=0x81;
Write_Ds1302_Byte(0x8e,0x00);
for(i=0;i<7;i++)
{
time[i]=Read_Ds1302_Byte(j);
j +=2;
}
Write_Ds1302_Byte(0x8e,0x80);
}
void Write_Ds1302(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++)
{
SCK=0;
IO=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/10)<<4)|(dat%10));
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(IO)
temp|=0x80;
SCK=1;
}
RST=0; _nop_();
SCK=0; _nop_();
SCK=1; _nop_();
IO=0; _nop_();
IO=1; _nop_();
return (((temp/16)*10)+(temp%16));
}
//
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;
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;
}
//
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;
}
//
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;
}
void init(void)
{
P2=(P2&0X1F)|0XA0;
P0=0X00;
P2=(P2&0X1F)|0X80;
P0=0XFF;
P2=(P2&0X1F)|0XC0;
P0=0XFF;
P2=(P2&0X1F)|0XE0;
P0=0XFF;
}
void SMG_output(void)
{
uchar i;
for(i=0;i<8;i++)
{
P2=(P2&0X1F)|0XC0;
P0=(1 << i);
P2=(P2&0X1F)|0XE0;
P0=tab[SMG[i]];
Delay1ms();
}
P2=(P2&0X1F)|0XC0;
P0=0XFF;
P2=(P2&0X1F)|0XE0;
P0=0XFF;
}
void Delay1ms(void) //@11.0592MHz
{
unsigned char i, j;
_nop_();
_nop_();
_nop_();
i = 11;
j = 190;
do
{
while (--j);
} while (--i);
}
没有合适的资源?快使用搜索试试~ 我知道了~
蓝桥杯单片机开发板上的外设例程
共115个文件
obj:11个
c:11个
lst:11个
需积分: 10 1 下载量 151 浏览量
2022-10-22
13:21:24
上传
评论 1
收藏 267KB ZIP 举报
温馨提示
内有蓝桥杯单片机开发板上的外设模块 LED 独立按键 矩阵按键 数码管 定时器 PWM波 DS18B20温度检测 DS1302时间显示 PCF8591的AD转换和DA转换 NE555方波发射器的频率检测 超声波测量距离 EEPROM的数据存储读和写 文章有代码讲解
资源详情
资源评论
资源推荐
收起资源包目录
蓝桥杯单片机开发板上的外设例程 (115个子文件)
onewire.__i 144B
main.c 5KB
main.c 4KB
main.c 3KB
main.c 2KB
main.c 2KB
main.c 2KB
main.c 2KB
main.c 2KB
main.c 1KB
onewire.c 1KB
main.c 487B
Dkey 7KB
DS1302 10KB
DS18B20 12KB
EEPROM 16KB
onewire.h 283B
DS18B20.hex 7KB
PCF8591.hex 4KB
EEPROM.hex 3KB
NE555.hex 3KB
Ultrasound.hex 3KB
PWM.hex 2KB
DS1302.hex 2KB
Dkey.hex 1KB
SMG.hex 1KB
led.hex 305B
PCF8591.build_log.htm 1KB
Dkey.build_log.htm 1KB
Ultrasound.build_log.htm 1KB
DS18B20.build_log.htm 1017B
EEPROM.build_log.htm 1012B
NE555.build_log.htm 1008B
SMG.build_log.htm 996B
PWM.build_log.htm 996B
led.build_log.htm 995B
DS1302.build_log.htm 991B
led 5KB
Ultrasound.lnp 101B
PCF8591.lnp 95B
DS18B20.lnp 95B
DS1302.lnp 93B
EEPROM.lnp 93B
NE555.lnp 91B
Dkey.lnp 89B
SMG.lnp 87B
PWM.lnp 87B
led.lnp 87B
main.lst 11KB
main.lst 8KB
main.lst 8KB
main.lst 6KB
main.lst 6KB
main.lst 6KB
main.lst 5KB
main.lst 4KB
main.lst 4KB
onewire.lst 3KB
main.lst 2KB
EEPROM.m51 19KB
PCF8591.m51 15KB
DS18B20.m51 13KB
DS1302.m51 10KB
PWM.m51 10KB
Ultrasound.m51 9KB
NE555.m51 9KB
SMG.m51 7KB
Dkey.m51 7KB
led.m51 4KB
NE555 8KB
main.obj 18KB
main.obj 14KB
main.obj 13KB
main.obj 11KB
main.obj 10KB
main.obj 9KB
main.obj 9KB
main.obj 8KB
main.obj 7KB
onewire.obj 7KB
main.obj 5KB
PCF8591 13KB
PWM 9KB
DS18B20.uvgui.qx 89KB
led.uvgui.qx 89KB
SMG.uvgui.qx 89KB
Dkey.uvgui.qx 89KB
Ultrasound.uvgui.qx 88KB
EEPROM.uvgui.qx 88KB
DS1302.uvgui.qx 88KB
NE555.uvgui.qx 88KB
PCF8591.uvgui.qx 88KB
PWM.uvgui.qx 88KB
SMG 7KB
Ultrasound 9KB
Dkey.uvopt 6KB
PCF8591.uvopt 5KB
Ultrasound.uvopt 5KB
SMG.uvopt 5KB
DS1302.uvopt 5KB
共 115 条
- 1
- 2
月明Mo
- 粉丝: 1w+
- 资源: 14
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0