1位数码管上先显示零,按下SW0键(外中断1)蜂鸣器发出"答"的一声,数码管递增加数,
到9回零;按下SW1键(外中断0)蜂鸣器发出"笛"的一声,数码管递减减数,到0回9,用外中
断和定时器编程实现? 这是我写代码,但是结果。。。。。 图片上的代码跟这个是一样的
#include "stc15f2k60s2.h"
sbit k=P3^2;
sbit k2=P3^3;
int a,b;
//HC595½Ó¿Ú¶¨Òå
sbit DAT = P3^7; //HC595´®ÐÐÊý¾ÝÏß
sbit SCK = P4^2; //HC595ÒÆλʱÖÓÏß
sbit RCK = P4^1; //HC595Ëø´æʱÖÓÏß
sbit OE = P4^5; //HC595Êä³öÔÊÐíÏß
unsigned char code duanxuan[16]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
//È«¾Ö±äÁ¿
//unsigned char duanxuan=0x7d; //¶ÎÑ¡
unsigned char weixuan=0x80; //λѡ
void delay (unsigned char ms)
{
unsigned char a;
while ((ms--)!=0)
{
for (a=0;a<600;a++)
{}
}
}
void suocunqi(unsigned char dis,unsigned char dig) //¿ØÖƶÎÑ¡ λѡ
{
unsigned char i; //¶¨Òå¼ÆÊýÆ÷
unsigned int temp=0; //¶¨ÒåÊý¾Ý±äÁ¿
temp=dis; //½«Á½¸ö8λ±äÁ¿ºÏ²¢³ÉÒ»¸ö16×Ö½Ú±äÁ¿
temp=temp<<8 | dig;
OE=1; //HC595Êä³öÔÊÐíÏß ¸ßµçƽ//¹Ø±ÕÏÔʾ
for(i=0;i<16;i++) //ËͳöÊý¾Ý
{
SCK=0; //HC595ÒÆλʱÖÓÏß µÍµçƽ//ÖõÍʱÖÓÏß
if( (temp&0x8000)==0x8000 ) //ÏÈ·¢Ë͸ß×Ö½Ú
{
DAT=1; //HC595´®ÐÐÊý¾ÝÏß ¸ßµçƽ //ÖøßÐźÅÏß
}
else
{
DAT=0; //HC595´®ÐÐÊý¾ÝÏß µÍµçƽ//ÖõÍÐźÅÏß
}
SCK=1; //HC595ÒÆλʱÖÓÏß ¸ßµçƽ//ÖøßʱÖÓÏß
temp<<=1; //Êý¾Ý×óÒÆ
}
//Ëø´æÐźÅÉÏÉýÑØËø´æÊý¾Ý
RCK=0; //HC595Ëø´æʱÖÓÏß µÍµçƽ
RCK=1; //HC595Ëø´æʱÖÓÏß ¸ßµçƽ
OE=0; //HC595Êä³öÔÊÐíÏß µÍµçƽ//´ò¿ªÏÔʾ
}
void fasongshuju (unsigned char Dat,unsigned char dig ) //·¢Ë͵ãÁÁÊýÂë¹ÜµÄλ Óë ¶Î µÄÊý¾Ý
{
suocunqi(Dat,~dig);
}
void chushihua (void) //³õʼ»¯º¯Êý
{
DAT=1; //HC595´®ÐÐÊý¾ÝÏß ¸ß
SCK=1; //HC595ÒÆλʱÖÓÏß ¸ß
RCK=1; //HC595Ëø´æʱÖÓÏß ¸ß
//Êä³öÔÊÐí£¬¿ªÊ¼ÏÔʾ
OE=0; //HC595Êä³öÔÊÐíÏß µÍ
}
void main(void) //Ö÷º¯Êý
{
EA=1;
ET0=1;
EX0=1;
IT0=1;
IT1=1;
EX1=1;
TMOD=0X01;
TH0=(65536-3000)/256;
TL0=(65536-3000)%256;
chushihua (); //µ÷Óà ³õʼ»¯º¯Êý
fasongshuju(duanxuan[0],weixuan);
while(1)
{
if(k==0)
{
delay (20);
while(k2==1)
{
a++;
if(a>9)
{
a=0;
}
}
}
if(k2==0)
{
delay(20);
while(k==1)
{
a--;
if(a==0)
{
a=9;
}
}
}
}
}
void
time0 (void ) interrupt 1 using 1
{
fasongshuju(duanxuan[a],weixuan);
TH0=(65536-3000)/256;
TL0=(65536-3000)%256;
}
void int0 (void ) interrupt 0 using 0
{
TR0=1;
}
void int1 (void ) interrupt 2 using 2
{
TR0=1;
}
-
-
-
-
-
一周热门 更多>