TA的每日心情 | 奋斗 2018-2-22 19:39 |
---|
签到天数: 316 天 连续签到: 1 天 [LV.8]以坛为家I
|
程序出了点问题请帮忙看一下:
屏幕上显示的一直在闪烁,希望它能稳定的静态变化。。。
#include"reg52.h"
#include<intrins.h>//_nop_4微
sbit RS = P2^4; //定义端口
sbit RW = P2^5;
sbit EN = P2^6;
#define RS_CLR RS=0
#define RS_SET RS=1
#define RW_CLR RW=0
#define RW_SET RW=1
#define EN_CLR EN=0
#define EN_SET EN=1
#define DataPort P0
#define uchar unsigned char
#define uint unsigned int
void delay(uchar c)
{
uchar a,b;
for(a=110;a>0;a--)
for(b=c;b>0;b--);
}
uchar hwtime,jieshou_ok,chuli_ok;
uchar code duanma[]="0123456789ABCDEF";
uchar table[8];
uchar hwleima[4];
uchar hwweishu[33];
/*******1602函数**************/
void LCD_Write_Com(uchar com)
{
delay(10) ;
RS_CLR;
RW_CLR;
EN_SET;
DataPort= com;
_nop_();
EN_CLR;
}
void LCD_Write_Data(uchar Data)
{
delay(10) ;
RS_SET;
RW_CLR;
EN_SET;
DataPort= Data;
_nop_();
EN_CLR;
}
void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s)
{
if (y == 0)
{
LCD_Write_Com(0x80 + x); //表示第一行
}
else
{
LCD_Write_Com(0xC0 + x); //表示第二行
}
while (*s)
{
LCD_Write_Data( *s);
s ++;
}
}
/*------------------------------------------------
清屏函数
------------------------------------------------*/
void LCD_Clear(void)
{
LCD_Write_Com(0x01);
delay(10) ;
}
/*------------------------------------------------
初始化函数
------------------------------------------------*/
void LCD_Init(void)
{
LCD_Write_Com(0x38); /*显示模式设置*/
delay(10) ;
LCD_Write_Com(0x38);
delay(10) ;
LCD_Write_Com(0x38);
delay(10) ;
LCD_Write_Com(0x38);
LCD_Write_Com(0x08); /*显示关闭*/
LCD_Write_Com(0x01); /*显示清屏*/
LCD_Write_Com(0x06); /*显示光标移动设置*/
delay(10) ;
LCD_Write_Com(0x0C); /*显示开及光标设置*/
}
/********************************/
void quchu()
{
table[0]=duanma[hwleima[0]/16];
table[1]=duanma[hwleima[0]%16];
table[2]=duanma[hwleima[1]/16];
table[3]=duanma[hwleima[1]%16];
table[4]=duanma[hwleima[2]/16];
table[5]=duanma[hwleima[2]%16];
table[6]=duanma[hwleima[3]/16];
table[7]=duanma[hwleima[3]%16];
}
void display()
{
static uchar i;
quchu();
LCD_Init();
LCD_Clear();//清屏
LCD_Write_Com(0xc0+1);
for(i=0;i<8;i++)
{
LCD_Write_Data(table[i]);
}
LCD_Write_String(1,0,"hongwai jiema:");
}
void time0() interrupt 1
{
hwtime++;
}
void time1() interrupt 3
{
TH1=(65535-2000)/256;
TL1=(65535-2000)%256;
//display();
}
void external() interrupt 0
{
uchar starflag;
static char i;//必须为静态成员
if(starflag)
{
if(hwtime<63&&hwtime>=33)
i=0;
hwweishu[i]=hwtime;
hwtime=0;
i++;
if(i==33)
{
jieshou_ok=1;
i=0;
}
}
else //必要的判断;舍弃错误的头码信号;重置
{
hwtime=0;
starflag=1;
}
}
void itime0()
{
EA=1;
TMOD=0x02;
TH0=0x00;
TL0=0x00;
ET0=1;
TR0=1;
}
void itime1()
{
TMOD|=0x01;//很有必要
TH1=(65535-2000)/256;
TL1=(65535-2000)%256;
ET1=1;
TR1=1;
EA=1;
}
void iexternal()
{
EX0=1;
IT0=1;
EA=1;
}
void chuli()
{
uchar i,j;
uchar k=1,dats,shuzhi;
for(i=0;i<4;i++)
{
for(j=0;j<8;j++)//j值非常关键
{
dats=hwweishu[k];
if(dats>7)
shuzhi|=0x80;
if(j<7)//控制右移7次,0>>7=0x00;1>>7=0xff;
{
shuzhi>>=1;
}
k++;
}
hwleima[i]=shuzhi;
shuzhi=0;
}
}
void main()
{
itime0();
itime1();
iexternal();
while(1)
{ display();
if(jieshou_ok)
{
chuli();
jieshou_ok=0;
}
if(chuli_ok)
{
quchu();
}
}
}
|
|