基于单片机的重要C语言程序Word格式.docx
《基于单片机的重要C语言程序Word格式.docx》由会员分享,可在线阅读,更多相关《基于单片机的重要C语言程序Word格式.docx(15页珍藏版)》请在冰豆网上搜索。
P0=0x6d;
delayms(100);
P1=0xfb;
P0=0x07;
显示hello
#include"
AT89x52.h"
voiddelayms(intms)
P0=0x3f;
delayms(10);
P0=0x38;
P1=0xf7;
P0=0x79;
P1=0xef;
P0=0x76;
Xianshiuzi
charcodetable[]={0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x7f,0x6f};
voidshuplay(intshu)
//11111110
P0=table[shu%10];
//
delayms(5);
//11111101
P0=table[shu%100/10];
//
//11110111
P0=table[shu/100];
voidmain(){
intled=1000,i;
while
(1){
for(i=20;
i--)
shuplay(led);
led=led+1;
数码管显示0~1000
#include"
charcodetable[]={0x3f,0x06,0x5b,0x4f,0x66,
P0=table[shu%100/10]+0*80;
intled,i;
数码管数字加文字
}
voidHELLO()
{
while(123){
HELLO();
按键
#include"
sbitkeyA=P2^0;
sbitkeyB=P2^1;
voiddelayms(intms){
intmyKey;
while
(1){
myKey=0;
if(keyA==0);
{
delayms(5);
if(keyA==0)
myKey=1;
if(keyB==0);
if(keyB==0)
myKey=2;
P3=myKey;
Jianpan
reg52.h"
#defineucharunsignedchar
#defineunitunsignedint
charcodetable[19]={0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f,
0x77,0x7c,0x39,0x5e,0x79,
0x71,0x73,0xc3,0x00};
voiddelay(intms)
for(i=100;
}
voiddisplay(charshu)
{
P1=0xf0;
P0=table[shu];
ucharkey_scan(void)
ucharkey_code;
ucharkey_value;
ucharrow=0;
ucharline=0;
P2=0x0f;
if((P2&
0x0f)!
=0x0f)
{
delay(10);
key_code=P2&
0x0f;
switch(key_code)
case0x0e:
row=0;
break;
case0x0d:
row=1;
case0x0b:
row=2;
case0x07:
row=3;
default:
;
P2=0xf0;
key_code=P2&
0xf0;
case0xe0:
line=0;
case0xd0:
line=1;
case0xb0:
line=2;
case0x70:
line=3;
key_value=row*4+line;
while((P2&
return(key_value);
voidmain(void)
intmykey;
{mykey=key_scan();
display(mykey);
Timer
intmiao;
TMOD=0x01;
ET0=1;
TH0=(65535-46083)/256;
TL0=(65535-46083)%256;
TR0=1;
EA=1;
while
(1)
P3=~miao;
voidtimer0()interrupt1
intcount;
count++;
if(count==20)
count=0;
miao++;
TH0=19452/256;
TL0=19452%256;
独立键盘
sbitKeyA=P2^4;
sbitKeyB=P2^5;
sbitKeyC=P2^6;
0x6d,0x7d,0x07,0x7f,0x6f};
voiddelayms(intms)
voidshuplay(intshu)
delayms(4);
P0=table[shu/10];
charKeyscan()
charkey;
if(KeyA==0){
delayms(5);
if(KeyA==0)
key=1;
if(KeyB==0){
if(KeyB==0)
key=2;
if(KeyC==0){
if(KeyC==0)
key=3;
returnkey;
voidmain()
while
(2)
mykey=Keyscan();
shuplay(mykey);
用数码管控制灯
intmiao=100;
voidtimeplay(intmiao)
P0=table[miao%10];
P0=table[miao/10%10];
P0=table[miao/100];
timeplay(miao);
if(miao==80)
{P3=0xff;
P3=0x00;
小车
sbitIN1=P3^0;
sbitIN2=P3^1;
sbitIN3=P3^2;
sbitIN4=P3^3;
sbitENA=P3^4;
sbitENB=P3^5;
voiddelayms(intms){
for(i=150;
voidcarleft(){
IN1=1;
IN2=0;
IN3=1;
IN4=0;
ENA=1;
ENB=1;
voidcaright(){
IN1=0;
IN2=1;
IN3=0;
IN4=1;
voidcarforward(){
voidmain(){
while
(1){
carleft();
delayms(500);
caright();
carforward();