51c单片机开发

//TX-1C实验板
//芯片 STC89C52RC
//晶振频率 11.0592MHz
//=====一个LED灯闪
#include
#define uint unsigned int
sbit led1=P1^0;
void delay1s();
void main()
{
	while(1)
	{
		led1=0;
		delay1s();
		led1=1;
		delay1s();
	}
}
void delay1s()
{
	uint i,j;
	for(i=500;i>0;i--)
		for(j=110;j>0;j--);
}
//======流水灯程序
#include
#include
#define uint unsigned int
#define uchar unsigned char
void delayms(uint xms);
uchar aa;
void main()
{
	aa=0xfe;
	while(1)
	{
		P1=aa;;
		delayms(500);
		aa=_crol_(aa,1);
	}
}
void delayms(uint xms)
{
	uint i,j;
	for(i=xms;i>0;i--)
		for(j=110;j>0;j--);
}
//=====启动蜂鸣
#include
#include
#define uint unsigned int
#define uchar unsigned char
sbit beep=P2^3;
void delay(uint z);
void main()
{	
	while(1)
	{
		beep=0;
		delay(100);	
		beep=1;
		delay(10000000);
	}
}
void delay(uint z)
{
	uint x,y;
	for(x=z;x>0;x--)
		for(y=110;y>0;y--);
}
//======数码管的显示;数字6
#include
sbit dula=P2^6;
sbit wela=P2^7;
void main()
{
	wela=1;
	P0=0xfe;
	wela=0;
	dula=1;
	P0=0x7d;
	dula=0;
	while(1);
}
//======数码管的静态显示
#include
#define uint unsigned int 
#define uchar unsigned char
sbit dula=P2^6;
sbit wela=P2^7;
uchar num;
uchar code table[]={
	0x3f,0x06,0x5b,0x4f,
	0x66,0x6d,0x7d,0x07,
	0x7f,0x6f,0x77,0x7c,
	0x39,0x5e,0x79,0x71
};
void delayms(uint);
void main()
{
	wela=1;
	P0=0xc0;
  wela=0;
	while(1)
	{
		for(num=0;num<16;num++)
			{
				dula=1;
				P0=table[num];
				dula=0;
				delayms(1000);
			}
	}
}
void delayms(uint xms)
{
	 uint x,y;
	 for(x=xms;x>0;x--)
	  for(y=110;y>0;y--);
}
//======数码管的动态显示
#include
#define uint unsigned int 
#define uchar unsigned char
sbit dula=P2^6;
sbit wela=P2^7;
uchar code table[]={
	0x3f,0x06,0x5b,0x4f,
	0x66,0x6d,0x7d,0x07,
	0x7f,0x6f,0x77,0x7c,
	0x39,0x5e,0x79,0x71
};
void delayms(uint);
void main()
{
	while(1)
	{
		dula=1;
		P0=table[1];
		dula=0;
		P0=0xff;
		wela=1;
		P0=0xfe;
		wela=0;
		delayms(500);
		
		dula=1;
		P0=table[2];
		dula=0;
		P0=0xff;
		wela=1;
		P0=0xfd;
		wela=0;
		delayms(500);
		
		dula=1;
		P0=table[3];
		dula=0;
		P0=0xff;
		wela=1;
		P0=0xfb;
		wela=0;
		delayms(500);
		
		dula=1;
		P0=table[4];
		dula=0;
		P0=0xff;
		wela=1;
		P0=0xf7;
		wela=0;
		delayms(500);
		
		dula=1;
		P0=table[5];
		dula=0;
		P0=0xff;
		wela=1;
		P0=0xef;
		wela=0;
		delayms(500);
		
		dula=1;
		P0=table[6];
		dula=0;
		P0=0xff;
		wela=1;
		P0=0xdf;
		wela=0;
		delayms(500);
		
	}
}
void delayms(uint xms)
{
	 uint x,y;
	 for(x=xms;x>0;x--)
	  for(y=110;y>0;y--);
}
//======定时器0工作方式1=====
#include
#define uchar unsigned char
#define uint unsigned int 
uchar num;
void main()
{
	P1=0x55;
	TMOD=0x01;
	TH0=(65536-50000)/256;
	TL0=(65536-50000)%256;
	EA=1;
	ET0=1;
	TR0=1;
	while(1)
	{
		if(num==20)
		{
			num=0;
			P1=~P1;
		}
	}
}
void t0_time() interrupt 1
{
	TH0=(65536-50000)/256;
	TL0=(65536-50000)%256;
	num++;
}
//=======用定时器0的方式1实现第一个发光管以ms间隔闪烁,用定时器1的方式1实现数码管前两位59s循环计时。
#include
#define uchar unsigned char
#define uint unsigned int
sbit dula=P2^6;
sbit wela=P2^7;
sbit led1=P1^0;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void delayms(uint);
void display(uchar,uchar);
uchar num,num1,num2,shi,ge;
void main()
{
	led1=0;
	TMOD=0x11;//00010001
	TH0=(65536-50000)/256;
	TL0=(65539-50000)%256;
	TH1=(65536-50000)/256;
	TL1=(65539-50000)%256;
	EA=1;
	ET0=1;
	ET1=1;
	TR0=1;
	TR1=1;
	while(1)
	{
		display(shi,ge);
	}
}
void display(uchar s,uchar g)
{
	dula=1;
	P0=table[s];
	dula=0;
	P0=0xff;
	wela=1;
	P0=0xfe;
	wela=0;
	delayms(5);
	
	dula=1;
	P0=table[g];
	dula=0;
	P0=0xff;
	wela=1;
	P0=0xfd;
	wela=0;
	delayms(5);
}
void delayms(uint xms)
{
	uint i,j;
	for(i=xms;i>0;i--)
		for(j=110;j>0;j--);
}
void t0_time() interrupt 1
{
	TH0=(65536-50000)/256;
	TL0=(65539-50000)%256;
	num1++;
	if(num1==20)
	{
		num1=0;
		led1=~led1;
	}
}
void t1_time() interrupt 3
{
	TH1=(65536-50000)/256;
	TL1=(65539-50000)%256;
	num2++;
	if(num2==20)
	{
		num2=0;
		num++;
		if(num==60)
			num=0;
		shi=num/10;
		ge=num%10;
	}
}
//===========用数码管的前两位显示一个十进制数,变化范围从00~59,开始时显示00,每按下s2键一次,数值加一;
每按下s3键一次,数值减一;
每按下s4键一次,数值归零;
每按下s5键一次,数值停止自动加1,保持显示原数,再按s5利用定时器功能数值开始自动每秒加1;

#include
#define uchar unsigned char
#define uint unsigned int
sbit key1=P3^4;//s2
sbit key2=P3^5;//s3
sbit key3=P3^6;//s4
sbit key4=P3^7;//s5

sbit dula=P2^6;
sbit wela=P2^7;

uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void delayms(uint);
uchar numt0,num;
void display(uchar);
void init();
void keyscan();
void main()
{
	init();
	while(1)
	{
		keyscan();
		display(num);
	}
}
void display(uchar numdis)
{
	uchar s,g;
	s=numdis/10;
	g=numdis%10;
	
	dula=1;
	P0=table[s];
	dula=0;
	P0=0xff;
	wela=1;
	P0=0xfe;
	wela=0;
	delayms(5);
	
	dula=1;
	P0=table[g];
	dula=0;
	P0=0xff;
	wela=1;
	P0=0xfd;
	wela=0;
	delayms(5);
}
void delayms(uint xms)
{
	uint i,j;
	for(i=xms;i>0;i--)
		for(j=110;j>0;j--);
}
void init()
{
	TMOD=0x01;
	TR0=1;
	TH0=(65536-45872)/256;
	TL0=(65536-45872)%256;
	EA=1;
	ET0=1;
	numt0=0;
	num=0;
}
void keyscan()
{
	if(key1==0)
	{
		delayms(10);
		if(key1==0)
		{
			num++;
			if(num==60)
				num=0;
			while(!key1);
		}
	}
	
	if(key2==0)
	{
		delayms(10);
		if(key2==0)
		{
			if(num==0)
				num=60;
			num--;
			while(!key2);
		}
	}
	
	if(key3==0)
	{
		delayms(10);
		if(key3==0)
		{
			num=0;
			while(!key3);
		}
	}
	
	if(key4==0)
	{
		delayms(10);
		if(key4==0)
		{
			while(!key4);
			TR0=~TR0;
		}
	}
}
void t0_time() interrupt 1
{
	TH0=(65536-45872)/256;
	TL0=(65536-45872)%256;
	numt0++;
	if(numt0==20)
	{
		numt0=0;
		num++;
		if(num==60)
			num=0;
	}
}
//=========实验板上电时,数码管不显示,顺序按下矩阵键盘后,在数码管上依次显示0~F,6个数码管同时静态显示即可
#include
#define uchar unsigned char
#define uint unsigned int

sbit dula=P2^6;
sbit wela=P2^7;

uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};

void delayms(uint);
void display(uchar num);
void matrixkeyscan();

void main()
{
	P0=0;
	dula=1;
	dula=0;
	P0=0xc0;
	wela=1;
	wela=0;
	while(1)
	{
		matrixkeyscan();
	}
}

void display(uchar num)
{
	P0=table[num];
	dula=1;
	dula=0;
}

void delayms(uint xms)
{
	uint i,j;
	for(i=xms;i>0;i--)
		for(j=110;j>0;j--);
}

void matrixkeyscan()
{
	uchar temp,key;
	P3=0xfe;
	temp=P3;
	temp=temp&0xf0;
	if(temp!=0xf0)
	{
		delayms(10);
		temp=P3;
		temp=temp&0xf0;
		if(temp!=0xf0)
		{
			temp=P3;
			switch(temp)
			{
				case 0xee:
					key=0;
				break;
				case 0xde:
					key=1;
				break;
				case 0xbe:
					key=2;
				break;
				case 0x7e:
					key=3;
				break;
				default:
					key=0;
				break;
			}
			while(temp!=0xf0)
			{
				temp=P3;
				temp=temp&0xf0;
			}
			display(key);
		}
	}
	
	P3=0xfd;
	temp=P3;
	temp=temp&0xf0;
	if(temp!=0xf0)
	{
		delayms(10);
		temp=P3;
		temp=temp&0xf0;
		if(temp!=0xf0)
		{
			temp=P3;
			switch(temp)
			{
				case 0xed:
					key=4;
				break;
				case 0xdd:
					key=5;
				break;
				case 0xbd:
					key=6;
				break;
				case 0x7d:
					key=7;
				break;
				default:
					key=0;
				break;
			}
			while(temp!=0xf0)
			{
				temp=P3;
				temp=temp&0xf0;
			}
			display(key);
		}
	}
	
	P3=0xfb;
	temp=P3;
	temp=temp&0xf0;
	if(temp!=0xf0)
	{
		delayms(10);
		temp=P3;
		temp=temp&0xf0;
		if(temp!=0xf0)
		{
			temp=P3;
			switch(temp)
			{
				case 0xeb:
					key=8;
				break;
				case 0xdb:
					key=9;
				break;
				case 0xbb:
					key=10;
				break;
				case 0x7b:
					key=11;
				break;
				default:
					key=0;
				break;
			}
			while(temp!=0xf0)
			{
				temp=P3;
				temp=temp&0xf0;
			}
			display(key);
		}
	}
	
	P3=0xf7;
	temp=P3;
	temp=temp&0xf0;
	if(temp!=0xf0)
	{
		delayms(10);
		temp=P3;
		temp=temp&0xf0;
		if(temp!=0xf0)
		{
			temp=P3;
			switch(temp)
			{
				case 0xe7:
					key=12;
				break;
				case 0xd7:
					key=13;
				break;
				case 0xb7:
					key=14;
				break;
				case 0x77:
					key=15;
				break;
				default:
					key=0;
				break;
			}
			while(temp!=0xf0)
			{
				temp=P3;
				temp=temp&0xf0;
			}
			display(key);
		}
	}
}
//=========用单片机控制ADC0804进行模数转换,当拧松实验板上A/D旁边的电位器Re2时,在数码管的前三位以十进制方式动态显示出A/D转换后的数字量(8位A/D转换后数值在0~255变化)
#include
#include
#define uchar unsigned char
#define uint unsigned int

sbit dula=P2^6;
sbit wela=P2^7;

sbit adwr=P3^6;
sbit adrd=P3^7;

uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};

void delayms(uint);
void display(uchar,uchar,uchar);

void main()
{
	uchar a,A1,A2,A3,adval;
	wela=1;
	P0=0x7f;
	wela=0;
	while(1)
	{
		adwr=1;
		_nop_();
		adwr=0;
		_nop_();
		adwr=1;
		for(a=10;a>0;a--)
		{
			display(A1,A2,A3);
		}
		P1=0xff;
		adrd=1;
		_nop_();
		adrd=0;
		_nop_();
		adval=P1;
		adrd=1;
		A1=adval/100;
		A2=adval%100/10;
		A3=adval%10;
	}
}

void display(uchar bai,uchar shi,uchar ge)
{
	dula=1;
	P0=table[bai];
	dula=0;
	P0=0xff;
	wela=1;
	P0=0x7e;
	wela=0;
	delayms(5);
	
	dula=1;
	P0=table[shi];
	dula=0;
	P0=0xff;
	wela=1;
	P0=0x7d;
	wela=0;
	delayms(5);
	
	dula=1;
	P0=table[ge];
	dula=0;
	P0=0xff;
	wela=1;
	P0=0x7b;
	wela=0;
	delayms(5);
}

void delayms(uint xms)
{
	uint i,j;
	for(i=xms;i>0;i--)
		for(j=110;j>0;j--);
}
//=================用单片机控制DAC0832芯片输出电流,让发光二极管D12由灭均匀变到最亮,再由最亮均匀熄灭。在最亮和最暗时使用蜂鸣器分别警报一声,完成整个周期时间控制在5s左右,循环变化。
#include
#define uchar unsigned char
#define uint unsigned int

sbit dula=P2^6;
sbit wela=P2^7;

sbit dawr=P3^6;
sbit dacs=P3^2;
sbit beep=P2^3;

void delayms(uint);

void main()
{
	uchar val,flag;
	dula=0;
	wela=0;
	dacs=0;
	dawr=0;
	P0=0;
	while(1)
	{
		if(flag==0)
		{
			val+=5;
			P0=val;
			if(val==255)
			{
				flag=1;
				beep=0;
				delayms(100);
				beep=1;
			}
			delayms(50);
		}
		else
		{
			val-=5;
			P0=val;
			if(val==0)
			{
				flag=0;
				beep=0;
				delayms(100);
				beep=1;
			}
			delayms(50);
		}
	}
}

void delayms(uint xms)
{
	uint i,j;
	for(i=xms;i>0;i--)
		for(j=110;j>0;j--);
}
//=========在上位机上用串口调试助手发送一个字符X,单片机收到字符后返回给上位机”Xi get”,串口波特率设为9600bps.
#include
#define uchar unsigned char
#define uint unsigned int 
uchar flag,a,i;
uchar code table[]="I get ";
void init()
{
	TMOD=0x20;//设定T1定时器工作方式2
	TH1=0xfd;//T1定时器装初值
	TL1=0xfd;//T1定时器装初值
	TR1=1;	//启动T1定时器
	SM0=0;//设定串口工作方式1
	SM1=1;// 设定串口工作方式1
	REN=1;//允许串行接收数据
	EA=1;//开总中断
	ES=1;//开串口中断
}
void main()
{
	init();
	while(1)
	{
		if(flag==1)
		{
			ES=0;
			//
			SBUF=a;//发送数据
			while(!TI);
			TI=0;
			//
			for(i=0;i<6;i++)
			{
				SBUF=table[i];
				while(!TI);
				TI=0;
			}
			//
			ES=1;
			flag=0;
		}
	}
}
void ser() interrupt 4
{
	RI=0;
	a=SBUF;//接收数据
	flag=1;
}
//==================
#include
#include
#include

#define uchar unsigned char
#define uint unsigned int 

sbit dula=P2^6;
sbit wela=P2^7;
sbit adwr=P3^6;
sbit adrd=P3^7;

uchar flag,a;
uchar flag_uart,flag_time,flag_on,a,i,t0_num,ad_val;
float ad_vo;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void delayms(uint xms)
{
	uint i,j;
	for(i=xms;i>0;i--)
		for(j=110;j>0;j--);
}
void init()
{
	TMOD=0x21;
	//SCON=0X50;
	TH0=(65536-50000)/256;
	TH0=(65536-50000)%256;
	
	TH1=0xfd;
	TL1=0xfd;
	TR1=1;	
	SM0=0;
	SM1=1;
	REN=1;
	EA=1;
	ES=1;
}
void display(uchar value)
{
	uchar b,s,g;
	b=value/100;
	s=value%100/10;
	g=value%10;
	
	dula=1;
	P0=table[b];
	dula=0;
	P0=0xff;
	wela=1;
	P0=0x7e;
	wela=0;
	delayms(5);
	
	dula=1;
	P0=table[s];
	dula=0;
	P0=0xff;
	wela=1;
	P0=0x7d;
	wela=0;
	delayms(5);
	
	dula=1;
	P0=table[g];
	dula=0;
	P0=0xff;
	wela=1;
	P0=0x7b;
	wela=0;
	delayms(5);
}

uchar get_ad()
{
	uchar adval;
	adwr=1;
	_nop_();
	adwr=0;
	_nop_();
	adwr=1;
	P1=0xff;
	adrd=1;
	_nop_();
	adrd=0;
	_nop_();
	adval=P1;
	adrd=1;
	return adval;
}
void main()
{
	init();
	wela=1;
	P0=0x7f;
	wela=0;
	while(1)
	{
		if(flag_uart==1)
		{
			flag_uart=0;
			ES=0;
			TI=1;
			switch(flag_on)
			{
				case 0:
					puts("turn on ad!\n");
				break;
				case 1:
					puts("turn off ad!\n");
				break;
				case 2:
					puts("error!\n");
				break;
//				default:
//					puts("warn!");
//				break;
			}
			while(!TI);
			TI=0;
			ES=1;
		}
		if(flag_time==1)
		{
			flag_time=0;
			ad_val=get_ad();
			ad_vo=(float)ad_val*5.0/256.0;
			ES=0;
			TI=1;
			printf("the voltage is %fV\n",ad_vo);
			while(!TI);
			TI=0;
			ES=1;
		}
		display(ad_val);
	}
}
void timer0() interrupt 1
{
	TH0=(65536-50000)/256;
	TH0=(65536-50000)%256;
	t0_num++;
	if(t0_num==20)
	{
		t0_num=0;
		flag_time=1;
	}
}
void ser() interrupt 4
{
	RI=0;
	a=SBUF;
	flag_uart=1;
	if(a==1)
		flag_on=0;
	else if(a==2)
		flag_on=1;
	else
		flag_on=2;
}
//=================





















转载于:https://www.cnblogs.com/javafly/p/6037224.html

你可能感兴趣的:(嵌入式)