打印
[KungFu8位 MCU]

单片机的最简单的几个编程程序

[复制链接]
2264|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
okyouwin|  楼主 | 2016-12-28 16:01 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
独立键盘

#include

sbit LED = P0^0;

sbit KEY = P1^0;

void main()

{

while(1)

{

if(0 == KEY)

{

LED = 0;

}

else

{

LED = 1;

}

}

}

矩阵键盘

#include

#include "delay.h"

#define SEGPORT P0

#define KEYPORT P3

sbit wei_select = P1^0;

sbit duan_select = P1^1;

unsigned char segport[2] = {0xfe,0xfd};

unsigned char segdata[16] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

unsigned char TempData[2];

void display_seg()

{

static unsigned char i = 0;

SEGPORT = 0xff;

wei_select = 1;

wei_select = 0;

SEGPORT = 0x0;

duan_select = 1;

duan_select = 0;

SEGPORT = segport[i];

wei_select = 1;

wei_select = 0;

SEGPORT = TempData[i];

duan_select = 1;

duan_select = 0;

i++;

if(2 == i)

{

i = 0;

}

}

void time0_init()

{

EA = 1;

TMOD = 0x01;

TH0 = (65536 - 200) / 256;

TL0 = (65536 - 200) % 256;

ET0 = 1;

TR0 = 1;

}

void time0_isr() interrupt 1

{

TH0 = (65536 - 200) / 256;

TL0 = (65536 - 200) % 256;

display_seg();

}

unsigned char key_scan()

{

unsigned char keyvalue;

KEYPORT = 0xf0;

if(KEYPORT != 0xf0)

{

delay_ms(10);

if(KEYPORT != 0xf0)

{

KEYPORT = 0xfe;

if(KEYPORT != 0xfe)

{

keyvalue = KEYPORT;

keyvalue &= 0xf0;

keyvalue += 0x0e;

while(KEYPORT != 0xfe);

delay_ms(10);

while(KEYPORT != 0xfe); //去抖动

return keyvalue;

}

KEYPORT = 0xfd;

if(KEYPORT != 0xfd)

{

keyvalue = KEYPORT;

keyvalue &= 0xf0;

keyvalue += 0x0d;

while(KEYPORT != 0xfd);

delay_ms(10);

while(KEYPORT != 0xfd);

return keyvalue;

}

KEYPORT = 0xfb;

if(KEYPORT != 0xfb)

{

keyvalue = KEYPORT;

keyvalue &= 0xf0;

keyvalue += 0x0b;

while(KEYPORT != 0xfb);

delay_ms(10);

while(KEYPORT != 0xfb);

return keyvalue;

}

KEYPORT = 0xf7;

if(KEYPORT != 0xf7)

{

keyvalue = KEYPORT;

keyvalue &= 0xf0;

keyvalue += 0x07;

while(KEYPORT != 0xf7);

delay_ms(10);

while(KEYPORT != 0xf7);

return keyvalue;

}

}

}

return 0xff;

}

unsigned char key_num ()

{

unsigned char num;

num = key_scan();

switch(num)

{

case 0x7e:return 1;break;

case 0xbe:return 2;break;

case 0xde:return 3;break;

case 0xee:return 4;break;

case 0x7d:return 5;break;

case 0xbd:return 6;break;

case 0xdd:return 7;break;

case 0xed:return 8;break;

case 0x7b:return 9;break;

case 0xbb:return 10;break;

case 0xdb:return 11;break;

case 0xeb:return 12;break;

case 0x77:return 13;break;

case 0xb7:return 14;break;

case 0xd7:return 15;break;

case 0xe7:return 16;break;

default:return 0xff;break;

}

}

void main()

{

unsigned char num = 0;

time0_init();

while(1)

{

num = key_num();

if(num != 0xff)

{

TempData[0] = segdata[num / 10];

TempData[1] = segdata[num % 10];

}

}

}

lcd

#include

#include "delay.h"

sbit RS = P2^4;

sbit RW = P2^5;

sbit E = P2^6;

#define DATAPORT P0

void lcd_write(unsigned char com,unsigned char dat)

{

if(1 == com)//命令

{

RS = 0;

}

else if(0 == com)//数据

{

RS = 1;

}

RW = 0;

DATAPORT = dat;

E = 1;

delay_us(10);

E = 0;

}

void lcd_init()

{

delay_ms(15);

lcd_write(1,0x38);

delay_ms(5);

lcd_write(1,0x38);

delay_ms(5);

lcd_write(1,0x38);

delay_ms(5);

lcd_write(1,0x08);

delay_ms(5);

lcd_write(1,0x01);

delay_ms(5);

lcd_write(1,0x06);

delay_ms(5);

lcd_write(1,0x0c);

delay_ms(5);

}

void main()

{

lcd_init();

lcd_write(1,0x80);

lcd_write(0,'A');

lcd_write(0,'Q');

while(1);

}


沙发
新人求带| | 2017-2-28 19:09 | 只看该作者
建议按键用零耗时按键,加入中断扫描。

使用特权

评论回复
板凳
missnihao| | 2017-3-21 13:52 | 只看该作者
Mark!学习一下~

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:把每天当做世界末日、。、

56

主题

765

帖子

3

粉丝