目前共有7篇帖子。 內容轉換:不轉換▼
 
點擊 回復
597 6
【程序】AVR独立键盘
一派護法 十九級
1樓 發表于:2012-7-22 22:05
//这是我编写的
#include <iom16v.h>
#include <macros.h>
unsigned char num=0;
unsigned char const s8[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
void delay_ms(unsigned int k)
{
 unsigned int i,j;
    for (i=0;i<k;i++)
        for (j=0;j<1140;j++);
}
void delay_500us()
{
   unsigned int j;
  for (j=0;j<570;j++);
}
void Beep()
{
    unsigned int i;
    for (i=0;i<300;i++)
 {
        PORTD=~BIT(6);
        delay_500us();
        PORTD=0xff;
        delay_500us();
 }
}
void main()
{
    DDRA=PORTA=DDRC=PORTC=PORTD=DDRD=0xff; //数码管和蜂鸣器为输出
 PORTB|=BIT(4); //矩阵键盘为输入(带上拉)
 DDRB&=~BIT(4);
 
 //数码管显示0
 PORTC=~BIT(7); //7为右边最后一位数码管
 PORTA=s8[0];
 while (1)
 {
     if ((PINB&BIT(4)&0x10)==0) //必须打括号!!!
  {
      delay_ms(15);
   if ((PINB&BIT(4)&0x10)==0)
   {
       Beep();
    num++;
    if (num==16)
        num=0;
   }
   while (!(PINB&BIT(4)&0x10));
  }
  PORTA=s8[num]; //显示到数码管上
 }
}
一派護法 十九級
2樓 發表于:2012-7-22 22:12

#include <iom16v.h>
#include <macros.h>
unsigned long num=0;
unsigned char const s8[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
void delay_ms(unsigned int k)
{
 unsigned int i,j;
    for (i=0;i<k;i++)
        for (j=0;j<1140;j++);
}
void delay_500us()
{
   unsigned int j;
  for (j=0;j<570;j++);
}
void Beep()
{
    unsigned int i;
    for (i=0;i<300;i++)
 {
        PORTD=~BIT(6);
        delay_500us();
        PORTD=0xff;
        delay_500us();
 }
}
unsigned long power(unsigned char times)
{
    unsigned long num=10;
 unsigned char i;
 if (times==0)
    return 1;
 else
 {
    for (i=1;i<times;i++)
        num*=10;
    return num;
    }
}
void main()
{
    unsigned char i;
    DDRA=PORTA=DDRC=PORTC=PORTD=DDRD=0xff; //数码管和蜂鸣器为输出
 PORTB|=BIT(4); //矩阵键盘为输入(带上拉)
 DDRB&=~BIT(4);
 
 //数码管显示0
 //PORTC=~BIT(7); //7为右边最后一位数码管
 //PORTA=s8[0];
 while (1)
 {
     if ((PINB&BIT(4)&0x10)==0) //必须打括号!!!
  {
      delay_ms(15);
   if ((PINB&BIT(4)&0x10)==0)
   {
       Beep();
    num++;
    if (num>99999999)
              num=0;
   }
   while (!(PINB&BIT(4)&0x10));
  }
  //PORTA=s8[num]; //显示到数码管上
  for (i=0;i<8;i++) //i+1为从右数的位数
     {
         PORTC=~BIT(7-i);
      PORTA=s8[num%power(i+1)/power(i)];
      delay_ms(1);
      PORTC=0xff;
      PORTA=0xff;
     }
 }
}

 

 

 

 

 

这是改进版,可以显示8位数字

一派護法 十九級
3樓 發表于:2012-7-22 22:13

回复:2楼

这个程序加入了:

http://www.arslanbar.com/post.php?i=1932

一派護法 十九級
4樓 發表于:2012-7-22 22:20

那个&0x10可以改成:

if ((PINB&BIT(4)&BIT(4))==0)

一派護法 十九級
5樓 發表于:2012-7-22 22:23

回复:4楼

if ((PINB&BIT(4)/*&BIT(4)*/)==0)

少与一次bit4也行

看来视频教程忘了#define K5里面已经与了一次了

一派護法 十九級
6樓 發表于:2012-7-22 22:24

以后要判断某一位是多少直接PINn&BIT(n)就行了

用不着与两次

一派護法 十九級
7樓 發表于:2012-7-22 22:48

由于蜂鸣器响的时候会耽误时间,所以这里用不着加按键消抖程序。

 

#include <iom16v.h>
#include <macros.h>
unsigned long num=0;
unsigned char const s8[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
void delay_ms(unsigned int k)
{
 unsigned int i,j;
    for (i=0;i<k;i++)
        for (j=0;j<1140;j++);
}
void delay_500us()
{
   unsigned int j;
  for (j=0;j<570;j++);
}
void Beep()
{
    unsigned int i;
    for (i=0;i<300;i++)
 {
        PORTD=~BIT(6);
        delay_500us();
        PORTD=0xff;
        delay_500us();
 }
}
unsigned long power(unsigned char times)
{
    unsigned long num=10;
 unsigned char i;
 if (times==0)
    return 1;
 else
 {
    for (i=1;i<times;i++)
        num*=10;
    return num;
    }
}
void main()
{
    unsigned char i;
    DDRA=PORTA=DDRC=PORTC=PORTD=DDRD=0xff; //数码管和蜂鸣器为输出
 PORTB|=BIT(4); //矩阵键盘为输入(带上拉)
 DDRB&=~BIT(4);
 
 //数码管显示0
 //PORTC=~BIT(7); //7为右边最后一位数码管
 //PORTA=s8[0];
 while (1)
 {
     if ((PINB&BIT(4)/*&BIT(4)*/)==0) //必须打括号!!!
  {
      //delay_ms(15);
   //if ((PINB&BIT(4)/*&BIT(4)*/)==0)
   //{
       Beep();
    num++;
    if (num>99999999)
              num=0;
   //}
   //while (!(PINB&BIT(4)/*&BIT(4)*/));
  }
  //PORTA=s8[num]; //显示到数码管上
  for (i=0;i<8;i++) //i+1为从右数的位数
     {
         PORTC=~BIT(7-i);
      PORTA=s8[num%power(i+1)/power(i)];
      delay_ms(1);
      PORTC=0xff;
      PORTA=0xff;
     }
 }
}

回復帖子

內容:
用戶名: 您目前是匿名發表
驗證碼:
(快捷鍵:Ctrl+Enter)
 

本帖信息

點擊數:597 回複數:6
評論數: ?
作者: 巨大八爪鱼
最後回復:巨大八爪鱼
最後回復時間:2012-7-22 22:48
 
©2010-2024 Arslanbar Ver2.0
除非另有聲明,本站採用創用CC姓名標示-相同方式分享 3.0 Unported許可協議進行許可。