标题:
自己做的旋转月球无线感应彩色灯单片机源程序
[打印本页]
作者:
丁后庄
时间:
2018-10-21 15:29
标题:
自己做的旋转月球无线感应彩色灯单片机源程序
旋转月球灯的感应部分程序,可以无极调光
单片机源程序如下:
#include "main.h"
unsigned short TimeCounter_100ms;
unsigned char TimeFlag_100ms;
unsigned short TimeCounter_500ms;
unsigned char TimeFlag_500ms;
unsigned short TimeCounter_30ms;
unsigned char TimeFlag_30ms;
volatile unsigned int shuju[3]={0}; //存放数据
unsigned int temp=0; //临时变量
unsigned char i,j; //数组及移位计数用
unsigned char biaozhi=0; //起始码标志位
unsigned char flag=1; //有键按下,打开定时器
unsigned int count=0;
unsigned char ledflag=0;
unsigned char xunhuan=1;
unsigned char suo=0;
/****************************************************************
*模块名称:初始化函数模块
*功能描述:
*修改日期:2008-9-8
*****************************************************************/
void SystemInitial(void)
{
OSCCON=0x71;//8M内部RC振荡器
INTCON=0x00;
PIE1=0x00;
SETBIT(PIE1,0);//TMR1IE置1
ANSEL =0 ;
ANSELH = 0;
TRISA=0B10111111;
PORTA=0B00000000;
TimeCounter_100ms=0;
TimeFlag_100ms=0;
TimeCounter_500ms=0;
TimeFlag_500ms=0;
TimeCounter_30ms=0;
TimeFlag_30ms=0;
}
void main(void)
{
SystemInitial();
LedInitial();
Timer0Initial();
Ir_Init(); //初始化函数
PEIE=1; //PEIE: 外设中断允许位
GIE=1; //开全局中断*/
while(1)
{
if(shuju[1]==0XF906 ){shuju[1]=0;LedOff();suo=0;}
if(shuju[1]==0X3BC4 ){shuju[1]=0;LedOn();suo=0;}
if(shuju[1]==0Xd926 ){shuju[1]=0;Led1();suo=0;}
if(shuju[1]==0X59A6 ){shuju[1]=0;Led2();suo=0;}
if(shuju[1]==0X7986 ){shuju[1]=0;Led3();suo=0;}
if(shuju[1]==0x916E ){shuju[1]=0;Led4();suo=0;}
if(shuju[1]==0xa15e ){shuju[1]=0;Led5();suo=0;}
if(shuju[1]==0x817e ){shuju[1]=0;Led6();suo=0;}
if(shuju[1]==0x11ee ){shuju[1]=0;Led7();suo=0;}
if(shuju[1]==0X21DE ){shuju[1]=0;Led8();suo=0;}
if(shuju[1]==0X01FE ){shuju[1]=0;LedBlue();suo=0;}
if(shuju[1]==0xb14e ){shuju[1]=0;LedGreen();suo=0;}
if(shuju[1]==0x31Ce ){shuju[1]=0;LedRed();suo=0;}
if(shuju[1]==0x49b6 )
{
shuju[1]=0;
if(ledflag>1)ledflag--;
suo=1;
xunhuan=0;
}//左
/* if(shuju[1]==0x8976)
{
shuju[1]=0;
xunhuan=1;
suo=1;
}//循环
*/
if(shuju[1]==0x09f6 )
{
shuju[1]=0;
if(ledflag<12)ledflag++;
suo=1;
xunhuan=0;
}//右
if(suo==1)
{
if(ledflag==1){LedOn();}
if(ledflag==2){Led1();}
if(ledflag==3){Led2();}
if(ledflag==4){Led3();}
if(ledflag==5 ){Led4();}
if(ledflag==6){Led5();}
if(ledflag==7){Led6();}
if(ledflag==8){Led7();}
if(ledflag==9){Led8();}
if(ledflag==10){LedBlue();}
if(ledflag==11){LedGreen();}
if(ledflag==12){LedRed();}
}
if(xunhuan==1)
{
/* CLRBIT(PORTD,0);delay(2000);
SETBIT(PORTD,0);delay(2000);
Led2();delay(2000);
Led3();delay(2000);
Led4();delay(2000);
Led5();delay(2000);
Led6();delay(2000);
Led7();delay(2000);
Led8();delay(2000);
LedBlue();delay(2000);
LedGreen();delay(2000);
LedRed();delay(2000);
*/
}
if(TimeFlag_100ms==1)
{
TimeFlag_100ms=0;
}
if(TimeFlag_500ms==1)
{
TimeFlag_500ms=0;
}
if(TimeFlag_30ms==1)
{
TimeFlag_30ms=0;
}
}
}
void Timer0Initial(void)//TIMER0初始化
{
T0CS=0; // 选通Fosc/4
PSA=0; //设定为用TIMER0,而不是WDT
PS2=0; //1:2 预除2倍,数完250次=(1us)*250=250us
PS1=0;//PS2,PS1,PS0为除频设置
PS0=0;
T0IF=0;//设置溢位初始值为0
TMR0=6; //预设TMR0=6,运行250次会进位
T0IE=1;
}
void Ir_Init(void)//初始化函数
{
SETBIT(TRISB,0);//RB0为输入
CLRBIT(ANSEL,12);//作为数字io
INTE=1; //允许外部中断
RBIE=1; //允许RB口中断
INTEDG=0;
WPUB=0X01; //RB2上拉复位
T1CON=0x10;//配置定时器2倍预分频,使用内部时钟,禁止定时器
TMR1H=0;//计数值清0
TMR1L=0;
}
void interrupt Timer0_ISR(void)//TIMER0 1ms定时
{
////////////////////////////
if(T0IF==1)
{
T0IF=0;
count++;
if(count==4)
{
TMR0=6;
count=0;
TimeCounter_30ms++;
if(TimeCounter_30ms>=30)
{
TimeFlag_30ms=TURE;
TimeCounter_30ms=0;
}
TimeCounter_100ms++;
if(TimeCounter_100ms>=100)
{
TimeFlag_100ms=TURE;
TimeCounter_100ms=0;
}
TimeCounter_500ms++;
if(TimeCounter_500ms>=500)
{
TimeFlag_500ms=TURE;
TimeCounter_500ms=0;
}
}
}
//////////////////////////////
if(INTF==1) //判断是否为CCP1模块中断,当在一个系统中时会用
{
if(flag==1)
{
TMR1H=0;
TMR1L=0;
TMR1ON=1; //开定时器
flag=0;
}else{
TMR1ON=0; //先停定时器
temp=TMR1L+TMR1H*256; //取数据并判断
TMR1H=0; //清0计数值
TMR1L=0;
TMR1ON=1; //开定时器等待下一个脉冲到来
if(temp>12600&&temp<14600&&biaozhi==0) //起始码13500
{
biaozhi=1; //当检测到起始码时,下次直接进行入数据判断
i=0;
j=0;
//LedBlue();
}
else //数据判断区
{
if((temp < 1300)&&(temp > 1000)) //时间在2.2ms左右的为数码1120
{
shuju[i]=shuju[i]<<1;
j++;
}else if((temp < 2390)&&(temp > 2000)) //时间在4ms左右的为为数码1 2240
{
shuju[i]=(shuju[i]<<1)+1;
j++;
}
else
biaozhi=0; //否则为终止码,清0起始码标志位,准备下一次按键的接收
}
……………………
…………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
Program.zip
(403.94 KB, 下载次数: 7)
2018-10-21 15:28 上传
点击文件名下载附件
下载积分: 黑币 -5
作者:
admin
时间:
2018-10-21 18:42
补全原理图或者详细说明一下电路连接即可获得100+黑币
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1