📄 led.c
字号:
#include "pic.h"
#include "testc.h"
#define uint unsigned int
#define uchar unsigned char
#define z_max 38
//#define MAX_LEN 6
#define MAX_LED_TYPE 8
#define MAX_MODE 12
#define CHANGE_SLOWLY 4
#define SHUN_BIAN 0
#define NI_BIAN 1
#define nop() asm("nop")
#define clrwdt() asm("CLRWDT")
#define TIAOBIAN_TIME 750
#define LIUSHUI_TIME 200
#define PAOMA_TIME 50
#define JIANBIAN_TIME 800
#define PIC12C508
#ifdef PIC12C508
#define IR GP3 // GP0:R,GP1:G,GP2:B
#define PORTB GPIO
#define wei 0x80
//const uchar LED_TABLE[MAX_LEN]={0x01,0x02,0x04,0x07,0x00,0x00};
const uchar led[9]={0x01,0x02,0x04,0x05,0x03,0x06,0x07,0x00,0x10};//const uchar led[8]={0x07,0x03,0x05,0x03,0x01,0x02,0x04,0x00};
const uchar z[z_max]={0x00,0x07,0x87,0x03,0x83,0x02,0x02,0x07,0x87,0x05,0x85,0x01,0x01,0x07,0x87,0x06,0x86,0x04,0x04,0x07, 0x00,0x01,0x01,0x03,0x83,0x02,0x02,0x06,0x86,0x04,0x04,0x05,0x05,0x07,0x87,0x03,0x83,0x01};//前20个为一组变化,后为二组
#else
#define IR RB1
#define wei 0x01
//const uchar LED_TABLE[MAX_LEN]={0x80,0x40,0x20,0xe0,0x00,0x00};
const uchar led[9]={0x80,0x40,0x20,0xa0,0xc0,0x60,0xe0,0x00,0x10};//const uchar led[8]={0xe0,0xc0,0xa0,0x60,0x80,0x40,0x20,0x00};
const uchar z[z_max]={0x00,0xe0,0xe1,0xc0,0xc1,0x40,0x40,0xe0,0xe1,0xa0,0xa1,0x80,0x80,0xe0,0xe1,0x60,0x61,0x20,0x20,0xe0, 0x00,0x80,0x80,0xc0,0xc1,0x40,0x40,0x60,0x61,0x20,0x20,0xa0,0xa0,0xe0,0xe1,0xc0,0xc1,0x80};//前20个为一组变化,后为二组
#endif
uchar mode,ChangeIndex,flash_time,test;
uint time_2ms_cnt,timer,sys;
bit flag,f_have_key,f_zero,f_first_time,f_direction,f_timer,f_start_to_change,f_light;
//const uchar z[z_max]={0,1,4,0,1,2,5,1,2,0,3,2,0,5,6,3,1,3,6,4,2,4,6,5};
void KeyScan(void);
void KeyDeal(void);
void light(uchar p);
#ifdef PIC12C508
void ini(void)
{
TRIS = 0x28; //GP0,GP1,GP2,GP3 OUTPUT
OPTION = 0xc7;
clrwdt();
PORTB = 0;
PORTB = 0xff;
PORTB = 0;
mode = 0;
ChangeIndex = 0;
f_have_key=1;
f_direction=SHUN_BIAN; // 顺
timer = 0;
f_timer = 0;
f_start_to_change = 1;
f_first_time = 1;
f_zero = 0;
time_2ms_cnt = TIAOBIAN_TIME;
}
#else
void ini(void)
{
TRISB = 0x0f; //RB7,RB6,RB5,RB4 OUTPUT
TRISA = 0xff;
OPTION = 0x07;
clrwdt();
PORTB = 0;//led[ChangeIndex++];
mode = 0;
ChangeIndex = 0;
f_have_key=1;
f_direction=SHUN_BIAN; // 顺
timer=0;
f_timer = 0;
f_start_to_change = 1;
f_first_time = 1;
f_zero = 0;
time_2ms_cnt = TIAOBIAN_TIME;
}
#endif
void delay(uchar time)
{
uint i;
while(time--)
{
i=100;
while(i--)
continue;
}
clrwdt();
}
void light(uchar p)
{
uchar i,j,k,l;
if(z[p]&wei) //渐变暗
{ i=201;
while(i--)
{ clrwdt();
if(i>194)
j=180;
else
j=6;
l=j;
while(l--)
{
PORTB = z[p];
k=i;
while(k--);
clrwdt();
PORTB = z[p+1];
k=201-i;
while(k--);
}
}
}
else //渐变亮
{
i=201;
while(i--)
{ clrwdt();
if(i>190)
j=20;
else
{if(j==10)
j=10;
else
j--; }
l=j;
//j=18;
while(l--)
{
PORTB = z[p];
k=i;
while(k--);
clrwdt();
PORTB = z[p+1];
k=201-i;
while(k--);
}
}
}
}
void KeyScan(void)
{
clrwdt();
if(f_first_time)
{
sys++;
if(sys>30000)
{
mode++;
sys=0;
f_have_key=1;
}
}
}
void KeyDeal(void)
{
clrwdt();
if(!f_have_key)
return;
f_have_key = 0;
timer = 0;
f_timer=1;
if(mode == 0) // 跳变
{
ChangeIndex = 0;
time_2ms_cnt = TIAOBIAN_TIME; // 1.5s
}
else if(mode==1) // 顺流水
{
time_2ms_cnt = LIUSHUI_TIME; // 0.5s
f_direction=SHUN_BIAN; // 顺
ChangeIndex = 0;
}
else if(mode == 2) // 逆流水
{
time_2ms_cnt = LIUSHUI_TIME;
f_direction = NI_BIAN; // 逆
ChangeIndex = 2; // 6
}
else if(mode==3) // 顺跑马
{
time_2ms_cnt = PAOMA_TIME; // 0.5s
f_direction=SHUN_BIAN; // 顺
ChangeIndex = 0;
}
else if(mode == 4) // 逆跑马
{
time_2ms_cnt = PAOMA_TIME; // 0.5s
f_direction=NI_BIAN; // 逆
ChangeIndex = 2; // 6
}
else if(mode<12) // 定位
{
ChangeIndex = 0;
}
else if(mode==12) // 渐变
{
test=0;
f_first_time = 0;
ChangeIndex = 20;
time_2ms_cnt = JIANBIAN_TIME;
}
}
void Display(void)
{
clrwdt();
switch(mode)
{
case 0:
if(!f_start_to_change)
break;
f_start_to_change = 0;
PORTB = led[ChangeIndex++];
if(ChangeIndex>=MAX_LED_TYPE-1)
ChangeIndex = 0;
break;
case 1:
case 2:
case 3:
case 4:
if(!f_start_to_change)
break;
f_start_to_change = 0;
if(f_direction==SHUN_BIAN)
{
PORTB=led[ChangeIndex++];
if(ChangeIndex>2)
ChangeIndex = 0;
}
else
{
PORTB=led[ChangeIndex--];
if(ChangeIndex==0xff)
ChangeIndex=2;
}
break;
case 5: // 定位
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
PORTB = led[mode-5];
break;
case 12: // 渐变
if(!f_start_to_change)
break;
f_start_to_change = 0;
light(ChangeIndex++);
ChangeIndex++;
if(ChangeIndex>37)
{ ChangeIndex = 22;
test++;
if(test>1)
{
mode=0;
f_first_time = 1;
f_have_key=1;
}
}
break;
default:
break;
}
}
void main(void)
{
ini();
delay(5);
delayms();
while(1)
{
KeyScan();
KeyDeal();
//mode=12;
//time_2ms_cnt = JIANBIAN_TIME;
delay(2); // 2ms
if(f_timer)
timer++;
if(timer==time_2ms_cnt)
{
f_start_to_change = 1;
timer = 0;
}
//mode = 12;
Display();
clrwdt();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -