本文目录一览:
用C语言编写单片机程序
#include
sbit
csb_SAT=
P1^0;
//定义超声波发生脚
void
delay(void)
//延时25微秒,12T芯片用12M误差
0us
{
unsigned
char
a;
for(a=11;a0;a--);
}
void
csb_fs(int
dat)
{while(dat--){csb_SAT=1;delay();csb_SAT=0;delay();}}
//发射
void
main()
{
EA=1;
while(1)
{
csb_fs(10);
delay();delay();delay();delay();delay();delay();delay();delay();delay();
IE=0x83;
//INT0允许中断
while(IE);
P1_1=1;delay();P1_1=0;delay();P1_1=1;delay();P1_1=0;delay();P1_1=1;delay();P1_1=0;delay();
}
}
void
int0_chuankou(void)
interrupt
using
1
//外部INT0中断子程序
{
IE=0;//禁止INT0中断
}
单片机 c语言编写
单片机C语言编写与普通电脑C语言类似,只不过硬件成分更多些。比如:
#includereg51.h
#define uchar unsigned char
uchar code ledtab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//0-9
unsigned char sec=0,min=0,hour=12,scanled;
unsigned char key,time;
unsigned char disdat[6];
sbit K=P1^0;
void dischg()
{
disdat[0]=sec%10;
disdat[1]=sec/10;
disdat[2]=min%10;
disdat[3]=min/10;
disdat[4]=hour%10;
disdat[5]=hour/10;
}
void t0isr() interrupt 1 //秒计时
{
TR0=0;
TH0=0x3c;
TL0=0xb0;
TR0=1;
time++;
if(time==20)
{
time=0;
sec++;
if(sec59)
{
sec=0;
min++;
if(min59)
{
min=0;
hour++;
if(hour23)hour=0;
}
}
}
dischg();
}
void t1isr() interrupt 3 //显示
{
TH1=0xec;
TL1=0x78;
switch(scanled)
{
case 0:
P2=0x01;
P0=~ledtab[disdat[5]];
break;
case 1:
P2=0x02;
P0=~ledtab[disdat[4]];
break;
case 2:
P2=0x04;
P0=~ledtab[disdat[3]];
break;
case 3:
P2=0x08;
P0=~ledtab[disdat[2]];
break;
case 4:
P2=0x10;
P0=~ledtab[disdat[1]];
break;
case 5:
P2=0x20;
P0=~ledtab[disdat[0]];
break;
default:break;
}
scanled++;
scanled%=6;
}
main()
{
TMOD=0x11;
TH0=0x3c;
TL0=0xb0;
TH1=0xec;
TL1=0x78;
EA=1;
TR1=1;
ET0=1;
ET1=1;
sec=50;
min=59;
hour=23;
scanled=0;
time=0;
key=0;
dischg();
while(1)
{
if(K==0)
{
while(K==0);
key++;
key%=2;
}
switch(key)
{
case 0:
TR0=1;
break;
case 1:
TR0=0;
break;
}
}
}
谁能帮我写一下单片机C语言程序?
#includereg51.h
sbit LED=P1^0;
sbit K1=P1^1;
sbit K2=P1^2;
void delay(int ms)
{
unsigned char i;
while(ms--)for(i=0;i120;i++);
}
void t0isr(void) interrupt 1
{
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
LED=~LED;
}
main()
{
TMOD=0x01;
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
ET0=1;
EA=1;
while(1)
{
if(K1==0)
{
delay(10);
if(K1==0)
{
while(K1==0);
TR0=1;
cnt=0;
}
}
if(K2==0)
{
delay(10);
if(K2==0)
{
while(K2==0);
TR0=0;
LED=1;
}
}
}
}