⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 armok0137250.txt

📁 用Proteus仿真DS1302新手入门,使用Proteus就可以自由使用DS1302了.
💻 TXT
字号:


//#include "hd44780.h"

#include <stdio.h>
#include <stdlib.h>
#include <avr/io.h>

/******************************************************************************/
#define datap       PORTC  //定义数据寄存器端口
#define dirp     	DDRC
#define pinp		PINC
#define rst  0			//definitions
#define clk  1
#define io   2 
  
#define prt_set_rst()  		 (datap |= (1<<rst))   //数据寄存器置位与清零
#define prt_set_clk()  		 (datap |= (1<<clk))
#define prt_set_io()    	 (datap |= (1<<io))
#define prt_clear_rst()	 	 (datap &= ~(1<<rst))
#define prt_clear_clk() 	 (datap &= ~(1<<clk))
#define prt_clear_io()  	 (datap &= ~(1<<io))

#define ddr_set_rst()  		 (dirp |= (1<<rst))   //方向寄存器置位与清零
#define ddr_set_clk()  		 (dirp |= (1<<clk))
#define ddr_set_io()   	     (dirp |= (1<<io))
#define ddr_clear_rst()	     (dirp &= ~(1<<rst))
#define ddr_clear_clk()  	 (dirp &= ~(1<<clk))
#define ddr_clear_io()  	 (dirp &= ~(1<<io))
/******************************************************************************/
#define mon 1
#define tue 2
#define wed 3
#define thu 4
#define fri 5
#define sat 6
#define sun 7

#define sec_w 0x80
#define sec_r 0x81
#define min_r 0x83
#define hour_r 0x85
#define date_r 0x87
#define month_r 0x89
#define day_r 0x8b
#define year_r 0x8d
#define min_w 0x82
#define hour_w 0x84
#define date_w 0x86
#define month_w 0x88
#define day_w 0x8a
#define year_w 0x8c

#define w_protect 0x8e

void reset(void);		//define the functions
void write(unsigned char);
unsigned char read(void);
void write_byte(unsigned char,unsigned char);
unsigned char read_byte(unsigned char);
unsigned char get_hours(void);

unsigned char b10;
unsigned char bpm;
/**************************************************************************************/
//函数名	:LCD_DispXY
//属性		:私有
//功能		:设定显示位置
//输入参数	:posx,posy 设定的坐标  右上角为 0,0
//输出参数	:
//返回值	:
//修改日期	:
//修改人	:
/**************************************************************************************/
unsigned char get_hours(void)
{

	unsigned char i;
	unsigned char R_Byte;
	unsigned char TmpByte;

	reset();
	write(0x85);
	ddr_set_io();
	R_Byte = 0x00;
	prt_clear_io();

	ddr_clear_io();

	for(i = 0; i < 4; i++) //get the first 4 bits
	{
                TmpByte = 0;
                if((pinp&(1<<io)))
                        TmpByte = 1;
		TmpByte <<= 7;
		R_Byte >>= 1;
		R_Byte |= TmpByte;

		prt_set_clk();
		delay_nus(2);
		prt_clear_clk();
		delay_nus(2);
	}

        b10 = 0;
	if((pinp&(1<<io)))
                b10 = 1;

	prt_set_clk();
	delay_nus(2);
	prt_clear_clk();
	delay_nus(2);

        bpm = 0;
	if((pinp&(1<<io)))
                bpm = 1;

	prt_set_clk() ;
	delay_nus(2);
	prt_clear_clk();
	delay_nus(2);

	prt_clear_rst();
	prt_clear_clk();

	R_Byte	>>= 4;
	return R_Byte;
}
/**************************************************************************************/
//函数名	:LCD_DispXY
//属性		:私有
//功能		:设定显示位置
//输入参数	:posx,posy 设定的坐标  右上角为 0,0
//输出参数	:
//返回值	:
//修改日期	:
//修改人	:
/**************************************************************************************/

unsigned char read_byte(unsigned char w_byte)	//read the byte with register w_byte
{
	unsigned char temp;
	reset();
	write(w_byte);
	temp = read();
	prt_clear_rst();
	prt_clear_clk();
	return temp;
}
/**************************************************************************************/
//函数名	:LCD_DispXY
//属性		:私有
//功能		:设定显示位置
//输入参数	:posx,posy 设定的坐标  右上角为 0,0
//输出参数	:
//返回值	:
//修改日期	:
//修改人	:
/**************************************************************************************/
void write_byte(unsigned char w_byte, unsigned char w_2_byte)	//read the byte with register w_byte
{
	reset();
	write(w_byte);
	write(w_2_byte);
	prt_clear_rst();
	prt_clear_clk();
}
/**************************************************************************************/
//函数名	:LCD_DispXY
//属性		:私有
//功能		:设定显示位置
//输入参数	:posx,posy 设定的坐标  右上角为 0,0
//输出参数	:
//返回值	:
//修改日期	:
//修改人	:
/**************************************************************************************/
void reset(void)		//sets the pins to begin and end the ds1302 communication
{
	ddr_set_rst();
	prt_clear_clk();
	prt_clear_rst();
	prt_set_rst();
}
/**************************************************************************************/
//函数名	:LCD_DispXY
//属性		:私有
//功能		:设定显示位置
//输入参数	:posx,posy 设定的坐标  右上角为 0,0
//输出参数	:
//返回值	:
//修改日期	:
//修改人	:
/**************************************************************************************/
void write(unsigned char W_Byte)	//writes the W_Byte to the DS1302
{
	unsigned char i;
	DDRC = 0xFF;

	for(i = 0; i < 8; i++)
	{
		prt_clear_io();
		if(W_Byte &0x01)
		{
			prt_set_io();
		}
		prt_set_clk();
		delay_nus(2);
		prt_clear_clk();
		delay_nus(2);
		W_Byte >>=1;
	}
}
/**************************************************************************************/
//函数名	:LCD_DispXY
//属性		:私有
//功能		:设定显示位置
//输入参数	:posx,posy 设定的坐标  右上角为 0,0
//输出参数	:
//返回值	:
//修改日期	:
//修改人	:
/**************************************************************************************/
unsigned char read(void)		//reads the ds1302 reply
{
	unsigned char i;
	unsigned char R_Byte, R_Byte2, TmpByte;

	ddr_set_io();

	R_Byte = 0x00;
	R_Byte2 = 0x00;
	prt_clear_io();
	ddr_clear_io();
	for(i = 0; i < 4; i++) //get the first 4 bits
	{
                TmpByte = 0;
		if((pinp&(1<<io)))
                        TmpByte = 1;
		TmpByte <<= 7;
		R_Byte >>= 1;
		R_Byte |= TmpByte;

		prt_set_clk();
		delay_nus(2);
		prt_clear_clk();
		delay_nus(2);
	}
	for(i = 0; i < 4; i++) //get the next 3 bits
	{
                TmpByte = 0;
		if((pinp&(1<<io)))
                        TmpByte = 1;
		TmpByte <<= 7;
		R_Byte2 >>= 1;
		R_Byte2 |= TmpByte;

		prt_set_clk();
		delay_nus(2);
		prt_clear_clk();
		delay_nus(2);
	}
	R_Byte >>= 4;
	R_Byte2 >>= 4;
	R_Byte = (R_Byte2 * 10) + R_Byte;
	return R_Byte;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -