搜索
bottom↓
回复: 2

AT89C51端口的扩展问题

[复制链接]

出0入0汤圆

发表于 2009-10-28 09:30:53 | 显示全部楼层 |阅读模式
关于AT89C51的端口扩展。急求哪位高手帮忙解决。我的万年历没有星期显示 ,想再加个星期显示,用的是138扩展的C51的端口 但是不知道对不对
也不知道程序部分怎么改。原图是
原图 (原文件名:123.jpg)


扩展后的图 (原文件名:75B772E6-5D22-48E0-A49D-41BDE57AB93F.jpg)
下面的是源程序:
#define uchar unsigned char
#define uint  unsigned int
#define DQ P3_7
#include<AT89X51.H>

uint sec;
uint min=50;
uint hour=9;
uint day=16;
uint month=12;
uint yearl=8;
uint yearh=20;
uint tcnt;
uint cursor=0;
uchar a=0xff;
uchar code Seg[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

void delay(uint t)
{
    uint i;
    while(t--)
    {for (i=0;i<125;i++);}
}
void Tdelay(unsigned int i)
{
while(i--);
}
void Kdelay()
{
    uchar i,j;
    for(i=100;i>0;i--)
    for(j=248;j>0;j--);
}
Init_DS18B20(void)
{
unsigned char x=0;
DQ = 1;   
Tdelay(8);  
DQ = 0;   
Tdelay(80);
DQ = 1;   
Tdelay(14);
Tdelay(20);
}
//读一个字节
ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
  DQ = 0;
  dat>>=1;
  DQ = 1;
  if(DQ)
   dat|=0x80;
  Tdelay(4);
}
return(dat);
}

//写一个字节
WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
  DQ = 0;
  DQ = dat&0x01;
  Tdelay(5);
  DQ = 1;
  dat>>=1;
}
}

//读取温度
ReadTemperature(void)
{
unsigned char a=0;
unsigned char b=0;
unsigned int t=0;
float tt=0;
Init_DS18B20();
WriteOneChar(0xCC);
WriteOneChar(0x44);
Init_DS18B20();
WriteOneChar(0xCC);
WriteOneChar(0xBE);
a=ReadOneChar();
b=ReadOneChar();
t=b;
t<<=8;
t=t|a;
tt=t*0.0625;        
t= tt*10+0.5;      
return(t);
}

void display(uchar L1,uchar L2,uchar L3,uchar L4,uchar L5,uchar L6,uchar L7,uchar L8,uchar L9,uchar L10,uchar L11,uchar L12,uchar L13,uchar L14,uchar L15,uchar L16)
{
    P2=0x7F;P0=L1;delay(1);  //yearh
    P2=0xBF;P0=L2;delay(1);  //yearh
    if(cursor==6){P2=0xDF|a;P0=L3;delay(1);}else{P2=0xDF;P0=L3;delay(1);}   //yearl
    if(cursor==6){P2=0xEF|a;P0=L4;delay(1);}else{P2=0xEF;P0=L4;delay(1);}   //yearl
    if(cursor==5){P2=0xF7|a;P0=L5;delay(1);}else{P2=0xF7;P0=L5;delay(1);}   //month
    if(cursor==5){P2=0xFB|a;P0=L6;delay(1);}else{P2=0xFB;P0=L6;delay(1);}   //month
    if(cursor==4){P2=0xFD|a;P0=L7;delay(1);}else{P2=0xFD;P0=L7;delay(1);}   //day
    if(cursor==4){P2=0xFE|a;P0=L8;delay(1);}else{P2=0xFE;P0=L8;delay(1);}   //day
    P2=0xFF;
    if(cursor==3){P1_0=0x7F|a;P0=L9;delay(1);}else{P1=0x7F;P0=L9;delay(1);}   //hour
    if(cursor==3){P1_0=0xBF|a;P0=L10;delay(1);}else{P1=0xBF;P0=L10;delay(1);}  //hour
    if(cursor==2){P1=0xDF|a;P0=L11;delay(1);}else{P1=0xDF;P0=L11;delay(1);} //min
    if(cursor==2){P1=0xEF|a;P0=L12;delay(1);}else{P1=0xEF;P0=L12;delay(1);} //min
    if(cursor==1){P1=0xF7|a;P0=L13;delay(1);}else{P1=0xF7;P0=L13;delay(1);} //sec
    if(cursor==1){P1=0xFB|a;P0=L14;delay(1);}else{P1=0xFB;P0=L14;delay(1);} //sec
    P1=0xFD;P0=L15;delay(1); //temp
    P1=0xFE;P0=L16;delay(1); //temp
    P1=0xFF;
}

main()
{   
    uint i;   
    TMOD=0x02;            //设置模式为定时器T0的模式2 (8位自动重装计数初值的计数值)        
    TH0=0x06;             //设置计数器初值,靠TH0存储重装的计数值X0=256-250=6
    TL0=0x06;
    TR0=1;                //启动T0
    ET0=1;                //开启定时器T0中断允许
    EA=1;                 //开启中断总控制
    while(1)
    {
        if(P3_0==0)
        {
            Kdelay();
            if(P3_0==0)
            {
                cursor++;
                if(cursor>=7){cursor=0;}
            }
        }
        if(P3_1==0)
        {
            Kdelay();
            if(P3_1==0)
            {
                if(cursor==1){sec++;if(sec==60)sec=0;}
                if(cursor==2){min++;if(min==60)min=0;}
                if(cursor==3){hour++;if(hour==24)hour=0;}
                if(cursor==4){day++;if(day==31)day=0;}
                if(cursor==5){month++;if(month==12)month=0;}
                if(cursor==6){yearl++;if(yearl==100)yearl=0;}
                if(cursor==7){yearh++;if(yearh==30)yearh=20;}
            }
        }
        if(P3_2==0)
        {
            Kdelay();
            if(P3_2==0)
            {
                if(cursor==1){sec--;}
                if(cursor==2){min--;}
                if(cursor==3){hour--;}
                if(cursor==4){day--;}
                if(cursor==5){month--;}
                if(cursor==6){yearl--;}
                if(cursor==7){yearh--;}
            }
        }
        i=ReadTemperature();
        display(Seg[yearh/10],Seg[yearh%10],Seg[yearl/10],Seg[yearl%10],Seg[month/10],Seg[month%10],Seg[day/10],Seg[day%10],Seg[hour/10],Seg[hour%10],Seg[min/10],Seg[min%10],Seg[sec/10],Seg[sec%10],Seg[i/100],Seg[i/10%10]);     
    }
}

void t0(void)interrupt 1 using 0   //t0的中断程序
{
    tcnt++;
    if(tcnt==4000)//定时器的定时计数,4000次250us为1秒
    {
        tcnt=0;
        P3_3=~P3_3;
        a=~a;
        sec++;
        if(sec==60)
        {     
            sec=0;
            min++;
            if(min==60)
            {
                min=0;
                hour++;
                if(hour==24)
                {
                    hour=0;
                    day++;
                    if(month==2&&((yearl==0&&yearh%4==0)||(yearl!=0&&yearl%4==0))&&day==30)day=1;  
                    else if(month==2&&day==29)day=1;
                    else if((month==4||month==6||month==9||month==11)&&day==31)day=1;
                    else if(day==32)day=1;
                    if(day==1)
                    {
                    month++;
                    if(month==13)
                    {
                        month=1;
                        yearl++;
                        if(yearl==100)
                        {
                            yearl=0;
                            yearh++;
                            if(yearh==100)
                            {
                                yearh=20;
                            }
                        }
                    }
                }
                }
            }
        }
    }
}
请哪位高手帮忙改下
如果东西部够详细请留下邮箱 我将原文件发过去
十分感谢!!万分感谢!!!

阿莫论坛20周年了!感谢大家的支持与爱护!!

知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)

出0入0汤圆

发表于 2009-10-28 09:41:00 | 显示全部楼层
还有3个空闲IO,为什么要加138扩展?

出0入0汤圆

 楼主| 发表于 2009-10-31 20:28:03 | 显示全部楼层
哦  谢谢 我菜鸟
那我要是用P3.4 P3.5的话程序要怎么改啊
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-7-23 17:19

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表