您好,欢迎来到百家汽车网。
搜索
您的当前位置:首页单片机实验程序

单片机实验程序

来源:百家汽车网
LED闪烁实验

#include

void Delay10ms(unsigned int c); void main() {

while(1) {

P0 = 0x00;

Delay10ms(50); P0 = 0xff;

Delay10ms(50); } }

void Delay10ms(unsigned int c) {

unsigned char a, b; for (;c>0;c--)

{

for (b=38;b>0;b--) {

for (a=130;a>0;a--); } } }

流水灯实验

#include #include

void Delay10ms(unsigned int c); main() {

unsigned char LED; LED = 0xfe; while (1) {

P0 = LED; Delay10ms(50);

LED = LED << 1; if (P0 == 0x00) {

LED = 0xfe; }

} }

void Delay10ms(unsigned int c) unsigned char a, b; for (;c>0;c--) {

for (b=38;b>0;b--) {

for (a=130;a>0;a--); }

} }

键盘实验(按键和矩阵键盘) 1、按键控制LED实验 #include #include

#define GPIO_KEY P1 #define GPIO_LED P0

void Delay10ms(unsigned int c); unsigned char Key_Scan(); void main(void) {

Unsigned char ledValue, keyNum; ledValue = 0x01; while (1) {

keyNum = Key_Scan(); switch (keyNum) {

case(0xFE) : ledValue = 0x01; break;

case(0xFD) : ledValue = 0x02; break;

case(0xFB) : ledValue = 0x04; break;

case(0xF7) : ledValue = 0x08; break;

case(0xEF) : ledValue = 0x10; break;

case(0xDF) : ledValue = 0x20; break;

case(0xBF) : ledValue = 0x40; break;

case(0x7F) : ledValue = 0x80; break; default: break; }

GPIO_LED = ledValue; } }

unsigned char Key_Scan() {

unsigned char keyValue = 0 , i; if (GPIO_KEY != 0xFF) {

Delay10ms(1);

if (GPIO_KEY != 0xFF) {

keyValue = GPIO_KEY; i = 0;

while ((i<50) && (GPIO_KEY != 0xFF)) {

Delay10ms(1); i++; } } }

return keyValue; }

void Delay10ms(unsigned int c) {

unsigned char a, b;

for (;c>0;c--) {

for (b=38;b>0;b--) {

for (a=130;a>0;a--); }

} }

2、矩阵键盘(静态数码管显示矩阵键盘键值)

#include

#define GPIO_DIG P0 #define GPIO_KEY P1

unsigned char code DIG_CODE[17]={ 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,

0x79,0x71};

unsigned char KeyValue;

void Delay10ms(unsigned int c); void KeyDown(); void main(void) {

while(1) {

KeyDown(); GPIO_DIG ~DIG_CODE[KeyValue]; } }

void KeyDown(void) {

char a = 0;

GPIO_KEY=0x0f; if(GPIO_KEY!=0x0f)

= {

Delay10ms(1);

if(GPIO_KEY!=0x0f) {

GPIO_KEY=0X0F; switch(GPIO_KEY) {

case(0X07): KeyValue=0;break; case(0X0b): KeyValue=4;break; case(0X0d): KeyValue=8;break; case(0X0e): KeyValue=12;break; }

GPIO_KEY=0XF0; switch(GPIO_KEY)

{

case(0X70):

KeyValue=KeyValue+3;break; case(0Xb0):

KeyValue=KeyValue+2;break; case(0Xd0):

KeyValue=KeyValue+1;break; case(0Xe0):

KeyValue=KeyValue;break; }

while((a<50) && (GPIO_KEY!=0xf0)) {

Delay10ms(1); a++; } } } }

void Delay10ms(unsigned int c) {

unsigned char a, b; for (;c>0;c--) {

for (b=38;b>0;b--) {

for (a=130;a>0;a--); } }

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- baijiahaobaidu.com 版权所有 湘ICP备2023023988号-9

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务