0804_serial port

其实这个程序总的来说是有问题的

仿真图:

MacroAndConst.h

 

#ifndef _MACRO_AND_CONST_H_
#define _MACRO_AND_CONST_H_

typedef unsigned int uint16;
typedef unsigned int UINT;
typedef unsigned int uint;
typedef unsigned int UINT16;
typedef unsigned int WORD;
typedef unsigned int word;
typedef int int16;
typedef int INT16;
typedef unsigned long uint32;

typedef unsigned long UINT32;
typedef unsigned long DWORD;
typedef unsigned long dword;
typedef long int32;
typedef long INT32;
typedef signed char int8;
typedef signed char INT8;
typedef unsigned char byte;
typedef unsigned char BYTE;
typedef unsigned char uchar;
typedef unsigned char UINT8;
typedef unsigned char uint8;
typedef unsigned char BOOL;

#endif

 

led.h

#ifndef _LED_H_
#define _LED_H_

//typedef P0 Seg;
//typedef P2 Bit;
extern void display(uint db);

#endif

 

led.c

#include <reg52.h>
#include "MacroAndConst.h"
//#include "delay.h"
#include "time.h"

/*---------共阴极0~f数码管编码 ----------*/
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
/*****typedef int int16; 有符号的******/
static int16 LedTimeCount1=0; //LED计数器
static int16 LedTimeCount2=0;
static int16 LedTimeCount3=0;
static int16 LedTimeCount4=0;


void display(uint db) //数码管显示函数,用于显示模数转换后得到的数字量
{
uchar bw,sw,gw; //bw,sw,gw分别等于db百位,十位,个位上的数
db=db*100/51;
bw=db/100;
sw=db%100/10;
gw=db%10;

P2=0xfe; //点亮第一只数码管
P0=table[bw]|0x80; //最高位置0,点亮第一只数码管的小数点.


if(Time500us)
{
/*
Time500us=0;
LedTimeCount1++;
LedTimeCount2++;
LedTimeCount3++;
LedTimeCount4++;
if(LedTimeCount1>=4)
{
LedTimeCount1=-16;//LED计数器复位
//delay(5);
P2=0xfd; //点亮第二只数码管
P0=table[sw];
}
if(LedTimeCount2>=8)
{
LedTimeCount2=-16;
//delay(5);
P2=0xfb; //点亮第三只数码管
P0=table[gw];
}
if(LedTimeCount3>=12)
{
LedTimeCount3=-16;
//delay(5);
P2=0xf7; //点亮第四只数码管
P0=table[0]; //第四只数码管一直显示0
}
if(LedTimeCount4>=16)
{
LedTimeCount1=0;
LedTimeCount2=0;
LedTimeCount3=0;
LedTimeCount4=0;
//delay(5);
}
*/


Time500us=0;
LedTimeCount1++;
LedTimeCount2++;
LedTimeCount3++;
LedTimeCount4++;
if(LedTimeCount1>=4)
{
LedTimeCount1=0;//LED计数器复位
//delay(5);
P2=0xfd; //点亮第二只数码管
P0=table[sw];
if(LedTimeCount2>=8)
{
LedTimeCount2=0;
//delay(5);
P2=0xfb; //点亮第三只数码管
P0=table[gw];
if(LedTimeCount3>=12)
{
LedTimeCount3=0;
//delay(5);
P2=0xf7; //点亮第四只数码管
P0=table[0]; //第四只数码管一直显示0
if(LedTimeCount4>=16)
{
LedTimeCount4=0;
//delay(5);
}
}
}
}
}
}

 

time.h

#ifndef _TIME_H_
#define _TIME_H_

extern void Timer0Init(void);
extern bit Time500us;

#endif

 

time.c

#include <reg52.h>

bit Time500us = 0 ; // 500us时间片


/**********定时器初始化*******************/
void Timer0Init(void)
{
TMOD= 0x01;//定时器 0 工作方式 1
TH0 = 0xfe ; //定时器初始值500us
TL0 = 0x33 ;
TR0 = 1 ;
ET0 = 1 ;
EA = 1 ; //中断允许总控制位
}

void Time0Isr(void) interrupt 1
{
TH0 = 0xfe ; //500us
TL0 = 0x33 ;
Time500us = 1 ; //500uS 时标标志位置位
}

 

main.c

#include<reg52.h>
#include <intrins.h>
#include "macroandconst.h"
#include "led.h"
//#include "delay.h"
#include "time.h"

sbit wr=P3^6;
sbit rd=P3^7;
static uint16 TimeCount = 0 ; //计数器 typedef int int16; 有符号的

void main(void)
{
uchar i;
Timer0Init();
while(1)
{
wr=0; //在片选信号CS为低电平情况下(由于CS接地,所以始终为低电平),
_nop_(); //WR由低电平到高电平时,即上升沿时,AD开始采样转换
wr=1;
if(Time500us)
{ // delay(1); //延时1ms,等待采样转换结束,延时函数 delay(1)延时0.992ms,大约为1ms
// P1=0xff; //这条语句不能少,我也还不知道为什么
Time500us=0; //
TimeCount++;
if(TimeCount>=2)
{
TimeCount=0; //计数器复位

rd=0; //将RD脚置低电平后,再延时大于135ns左右(这里延时1us),
_nop_(); //即可从DB脚读出有效的采样结果,传送到P1口
for(i=0;i<10;i++) //刷新显示一段时间
display(P1); //显示从DB得到的数字量
}
}
}
}

posted @ 2014-03-05 21:32  clsong  阅读(323)  评论(0编辑  收藏  举报