實(shí)驗(yàn)1 單片機(jī)IO口應(yīng)用及數(shù)碼管顯示
1.單片機(jī)驅(qū)動(dòng)蜂鳴器的實(shí)驗(yàn);
a)說明:Lab51單片機(jī)實(shí)驗(yàn)板的蜂鳴器連接到單片機(jī)的P1.5
b)基本要求:控制蜂鳴器每2秒響0.5秒。
#include
#define unit unsigned int
voiddelay(unit x){
unit i=x;
unit j;
for(;i>0;--i){
for(j=x;j>0;--j);
}
}
voidmain()
{
while(1){
P1=0x00;
delay(250);//0.5秒
P1=0xff;
delay(420);//2秒
}
}
2.單片機(jī)驅(qū)動(dòng)繼電器輸出實(shí)驗(yàn);
a)說明:Lab51單片機(jī)實(shí)驗(yàn)板的蜂鳴器連接到單片機(jī)的P1.4
b)基本要求:控制繼電器每5秒吸合0.5秒。
#include#define unit unsigned int
voiddelay(unit x){
unit i=x;
unit j;
for(;i>0;--i){
for(j=x;j>0;--j);
}
}
sbit JiDian=P1^4;
voidmain()
{
while(1){
JiDian=0x00;
delay(250);//0.5秒
JiDian=0xff;
delay(700);//2秒
}
}
3.延時(shí)實(shí)現(xiàn)p2口LED流水燈效果(用循環(huán)移位指令)
#include#define unit unsigned int
unit table[]={~0x01,~0x02,~0x04,~0x08,~0x10,~0x20,~0x40,~0x80};
voiddelay(unit x){
unit i=x;
unit j;
for(;i>0;--i){
for(j=x;j>0;--j);
}
}
voidmain()
{
inti;
P2=0x00;
while(1){
for(i=0;i<8;i++){
P2=table[i];
delay(250);//2秒
}
}
}
4.p2口八個(gè)燈作二進(jìn)制加法。理解二進(jìn)值的計(jì)算
#include#define unit unsigned int
voiddelay(unit x){
unit i=x;
unit j;
for(;i>0;--i){
for(j=x;j>0;--j);
}
}
voidmain(){
while(1){
P2=0xff;
while(P2!=0x00){
P2++;
delay(250);//2秒
}
}
}
5.直接用IO口做位選信號(hào),控制8位數(shù)碼管上顯示1,2,3,4…F,循環(huán)顯示
說明:P0是位選,P2是段選#include
#define unit unsigned int
unit code NumTable[]={0x06,~0x24,~0x30,~0x19,~0x12,~0x02,0x87,0xff,~0x10};
voiddelay(unit x){
unit i=x;
unit j;
for(;i>0;--i){
for(j=x;j>0;--j);
}
}
voidDisplayNumByOrder(unit DelayNum){
inti;
for(i=0;i<=8;i++){
P0=NumTable[i];
delay(DelayNum);//2秒
}
}
voidmain(){
//P0是段選,P2是位選
P2=0x00;
while(1){
DisplayNumByOrder(250);
}
}
6.用譯碼器138做片選信號(hào),控制4位數(shù)碼管上做加1顯示。從0開始.
說明:JP15與JP16用8個(gè)短路冒連接,JP10(P0)與J12用8PIN排線連接#include
#include
#define unit unsigned int
unit code NumTable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//數(shù)字的編碼
//用譯碼器138做位選信號(hào),控制4位數(shù)碼管上做加1顯示。從0開始.
voiddelay_1ms(unit x){
unit i=x;
unit j;
for(;i>0;--i){
for(j=110;j>0;--j);
}
}
/**
在數(shù)碼管上顯示對(duì)應(yīng)的值
**/
voiddisplay(unsignedchark)
{
P0=NumTable[k];
delay_1ms(3);
}
sbit high=P2^4;
sbit mid=P2^3;
sbit low=P2^2;
/**
控制數(shù)碼管顯示后4位,并分解計(jì)數(shù)值
**/
voidDisplayNumByOrder(unit Count,unit delay_1msNum){
low=0;mid=0;high=0;display(0);
low=1;mid=0;high=0;display(0);
low=0;mid=1;high=0;display(0);
low=1;mid=1;high=0;display(0);
low=0;mid=0;high=1;display(Count%10/1);
low=1;mid=0;high=1;display(Count%1/100);
low=0;mid=1;high=1;display(Count%100/10);
low=1;mid=1;high=1;display(Count%10);
}
//一個(gè)是位選,一個(gè)是段選
voidmain(){
intcount=0;
while(1){
count++;
DisplayNumByOrder(count,300);
if(count==9)count=0;
}
}
7.利用動(dòng)態(tài)掃描方法在八位數(shù)碼管上顯示出穩(wěn)定的87654321.
#include#define unit unsigned int
unit table[]={~0x01,~0x02,~0x04,~0x08,~0x10,~0x20,~0x40,~0x80};
unit code NumTable[]={0x06,~0x24,~0x30,~0x19,~0x12,~0x02,0x87,0xff,~0x10};
voiddelay(unit x){
unit i=x;
unit j;
for(;i>0;--i){
for(j=x;j>0;--j);
}
}
voidDisplayNumByOrder(unit DelayNum){
inti;
for(i=0;i<=7;i++){
P0=NumTable[i];
P2=table[i];
delay(DelayNum);//5ms
}
}
//一個(gè)是位選,一個(gè)是段選
voidmain(){
//P0是位選,P2是段選
//P2=0x00;
while(1){
DisplayNumByOrder(5);
}
}
8.數(shù)碼管前三位顯示一個(gè)跑表,從到之間以1%秒速度運(yùn)行,當(dāng)按下一個(gè)獨(dú)立鍵盤時(shí)跑表停止,松開手后跑表繼續(xù)運(yùn)行。
#include#define unit unsigned int
unit code NumTable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//數(shù)字的編碼
//數(shù)碼管前三位顯示一個(gè)跑表,從到之間以1%秒速度運(yùn)行,當(dāng)按下一個(gè)獨(dú)立鍵盤時(shí)跑表停止,松開手后跑表繼續(xù)運(yùn)行。
sbit high=P2^4;
sbit mid=P2^3;
sbit low=P2^2;
sbit IsCountKey=P3^0;//是否計(jì)數(shù)的按鍵
constbit PRESSED=0;//0的時(shí)候運(yùn)行,1的時(shí)候暫停
//監(jiān)測
voiddelay_1ms(unit x){
unit i=x;
unit j;
for(;i>0;--i){
for(j=110;j>0;--j);
}
}
/**
在數(shù)碼管上顯示對(duì)應(yīng)的值
**/
voiddisplay(unsignedcharNum)
{
P0=NumTable[Num];
delay_1ms(1);
P0=0;//送完段選信號(hào)后,進(jìn)行消影的處理
}
/**
控制數(shù)碼管顯示后3位,并分解計(jì)數(shù)值
**/
voidDisplayNumByOrder(unit Count){
low=0;mid=0;high=0;display(0);
low=1;mid=0;high=0;display(0);
low=0;mid=1;high=0;display(0);
low=1;mid=1;high=0;display(0);
low=0;mid=0;high=1;display(0);
low=1;mid=0;high=1;display(Count%1/100);
low=0;mid=1;high=1;display(Count%100/10);
low=1;mid=1;high=1;display(Count%10);
}
//是否計(jì)數(shù)
voidIsCount(unit count){
if(PRESSED==IsCountKey){//當(dāng)按鍵按下
delay_1ms(10);//去抖動(dòng)
if(PRESSED==IsCountKey){//當(dāng)按鍵按下
while(PRESSED==IsCountKey){//當(dāng)按鍵一直按下
DisplayNumByOrder(count);//顯示原數(shù)值
}
}
}
}
//掃描鍵盤
voidmain(){
intcount=0;
while(1){
IsCount(count);
count++;
DisplayNumByOrder(count);
if(count==)count=0;
}
}
9.在上題的基礎(chǔ)上,用另外三個(gè)獨(dú)立鍵盤實(shí)現(xiàn)按下第一個(gè)時(shí)計(jì)時(shí)停止,按下第二個(gè)時(shí)計(jì)時(shí)開始,按下第三個(gè)是計(jì)數(shù)值清零從頭開始。
#include#define unit unsigned int
unit code NumTable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//數(shù)字的編碼
//數(shù)碼管前三位顯示一個(gè)跑表,從到之間以1%秒速度運(yùn)行,當(dāng)按下一個(gè)獨(dú)立鍵盤時(shí)跑表停止,松開手后跑表繼續(xù)運(yùn)行。
sbit high=P2^4;
sbit mid=P2^3;
sbit low=P2^2;
sbit Key1=P3^0;//開始
sbit Key2=P3^1;//暫停
sbit Key3=P3^2;//清零
constbit PRESSED=0;//按下
constbit SUSPEND=1;//0的時(shí)候運(yùn)行,1的時(shí)候暫停
//監(jiān)測
voiddelay_1ms(unit x){
unit i=x;
unit j;
for(;i>0;--i){
for(j=110;j>0;--j);
}
}
/**
在數(shù)碼管上顯示對(duì)應(yīng)的值
**/
voiddisplay(unsignedcharNum)
{
P0=NumTable[Num];
delay_1ms(1);
P0=0;//送完段選信號(hào)后,進(jìn)行消影的處理
}
/**
控制數(shù)碼管顯示后3位,并分解計(jì)數(shù)值
**/
voidDisplayNumByOrder(unit Count){
low=0;mid=0;high=0;display(0);
low=1;mid=0;high=0;display(0);
low=0;mid=1;high=0;display(0);
low=1;mid=1;high=0;display(0);
low=0;mid=0;high=1;display(0);
low=1;mid=0;high=1;display(Count%1/100);
low=0;mid=1;high=1;display(Count%100/10);
low=1;mid=1;high=1;display(Count%10);
}
//是否清零
voidIsClear(unit*count){
if(PRESSED==Key3){//當(dāng)按鍵按下
delay_1ms(10);//去抖動(dòng)
if(PRESSED==Key3){//當(dāng)按鍵按下
*count=0;
}
}
}
//是否暫停
voidIsSuspend(unit*count){
if(PRESSED==Key2){//當(dāng)按鍵按下
delay_1ms(10);//去抖動(dòng)
if(PRESSED==Key2){//當(dāng)按鍵按下
while(SUSPEND){
IsClear(count);//監(jiān)測是否需要清零
if(PRESSED==Key1)return;//跳出暫停
DisplayNumByOrder(*count);//顯示原數(shù)值
}
}
}
}
//掃描鍵盤
voidmain(){
intcount=0;
while(1){
IsSuspend(&count);
IsClear(&count);
count++;
DisplayNumByOrder(count);
if(count==)count=0;
}
}
10.按下16個(gè)矩陣鍵盤依次在數(shù)碼管上顯示1-16的平方。如按下第一個(gè)顯示1,第二個(gè)顯示4...
#include#include
#define unit unsigned int
#define uchar unsigned char
unit code NumTable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//數(shù)字的編碼
//10.按下16個(gè)矩陣鍵盤依次在數(shù)碼管上顯示1-16的平方。如按下第一個(gè)顯示1,第二個(gè)顯示4...
sbit high=P2^4;
sbit mid=P2^3;
sbit low=P2^2;
sbit Key1=P3^0;//開始
sbit Key2=P3^1;//暫停
sbit Key3=P3^2;//清零
constbit PRESSED=0;//按下
constbit SUSPEND=1;//0的時(shí)候運(yùn)行,1的時(shí)候暫停
//延時(shí)
voiddelay_1ms(unit x){
unit i=x;
unit j;
for(;i>0;--i){
for(j=110;j>0;--j);
}
}
/**
在數(shù)碼管上顯示對(duì)應(yīng)的值
**/
voiddisplay(unsignedcharNum)
{
P0=NumTable[Num];
delay_1ms(1);
P0=0;//送完段選信號(hào)后,進(jìn)行消影的處理
}
/**
控制數(shù)碼管顯示后3位,并分解計(jì)數(shù)值
**/
voidDisplayNumByOrder(unit Count){
low=0;mid=0;high=0;display(0);
low=1;mid=0;high=0;display(0);
low=0;mid=1;high=0;display(0);
low=1;mid=1;high=0;display(0);
low=0;mid=0;high=1;display(0);
low=1;mid=0;high=1;display(Count%1/100);
low=0;mid=1;high=1;display(Count%100/10);
low=1;mid=1;high=1;display(Count%10);
}
//是否清零
voidIsClear(unit*count){
if(PRESSED==Key3){//當(dāng)按鍵按下
delay_1ms(10);//去抖動(dòng)
if(PRESSED==Key3){//當(dāng)按鍵按下
*count=0;
}
}
}
//是否暫停
voidIsSuspend(unit*count){
if(PRESSED==Key2){//當(dāng)按鍵按下
delay_1ms(10);//去抖動(dòng)
if(PRESSED==Key2){//當(dāng)按鍵按下
while(SUSPEND){
IsClear(count);//監(jiān)測是否需要清零
if(PRESSED==Key1)return;//跳出暫停
DisplayNumByOrder(*count);//顯示原數(shù)值
}
}
}
}
uchar scanKey(){
uchar tmp,key;
P3=0xf0;
tmp=P3;
tmp=tmp&0xf0;
if(tmp!=0xf0){//確定列
switch(tmp){
case0xe0:key=1;break;
case0xd0:key=2;break;
case0xb0:key=3;break;
case0x70:key=4;break;
}
}
//確定行
P3=0x0f;
tmp=P3;
tmp=tmp&0x0f;
if(tmp!=0x0f){
switch(tmp){
case0x0d:key=key;break;
case0x0b:key=4+key;break;
case0x07:key=8+key;break;
}
}
returnkey;
}
//掃描鍵盤
voidmain(){
// int count=0;
while(1){
unit key=scanKey();
DisplayNumByOrder(key*key);
}
}
評(píng)論