本文目录一览:
求用C语言编程实现51单片机的LED灯移动
#includereg51.h
#define uchar unsigned char
sbit c10=P1^0;
sbit c11=P1^1;
sbit c12=P1^2;
sbit c13=P1^3;
uchar led1,led3;
void delay(uchar a)
{
uchar i,j;
for(i=0;ia;i++)
for(j=0;j120;j++);
}
main()
{
uchar keyval=0xff;
led1=0xfe;
led3=0xf8;
while(1)
{
if(c10==0)
{
delay(10);
if(c10==0)
{
while(c10==0);
keyval=0;
}
}
if(c11==0)
{
delay(10);
if(c11==0)
{
while(c11==0);
keyval=1;
}
}
if(c12==0)
{
delay(10);
if(c12==0)
{
while(c12==0);
keyval=2;
}
}
if(c13==0)
{
delay(10);
if(c13==0)
{
while(c13==0);
keyval=3;
}
}
switch(keyval)
{
case 0:
P0=led1;
led1=(led11)|0x01;
if(led1==0xff)led1=0xfe;
delay(100);
case 1:
P0=led1;
led1=(led11)|0x80;
if(led1==0xff)led1=0x7f;
delay(100);
case 2:
P0=led3;
led3=(led31)|0x01;
if((led30xf0)==0x30)led3=0x3e;
if((led30xf0)==0x70)led3=0x7c;
if((led30xf0)==0xf0)led3=0xf8;
break;
case 3:
P0=led3;
led3=(led31)|0x80;
if((led30x0f)==0x0c)led3=0x7c;
if((led30x0f)==0x0e)led3=0x3e;
if((led30x0f)==0x0f)led3=0x1f;
break;
default:break;
}
}
}
C语言控制LED灯
//! 没有硬件方案,以下只包含逻辑
int push_count = 0;//记录按钮按下次数
//! 处理亮灯
void light(int led_num)
{
switch(led_num)
{
case 1:
{
;// 点亮 灯1
} break;
case 2:
{
;// 点亮 灯1
} break;
default:
; //灭 所有 灯
break;
}
}
//! 处理按键中断
void push_interrupt()
{
//判断按键是否有效
push_count ++;
if(1 == push_count)
{
light(1);
}
else if(2 == push_count)
{
light(2);
}
else if(8 == push_count)
{
light(0);
push_count = 0;
}
}
led旋转显示的c语言编程,基于c51单片机40引脚
#include "reg52.h" //单片机头文件
#include led.h
sfr P4=0xe8;
sbit KEY= P4 ^ 3; //红外感应 A04E
/*********************************************************************************************
字幕数组
*********************************************************************************************/
unsigned char code zimu[] = {// (取码:从上到下从左到右,纵向8位下高位 )
//空格
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//"仙",
0x40,0x00,0x20,0x00,0xF8,0xFF,0x07,0x00,0x00,0x00,0xF0,0xFF,0x00,0x40,0x00,0x40,
0x00,0x40,0xFF,0x7F,0x00,0x40,0x00,0x40,0x00,0x40,0xF0,0xFF,0x00,0x00,0x00,0x00,
//"剑",
0x40,0x00,0x20,0x20,0x50,0x61,0x48,0x2E,0x44,0x20,0x43,0x2F,0x64,0x10,0x48,0x1C,
0x10,0x13,0x20,0x00,0xF8,0x0F,0x00,0x40,0x00,0x80,0xFF,0x7F,0x00,0x00,0x00,0x00,
//"奇",
0x40,0x00,0x40,0x00,0x44,0x00,0x44,0x3E,0x64,0x12,0x54,0x12,0x4C,0x12,0x47,0x12,
0x4C,0x3F,0x54,0x42,0x74,0x80,0xC6,0x7F,0x44,0x00,0x60,0x00,0x40,0x00,0x00,0x00,
//"侠",
0x80,0x00,0x40,0x00,0x20,0x00,0xF8,0xFF,0x07,0x81,0x08,0x41,0x28,0x21,0x48,0x11,
0x08,0x0D,0xFF,0x03,0x08,0x0D,0x48,0x11,0x2C,0x61,0x88,0xC1,0x00,0x41,0x00,0x00,
//"传",
0x80,0x00,0x40,0x00,0x20,0x00,0xF8,0xFF,0x47,0x00,0x48,0x00,0x48,0x02,0x48,0x0B,
0xC8,0x12,0x7F,0x62,0x48,0xD2,0x48,0x0A,0x4C,0x06,0x68,0x02,0x40,0x00,0x00,0x00,
//"四",
0x00,0x00,0xFC,0x7F,0x04,0x20,0x04,0x28,0x04,0x24,0xFC,0x23,0x04,0x20,0x04,0x20,
0x04,0x20,0xFC,0x21,0x04,0x22,0x04,0x22,0x04,0x22,0xFE,0x7F,0x04,0x00,0x00,0x00,
//空格
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//"谪",
0x40,0x00,0x42,0x00,0xCC,0x7F,0x00,0x20,0x04,0x10,0xE4,0xFF,0x24,0x01,0x2C,0x7D,
0x35,0x25,0xE6,0x27,0x34,0x25,0xAC,0x7D,0x24,0x81,0xF6,0x7F,0x24,0x00,0x00,0x00,
//"仙",
0x40,0x00,0x20,0x00,0xF8,0xFF,0x07,0x00,0x00,0x00,0xF0,0xFF,0x00,0x40,0x00,0x40,
0x00,0x40,0xFF,0x7F,0x00,0x40,0x00,0x40,0x00,0x40,0xF0,0xFF,0x00,0x00,0x00,0x00,
//空格
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//空格
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
/*********************************************************************************************
函数名:LED显示用延时函数
调 用:delay0();
参 数:无
返回值:无
结 果:占用CPU方式延时500微秒时间
备 注:晶振11.0592m
/*********************************************************************************************/
void delay0(void) //误差 -0.46875us
{
unsigned char a,b;
for(b=1;b0;b--)
for(a=227;a0;a--);
}
/*********************************************************************************************/
/*********************************************************************************************
函数名:CPU延时函数
调 用:delay1();
参 数:无
返回值:无
结 果:占用CPU方式延时4000微秒时间
备 注:晶振11.0592M
/*********************************************************************************************/
void delay1(void) //误差 -0.000000000005us
{
unsigned char b,c;
for(c=126;c0;c--)
for(b=13;b0;b--);
}
/*********************************************************************************************/
/*********************************************************************************************
函数名:关闭所有LED灯(操作硬件)
调 用:DISPLAY_OFF();
参 数:无
返回值:无
结 果:所有LED灯熄灭
备 注:
/**********************************************************************************************/
void DISPLAY_OFF (void){
delay0();//显示停留
P0 = 0xff;
P3 = 0xff;
}
/**********************************************************************************************/
/*********************************************************************************************
函数名:单帧数据送入显示(操作硬件)
调 用:display(?,?);
参 数:16位显示数据(下高位)(d:上面8位,e:下面8位)
返回值:无
结 果:点亮对应数据的LED灯
备 注:
/**********************************************************************************************/
void display(unsigned char d,unsigned char e){ //第1列横向显示程序
P3=~d;
P0=~e;
DISPLAY_OFF();
}
/*********************************************************************************************
函数名:初始化程序
调 用:init();
参 数:无
返回值:无
结 果:IO接口全部置1
/**********************************************************************************************/
void init (void){
P1 = 0xff; //初始IO接口状态
P0 = 0xff;
P2 = 0xff;
P4 = 0xff;
}
/**********************************************************************************************/
/*********************************************************************************************
函数名:主函数
调 用:无
参 数:无
返回值:无
结 果:程序开始处,无限循环
备 注:
/**********************************************************************************************/
void main (void){
unsigned char a;
unsigned int c=0;//数据指针
init();//初始程序
while(1){
if(KEY== 0)
{
delay1();
while(KEY== 0);
for(a=0;a160;a++){
if(c1279)c=c-1280;
display(tab1[c],tab1[c+1]);
c=c+2;//产生显示一列移位
}
if(c316)c=c+964; //防止指针溢出c=c+1280-316并实现字幕首尾无缝对接
else c=c-316; //调节滚动速度,转一圈产生384-376=8列位移
}
}
}
tab1[]和tab_love[]放在一个点H文件里面 里面是去摸的数组 不行再说 同样的硬件