先把现在完成的主程序的代码贴出来哈!
#include "STC12C5A60AD.H"
#include "MY_IO_DEFINE.H"
#include "BASIC_VAL_FUN.H"
#include "COM_PC.H"
#include "AT24Cxx.H"
#include "DS1302.H"
#include "LCD1602.H"
#include <intrins.h>
/******************************定义全局变量*********************************/
unsigned char second,minute,hour,day,week,month,year; //秒、分、时、星期、日、月、年
unsigned char time[8] = {0xde, 0, 0, 0, 0, 0, 0, 0};
uchar code initial[] = " 2010-06-07 MON ";
uchar code displaytime[] = " 12:30:00 ";
uchar code weekdis[] = "MONTUEWEDTHUFRISATSUN";
uchar code dispos[] = {0x80, 0xca, 0xc7, 0xc4, 0x89, 0x86, 0x8c, 0x83};
uchar num;
void InitialLCDTime(void)
{
WriteLCDCOM(0x80);
for(num=0;num<16;num++)
{
WriteLCDData(initial[num]);
delayms(5);
}
WriteLCDCOM(0x80 + 0x40);
for(num=0;num<16;num++)
{
WriteLCDData(displaytime[num]);
delayms(5);
}
}
/****************************************************************************
函数功能:LCD显示子程序
入口参数:k
出口参数:
****************************************************************************/
void ShowTime(void)
{
WriteLCDCOM(0x80+0x40+0x04); //小时
delayms(3);
WriteLCDData(hour/16 + '0');
delayms(15);
WriteLCDData(hour%16 + '0');
delayms(15);
WriteLCDCOM(0x80+0x40+0x07); //分钟
delayms(3);
WriteLCDData(minute/16 + '0');
delayms(15);
WriteLCDData(minute%16 + '0');
delayms(15);
WriteLCDCOM(0x80+0x40+0x0a); //秒
delayms(3);
WriteLCDData(second/16 + '0');
delayms(15);
WriteLCDData(second%16 + '0');
delayms(15);
WriteLCDCOM(0x80+0x03); //年
delayms(3);
WriteLCDData((year>>4&0x0f) + '0');
delayms(15);
WriteLCDData((year&0x0f) + '0');
delayms(15);
WriteLCDCOM(0x80+0x06); //月
delayms(3);
WriteLCDData(month/16 + '0');
delayms(15);
WriteLCDData(month%16 + '0');
delayms(15);
WriteLCDCOM(0x80+0x09); //日
delayms(3);
WriteLCDData(day/16 + '0');
delayms(15);
WriteLCDData(day%16 + '0');
delayms(15);
WriteLCDCOM(0x80+0x0c); //周
delayms(3);
// for(i=0; i<3; i++);
{
WriteLCDData(weekdis[(week%16-1)*3]);
delayms(15);
WriteLCDData(weekdis[(week%16-1)*3 + 1]);
delayms(15);
WriteLCDData(weekdis[(week%16-1)*3 + 2]);
delayms(15);
}
}
/*****************************************************************************
函数功能:初始化DS1302子程序
入口参数:time[](全局变量)
出口参数:
*****************************************************************************/
void WriteTime()
{
WriteDS1302(WRITE_PROTECT,UNLOCK); //写保护寄存器,在对时钟或RAM写前WP一定要为0
WriteDS1302(WRITE_HOUR,hour); //小时
WriteDS1302(WRITE_SECOND,second); //秒
WriteDS1302(WRITE_MINUTE,minute); //分
WriteDS1302(WRITE_DAY, day); //日
WriteDS1302(WRITE_MONTH, month); //月
WriteDS1302(WRITE_WEEK, week); //星期
WriteDS1302(WRITE_YEAR, year); //年
WriteDS1302(WRITE_PROTECT,LOCK); //写保护寄存器
}
/*****************************************************************************
函数功能:读DS1302时间子程序
入口参数:
出口参数:全局变量(second,minute,hour,week,day,month,year)
*****************************************************************************/
void ReadTime()
{
second=ReadDS1302(READ_SECOND); //秒寄存器
minute=ReadDS1302(READ_MINUTE); //分
hour=ReadDS1302(READ_HOUR); //时
week=ReadDS1302(READ_WEEK); //星期
day=ReadDS1302(READ_DAY); //日
month=ReadDS1302(READ_MONTH); //月
year=ReadDS1302(READ_YEAR); //年
}
void InitialTime()
{
second = 0x00;
minute = 0x00;
hour = 0x12;
month = 0x01;
year = 0x10;
day = 0x01;
week = 0x05;
}
void main()
{
uchar LED;
uchar i, j, k;
uchar uLedDelayCount;
uchar uLedWei; // 8端数码管的位选信号
uchar code disled[10] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};
uchar code array[8] = {0xFE, 0xFD, 0xFB, 0xF7, 0xEF, 0xDF, 0xBF, 0x7F};
// 1,实现跑马灯
/*
LED = 0xFE;
P1 = 0xFF;
delayms(500);
for(i = 0; i<7; i++)
{
P1 = LED;
delayms(200);
LED = _crol_(LED,1); //循环左移1位,点亮下一个LED
}
LED = 0x7F;
for(i = 0; i<8; i++)
{
P1 = LED;
LED = _cror_(LED,1); //循环右移1位,点亮下一个LED
delayms(200);
}
*/
// 2,实现数码管
// 问题: 复位的时候发现第8个LED灯会显示8,为什么呢?
// 而且在不放单片机的时候 ,第1个LED灯会亮
/*
P0 = 0x00;
delayus(10);
// 位选 74HC138 // 输出啥是啥 没有锁存
// 段选 74HC573 // 需要先使能输出,再禁止输出
// 要实现的功能 : 第1个LED亮500ms ;第1、2个LED同时亮500ms; 第1、2、3个LED同时亮500ms, 以此类推,直到8个LED灯同时点亮500ms后 全部熄灭
// 也可用500ms的定时器实现
for (k = 1; k<=8; k++)
{
uLedDelayCount = 500 / k; // 其实是 500/2/k
for(j = 0; j<uLedDelayCount; j++)
{
uLedWei = 0;
for(i = 0; i<k ; i++)
{
P0 = 0x00; // 先熄灭所有的LED段选
P2 = uLedWei; // 位选数据,74HC138
LED_DUAN = 1; // 74HC573 锁存使能端
P0 = disled[i]; // 段选数据,74HC573
LED_DUAN = 0;
delayms(1);
uLedWei = uLedWei + 1;
}
}
}
// 熄灭8位LED灯
LED_DUAN = 1;
P0 = 0;
LED_DUAN = 0;
*/
// 3,实现LCD1602 或者 LCD12864
InitialLCD();
ShowLCDMsg(8);
// 4,实现串口通信
Initial_Com_PC(Baud_Rate_9600);
SendOneByte(0xFE);
// 5,实现24C02
P1 = 0xFF;
AT24CxxDisableProtect();
AT24CxxInit();
AT24CxxWriteMulByte(0x10, array, 8);
P1 = AT24CxxReadSet(0x16); //从指定地址“0x16中读取数据并送P1口显 0xBF -> 0b10111111 P1_6口灯亮
// 6,实现DS1302
InitialLCDTime();
InitialTime();
WriteTime();
ReadTime();
ShowTime();
// 7,实现红外发射接收
// 8, 实现AD采集
// 9,把以上功能统一起来,并加入按键控制
while(1)
{
}
}
下载地址
code.rar