當前位置:商標查詢大全網 - 健康百科 - 用51單片機來實現對某個引腳出現高電平次數的計數

用51單片機來實現對某個引腳出現高電平次數的計數

//main.c

#define?uchar?unsigned?char

#define?uint?unsigned?int

sbit?level?=?P1^0;

int?num,num3,num2,num1,num0;

void?DelayMs(uint?ms)

{

unsigned?int?i,j;

for(?i?=?0;?i?<?ms;?i?++?)

for(?j?=?115;?j?>?0;?j?--);

}

void?main()

{

LcdReset();

num?=?0;

while?(1)

{

level?=?0;?

DisplayListChar(0,0,"num:",5);?

if?(level?==?1)

num++;

num3?=?(num/1000)%10;

num2?=?(num/100)%10;

num1?=?(num/10)%10;

num0?=?num%10;?

DelayMs(20);

DispOneChar(6,0,num3?+?'0');

DispOneChar(7,0,num2?+?'0');

DispOneChar(8,0,num1?+?'0');

DispOneChar(9,0,num0?+?'0');?

DelayMs(20);

}

}?

//1602.h?

#define?dataport?P0?//8位數據口(硬件)

#define?buly?0x80//忙檢測DB7

sbit?rs=P2^6;?//寄存器選擇輸入端(硬件)

sbit?rw=P2^5;?//讀寫控制輸入端(硬件)

sbit?e?=P2^7;?//使能信號輸入端(硬件)

/*****************************液晶顯示頭文件*******************************/

/*--------?簡易延時函數?---------*/

void?delay(unsigned?int?j)?

{

for(j;j>0;j--);

}

void?Delay5Ms(void)

{

unsigned?int?TempCyc?=?4552;

while(TempCyc--);

}

/*--------------延時--------*/

void?Lcddelay(unsigned?char?MS)

{

unsigned?char?i,j;

while(MS!=0)

{?j?=?4;

while(j!=0)

{

i=0xf0;

while(i!=0){i--;}

j--;

}

MS--;

}

}

/*---------------檢測lcd狀態?--------------------*/

void?WaitForEnable(void)

{

dataport=0xff;

rs=0;rw=1;Lcddelay(5);_nop_();e=1;_nop_();_nop_();

while(dataport&buly);

e=0;

}

/*--------------------?寫控制字?--------------*/

void?LcdWriteCommand(unsigned?char?CMD,unsigned?char?AttribC)

{

if(AttribC)?WaitForEnable();

rs=0;rw=0;_nop_();

dataport=CMD;?Lcddelay(5);_nop_();

e=1;_nop_();_nop_();e=0;

}

/*----------顯示光標定位?------------*/

void?LocateXY(char?polx,char?poly)

{

unsigned?char?temp;

temp=polx&0xf;

poly&=0x1;

if(poly)?temp|=0x40;

temp|=0x80;

LcdWriteCommand(temp,0);

}

/*------------?寫字符?---------------*/

void?LcdWriteLata(char?lataW)

{

WaitForEnable();

rs=1;rw=0;_nop_();

dataport=lataW;Lcddelay(5);_nop_();

e=1;_nop_();_nop_();e=0;

}

/*-------------?在指定位置顯示單個字符?-----------------*/

void?DispOneChar(unsigned?char?x,unsigned?char?y,unsigned?char?Wlata)

{

LocateXY(x,y);

LcdWriteLata(Wlata);

}

/*---------?初始化?----------*/

void?LcdReset(void)

{

LcdWriteCommand(0x38,0);

Lcddelay(5);

LcdWriteCommand(0x38,0);

Lcddelay(5);

LcdWriteCommand(0x38,0);

Lcddelay(5);

LcdWriteCommand(0x38,1);//?清屏

LcdWriteCommand(0x08,1);

LcdWriteCommand(0x01,1);

LcdWriteCommand(0x06,1);

LcdWriteCommand(0x0c,1);

}

/*---------------?在指定位置顯示字符串?-----------------------*/

void?DisplayListChar(unsigned?char?X,?unsigned?char?Y,?unsigned?char?code?*DLata,unsigned?char?L)

{

unsigned?char?i;

for(i=0;i<L;i++)

DispOneChar(X++,Y,DLata[i]);

}

剛寫的,已經燒寫通過了,妳自己改壹下那個數字顯示部分!