Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1054034
  • 博文数量: 178
  • 博客积分: 10222
  • 博客等级: 上将
  • 技术积分: 2215
  • 用 户 组: 普通用户
  • 注册时间: 2008-01-03 11:27
个人简介

有所追求

文章分类

全部博文(178)

文章存档

2012年(1)

2011年(5)

2010年(3)

2009年(78)

2008年(91)

我的朋友

分类:

2009-03-14 22:13:14

/***************************************************
依次显示红粉蓝青四色渐变
****************************************************/
#include
#include
#include
#define uchar unsigned char
#define uint unsigned int
#define MAX_RANK  512
//外中断0
data uchar ColorStyle = 0;           //色彩类型
data uchar EveryRecycleNum = 0;      //每级已经循环次数
data int GrayRank = MAX_RANK;        //当前灰度等级
data int TimerNum;
data uchar TotalRecycleNum = 1;      //每级总的循环次数
bdata uchar Flag;                    //
             
sbit Dir = Flag^0;                   //方向标志,等于0表示渐亮,等于1表示渐暗
sbit JianBian = Flag^1;              //等于1,继续执行渐变程序,等于0,跳出
sbit irq_flag = Flag^2;
sbit RED1 = P0^0;
sbit GRE1 = P0^1;
sbit BLU1 = P0^2;
sbit RED2 = P0^3;
sbit GRE2 = P0^4;
sbit BLU2 = P0^5;
void ClrColor(void);
void SendColor(void);
void SetTimer0(void);
code uchar TimeTable[] =             //512等级,1.5起,每级0.012ms
{
0xfa,0x99,0xfa,0x8e,0xfa,0x83,0xfa,0x78,0xfa,0x6d,0xfa,0x62,0xfa,0x57,0xfa,0x4c,
0xfa,0x41,0xfa,0x36,0xfa,0x2b,0xfa,0x1f,0xfa,0x14,0xfa,0x9,0xf9,0xfe,0xf9,0xf3,
0xf9,0xe8,0xf9,0xdd,0xf9,0xd2,0xf9,0xc7,0xf9,0xbc,0xf9,0xb1,0xf9,0xa6,0xf9,0x9b,
0xf9,0x90,0xf9,0x85,0xf9,0x7a,0xf9,0x6f,0xf9,0x63,0xf9,0x58,0xf9,0x4d,0xf9,0x42,
0xf9,0x37,0xf9,0x2c,0xf9,0x21,0xf9,0x16,0xf9,0xb,0xf9,0x0,0xf8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0xef,0xc1,0xef,0xb6,0xef,0xab,0xef,0xa0,0xef,0x95,
0xef,0x8a,0xef,0x7f,0xef,0x74,0xef,0x69,0xef,0x5e,0xef,0x53,0xef,0x48,0xef,0x3d,
0xef,0x31,0xef,0x26,0xef,0x1b,0xef,0x10,0xef,0x5,0xee,0xfa,0xee,0xef,0xee,0xe4,
0xee,0xd9,0xee,0xce,0xee,0xc3,0xee,0xb8,0xee,0xad,0xee,0xa2,0xee,0x97,0xee,0x8c,
0xee,0x81,0xee,0x75,0xee,0x6a,0xee,0x5f,0xee,0x54,0xee,0x49,0xee,0x3e,0xee,0x33,
0xee,0x28,0xee,0x1d,0xee,0x12,0xee,0x7,0xed,0xfc,0xed,0xf1,0xed,0xe6,0xed,0xdb,
0xed,0xd0,0xed,0xc5,0xed,0xb9,0xed,0xae,0xed,0xa3,0xed,0x98,0xed,0x8d,0xed,0x82,
0xed,0x77,0xed,0x6c,0xed,0x61,0xed,0x56,0xed,0x4b,0xed,0x40,0xed,0x35,0xed,0x2a,
0xed,0x1f,0xed,0x14,0xed,0x9,0xec,0xfd,0xec,0xf2,0xec,0xe7,0xec,0xdc,0xec,0xd1,
0xec,0xc6,0xec,0xbb,0xec,0xb0,0xec,0xa5,0xec,0x9a,0xec,0x8f,0xec,0x84,0xec,0x79,
0xec,0x6e,0xec,0x63,0xec,0x58,0xec,0x4d,0xec,0x41,0xec,0x36,0xec,0x2b,0xec,0x20,
0xec,0x15,0xec,0xa,0xeb,0xff,0xeb,0xf4,0xeb,0xe9,0xeb,0xde,0xeb,0xd3,0xeb,0xc8,
0xeb,0xbd,0xeb,0xb2,0xeb,0xa7,0xeb,0x9c,0xeb,0x90,0xeb,0x85,0xeb,0x7a,0xeb,0x6f,
0xeb,0x64,0xeb,0x59,0xeb,0x4e,0xeb,0x43,0xeb,0x38,0xeb,0x2d,0xeb,0x22,0xeb,0x17,
0xeb,0xc,0xeb,0x1,0xea,0xf6,0xea,0xeb,0xea,0xe0,0xea,0xd4,0xea,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,0x1f,0xe6,0x14,0xe6,0x9,0xe5,0xfe,0xe5,0xf3,0xe5,0xe8,
0xe5,0xdd,0xe5,0xd2,0xe5,0xc7,0xe5,0xbc,0xe5,0xb0,0xe5,0xa5,0xe5,0x9a,0xe5,0x8f,
0xe5,0x84,0xe5,0x79,0xe5,0x6e,0xe5,0x63,0xe5,0x58,0xe5,0x4d,0xe5,0x42,0xe5,0x37,
0xe5,0x2c,0xe5,0x21,0xe5,0x16,0xe5,0xb,0xe4,0xff,0xe4,0xf4,0xe4,0xe9,0xe4,0xde,
0xe4,0xd3,0xe4,0xc8,0xe4,0xbd,0xe4,0xb2,0xe4,0xa7,0xe4,0x9c,0xe4,0x91,0xe4,0x86
};

///////////////////////////////////////////////////////////////

#define BOMBCNT  40
#define CUSTOMER 1  //CUSTOM
#define DESIGNER 0  //USER
#define BOMB P2_3
data uint seed=0;
data uchar seedH,seedL;
data uchar FlagBomb;

void KeyScan(void);

////////////////////////////////////////////////////////////
//以下IIC
#define OP_READ 0xa1                            // 器件地址以及读取操作
#define OP_WRITE 0xa0                           // 器件地址以及写入操作
sbit SDA = P3^6;                                //位定义
sbit SCL = P3^7;                                //位定义
void LED_display(uchar i);                      //数码管显示编码获取函数,例如,i=0,则P0=display[0],即显示数字“0”
void start();                                   //开始位
void stop();                                    //停止位
uchar shin();                                   //从AT24C02移入数据到MCU
bit shout(uchar write_data);                    //从MCU移出数据到AT24C02
void write_byte( uchar addr, uchar write_data); //在指定地址addr处写入数据write_data
void fill_byte(uchar fill_size,uchar fill_data);//填充数据fill_data到EEPROM内fill_size字节
void delayms(uchar ms);                         // 延时子程序
uchar read_current();                           // 在当前地址读取
uchar read_random(uchar random_addr);           // 在指定地址读取
void LED_display(uchar i)
{
// P0 = display[i];
}
void AT24C02(void)
{
 uchar temp;
 uint j;
 SDA = 1;
 SCL = 1;
 temp=read_random(0);
 if(temp {
  P0=0xff;
  for (j = 0; j<5000;j++);                      //延时
  write_byte(0, temp+1);
 }
 else if(temp>BOMBCNT)
  {
   write_byte(0, 0);
  }
 else
  {
/********下面产生随机数********/
   seedL=read_random(1);
   seedH=read_random(2);
   seed=seedH;
   seed<<=8;
   seed=seed+seedL;         //复原种子
   seed=seed*1664525+1;     //生成新种子
   seedL=seed;
   seedH=seed>>8;
   
   write_byte(1, seedL);    //
   write_byte(2, seedH);
   srand(seed);
   FlagBomb= rand()%30;
   if((FlagBomb%3==0)||(FlagBomb%4==0))
    while(1);    
  }
}
void start()
//开始位
{
  SDA = 1;
  SCL = 1;
  _nop_();
  _nop_();
  SDA = 0;
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  SCL = 0;
}
void stop()
// 停止位
{
  SDA = 0;
  _nop_();
  _nop_();
  SCL = 1;
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  SDA = 1;
}
uchar shin()// 从AT24C02移入数据到MCU
{
 uchar i,read_data;
 for(i = 0; i < 8; i++)
 {
  SCL = 1;
  read_data <<= 1;
  read_data |= (uchar)SDA;
  SCL = 0;
 }
 return(read_data);
}
bit shout(uchar write_data) // 从MCU移出数据到AT24C02
{
 uchar i;
 bit ack_bit;
 for(i = 0; i < 8; i++)     // 循环移入8个位
 {
  SDA = (bit)(write_data & 0x80);
  _nop_();
  SCL = 1;
  _nop_();
  _nop_();
  SCL = 0;
  write_data <<= 1;
 }
 SDA = 1;                   // 读取应答
 _nop_();
 _nop_();
 SCL = 1;
 _nop_();
 _nop_();
 _nop_();
 _nop_();
 ack_bit = SDA;
 SCL = 0;
 return ack_bit;            // 返回AT24C02应答位
}
void write_byte(uchar addr, uchar write_data) // 在指定地址addr处写入数据write_data
{
 start();
 shout(OP_WRITE);
 shout(addr);
 shout(write_data);
 stop();
 delayms(10); // 写入周期
}
void fill_byte(uchar fill_size,uchar fill_data)// 填充数据fill_data到EEPROM内fill_size字节
{
 uchar i;
 for(i = 0; i < fill_size; i++)
 {
  write_byte(i, fill_data);
 }
}
uchar read_current()
// 在当前地址读取
{
 uchar read_data;
 start();
 shout(OP_READ);
 read_data = shin();
 stop();
 return read_data;
}
uchar read_random(uchar random_addr)
// 在指定地址读取
{
 start();
 shout(OP_WRITE);
 shout(random_addr);
 return(read_current());
}
void delayms(uchar ms)
// 延时子程序
{
 uchar i;
 while(ms--)
 {
  for(i = 0; i < 120; i++);
 }
}
//IIC END
////////////////////////////////////////////////////////////
  
void ClrColor(void)
{
 RED1 = 1;
 GRE1 = 1;
 BLU1 = 1;    
 RED2 = 1;
 GRE2 = 1;
 BLU2 = 1;
}
void SendColor(void)
{
 switch(ColorStyle)
 {
  case 0:
   RED1 = 0;
   GRE1 = 1;
   BLU1 = 1;
   
   RED2 = 0;
   GRE2 = 1;
   BLU2 = 1;
   break;
  case 1:
   RED1 = 0;
   GRE1 = 1;
   BLU1 = 0;
   RED2 = 0;
   GRE2 = 1;
   BLU2 = 0;
   break;
  case 2:
   RED1 = 1;
   GRE1 = 1;
   BLU1 = 0;
   RED2 = 1;
   GRE2 = 1;
   BLU2 = 0;
   break;
  case 3:
   RED1 = 1;
   GRE1 = 0;
   BLU1 = 0;
   RED2 = 1;
   GRE2 = 0;
   BLU2 = 0;
   break;
  default:
   break;
  }
}
void SetTimer0()
{
 TH0 = TimeTable[(GrayRank<<1)];
 TL0 = TimeTable[(GrayRank<<1)+1];
 TR0 = 1;
}
void EXT_irq(void) interrupt 0 using 0
{
 EveryRecycleNum++;                      //每产生一次中断,每级循环计数加1,再通过下句进行判断是否达到总循环计数
 if(EveryRecycleNum == TotalRecycleNum)  //原来是>=TotalRecycleNum,在最亮时会闪烁
 {
  EveryRecycleNum = 0;
  if(Dir == 0)   
  {
   GrayRank--;
   if(GrayRank == 0)                     //原来<=0,在最亮时有闪烁
   {
    Dir = 1;
   }
  }
  else if(Dir == 1)
  {
   GrayRank++;
  
   if(GrayRank == MAX_RANK)              //等级为60级时,此处改为59;=60时,当灯灭了之后会闪一下 
   {
    Dir = 0;
    ColorStyle++;
    if(ColorStyle == 4)
    {
     ColorStyle = 0;
    }
   }
  }
 } 
 SetTimer0();
 ClrColor(); 
}
void Timer0_irq(void) interrupt 1 using 1
{
 SendColor();
}
void main(void)
{
 P2 = 0xff;       //P23口用于输入
 if(BOMB)
 {
//  write_byte(3, 0); 
  AT24C02();      //void AT24C02(uchar BOMBCNT,uchar ADDR,uchar BOMBMOD)
 }
 else
 {
  write_byte(0, 0);
//  AT24C02(250,3,DESIGNER);
 }
 TMOD = 0x01;
 IT0 = 1;              //边沿触发,低电平必须延长至少一个周期,不能置0,不然会产生多余中断
 ET0 = 1;
 EX0 = 1;
 EA  = 1;       
 
 Dir = 0;    
 JianBian = 1;
 while(1)
 {
  if(JianBian != 1)    //在外中断里设一个计数器,当计数值到,设JianBian = 0,则跳出死循环
  {                    //进入下一个模式,本程序还没加这个计数器,等最后调试再加
   EX0 = 0;
   break;
  }
 }
阅读(1020) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~