STC11F LED部分:
#include "STC11.h"
#define u8 unsigned char
#define u16 unsigned int
//全亮
//u8 code Picture1[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f} ;
//u8 code Picture2[8]={0xe7,0xdb,0xbd,0x7e,0x7e,0xbd,0xdb,0xe7};
//u8 code Picture3[8]={0x00,0x7e,0x7e,0x7e,0x7e,0x7e,0x7e,0x00};
//半亮
u8 code Picture1[8]={0x7e,0xbd,0xdb,0xe7,0xff,0xff,0xff,0xff} ;
u8 code Picture2[8]={0xe7,0xdb,0xbd,0x7e,0xff,0xff,0xff,0xff};
u8 code Picture3[8]={0x00,0x7e,0x7e,0x7e,0xff,0xff,0xff,0xff};
u8 code Picture4[8]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe} ;
void delay(unsigned char k)
{
unsigned char i;
for(;k>0;k--)
for(i=0;i<10;i++)
;
}
void show(u8 pic[8],u8 short_n,u16 long_n)
{
for(;long_n>0;long_n--)
{
P3M1=0x7f;
P3M0=0x80;//P37为推免输出
P37=0;
P1=pic[0];
delay(short_n);
P37=1;
P1=pic[4];
delay(short_n);
P3M1=0xbf;
P3M0=0x40;//P36为推免输出
P36=0;
P1=pic[1];
delay(short_n);
P36=1;
P1=pic[5];
delay(short_n);
P3M1=0xdf;
P3M0=0x20;//P35为推免输出
P35=0;
P1=pic[2];
delay(short_n);
P35=1;
P1=pic[6];
delay(short_n);
P3M1=0xef;
P3M0=0x10;//P34为推免输出
P34=0;
P1=pic[3];
delay(short_n);
P34=1;
P1=pic[7];
delay(short_n);
}
}
void show_single(u8 pic[8],u8 short_n,u16 long_n)
{
unsigned char k;
P3M1=0x7f;
P3M0=0x80;//P37为推免输出
P37=0;
for(;long_n>0;long_n--)
for(k=0;k<8;k++)
{
P1=pic[k];
delay(short_n);
}
}
void main()
{
P3M1=0xff;
P3M0=0x00;//P3口为高阻
P1=0;
delay(10);
while(1)
{
show(Picture1,20,3000);
show(Picture2,20,3000);
show(Picture3,20,3000);
show_single(Picture4,200,800);
}
}
STC11F 调速部分:
#include "reg51.h"
//在测试的时候,一定要去掉飞轮!!!!!!!!!
sbit Light=P1^0;
sbit Pwm=P1^1;
sbit Accelerate=P1^2; //该引脚接地,加大油门控制
sbit Stop=P1^3; //该引脚接地,停止
unsigned int k=0;
unsigned char a=0;
void Control()
{
if(Accelerate==0&&k<1100) //检测是否加油门
k++;
if(Stop==0)
k=0;
}
//void delay(unsigned char j)
//{
// unsigned char i;
// for(;j>0;j--)
// for(i=0;i<255;i++)
// ;
//}
/**************************************************************
函数功能:主函数
**************************************************************/
void main(void)
{
EA=1; //开总中断
ET1=1; //定时器T1中断允许
TMOD=0x10; //TMOD=0001 000B,使用定时器T1的模式1,16位
TH1=0xfc-k/256; //定时器T1的高8位赋初值
TL1=0xd3-k%256; //定时器T1的高8位赋初值
TR1=1; //启动定时器T1
Light=0;
while(1)//无限循环等待中断
{
while(TF1==0) ;
TF1=0;
Control();
//delay(5);
}
}
/**************************************************************
函数功能:定时器T1的中断服务程序
在此函数中严重禁止使用如下格式赋值!!!!!!!!!!!!!!!
TH1=(65536-k)/256; //定时器T1的高8位赋初值
TL1=(65536-(125-k))%256; //定时器T1的高8位赋初值
不然输出来的计时长度就会变长!!!!!!!!!!!!!!!!!!
**************************************************************/
void Time1(void) interrupt 3 using 0 //“interrupt”声明函数为中断服务函数
{
unsigned char kh,kl;
kh=k>>8;
kl=k;
TR1=0; //关闭定时器T1
if(a==0)
{
a=1;
Pwm=1; //高电平
TH1=0xfc-kh; //定时器T1的高8位赋初值
TL1=0xff-kl; //定时器T1的低8位赋初值, 不能用小于0xff的数-kl,不然就会借位溢出!
}
else
{
a=0;
Pwm=0; //低电平
TH1=0xab+kh; //定时器T1的高8位赋初值
TL1=0x00+kl; //定时器T1的低8位赋初值 ,不能用大于0x00的数+kl,不然就会溢出!
}
TR1=1; //启动定时器T1
}