{
P0=0; P1=0x2c;
z=0; // 100为100%的占空比 // 机器周期 led=1; while(1) {
keyscan(); led=0; delay(z); led=1;
delay(100-z); } }
拓展2(自动控制灯的亮灭)
#include
#define uchar unsigned char #define uint unsigned int
sbit adcs=P1^7;
//A/D使能端
sbit adrd=P3^7; sbit adwr=P3^6; sbit led=P1^5; uint ad_data,num;
void delay(uint z) {
uint x,y;
for(x=z;x>0;x--) for(y=10;y>0;y--); }
void read_ad() {
adcs=0;
adwr=0;delay(10);adwr=1;delay(10); adrd = 0;delay(10);ad_data =P2;delay(10);adrd=1; 换结果(/rd>200ns) adcs=1; }
void main() {
uint z[]={0,10,20,30,40,50,60,70,80,90};
16
//50微 //启动AD,开始 //读数据读取转
P0=0xff; //初始化 while(1) {
read_ad();
if(ad_data<=50)
num=0;
//判断外界光的强弱从而判断灯应该亮的等级
else if(ad_data>50&&ad_data<=60) num=1;
else if(ad_data>60&&ad_data<=73) num=2;
else if(ad_data>73&&ad_data<=87) num=3;
else if(ad_data>87&&ad_data<=120) num=4;
else if(ad_data>120&&ad_data<=140) num=5;
else if(ad_data>140&&ad_data<=160) num=6;
else if(ad_data>160&&ad_data<=180) num=7;
else if(ad_data>180&&ad_data<=215) num=8;
else if(ad_data>215&&ad_data<=255) num=9;
led=0;delay(z[num]); led=1;delay(100-z[num]); } }
17