#include "reg51.h"
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sfr P0M1 = 0x93;
sfr P0M0 = 0x94;
sfr P1M1 = 0x91;
sfr P1M0 = 0x92;
sfr P2M1 = 0x95;
sfr P2M0 = 0x96;
sfr P3M1 = 0xb1;
sfr P3M0 = 0xb2;
sfr P4M1 = 0xb3;
sfr P4M0 = 0xb4;
sfr P5M1 = 0xC9;
sfr P5M0 = 0xCA;
sfr P6M1 = 0xCB;
sfr P6M0 = 0xCC;
sfr P7M1 = 0xE1;
sfr P7M0 = 0xE2;
sbit jia =P1^4; //按键
sbit pwm=P3^5;
sfr AUXR = 0x8e;
sfr INT_CLKO = 0x8f;
sbit T0CLKO = P3^5;
unsigned char n,count,jd;
void delay(unsigned int);
void delay500us() //@11.0592MHz 0.5ms
{
unsigned char i, j;
_nop_();
_nop_();
i = 6;
j = 93;
do
{
while (--j);
} while (--i);
}
void timer0() interrupt 1//
{
TL0 = 0x66; //³õʼ»¯¶¨Ê±Æ÷³õÖµºÍÖØ×°Öµ
TH0 = 0xEA;
if(count<jd)//ÅжÏ0.5ms´ÎÊýÊÇ·ñСÓڽǶȱêʶ
pwm=1;//ȷʵСÓÚ£¬pwmÊä³ö¸ßµçƽ
else
pwm=0;//´óÓÚÔòÊä³öµÍµçƽ
count=(count+1);//0.5ms´ÎÊý¼Ó1
count=count%40;//´ÎÊýʼÖÕ±£³ÖΪ40¼´±£³ÖÖÜÆÚΪ20ms
}
void Timer0Init(void) //500??@11.0592MHz
{
AUXR |= 0x80; //?????1T??
TMOD &= 0xF0; //???????
TL0 = 0x66; //??????
TH0 = 0xEA; //??????
TF0 = 0; //??TF0??
TR0 = 1; //???0????
}
void keyscan() //°´¼üɨÃè
{
if(jia==0) //½Ç¶ÈÔö¼Ó°´¼üÊÇ·ñ°´ÏÂ
{
delay(10); //°´ÏÂÑÓʱ£¬Ïû¶¶
if(jia==0) //ȷʵ°´ÏÂ
{
jd++; //½Ç¶È±êʶ¼Ó1
count=0; //°´¼ü°´Ï Ôò20msÖÜÆÚ´ÓпªÊ¼
if(jd==6)
jd=5; //ÒѾ­ÊÇ180¶È£¬Ôò±£³Ö
while(jia==0); //µÈ´ý°´¼ü·Å¿ª
}
}
}
void main()
{
P0M0 = 0x00;
P0M1 = 0x00;
P1M0 = 0x00;
P1M1 = 0x00;
P2M0 = 0x00;
P2M1 = 0x00;
P3M0 = 0x00;
P3M1 = 0x00;
P4M0 = 0x00;
P4M1 = 0x00;
P5M0 = 0x00;
P5M1 = 0x00;
P6M0 = 0x00;
P6M1 = 0x00;
P7M0 = 0x00;
P7M1 = 0x00;
AUXR = 0x80; //¶¨Ê±Æ÷0Ϊ1Tģʽ
INT_CLKO = 0x01; //ʹÄܶ¨Ê±Æ÷0µÄʱÖÓÊä³ö¹¦ÄÜ
TMOD &= 0xf0; //ÉèÖö¨Ê±Æ÷0Ϊģʽ0(16λ×Ô¶¯ÖØ×°ÔØ)
T0CLKO = 1; //³õʼ»¯Ê±ÖÓÊä³ö½Å(ÈíPWM¿Ú)
TR0 = 1; //¶¨Ê±Æ÷0¿ªÊ¼¼Æʱ
ET0 = 1; //ʹÄܶ¨Ê±Æ÷0ÖжÏ
EA = 1;
jd=3;//¶æ»ú¾ÓÖÐ
count=0;
Time0_Init() ;
while(1)
{
keyscan(); //°´¼üɨÃè
}
}
|