以一下一个网上看到的幻彩LED驱动程序,我在51开发板上编译通过了,可是
灯圈一直亮,按K1,K2都没反应,求帮助!
单片机源程序如下:
- #include<reg52.h>
- #include<intrins.h> //写入头文件
- #define uchar unsigned char
- sbit Send_Dat=P3^7; //数据输出,接IN脚
- sbit key3=P2^1;
- sbit key2=P2^4;
- sbit key1=P2^7;
- uchar show[720]; // 8*3*10
- uchar xdata color[10][3]={
- 0xff,0x01,0x00, //0
- 0xff,0x01,0x00,
- 0xff,0x01,0x00,
- 0xff,0x01,0x00,
- 0xff,0x01,0x00,
- 0xff,0x01,0x00,
- 0xff,0x01,0x00,
- 0xff,0x01,0x00,
- 0xff,0x01,0x00,
- 0xff,0x01,0x00,
- 0xff,0x01,0x00, //10
- 0xff,0x01,0x00,
- 0xff,0x01,0x00,
- 0xff,0x01,0x00,
- 0xff,0x01,0x00,
- 0xff,0x01,0x00,
- 0xff,0x01,0x00,
- 0xff,0x01,0x00,
- 0xff,0x01,0x00,
- 0xff,0x01,0x00,
- 0xff,0x01,0x00, //20
- 0xff,0x01,0x00,
- 0xff,0x01,0x00,
- 0xff,0x01,0x00,
- 0xff,0x01,0x00,
- 0xff,0x01,0x00,
- 0xff,0x01,0x00,
- 0xff,0x01,0x00,
- 0xff,0x01,0x00,
- 0xff,0x01,0x00, //29
- }; //G R B
- uchar code GRB[7][3]={
- 0XFF,0X00,0X00,//绿
- 0XFF,0XFF,0X00,//黄
- 0X00,0XFF,0X00,//红
- 0X00,0XFF,0XFF,//品红
- 0X00,0X00,0XFF,//蓝
- 0XFF,0X00,0XFF,//青
- 0XFF,0XFF,0XFF,//白
- };
- void delay();
- void delays();
- /*写入数据时序*/
- void Send_A_bit(uchar VAL)
- {
- if (VAL==1)
- {
- Send_Dat=1;
- _nop_();_nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();
- Send_Dat=0;
- _nop_();_nop_();
- }
- else
- {
- Send_Dat=1;
- _nop_();_nop_();_nop_();_nop_();
- Send_Dat=0;
- _nop_();_nop_();_nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();_nop_();
- }
- }
- void SendAllData()
- {
- unsigned int i;
- for(i=0;i<720;i++)
- Send_A_bit(show[i]);
- }
- void WriteData()
- {
- unsigned char i,j;
- for(i=0;i<10;i++)
- {
- for(j=0;j<3;j++)
- {
- show[24*i+8*j+0]=color[i][j]/128%2;
- show[24*i+8*j+1]=color[i][j]/64%2;
- show[24*i+8*j+2]=color[i][j]/32%2;
- show[24*i+8*j+3]=color[i][j]/16%2;
- show[24*i+8*j+4]=color[i][j]/8%2;
- show[24*i+8*j+5]=color[i][j]/4%2;
- show[24*i+8*j+6]=color[i][j]/2%2;
- show[24*i+8*j+7]=color[i][j]%2;
- }
- }
- }
- void change1(unsigned char k)
- {
- unsigned char i,j;
- for(i=0;i<10;i++)
- {
- for(j=0;j<3;j++)
- color[i][j]=GRB[k][j];
- }
- }
- void change2()
- {
- unsigned char i;
- for(i=0;i<10;i++)
- {
- if(color[i][0]>0&&color[i][2]==0)
- {
- color[i][0]--;color[i][1]++;
- }
- if(color[i][1]>0&&color[i][0]==0)
- {
- color[i][1]--;color[i][2]++;
- }
- if(color[i][2]>0&&color[i][1]==0)
- {
- color[i][2]--;color[i][0]++;
- }
- }
- }
- /*主函数*/
- unsigned char main()
- {
- uchar k=0;
- P2=0XFF;
- change1(0);
- while(1)
- {
- change1(k);
- Send_Dat=0;
- WriteData();SendAllData();
- Send_Dat=0;
- delays();
- k++;if(k>=7)k=0;
- if(key1==0) return 0;
- if(key2==0)
- {
- change1(0);
- while(key3==1)
- {
- change2();
- Send_Dat=0;
- WriteData();SendAllData();
- Send_Dat=0; //发送数据
- }
- }
- }
- }
- /*小延时函数*/
- /*大概1S吧,随便写的*/
- void delay() //100ms
- {
- unsigned char i, j, k;
- i = 5;
- j = 10;
- // k = 147;
- k=14;
- do
- {
- do
- {
- while (--k);
- } while (--j);
- } while (--i);
- }
- /*大延时函数*/
- /*十几秒*/
- void delays()
- {
- delay(); delay(); delay(); delay(); delay(); delay(); delay(); delay(); delay(); delay(); delay(); delay(); delay();
- delay(); delay(); delay(); delay(); delay(); delay(); delay(); delay(); delay();
- }
复制代码
|