📄 ds1302_mega16._c
字号:
/****************************************
工 程:DS1302实时时钟+DS18B20+1602液晶6线显示
创建日期:2007年3月30日
修改日期:2007年3月31日
创 建 人:朱海峰
邮 箱:ntzhf100@163.com
Q Q: 543376422
*****************************************/
#include <iom16v.h>
#include <macros.h>
#include "def.h"
#include "LCD_1602_4wires.h"
#include "DS18B20.h"
/******************************************
PIN DEFINE:
/RST PD6
SCLK PD4
IO PD5
******************************************/
/***************************************************************
COMMAND/ADDRESS FORMAT
bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0
1 RAM/CK A4 A3 A2 A1 A0 R/W
***************************************************************/
// second,mintue,hour,date,month,day,year,control,trickle charge,clock burst
//adderss: 0 1 2 3 4 5 6 7 8 9
/**************************************************************/
void Time_Init(const uint8_t *p_time);
void ChipInit(void);
void InitDS1302(void);
void TimeSet(void);
void ReadTime(void);
void DisplayTime(void);
void LCD_display_init(void);
void Display(uint8_t x, uint8_t y, uint8_t g_data);
void DS18B20(void);
//RAM_CLOCK=1,RAM操作;
//RAM_CLOCK=0,时钟日历操作;
unsigned char Read_DS1302(uint8_t RAM_CLOCK, uint8_t Address);
void Write(uint8_t RAM_CLOCK, uint8_t Address, uint8_t Dat);
/*******************************************/
//second,mintue,hour,date,month,day,year
const uint8_t Time[]={0x10,0x05,0x23,0x25,0x05,0x05,0x07};
uint8_t Day[7][4]={{"Mon"},{"Tue"},{"Wed"},{"Thu"},{"Fri"},{"Sat"},{"Sun"}};
const uint8_t Number[]={'0','1','2','3','4','5','6','7','8','9'};
unsigned char g_year,g_month,g_date,g_day,g_hour,g_mintue,g_second;
//uint8_t g_tempeature;
/******************************************/
void main(void)
{
uint8_t i;
ChipInit();
LCD_init();
InitDS1302();
Write(0,0x07,0x00); //CLOCK操作,控制寄存器,解除写保护
//Time_Init(&Time[0]); //初始化时间
LCD_display_init();
while(1)
{
for(i=0;i<50;i++)
{
ReadTime();
DisplayTime();
if(i == 0)
{
//BEEP_ON();
DS18B20();
//BEEP_OFF();
}
Delay_nms(100);
}
}
}
/******************************************/
void ChipInit(void)
{
NOP();
/*********************I/O口初始化**************************/
LCD_DATA_DDR |= LCD_DATA_MASK; //液晶数据口线
LCD_CONTROL_DDR |= RS|EN; //
DS1302_DDR |= RST|SCLK|IO;
DS1302_PORT = 0X00;
DS18B20_DDR |= DS18B20_IO;
/*********************************************************/
}
/******************************************/
void InitDS1302(void)
{
CLR_RST();
CLR_SCLK();
CLR_IO();
}
/****************************************/
void Time_Init(const uint8_t *p_time)
{
uint8_t i,ad=0;
for(i=8;i>0;i--)
{
Write(0,ad,*p_time);
ad++;
p_time++;
}
}
/******************************************/
void LCD_display_init(void)
{
LCD_write_string(1,1,"20 . .");
LCD_write_string(3,2,": :");
LCD_set_xy(15,2);
Units();
}
/******************************************/
void ReadTime(void)
{
uint8_t address;
address = 0;
g_second = Read_DS1302(0, address++);
g_mintue = Read_DS1302(0, address++);
g_hour = Read_DS1302(0, address++);
g_date = Read_DS1302(0, address++);
g_month = Read_DS1302(0, address++);
g_day = Read_DS1302(0, address++);
g_year = Read_DS1302(0, address);
}
/******************************************/
void DisplayTime(void)
{
Display(3,1,g_year);
Display(6,1,g_month);
Display(9,1,g_date);
Display(1,2,g_hour);
Display(4,2,g_mintue);
Display(7,2,g_second);
LCD_write_string(14,1,&Day[(g_day-1)][0]);
}
/*******************************************/
void Display(uint8_t x, uint8_t y, uint8_t g_data)
{
uint8_t temp_h,temp_l,m,n;
temp_h = temp_l = g_data;
temp_h &= 0xf0;
temp_h >>= 4;
temp_l &= 0x0f;
m = x;
n = y;
LCD_set_xy(m,n);
LCD_write_byte(0,Number[temp_h]);
LCD_write_byte(0,Number[temp_l]);
}
/******************************************/
void Write(uint8_t RAM_CLOCK, uint8_t Address, uint8_t Dat)
{
uint8_t i,j,temp=0x80;
temp &=~ 0x01;
if(RAM_CLOCK == 1)
{
temp |= (1<<6);
}
else
{
temp &=~(1<<6);
}
Address <<= 1;
temp |= Address;
SET_RST();
NOP();
for(j=0; j<2; j++)
{
for(i=0; i<8; i++)
{
if((temp & 0x01) == 0)
{
CLR_IO();
}
else
{
SET_IO();
}
NOP();
SET_SCLK();
NOP();
NOP();
CLR_SCLK();
NOP();
CLR_IO();
temp >>= 1;
}
temp = Dat;
}
CLR_IO();
NOP();
CLR_RST();
}
/******************************************/
unsigned char Read_DS1302(uint8_t RAM_CLOCK, uint8_t Address)
{
uint8_t i,temp=0x80;
temp |= 0x01;
if(RAM_CLOCK == 1)
{
temp |= (1<<6);
}
else
{
temp &=~(1<<6);
}
Address <<= 1;
temp |= Address;
SET_RST();
NOP();
for(i=0; i<7; i++)
{
if((temp & 0x01) == 0)
{
CLR_IO();
}
else
{
SET_IO();
}
NOP();
SET_SCLK();
NOP();
NOP();
CLR_SCLK();
NOP();
CLR_IO();
temp >>= 1;
}
SET_IO();
NOP();
SET_SCLK();
SET_IO_INPUT();
NOP();
for(i=0; i<8; i++)
{
CLR_SCLK();
NOP();
if((IO_PIN & IO) == 0)
{
temp &= ~0x80;
}
else
{
temp |= 0x80;
}
//temp >>=1;
if(i<7)
{
SET_SCLK();
temp >>=1;
}
else
{
CLR_SCLK();
}
NOP();
NOP();
}
CLR_IO();
NOP();
CLR_RST();
return (temp);
}
/******************************************/
void DS18B20(void)
{
uint8_t Th, Tl, i;
i=Init_18b20();
Write_1_byte(0xcc);
Write_1_byte(0x44);
Delay_nms(100);
i=Init_18b20();
Write_1_byte(0xcc);
Write_1_byte(0xbe);
Tl=Read_1_byte();
Th=Read_1_byte();
Temperature(Th,Tl);
//Temperature(12,45);
Delay_nms(100);
}
/*****************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -