新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > 簡易12684液晶和Atmega32的電子萬年歷

簡易12684液晶和Atmega32的電子萬年歷

作者: 時間:2013-12-12 來源:網絡 收藏


/**********************************************/

//從DS1302讀一個字節(jié)數(shù)據(jù)
unsigned char DS1302_ReadByte(void)
{
unsigned char i,dat = 0; //dat存放讀出的數(shù)據(jù),初始化為0
PORTB = ~(1 PB1); //DS1302的I/O口上拉不使能,
DDRB = ~(1 PB1); //DS1302的I/O口設置為輸入口,準備讀數(shù)據(jù)

for(i = 0;i 8;i++) //讀8位,低位在前,右移
{
dat >>= 1; //讀出的數(shù)據(jù)右移一位
PORTB |= (1 PB0); //DS1302的SCLK端口拉高
Delayus(10); //
PORTB = ~(1 PB0); //DS1302的SCLK端口拉低,產生下降沿,
Delayus(10);
if(PINB (1 PB1)) //讀數(shù)據(jù)端口狀態(tài)
{
dat |= 0x80; //如果數(shù)據(jù)端口位高,相應數(shù)據(jù)位置1
}
}
DDRB |= (1 PB1); //最后將數(shù)據(jù)端口設置為輸出
return dat; //返回讀出的數(shù)據(jù)
}

//向DS1302寫一個字節(jié)數(shù)據(jù)
void DS1302_WriteByte(unsigned char dat)
{
unsigned char i;

for(i = 0;i 8;i++) //寫8位,低位在前
{
PORTB = ~(1 PB0); //DS1302的SCLK置低
if(dat 0x01) //寫數(shù)據(jù)位
{
PORTB |= (1 PB1); //如果該位為1,則I/O口置高
}
else
{
PORTB = ~(1 PB1); //如果該位為0,則I/O口置低
}
Delayus(10); //
PORTB |= (1 PB0); //DS1302的SCLK置高,產生上升沿
dat >>= 1; //數(shù)據(jù)右移1位
}
}

//從DS1302的指定地址讀一個字節(jié)數(shù)據(jù)
unsigned char DS1302_ReadData(unsigned addr)
{
unsigned char data;

PORTB = ~(1 PB2); //拉低片選端
PORTB = ~(1 PB0);//拉低時鐘端
Delayus(10);
PORTB |= (1 PB2);//拉高片選端
Delayus(10);
DS1302_WriteByte(addr);//寫入操作命令(地址)
Delayus(10);
data = DS1302_ReadByte();//讀出數(shù)據(jù)
Delayus(10);
PORTB = ~(1 PB0); //拉低時鐘端
PORTB = ~(1 PB2); //拉低片選端

return data;
}

//向DS1302的指定地址寫一個字節(jié)數(shù)據(jù)
void DS1302_WriteData(unsigned char addr,unsigned data)
{
PORTB = ~(1 PB2); //拉低片選端
PORTB = ~(1 PB0);//拉低時鐘端
Delayus(10);
PORTB |= (1 PB2);//拉高片選端
Delayus(10);
DS1302_WriteByte(addr);//寫入操作命令(地址)
Delayus(10);
PORTB = ~(1 PB0);//拉低時鐘端
Delayus(10);
DS1302_WriteByte(data);//寫入數(shù)據(jù)
PORTB = ~(1 PB0); //拉低時鐘端

Delayus(10);
PORTB = ~(1 PB2); //拉低片選端
}

//對DS1302設置時間
void DS1302_SetTime(unsigned char *time)
{
unsigned char i;
unsigned char addr = 0x80;//寫入地址從秒寄存器開始

DS1302_WriteData(WR_PROTECT | WR,UPROTECT);//控制命令,WP位為0,允許寫操作
Delayms(5);
for(i = 0;i 7;i++)
{
DS1302_WriteData(addr | WR,time[i]);// 秒 分 時 日 月 星期 年
addr += 2;
Delayms(1);
}
DS1302_WriteData(WR_PROTECT | WR,PROTECT);//控制命令,WP位為1,不允許寫操作
}

//從DS1302讀取時間
void DS1302_GetTime(void)
{
unsigned char i;
PORTB = ~(1 PB2);
Delayus(10);
PORTB |= (1 PB2);
Delayus(10);
DS1302_WriteByte(0xbf);
for(i = 0;i 8;i++)
{
Get_Time[i] = DS1302_ReadByte();
}
PORTB = ~(1 PB2);
PORTB = ~(1 PB0);
}

//DS1302是否工作檢測
unsigned char DS1302_Check(void)
{
DS1302_WriteData(WR_PROTECT | WR,UPROTECT);
DS1302_WriteData(RAMBASE | WR,0x31);

if(DS1302_ReadData(RAMBASE | WR) == 0x31)
{
return TURE;
}
else
{
return FALSE;
}
}

void DS1302_DisCharge(void)
{
DS1302_WriteData(CHARGE|WR,TC_DISABLED);
Delayus(10);
}

unsigned char DS1302_Alarm(unsigned char hour,unsigned char min)
{

}

void System_Beep(void)
{

}

//DS1302初始化
void DS1302_Init(void)
{
DS1302_WriteData(WR_PROTECT | WR,UPROTECT); //寫入寫允許命令
DS1302_WriteData(SECOND | WR,CLK_START); //啟動振蕩器,DS1302開始工作
DS1302_WriteData( WR_PROTECT | WR,PROTECT); //控制命令,WP位為1,不允許寫操作
}


//us級別的延時函數(shù)
void Delayus(unsigned int lus)
{
while(lus--)
{
_delay_loop_2(3); //_delay_loop_2(1)是延時4個時鐘周期,參數(shù)為3則延時12
//個時鐘周期,本實驗用12M晶體,則12個時鐘周期為12/12=1us
}
}

//ms級別的延時函數(shù)
void Delayms(unsigned int lms)
{
while(lms--)
{
Delayus(800); //延時1ms
}
}

晶振相關文章:晶振原理

上一頁 1 2 3 下一頁

評論


相關推薦

技術專區(qū)

關閉