⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ldxzy.c

📁 MSP430的12位AD驱动程序 [05-10-29] 74HC165驱动程序 [05-10-29] 74HC595驱动程序
💻 C
字号:
#include <reg52.h>
#include <stdio.h>
#include <absacc.h>
#define uchar unsigned char
#define uint unsigned int
#define M8155 XBYTE[0x4100]
#define A8155 XBYTE[0x4101]
#define B8155 XBYTE[0x4102]
#define C8155 XBYTE[0x4103]
#define A887 XBYTE[0x800a]
#define B887 XBYTE[0x800b]
#define C887 XBYTE[0x800c]
#define D887 XBYTE[0x800d]
bdata uchar M887;
uchar Counts,DispB,Keys;
uchar at[6]={ 9,8,7,4,2,0 };
uchar dt[6]={ 0,5,22,16,31,0 };
uchar Led[8]= { 0,0,0,0,0,0,0,0 };
uchar DispBit[8]= { 0,0,0,0,0,0,0,0 };
uint DispCount=0,GBCount=0;
long MMS;
uchar adc,GB=0,UP=0,EBZ=0,MMBZ=0;
uchar code DispV[]={ 0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80 };
uchar code DispCode[]={ 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,
//                       0     1    2    3    4    5    6    7    8    9    A    b    c    d    E    F
			0x00,0x40,0x3e };
//                            -     U
uchar code EnCode[15][8]= { { 0,0,17,0,0,17,0,0 }, { 1,16,16,18,17,0,0,0 },  
			 { 2,16,16,0,0,17,0,0 }, { 3,16,0,0,0,0,0,0 },
			 { 4,16,0,0,0,0,0,0 }, { 5,16,0,0,0,17,0,0 },
			 { 6,16,0,0,0,17,0,0 }, { 7,16,0,0,0,17,0,0 },
			 { 8,16,16,0,0,17,0,0 }, { 9,16,16,0,0,17,0,0 },
			 { 10,16,16,18,17,0,0,0 }, { 11,16,16,18,17,0,0,0 },
			 { 12,16,16,0,0,0,0,0 }, { 13,16,16,0,0,0,0,0 },
			 { 14,16,16,0,0,0,0,0 } };

uint code LedIn[32]={ 0xaaab,0xaaac,0xaaad,0xaab2,0xaab3,0xaab4,0xaab5,0xaaca,0xaacb,0xaaaa,
//                     0 A    1 B    2 C    3 D   4 E    5 F    6 G    7 H    8 I    9 J
		     0xaacc,0xacaa,0xaad5,0xaad4,0xaccb,0xacab,0xaccd,0xab54,0xab2c,0xab2a,0xab32,0xab52,
//                     10K    11L     12M    13N    14O   15P    16Q    17R    18S    19T    20U    21V
		     0xad55,0xab2b,0xacd2,0xacb4,0xaad2,0xaad3,0xab2d,0xab33,0xab53,0xad4a };
//                    22W    23X     24Y    25Z   26播放 27确认 28换档 29功能 30删除 31取消
uchar *DAT=(uchar xdata *)0x0000;
uchar *DAT8155=(uchar xdata *)0x4000;
uchar *DAT887=(uchar xdata *)0x8000;
uchar ch,str[20];
uint tim0js,tim0zjs;
sbit uip_bit=M887^7;
sbit dacload=P1^0;        //串行DA
sbit dacclk=P1^1;
sbit dacdata=P1^2;
sbit adcdo=P1^3;          //串行AD
sbit adccs=P1^4;
sbit adcclk=P1^5;
sbit scl=P1^6;            //24c02
sbit sda=P1^7;
void LoadLed(uchar no);
void SaveLed(uchar no);
void InitTime(void);
void SetTime(void);
void ReadTime(void);
void DispTime(void);
void DispPower(void);
long ReadMM(void);
void SetMM(void);
void SetPT(uchar no);
void SetONOFF(uchar onoff);
void SetPower(uchar no);
void SetK(uchar no);
void GbMove1(uchar x);
void GbMove2(uchar x);
void delay1(uchar x);
uchar readadc(void);
void writedac(uchar x);
void adcck(void);
void dacck(void);
void flash();
void x24c01_init();
void start();
void stop();
void writex(uchar j);
uchar readx();
void clock();
uchar x24c02_read(uchar address);
void x24c02_write(uchar address,uchar info);

void dacck(void)
{
   dacclk=1; delay1(2); dacclk=0; delay1(2);
}
void adcck(void)
{
   adcclk=1; delay1(2); adcclk=0; delay1(2);
}
void flash()
{
   uchar u;
   u=1;
}
void x24c02_init()
{ 
   scl=1; flash(); sda=1; flash(); 
}
void start()
{ 
   sda=1; flash(); scl=1; flash(); sda=0; flash(); scl=0; flash();
}
void stop()
{ 
   sda=0; flash(); scl=1; flash(); sda=1; flash(); 
}
void writex(uchar j)
{
   uchar i,temp;
   temp=j;
   for (i=0;i<8;i++){
      temp=temp<<1; scl=0; flash(); sda=CY; flash(); scl=1; flash(); 
   }
   scl=0; flash(); sda=1; flash();
}
uchar readx()
{
   uchar i,j,k=0;
   scl=0; flash(); sda=1;
   for (i=0;i<8;i++){
      flash(); scl=1; flash();
      if (sda==1) j=1;
      else j=0;
      k=(k<<1)|j; scl=0;
   }
   flash(); return(k);
}
void clock()
{
   uchar i=0;
   scl=1; flash();
   while ((sda==1)&&(i<255))i++;
   scl=0; flash();
}
uchar x24c02_read(uchar address)  //从24C02地址address读一字节数据
{
   uchar i;
   start(); writex(0xa0);
   clock(); writex(address);
   clock(); start();
   writex(0xa1); clock();
   i=readx(); stop();
   delay1(10);
   return(i);
}
void x24c02_write(uchar address,uchar info)  //向24C02地址address写数据info
{
   EA=0;
   start(); writex(0xa0);
   clock(); writex(address);
   clock(); writex(info);
   clock(); stop();
   EA=1;
   delay1(50);
}

uchar readadc(void)    //读串行AD
{  uchar i;
   adccs=0; adcck(); ch=0;
   for (;adcdo==1;) adcck();
   for (i=0; i<8; i++) { adcck(); ch=(ch<<1)|adcdo; }
   adccs=1;
   return(ch);
}

void delay1(uchar x)
{
   uint i;
   for(i=0;i<x;i++);
}

void writedac(uchar x)    //向串行DA输出x
{  uchar i;
   ch=x;  dacload=1;
   dacdata=0; dacck(); dacdata=0; dacck(); dacdata=1; dacck();
   for (i=0; i<8; i++) { ch=ch<<1; dacdata=CY; dacck(); }
   dacload=0; delay1(2); dacload=1; delay1(2);
}
Timer0() interrupt 1 using 1
{
    TH0=0xff;TL0=0xab;
    tim0js++;tim0zjs++;
}
exint1() interrupt 2 using 2  //红外遥控器接收中断程序
{
    uchar mark,zjs,l;uint data1;
    ET1=0;DispB=8;EX1=0;PT0=1;ET0=1;TR0=1;TH0=0xff;TL0=0xab;
    tim0js=0;mark=0;zjs=0;tim0zjs=0; T0=1;
    if(T0==1) mark=1;
    else mark=0;
    for(;zjs<26;) {
      if(tim0zjs>=780) break;
      if((tim0zjs>25)&&(zjs<2)) break;
      if((T0==1)&&(mark==0)) {
	 mark=1; zjs++;
	 if(tim0js>=17) {
	    tim0js=0; TH0=0xff;TL0=0xab;
	    data1=(data1<<1)|0; data1=(data1<<1)|0;
	    zjs++; continue;
	 }
	 if(tim0js<17) {
	    tim0js=0; TH0=0xff;TL0=0xab;
	    data1=(data1<<1)|0; continue;
	 }
     }
     if((T0==0)&&(mark==1)) {
	mark=0; zjs++;
	if(tim0js>=17) {
	   tim0js=0; TH0=0xff;TL0=0xab;
	   data1=(data1<<1)|1;  data1=(data1<<1)|1;
	   zjs++; continue;
	}
	if(tim0js<17) {
	   tim0js=0; TH0=0xff;TL0=0xab;
	   data1=(data1<<1)|1; continue;
	}
     }
   }
   EX1=1;PT0=0;PT1=1;ET0=0;TR0=0;TH0=0xff;TL0=0xab;
   Keys=0xff;
   for (l=0; l<32; l++) {
      if (data1==LedIn[l]) {
	 Keys=l; EX1=0; break;
      }
   }
   ET1=1;
}
exint0() interrupt 0 using 3
{
   INT0=0;  adc=readadc();
   M887=C887;  INT0=1;
}
void InitTime(void)  //初始化时钟
{ 
   A887=0x2f; B887=0x46; M887=C887; M887=D887;
}
void DispTime(void)
{
   ReadTime();
   Led[0]=dt[3]/10;   Led[1]=dt[3]%10;   Led[3]=dt[4]/10;
   Led[4]=dt[4]%10;   Led[6]=dt[5]/10;   Led[7]=dt[5]%10;
}
void ReadTime(void)
{  uchar i;
   do { M887=A887; } while (uip_bit);
   for (i=0; i<6; i++) dt[i]=*(DAT887+at[i]);
}
long ReadMM(void)  //读密码
{  long ms;
   ms=x24c02_read(1*2)*10000+x24c02_read(2*2)*100+x24c02_read(3*2);
   return (ms);
}
void SetMM(void)   //设置密码
{  uchar mm;
   mm=Led[2]*10+Led[3]; x24c02_write(1*2,mm);
   mm=Led[4]*10+Led[5]; x24c02_write(2*2,mm);
   mm=Led[6]*10+Led[7]; x24c02_write(3*2,mm);
}
void SetTime(void)   //设置时间
{  uchar i;
   dt[3]=Led[3]*10+Led[4]; dt[4]=Led[6]*10+Led[7];
   B887=0x80;
   for (i=3; i<6; i++) { *(DAT887+at[i])=dt[i]; }
   InitTime();
}
void DispPower(void)  显示AD值
{
   Led[5]=adc/100; Led[6]=(adc/10)%10; Led[7]=adc%10;
}
void SetPT(uchar no)
{  uchar nn;  //no=0,1,2
   nn=Led[2]; x24c02_write((5+no*3)*2,nn);
   nn=Led[3]*10+Led[4]; x24c02_write((6+no*3)*2,nn);
   nn=Led[6]*10+Led[7]; x24c02_write((7+no*3)*2,nn);
}
void SetONOFF(uchar onoff)
{  uchar o;  //onoff=0,1
   o=Led[3]*10+Led[4]; x24c02_write((14+onoff*2)*2,o);
   o=Led[6]*10+Led[7]; x24c02_write((15+onoff*2)*2,o);
}
void SetPower(uchar no)
{  uchar p;  //no=0,1
   p=Led[5]; x24c02_write((20+no*2)*2,p);
   p=Led[6]*10+Led[7]; x24c02_write((21+no*2)*2,p);
}
void SetK(uchar no)
{  uchar k;  //no=0,1,2
   k=Led[3]*10+Led[4]; x24c02_write((24+no*3)*2,k);
   k=Led[5]; x24c02_write((25+no*3)*2,k);
   k=Led[6]*10+Led[7]; x24c02_write((26+no*3)*2,k);
}
void GbMove1(uchar x)  //移动光标
{  uchar i;
   GB=0xff;
   for (i=x; i<=7; i++) {
      if (Led[i]<=9) {
	 GB=i; break;
      }
   }
   if (GB>9) {
      for (i=2; i<x; i++) {
	 if (Led[i]<=9) {
	    GB=i; break;
	 }
      }
   }
}
void GbMove2(uchar x)  //移动光标
{  uchar i;
   GB=0xff;
   for (i=x; i>=2; i--) {
      if (Led[i]<=9) {
	 GB=i; break;
      }
   }
   if (GB>9) {
      for (i=7; i<x; i--) {
	 if (Led[i]<=9) {
	    GB=i; break;
	 }
      }
   }
   if (GB>7) GB=7;
}
void LoadLed(uchar no)   //读取参数
{  uchar i; //no=0,1,---14
   for (i=0;i<8;i++) Led[i]=EnCode[no][i];
   switch (no) {
      case 0:
	 DispTime(); break;
      case 1:
	 DispPower(); break;
      case 2:
	 ReadTime();
	 Led[3]=dt[3]/10;   Led[4]=dt[3]%10;
	 Led[6]=dt[4]/10;   Led[7]=dt[4]%10;
	 break;
      case 3:
	 i=x24c02_read(1*2); Led[2]=i/10; Led[3]=i%10;
	 i=x24c02_read(2*2); Led[4]=i/10; Led[5]=i%10;
	 i=x24c02_read(3*2); Led[6]=i/10; Led[7]=i%10;
	 break;
      case 4:
	 i=x24c02_read(1*2); Led[2]=i/10; Led[3]=i%10;
	 i=x24c02_read(2*2); Led[4]=i/10; Led[5]=i%10;
	 i=x24c02_read(3*2); Led[6]=i/10; Led[7]=i%10;
	 break;
      case 5:
      case 6:
      case 7:
	 i=x24c02_read((5+(no-5)*3)*2); Led[2]=i;
	 i=x24c02_read((6+(no-5)*3)*2); Led[3]=i/10; Led[4]=i%10;
	 i=x24c02_read((7+(no-5)*3)*2); Led[6]=i/10; Led[7]=i%10;
	 break;
      case 8:
      case 9:
	 i=x24c02_read((14+(no-8)*2)*2); Led[3]=i/10; Led[4]=i%10;
	 i=x24c02_read((15+(no-8)*2)*2); Led[6]=i/10; Led[7]=i%10;
	 break;
      case 10:
      case 11:
	 i=x24c02_read((20+(no-10)*2)*2); Led[5]=i%10;
	 i=x24c02_read((21+(no-10)*2)*2); Led[6]=i/10; Led[7]=i%10;
	 break;
      case 12:
      case 13:
      case 14:
	 i=x24c02_read((24+(no-12)*3)*2); Led[3]=i/10; Led[4]=i%10;
	 i=x24c02_read((25+(no-12)*3)*2); Led[5]=i%10;
	 i=x24c02_read((26+(no-12)*3)*2); Led[6]=i/10; Led[7]=i%10;
	 break;
   }
}
void SaveLed(uchar no)  //设置参数
{  long mmd;
   switch (no) {
      case 2:
	 SetTime(); break;
      case 3:
	 mmd=Led[2]*100000+Led[3]*10000+Led[4]*1000+Led[5]*100+Led[6]*10+Led[7];
	 if (mmd==MMS) MMBZ=1;
	 else MMBZ=0;
	 break;
      case 4:
	 SetMM(); break;
      case 5:
      case 6:
      case 7:
	 SetPT(no-5); break;
      case 8:
      case 9:
	 SetONOFF(no-8); break;
      case 10:
      case 11:
	 SetPower(no-10); break;
      case 12:
      case 13:
      case 14:
	 SetK(no-12); break;
   }
}
Timer1() interrupt 3  //LED显示中断程序
{  uchar AA,BB;
   if(Counts++>6) {
      Counts=0; BB=DispV[DispB];
      if ((GB>1)&&(GB==DispB)&&(GBCount<200)) AA=DispCode[16];
      else {
	 if ((UP>=12)&&(DispB==4)) AA=DispCode[Led[DispB]]|0x80;
	 else AA=DispCode[Led[DispB]];
      }
      B8155=BB; A8155=AA;
      DispB=(DispB>7) ? 0 : ++DispB;
      GBCount=(GBCount>400) ? 0 : ++GBCount;
   }
}
void main(void)
{
   uchar i,j,AD;
   Counts=0; DispB=0; DispCount=0; Keys=0xff;
   TMOD=0x22; TH1=0x0a; TL1=0x0a; M8155=0x0f;
   InitTime(); adccs=1; adcclk=0; dacclk=0; dacload=1;
   PT1=1;EX1=1; EX0=1; TR1=1; ET1=1; EA=1;
   MMS=ReadMM();
   for (i=0;i<8;i++) Led[i]=EnCode[0][i];
   while(1){
      if (DispCount++>1000) {
	 DispCount=0;
	 if (UP==0) DispTime();
	 if (UP==1) DispPower();
	 if (Keys!=0xff) {
	    if ((Keys<=9)&&(EBZ==1)) {
	       Led[GB]=Keys;
	       GB=(GB==7) ? 2 : ++GB;
	       GbMove1(GB);
	    }
	    else {
	       switch (Keys) {  //键盘处理
		  case 19:  //KeyUP
		     if (EBZ==1) {
			UP=(UP==1) ? 14 : --UP;
			LoadLed(UP);
			if (GB>1) GbMove1(2);
		     }
		     break;
		  case 23:  //KeyDOWN
		     if (EBZ==1) {
			UP=(UP>=14) ? 1 : ++UP;
			LoadLed(UP);
			if (GB>1) GbMove1(2);
		     }
		     break;
		  case 24:  //KeyLEFT
		     if ((EBZ==1)&&(UP>1)) {
			GB=(GB<=2) ? 7: --GB;
			GbMove2(GB);
		     }
		     else GB=0;
		     break;
		  case 25:  //KeyRIGHT
		     if ((EBZ==1)&&(UP>1)) {
			GB=(GB>=7) ? 2 : ++GB;
			GbMove1(GB);
		     }
		     else GB=0;
		     break;
		  case 27:  //KeyENTER
		     if ((EBZ==1)&&(UP>1)) {
			SaveLed(UP);
			GB=0;
		     }
		     break;
		  case 29:  //KeyFUNCT
		     EBZ=(EBZ==0) ? 1 : 0;
		     if (EBZ==0) {
			LoadLed(0); MMBZ=0; UP=0; GB=0;
		     }
		     else {
			LoadLed(1); UP=1; GB=0;
		     }
		     break;
		  case 31:  //KeyESC
		     if (EBZ==1) {
			EBZ=0;
			LoadLed(0); GB=0; UP=0; MMBZ=0;
		     }
		     break;
	       }
	    }
	    delay1(3000);
	    EX1=1;
	 }
	 Keys=0xff;
      }
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -