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

📄 serio.c

📁 基于51单片机的时钟芯片DS1302的驱动程序。。。希望对大家有用
💻 C
字号:
//serio.c
/*******************************************************************/
#include "serio.h"
#include "reg52.h"
#define uchar unsigned char
#define uint32 unsigned long
/*******************************************************************/
void UART0_Init()//initial uart
{
SCON = 0x50;
//SCON: serail mode 1, 8-bit UART, REN=1,Buard=(2^SMOD)(Timer1 溢出率 )/32
TMOD |= 0x20; //TMOD:timer1 mode 2,8 位reload
PCON |= 0x80; //SMOD=1;
TH1 = 0xFD; //fosc=11.0592MHz,smod=0, Baud:4.8k--TH:F4,9.6K--TH:FD;
TL1 = 0xFD; //fosc=11.0592MHz,smod=1, Baud:19.2K--TH:FD;
IE |= 0x90; //ea=1,es=1,Enable Serial Interrupt
TR1 = 1; // timer 1 run
}
/*******************************************************************/
void UART0_SendByte(uchar da)//send one byte to pc
{
EA = 0;
SBUF = da;
while(TI==0);
TI=0;
EA = 1;
}
/******************************************************************/
void UART0_SendStr(uchar *str)//send one string to pc
{
uchar i;
i=0;
while((*(str+i))!='\0')
{
UART0_SendByte(*(str+i));
i=i+1;
}
}
/******************************************************************/
void UART0_SendUint32(uint32 da)
{
uchar flag;//第一次非0 标志,1 有效
uchar temp;
flag=0;
da=da%100000000;//求余数
temp=da/10000000;//求千万位数
if(temp!=0 || flag!=0)
{
flag=1;
UART0_SendByte(temp+'0');
}
da=da%10000000;
temp=da/1000000;//求百万位数
if(temp!=0 || flag!=0)
{
flag=1;
UART0_SendByte(temp+'0');
}
da=da%1000000;
temp=da/100000;//求十万位数
if(temp!=0 || flag!=0)
{
flag=1;
UART0_SendByte(temp+'0');
}
da=da%100000;
temp=da/10000;//求万位数
if(temp!=0 || flag!=0)
{
flag=1;
UART0_SendByte(temp+'0');
}
da=da%10000;
temp=da/1000;//求千位数
if(temp!=0 || flag!=0)
{
flag=1;
UART0_SendByte(temp+'0');
}
da=da%1000;
temp=da/100;//求百位数
if(temp!=0 || flag!=0)
{
flag=1;
UART0_SendByte(temp+'0');
}
da=da%100;
temp=da/10;//求十位数
if(temp!=0 || flag!=0)
{
flag=1;
UART0_SendByte(temp+'0');
}
da=da%10;
UART0_SendByte(da+'0');//发送个位数
}

void receive(void) interrupt 4
{
uchar temp;
EA=0;
if(RI)
{
temp=SBUF;
switch(count)
{
case 0:if(temp==0x30)
count=1;
else
count=0;
break;
case 1:if(temp==0x30)
count=2;
else
count=0;
break;
case 2:if(temp==0x30)
count=3;
else
count=0;
break;
case 3:clock1.year=(temp&0x0f)<<4;
count=4;
break;
case 4:clock1.year=(temp&0x0f)+clock1.year;
count=5;
break;
case 5:clock1.month=(temp&0x0f)<<4;
count=6;
break;
case 6:clock1.month=(temp&0x0f)+clock1.month;
count=7;
break;
case 7:clock1.date=(temp&0x0f)<<4;
count=8;
break;
case 8:clock1.date=(temp&0x0f)+clock1.date;
count=9;
break;
case 9:clock1.hour=(temp&0x0f)<<4;
count=10;
break;
case 10:clock1.hour=(temp&0x0f)+clock1.hour;
count=11;
break;
case 11:clock1.minute=(temp&0x0f)<<4;
count=12;
break;
case 12:clock1.minute=(temp&0x0f)+clock1.minute;
count=13;
break;
case 13:clock1.sec=(temp&0x0f)<<4;
count=14;
break;
case 14:clock1.sec=(temp&0x0f)+clock1.sec;
count=15;
break;
case 15:clock1.week=(temp&0x0f);
tm1=&clock1;
SetTime(tm1);
count=0;
break;
}
RI=0;
}
else
TI=0;
EA=1;
}

⌨️ 快捷键说明

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