搜索
bottom↓
回复: 5

实践出真知之三----从跑马灯开始 (顺便搞定一个电话键盘)

[复制链接]

出0入0汤圆

发表于 2009-11-23 12:22:37 | 显示全部楼层 |阅读模式
标题说明:
    “窃取”的傻孩子《深入浅出AVR单片机》第三章的标题

帖子摘要:
    1.从零开始学AVR                      (说明:希望能对AVR还没入门的朋友有所帮助)
    2.学完第三章的一个电话键盘小实验
    3.实验总结        


(原文件名:1.jpg)


(原文件名:2.jpg)


(原文件名:3.jpg)


(原文件名:4.jpg)


(原文件名:5.jpg)


(原文件名:6.jpg)


(原文件名:7.jpg)


(原文件名:8.jpg)


(原文件名:9.jpg)


(原文件名:10.jpg)


(原文件名:11.jpg)



********************************************

(原文件名:1.jpg)


(原文件名:2.jpg)


(原文件名:3.jpg)


(原文件名:4.jpg)


(原文件名:5.jpg)

/*
FileName: 28KeyTest
Date:     2009/11/22
Time:     17:30-22:00
*/
#include<iom48v.h>
#include"led_dis.h"
#include"key.h"
#include"delay.h"

void Init(void)
{
    DDRB = 0XF0;
    DDRC = 0XFF;
    DDRD = 0XFF;
    PORTB = 0B11111110;
    PORTC = 0B11011111;
    PORTD = 0XFF;
}

void main(void)
{
    unsigned char KeyValue=0;
   
    Init();
   
    while(1)
    {
        KeyValue = Key_Scan();
        Led_Display(KeyValue);
    }
}

/****************************delay.h***********************/
#ifndef _DELAY_H_
#define _DELAY_H_

extern void Delay_ms(unsigned char Time);

#endif
/****************************delay.c***********************/
#include<macros.h>
#include"delay.h"

void Delay_ms(unsigned char Time)
{
    unsigned char n;
    while(Time>0)
    {
        for (n=1; n<187; n++)
        {
            NOP();
        }
        Time--;
    }



/****************************key.h***********************/
#ifndef _KEY_H_
#define _KEY_H_

#define Key_0 0x00
#define Key_1 0x01
#define Key_2 0x02
#define Key_3 0x03
#define Key_4 0x04
#define Key_5 0x05
#define Key_6 0x06
#define Key_7 0x07
#define Key_8 0x08
#define Key_9 0x09
#define Key_Time     10
#define Key_Uptri    11
#define Key_Downtri  12
#define Key_Alarm    13
#define Key_Set      14
#define Key_Mup      15
#define Key_Mdown    16
#define Key_Nongli   17
#define Key_Star     18
#define Key_Sharp    19
#define Key_TP       20
#define Key_X        21
#define Key_Down     22
#define Key_Up       23
#define Key_AC       24
#define Key_CE       25        
#define Key_Music    26
#define Key_Redial   27
#define NoKey        29

#define Key_X_1  PORTC |= 0B00011111; \
                 PORTC &= 0B11101111; \
                 PORTB |= 0B11110000
                  
#define Key_X_2  PORTC |= 0B00011111; \
                 PORTC &= 0B11110111; \
                 PORTB |= 0B11110000
                  
#define Key_X_3  PORTC |= 0B00011111; \
                 PORTC &= 0B11111011; \
                 PORTB |= 0B11110000
                  
#define Key_X_4  PORTC |= 0B00011111; \
                 PORTC &= 0B11111101; \
                 PORTB |= 0B11110000
                  
#define Key_X_5  PORTC |= 0B00011111; \
                 PORTC &= 0B11111110; \
                 PORTB |= 0B11110000
                  
#define Key_X_6  PORTC |= 0B00011111; \
                 PORTB |= 0B11111111; \
                 PORTB &= 0B11011111
                  
#define Key_X_7  PORTC |= 0B00011111; \
                 PORTB |= 0B11111111; \
                 PORTB &= 0B11101111
                  
#define Key_Scan_Port PINB
#define Key_Y_1 ((Key_Scan_Port|0B11110111)==0B11110111)                  
#define Key_Y_2 ((Key_Scan_Port|0B11111011)==0B11111011)                  
#define Key_Y_3 ((Key_Scan_Port|0B11111101)==0B11111101)                  
#define Key_Y_4 ((Key_Scan_Port|0B11111110)==0B11111110)                  
                  
extern unsigned char Key_Scan(void);

#endif
}

/****************************key.c***********************/
#include<iom48v.h>
#include<macros.h>
#include"key.h"
#include"delay.h"


unsigned char Key_Scan(void)
{
    /****扫描第一行****/
    Key_X_1;
    NOP();
    if (Key_Y_1)
        return Key_Time;
    else if(Key_Y_2)
        return Key_Uptri;
    else if(Key_Y_3)
        return Key_Downtri;
    else if(Key_Y_4)
        return Key_Alarm;
        
    Key_X_2;
    NOP();
    if (Key_Y_1)
        return Key_Set;
    else if(Key_Y_2)
        return Key_Mup;
    else if(Key_Y_3)
        return Key_Mdown;
    else if(Key_Y_4)
        return Key_Nongli;
        
    Key_X_3;
    NOP();  
    if (Key_Y_1)
        return Key_1;
    else if(Key_Y_2)
        return Key_4;
    else if(Key_Y_3)
        return Key_7;
    else if(Key_Y_4)
        return Key_Star;
        
    Key_X_4;
    NOP();
    if (Key_Y_1)
        return Key_2;
    else if(Key_Y_2)
        return Key_5;
    else if(Key_Y_3)
        return Key_8;
    else if(Key_Y_4)
        return Key_0;
        
    Key_X_5;
    NOP();
    if (Key_Y_1)
        return Key_3;
    else if(Key_Y_2)
        return Key_6;
    else if(Key_Y_3)
        return Key_9;
    else if(Key_Y_4)
        return Key_Sharp;
        
    Key_X_6;
    NOP();
    if (Key_Y_1)
        return Key_TP;
    else if(Key_Y_2)
        return Key_X;
    else if(Key_Y_3)
        return Key_Down;
    else if(Key_Y_4)
        return Key_Up;
        
    Key_X_7;
    NOP();
    if (Key_Y_1)
        return Key_AC;
    else if(Key_Y_2)
        return Key_CE;
    else if(Key_Y_3)
        return Key_Music;
    else if(Key_Y_4)
        return Key_Redial;
        
    return NoKey;
}







/****************************led_dis.h***********************/
#ifndef _LED_DIS_H_
#define _LED_DIS_H_

#include<iom48v.h>

#define Led_0 0
#define Led_1 1
#define Led_2 2
#define Led_3 3
#define Led_4 4
#define Led_5 5
#define Led_6 6
#define Led_7 7
#define Led_8 8
#define Led_9 9
#define Led_A 0X0A
#define Led_B 0X0B
#define Led_C 0X0C
#define Led_D 0X0D
#define Led_E 0X0E
#define Led_F 0X0F
#define Led_H 16
#define Led_L 17
#define Led_P 18
#define Led_r 19
#define Led_U 20
#define Led_T 21
#define Led_n 22
#define Led_S 5
#define Led_d 23
#define Led_X1 24
#define Led_X2 25
#define Led__  26
#define Led_Pls1 27
#define Led_Pls2 28
#define Led___   29
#define Led_NULL 30

#define Led1 PORTD |= 0B10000000
#define Led2 PORTC |= 0B00100000
#define Led1M PORTD &= 0B01111111
#define Led2M PORTC &= 0B11011111

#define LedDisPort PORTD

extern void Led_Display(unsigned char KeyValue);

#endif

/****************************led_dis.c***********************/
#include<iom48v.h>
#include"led_dis.h"
#include"delay.h"
#include"key.h"


const unsigned char DisTab[]=
{
    0xC0,0xF9,0xA4,0xB0,0x99,    //0,1,2,3,4
    0x92,0x82,0xF8,0x80,0x90,    //5,6,7,8,9
    0x88,0x83,0xC6,0xA1,0x86,    //A,b,C,d,E
    0x8E,0x89,0xC7,0x8C,0xAF,    //F,H,L,P,r
    0xC1,0x87,0xC8,0xA1,0x93,    //U,T,n,d,X1,
    0x9A,0xBF,0xB9,0x8F,0xF7,0XFF   //X2,-,Pls1,Pls2,NULL
};  

void Led_Display(unsigned char KeyValue)
{
    static unsigned char L1=Led___, L2=Led___;     //验证一下static
   
    switch(KeyValue)
    {
        case NoKey:        break;      
        case Key_1:        L1=Led_NULL; L2=Led_1; break;
        case Key_2:        L1=Led_NULL; L2=Led_2; break;
        case Key_3:        L1=Led_NULL; L2=Led_3; break;
        case Key_4:        L1=Led_NULL; L2=Led_4; break;
        case Key_5:        L1=Led_NULL; L2=Led_5; break;
        case Key_6:        L1=Led_NULL; L2=Led_6; break;
        case Key_7:        L1=Led_NULL; L2=Led_7; break;
        case Key_8:        L1=Led_NULL; L2=Led_8; break;
        case Key_9:        L1=Led_NULL; L2=Led_9; break;
        case Key_0:        L1=Led_NULL; L2=Led_0; break;
        case Key_Time:     L1=Led_T;    L2=Led_E; break;
        case Key_Uptri:    L1=Led_U;    L2=Led_P; break;
        case Key_Downtri:  L1=Led_D;    L2=Led_n; break;
        case Key_Alarm:    L1=Led_A;    L2=Led_L; break;
        case Key_Set:      L1=Led_S;    L2=Led_E; break;
        case Key_Mup:      L1=Led_n;    L2=Led_P; break;
        case Key_Mdown:    L1=Led_n;    L2=Led_d; break;
        case Key_Nongli:   L1=Led_n;    L2=Led_L; break;
        case Key_Star:     L1=Led_S;    L2=Led_r; break;
        case Key_Sharp:    L1=Led_S;    L2=Led_P; break;
        case Key_TP:       L1=Led_T;    L2=Led_P; break;
        case Key_X:        L1=Led_X1;   L2=Led_X2; break;
        case Key_Down:     L1=Led__;    L2=Led__; break;
        case Key_Up:       L1=Led_Pls1; L2=Led_Pls2; break;
        case Key_AC:       L1=Led_A;    L2=Led_C; break;
        case Key_CE:       L1=Led_C;    L2=Led_E; break;
        case Key_Music:    L1=Led_U;    L2=Led_C; break;
        case Key_Redial:   L1=Led_r;    L2=Led_L; break;
    }
   
    LedDisPort = DisTab[L1];// | 0B10000000;
    Led1;
    Delay_ms(1);
    Led1M;
     
    LedDisPort = DisTab[L2] & 0B01111111;//while(1);
    Led2;
    Delay_ms(1);
    Led2M;
   
}
****************************************************************
总结:
    在读取引脚的电平之前一定要先延时一定时间,刚开始时我因为没延时导致按部分按键读取错误,浪费了整整一天的调试时间。

阿莫论坛20周年了!感谢大家的支持与爱护!!

月入3000的是反美的。收入3万是亲美的。收入30万是移民美国的。收入300万是取得绿卡后回国,教唆那些3000来反美的!

出0入0汤圆

发表于 2009-11-23 12:53:07 | 显示全部楼层
进步不小。

出0入0汤圆

发表于 2009-11-23 14:05:22 | 显示全部楼层
笨小孩 和 傻孩子 似乎很像啊。

出0入296汤圆

发表于 2009-11-23 14:06:07 | 显示全部楼层
关于这个延迟,有兴趣,请参考配套辅导教材:(从这里下载)http://www.ourdev.cn/bbs/bbs_content.jsp?bbs_sn=3239709&bbs_page_no=1&bbs_id=3039


(原文件名:1.JPG)

出0入0汤圆

 楼主| 发表于 2009-11-23 15:23:23 | 显示全部楼层
谢谢:【3楼】 Gorgon Meducer 傻孩子

出0入0汤圆

发表于 2009-12-1 08:21:46 | 显示全部楼层
mark。
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-4-27 18:31

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表