完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
`可不可以帮忙把这个程序改下`
|
|
相关推荐
26 个讨论
|
|
|
|
|
|
|
|
本帖最后由 爱我别走 于 2013-8-16 23:47 编辑
哈哈,我给你来个逐条翻译,顺便帮你把程序注释了一下。希望可以帮助你 /*文件名 :speaker.c * 描述 :蜂鸣器happybirthday * 实验平台:51 --> msp430G2553,晶振 1M * 作者 :电子发烧友 * 修改 :2013/8/16 */ /*------------------------------------------------------------ 头文件包含 -------------------------------------------------------------*/ /*#include #include "msp430g2553.h" /*------------------------------------------------------------ 宏定义 -------------------------------------------------------------*/ /****it speaker=P2^7;*/ #define SPEAKER BIT0 #define PORT P1DIR /*------------------------------------------------------------ 全局变量 -------------------------------------------------------------*/ unsigned char timer0H,timer01,time; /*code*/const unsigned char happybirth[]={5,1,1,5,1,1,6,1,2,5,1,2,1,2,2,7,1,4,5,1,1,5,1,1,6,1,2,5,1,2,2,2,2,1,2,4,5,1,1,5,1,1,5,2,2,3,2,2,1,2,2,7,1,2,6,1,2,4,2,1,4,2,1,3,2,2,1,2,2,21,2,2,1,2,4}; /*code*/const unsigned char FREQH[]={0xF2,0xF3,0xF5,0xF5,0xF6,0xF7,0xF8,0xF9,0xF9,0xFA,0xFA,0xFB,0xFB,0xFC,0xFC,0xFC,0xFD,0xFD,0xFD,0xFD,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFF}; /*code*/const unsigned char FREQL[]={0x42,0xC1,0x17,0xB6,0xD0,0xD1,0xB6,0x21,0xE1,0x8C,0xD8,0x68,0xE9,0x5B,0x8F,0xEE,0x44,0x6B,0xB4,0xF4,0x2D,0x47,0x77,0xA2,0xB6,0xDA,0xFA,0x16}; /*------------------------------------------------------------ 延时子程序 注:延时长度请自行调整 -------------------------------------------------------------*/ void delay(unsigned char delaytime) { unsigned /*char*/int Dtimer1; unsigned /*char*/int Dtimer2; for(Dtimer1=0;Dtimer1 { for(Dtimer2=0;Dtimer2<8000;Dtimer2++); } TA0CTL = TASSEL_2 + MC_0; /*TR0=0;*/ } /*------------------------------------------------------------ 定时器0中断 -------------------------------------------------------------*/ /*void Timer() interrupt 1 { TR0=0; speaker=~speaker; TH0=timer0H; TL0=timer01; TR0=1; }*/ #pragma vector=TIMER0_A0_VECTOR __interrupt void Isr_Timer0_A0(void) { TA0CTL = TASSEL_2 + MC_0; /*TR0=0*/ PORT ^= SPEAKER; /*speaker=~speaker;*/ TA0CCR0 = timer0H*256 + timer01;/*TH0=timer0H; TL0=timer01;*/ TA0CTL = TASSEL_2 + MC_1; /* TR0=1;*/ } /*------------------------------------------------------------ song()子程序 -------------------------------------------------------------*/ void song() { /*TH0=timer0H; TL0=timer01; TR0=1; */ TA0CCR0 = timer0H*256 + timer01;/*TH0=timer0H;TL0=timer01;*/ delay(time); TA0CTL = TASSEL_2 + MC_1; /* TR0=1;*/ } /*------------------------------------------------------------ 主程序 -------------------------------------------------------------*/ void main(void) { unsigned char musicCounter,FMQCounter; /*TMOD=1; EA=1; ET0=1; */ WDTCTL = WDTPW + WDTHOLD; // Stop WDT TA0CTL = TASSEL_2 + MC_1; // SMCLK,up mode TA0CCTL0 = CCIE; // CCR0 interrupt enabled __enable_interrupt(); //开全局中断 while(1) { musicCounter=0; while(musicCounter<75) { FMQCounter=happybirth[musicCounter]+7*happybirth[musicCounter+1]-1; timer0H=FREQH[FMQCounter]; timer01=FREQL[FMQCounter]; time=happybirth[musicCounter+2]; musicCounter=musicCounter+3; song(); } } } |
|
|
|
|
|
|
|
|
|
|
|
大神,我有个51程序需要改成430的,毕业设计要用,能不能帮忙改下,感激不尽 #include #include #define uchar unsigned char #define uint unsigned int uchar x,y; //清屏时定义的起始地址 uchar flag,flag1,flag2; //标志位 温湿度传感器时使用 uchar wei; //位定义 uchar zijie; // 字节定义 uchar wdzs,wdxs,sdzs,sdxs,jiaoyan; uchar WZ,WX,SZ,SX,JY; ***it rs=P0^0; //液晶3个端口的定义 ***it rw=P0^1; ***it e =P0^2; ***it SJ= P0^3; //温湿度传感器接口 ***it sck=P0^4; ***it io=P0^5; ***it rst=P0^6; ***it key1=P2^2; ***it key2=P2^1; ***it key3=P2^0; uchar time_data[7]={13,1,11,11,00,00,00};//年,周,月,日,时,分,秒 uchar write_add[7]={0x8c,0x8a,0x88,0x86,0x84,0x82,0x80}; uchar read_add[7]={0x8d,0x8b,0x89,0x87,0x85,0x83,0x81}; uchar disp[13]=""; uchar time_data1[7]=""; //用来存放从1302中读出的时间 uchar time_data2[7]=""; uchar table1[]="温度"; uchar table2[]="湿度"; uchar table3[4]=""; uchar table4[4]=""; uchar table5[]="可显示五字!"; //可显示任意汉字数字及字符 uchar code image[]= { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x80,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x00,0x01,0x80,0x00, 0x00,0x60,0x00,0x18,0x00,0x00,0x00,0x00,0x00,0xC0,0x01,0xE0,0x00,0x01,0x80,0x00, 0x00,0x63,0xE0,0x18,0x00,0x0E,0x01,0xB0,0x00,0xC0,0x07,0xE0,0x00,0x03,0x40,0x00, 0x00,0x3F,0x80,0x7C,0x01,0xF8,0x00,0xF0,0x00,0xC0,0x00,0xF8,0x00,0x02,0x40,0x00, 0x00,0xC7,0x80,0x33,0x80,0x30,0x00,0xF0,0x01,0x98,0x1F,0x98,0x00,0x02,0x40,0x00, 0x00,0x3F,0x80,0xFF,0x80,0x30,0x06,0xFC,0x0F,0xF0,0x1F,0xC0,0x00,0x02,0x40,0x00, 0x00,0x1F,0x81,0xF9,0x80,0x30,0x03,0xF8,0x01,0x80,0x01,0x80,0x00,0x06,0x20,0x00, 0x00,0x6D,0x81,0xF9,0x80,0x30,0x03,0xF0,0x01,0xC0,0x01,0xF8,0x00,0x38,0x1C,0x00, 0x00,0xC1,0x81,0xBF,0x80,0x30,0x00,0xF0,0x01,0xE0,0x1F,0x80,0x01,0xE0,0x03,0x80, 0x00,0xC1,0x81,0xF9,0x83,0xFF,0x80,0xFF,0x03,0x30,0x01,0x80,0x00,0xC0,0x03,0x00, 0x00,0x03,0x81,0xB7,0x80,0x00,0x07,0xE0,0x06,0x38,0x01,0x80,0x00,0x38,0x3E,0x00, 0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x00,0x00,0x0C,0x03,0x80,0x00,0x04,0x20,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x40,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x40,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x40,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x40,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x80,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x80,0x00, 0x00,0x00,0xE0,0xC6,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x80,0x00, 0x00,0xFE,0xC0,0xC6,0x01,0x81,0x83,0xC1,0x80,0x00,0x00,0x00,0x00,0x01,0x00,0x00, 0x00,0x1F,0xC0,0xC3,0xC3,0x83,0x86,0xC3,0x80,0x00,0x00,0x00,0xE0,0x0F,0x80,0x00, 0x01,0xBF,0xC0,0xFF,0x87,0x87,0x86,0x67,0x80,0x00,0x00,0x01,0x98,0x18,0x80,0x00, 0x00,0xFF,0xC0,0xFF,0x01,0x81,0x8E,0x61,0x80,0x00,0x00,0x02,0x04,0x20,0x60,0x00, 0x00,0xFF,0xC0,0xF9,0x81,0x81,0x8C,0x61,0x80,0x00,0x00,0x04,0x04,0x20,0x20,0x00, 0x00,0xFE,0xC1,0xF3,0x01,0x81,0x8E,0x61,0x80,0x00,0x00,0x04,0x02,0xE0,0x20,0x00, 0x01,0x9C,0xC3,0xDE,0x01,0x81,0x86,0x61,0x80,0x00,0x00,0x04,0x01,0x80,0x20,0x00, 0x00,0x36,0xC0,0xCC,0x01,0x81,0x86,0xC1,0x80,0x00,0x00,0x08,0x01,0x80,0x30,0x00, 0x00,0x00,0xC1,0xFF,0xC1,0x81,0x83,0x81,0x80,0x00,0x00,0x08,0x19,0xE0,0x10,0x00, 0x00,0x00,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x2E,0x90,0x10,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x43,0x10,0x10,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x43,0x08,0x10,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x43,0x08,0x10,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x40,0x08,0x30,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x40,0x08,0x20,0x00, 0x00,0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x40,0x08,0x20,0x00, 0x00,0x0C,0x00,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x40,0x08,0x20,0x00, 0x00,0x7E,0x00,0xDE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x20,0x18,0x60,0x00, 0x00,0x1E,0x00,0xD8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x20,0x10,0x40,0x00, 0x00,0x3F,0x81,0xDE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x30,0x20,0x40,0x00, 0x00,0x66,0xC0,0xDB,0x80,0x07,0x00,0x00,0x00,0x00,0x00,0x01,0x10,0x20,0x80,0x00, 0x00,0x3E,0x00,0xDF,0x87,0xFC,0x00,0x00,0x00,0x00,0x00,0x01,0x88,0x60,0x80,0x00, 0x00,0x0C,0xC1,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x88,0x41,0x00,0x00, 0x01,0xFF,0x83,0xFB,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x4E,0x82,0x00,0x00, 0x00,0x0C,0x00,0xFF,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x43,0x02,0x00,0x00, 0x00,0x0C,0x00,0xCC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x0E,0x00,0x00, 0x00,0x1C,0x00,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x08,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x30,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x20,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0xC0,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x80,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, };//显示一幅图片(可以是任意一张图片) void delay_50us(uchar z); void delay_50ms(uchar z); void delay_1s(uchar z); void write_12864com(uchar com); void write_12864dat(uchar dat); void ini(); void clear(); void picture( uchar*p); void display1(); void display2(); //显示温湿度 void Delay (uint z) ; void delay_10us() ; void jieshou (void) ; void read (void) ; void write_1302_byte(uchar dat); void write_1302(uchar add,uchar dat); uchar read_1302(uchar add); void set_rct(); void read_rct(void) ; void time_pros(void); void display3();//显示日期 void display4();//显示时间 void tiaoshi();//时间调整 void main() { ini() ; //液晶初始化 set_rct(); //对时 clear(); //清屏 picture(image); // 显示图片 delay_1s(100); clear(); //清屏 while(1) { //display1();//显示第一行汉字 read(); //读取温湿度 display2() ;//显示温湿度(第二行) tiaoshi (); read_rct();//读取时间 time_pros(); //时间处理函数 display3(); //用来显示日期 (第三行) display4(); //用来显示时间(第四行) } } void delay_50us(uchar z) { uint j; for(;z>0;z--) for (j=19;j>0;j--); } void delay_50ms(uchar z) { uint j; for(;z>0;z--) for (j=6000;j>0;j--); } void delay_1s(uchar z) { uint i,j,k; for (i=z;i>0;i--) for(j=600;j>0;j--) for(k=10;k>0;k--); } void write_12864com(uchar com)//写指令 { rw=0; rs=0; delay_50us(1); P1=com;//数据接口 e=1; delay_50us(10); e=0; delay_50us(2); } void write_12864dat(uchar dat)//写数据 { rw=0; rs=1; delay_50us(1); P1=dat;//数据接口 e=1; delay_50us(10); e=0; delay_50us(2); } void ini()//初始化 { delay_50ms(2); write_12864com(0x30);// delay_50us(4); write_12864com(0x30); delay_50us(4); write_12864com(0x0c);//显示游标 ,反写 ,光标 delay_50us(4); write_12864com(0x01); //清除显示 delay_50us(240); //write_12864com(0x06); //模式设置 //delay_50us(10); } void clear(void) { uchar i,j,k; x=0x80; y=0x80; write_12864com(0x34); //打开扩展指令集 for (i=0;i<2;i++) { for (j=0;j<32;j++) { write_12864com(y+j); write_12864com(x); for (k=0;k<16;k++) { write_12864dat(0x00); } } x=0x88; } write_12864com(0x36);//打开绘图显示 write_12864com(0x30); } void picture( uchar*p) { uchar i,j,k; x=0x80; y=0x80; write_12864com(0x34); for (i=0;i<2;i++) { for (j=0;j<32;j++) { write_12864com(y+j); write_12864com(x); for (k=0;k<16;k++) { write_12864dat(*p++); } } x=0x88; } write_12864com(0x36); write_12864com(0x30); } void display1() //显示汉字 { uchar i,a; a=0; write_12864com(0x80);//地址 write_12864dat(0x20); write_12864dat(0x01); write_12864dat(0x01); write_12864dat(0x20); for (i=4;i<16;i++) { write_12864dat(table5[a]); a++; delay_50us(1); } } void display2()//显示汉字 用数组 { uchar i,a,b,c,d; a=0; b=0; c=0; d=0; write_12864com(0x90);//地址 for (i=0;i<4;i++) { write_12864dat(table1[a]); a++; delay_50us(1); } for (i=4;i<8;i++) { write_12864dat(table3); b++; delay_50us(1); } for (i=8;i<12;i++) { write_12864dat(table2[c]); c++; delay_50us(1); } for (i=12;i<16;i++) { write_12864dat(table4[d]); d++; delay_50us(1); } } void display3() //显示日期 { uchar a; if (disp[10]==0x31) a=0; if (disp[10]==0x32) a=1; if (disp[10]==0x33) a=2; if (disp[10]==0x34) a=3; if (disp[10]==0x35) a=4; if (disp[10]==0x36) a=5; if (disp[10]==0x37) a=6; write_12864com(0x88); write_12864dat(0x32); //2 write_12864dat(0x30); //0 write_12864dat(disp[12]); write_12864dat(disp[11]); write_12864dat(0xa3); write_12864dat(0xad); write_12864dat(disp[9]); write_12864dat(disp[8]); write_12864dat(0xa3); write_12864dat(0xad); write_12864dat(disp[7]); write_12864dat(disp[6]); write_12864dat(0x20); write_12864dat(0x20); write_12864dat(0xa2); write_12864dat(0xe5+a); } void display4() //显示时间 { write_12864com(0x98); write_12864dat(0x20); write_12864dat(0x20); write_12864dat(disp[5]); write_12864dat(disp[4]); write_12864dat(0xa1); write_12864dat(0xc3); write_12864dat(disp[3]); write_12864dat(disp[2]); write_12864dat(0xa1); write_12864dat(0xc3); write_12864dat(disp[1]); write_12864dat(disp[0]); write_12864dat(0x20); write_12864dat(0x20); write_12864dat(0x20); write_12864dat(0x20); } /****************DHT11程序****************/ void Delay (uint z) { uchar x,y; for(x=z;x>0;x--) For(y=110;y>0;y--); } void delay_10us() { uchar i; i--; i--; i--; i--; i--; i--; } void jieshou (void) //数据0 1确认 { uchar i; zijie=0; for(i=0;i<8;i++) { flag=2; while((!SJ)&&flag++); //1bit是否结束 delay_10us(); delay_10us(); delay_10us(); if(SJ) { wei=1; // 超过28us依然为高电平 表示为1 flag=2; while((SJ)&&flag++); } else wei=0; zijie<<=1; zijie|=wei; } } void read (void) { SJ=0; Delay(34); //主机拉低高于18ms SJ=1; delay_10us(); delay_10us(); delay_10us(); delay_10us(); SJ=1; //主机设为输入 判断从机响应信号 if(!SJ) { flag=2; while((!SJ)&&flag++);//判断从机是否发出 80us 的低电平响应号是否结束 flag=2; while((SJ)&&flag++); //判断从机是否发出 80us 的高电平 如发据接收状态 jieshou(); //数据接收状态 sdzs=zijie; //湿度整数8位 jieshou(); sdxs=zijie;//湿度小数8位 jieshou(); wdzs=zijie; //温度整数8位 jieshou(); wdxs=zijie; //温度小数8位 jieshou(); jiaoyan=zijie; //校验位 SJ=1; JY=(sdzs+sdxs+wdzs+wdxs); if(JY==jiaoyan) //数据校验 正确执行 { SZ=sdzs; SX=sdxs; WZ=wdzs; WX=wdxs; //JY=jiaoyan; } } table3[0]=0x3a; table3[1]=(char)(0x30+WZ/10); table3[2]=(char)(0x30+WZ%10); table3[3]=0x63; table4[0]=0x3a; table4[1]=(char)(0x30+SZ/10); table4[2]=(char)(0x30+SZ%10); table4[3]=0x25; } /***************DS1302程序*********************/ void write_1302_byte(uchar dat) //写字节 { uchar i; for (i=0;i<8;i++) { sck=0; io=dat&0x01; dat>>=1; sck=1; } } void write_1302(uchar add,uchar dat) { rst=0; _nop_(); sck=0; _nop_(); rst=1; _nop_(); write_1302_byte(add); write_1302_byte(dat); rst=0; _nop_(); io=1; sck=1; } uchar read_1302(uchar add) { uchar i,value; rst=0; _nop_(); sck=0; _nop_(); rst=1; _nop_(); write_1302_byte(add); for (i=0;i<8;i++) { value>>=1; sck=0; if(io) value=value|0x80; sck=1; } rst=0; _nop_(); sck=0; _nop_(); sck=1; io=1; return value; } void set_rct(void)//设置时间 { uchar i; for(i=0;i<7;i++) //10进制转换16进制 { t ime_data1=time_data%10+time_data/10*16; } write_1302(0x8e,0x00);//去除写保护 for(i=0;i<7;i++) { write_1302(write_add,time_data1); } write_1302(0x8e,0x80);//添加写保护 } void read_rct(void) //对时间 { uchar i; for(i=0;i<7;i++) { time_data2=read_1302(read_add); //此时数组里面是从里面读出来的时间(16进制的) } } void time_pros(void) //处理函数 { disp[0]=(char)(time_data2[6]%16+0x30); //6表示秒 disp[1]=(char)(time_data2[6]/16+0x30); disp[2]=(char)(time_data2[5]%16+0x30); //5表示分 disp[3]=(char)(time_data2[5]/16+0x30); disp[4]=(char)(time_data2[4]%16+0x30); //4表示时 disp[5]=(char)(time_data2[4]/16+0x30); disp[6]=(char)(time_data2[3]%16+0x30); //3表示日 disp[7]=(char)(time_data2[3]/16+0x30); disp[8]=(char)(time_data2[2]%16+0x30); //2表示月 disp[9]=(char)(time_data2[2]/16+0x30); d isp[10]=(char)(time_data2[1]%16+0x30); //1表示周 disp[11]=(char)(time_data2[0]%16+0x30); //0表示年 disp[12]=(char)(time_data2[0]/16+0x30); } void tiaoshi () //时间调整 { if (key1==0) { Delay(10); if(key1==0; { flag1++; if (flag1==1|flag1==2|flag1==3|flag1==4|flag1==5|flag1==6) { write_1302(0x8e,0x00);//允许写 write_1302(0x80,0x80); //时钟停止 } if(flag1==7) { flag1=0; write_1302(0x80,0x00); //启动时钟 write_1302(0x8e,0x80);// 进制写(添加写保护) } } while(!key1); //Delay(10); //while(!key1); } if (key2==0) { Delay(10); if (key2==0) { switch(flag1) { case 1: { time_data[0]=time_data[0]+1; if(time_data[0]==100) time_data[0]=0; } break; case 2: { time_data[2]=time_data[2]+1; if(time_data[2]==13) time_data[2]=1; } break; case 3: { time_data[3]=time_data[3]+1; if(time_data[3]==32) time_data[3]=1; } break; case 4: { time_data[1]=time_data[1]+1; if(time_data[1]==8) time_data[1]=1; } break; case 5: { time_data[4]=time_data[4]+1; if(time_data[4]==24) time_data[4]=00; } break; case 6: { time_data[5]=time_data[5]+1; if(time_data[5]==60) time_data[5]=00; } break; } } while(!key2); set_rct(); } if (key3==0) { Delay(10); if (key3==0) { switch(flag1) { case 1: { time_data[0]=time_data[0]-1; if(time_data[0]==-1) time_data[0]=99; } break; case 2: { time_data[2]=time_data[2]-1; if(time_data[2]==0) time_data[2]=12; } break; case 3: { time_data[3]=time_data[3]-1; if(time_data[3]==0) time_data[3]=31; } break; case 4: { time_data[1]=time_data[1]-1; if(time_data[1]==0) time_data[1]=7; } break; case 5: { time_data[4]=time_data[4]-1; if(time_data[4]==-1) time_data[4]=23; } break; case 6: { time_data[5]=time_data[5]-1; if(time_data[5]==-1) time_data[5]=59; } break; } while(!key3); set_rct(); } } } |
|
|
|
|
|
大家好我有一个51的程序,希望好心人帮忙改下成430的程序,利用GSM模块SIM900A,短信控制LED的开关。 /************************************************ 短信控制单片机LED7的亮与灭 手机发送openled7,单片机上的led7亮 手机发送closeled7,单片机上的led7灭 其余的led灯为指示灯,第一次可以看见程序的运行情况, 及程序运行到了什么地方了 *************************************************/ #include #include #define Buf_Max 100 //缓存长度,这个长度的最大值要根据单片机型号和程序的实际情况来,我的这个100几乎是实际情况的最大限度了 ***it LED1=P1^0; //指示灯,方便观察程序执行到哪一步了 ***it LED2=P1^1; ***it LED3=P1^2; ***it LED4=P1^3; ***it LED5=P1^4; ***it LED6=P1^5; ***it LED7=P1^6; ***it LED8=P1^7; unsigned char i=0; //定义缓存指针 unsigned char Rec_Buf[Buf_Max]; //定义缓存数组 void chuanko_init(); //声明串口初始化函数 void Send_ASCII(unsigned char *b); //声明发送字符(ASCII码)函数 void Delay_ms(unsigned int time); //声明延时函数 void CLR_Buf(); //声明清除缓存内容 void Send_Hex(unsigned char b); //声明发送字符(十六进制)函数 void GSM_init(); //GSM模块初始化 void execute_work(); //按短信内容执行工作 bit Hand(unsigned char *a); //声明判断缓存中是否有指定的字符串函数 void delay_10s(void) //误差 -0.000000003101us { unsigned char a,b,c; for(c=179;c>0;c--) for(b=131;b>0;b--) for(a=195;a>0;a--); } /******************************************** 主函数 *********************************************/ main() { delay_10s(); delay_10s(); delay_10s(); delay_10s(); delay_10s(); chuanko_init(); //初始化串口 delay_10s(); GSM_init(); //GSM模块初始化 while(1) { if(Hand("+CMTI")) //短信来时提示的类容+CMTI:。。。。 { LED5 = 0; //提示灯 CLR_Buf(); //清除缓存内容,由于上面设置了短信来时有提示,所以就有提示类容,所以就要清除缓存 Send_ASCII("AT+CMGR=1"); //发送读取信息指令 Send_Hex(0x0d); //发送回车符 Send_Hex(0x0a); //发送换行符 Delay_ms(50); //延时 while(!Hand("OK")); //等待设置成功 */ LED6 = 0; //点亮设置成功指示灯 execute_work(); //按短信内容执行工作 /////以后要修改程序执行的内容,就改这里//// CLR_Buf(); //清除缓存短信在单片机里的内容 Send_ASCII("AT+CMGD=1"); //删除读取后在GSM模块里的信息 Send_Hex(0x0d); //发送回车符 Send_Hex(0x0a); //发送换行符 Delay_ms(50); //延时 while(!Hand("OK")); //等待设置成功 CLR_Buf(); //清除缓存内容 LED8=0; //点亮设置成功指示灯 } } } /******************************************** GSM模块初始化 *********************************************/ void GSM_init() { CLR_Buf(); //清除缓存内容 Send_ASCII("AT"); //发送联机指令 Send_Hex(0x0d); //发送回车符 Send_Hex(0x0a); //发送换行符 Delay_ms(50); //延时,返回时需要时间,这个延时一般必须有,不然在单片机中程序可能就不能实现应有的效果 while(!Hand("OK")) //判断是否握手成功,如果不成功延时一会,再发送AT握手指令 CLR_Buf(); //清除缓存内容,缓存内容指的是GSM模块通过串口发送到单片机里的内容 LED1 = 0; //点亮握手成功指示灯 Send_ASCII("AT+CPMS="ME","ME","ME"");//所有操作都在ME(模块终端)中进行 Send_Hex(0x0d); //发送回车符 Send_Hex(0x0a); //发送换行符 Delay_ms(50); //延时 while(!Hand("OK")) //等待设置成功 CLR_Buf(); //清除缓存内容 LED2 = 0; //点亮设置成功指示灯 Send_ASCII("AT+CMGF=1"); //文本(TEXT)模式 Send_Hex(0x0d); //发送回车符 Send_Hex(0x0a); //发送换行符 Delay_ms(50); //延时 while(!Hand("OK")) //等待设置成功 CLR_Buf(); //清除缓存内容 LED3 = 0; //点亮设置成功指示灯 Send_ASCII("AT+CNMI=2,1");//设置当有新短信到来时提示 Send_Hex(0x0d); //发送回车符 Send_Hex(0x0a); //发送换行符 Delay_ms(100); //延时 while(!Hand("OK")) //等待设置成功 CLR_Buf(); //清除缓存内容 LED4 = 0; //点亮设置成功指示灯 } /******************************************** 串口初始化函数 *********************************************/ void chuanko_init(void)//串口初始化函数 { SCON = 0x50;//串口:方式1,允许发送和接收 TMOD = 0x20;//定时器1:模式2,8位自动重装模式,用于产生波特率 TH1 = 0xFD; //11.0592MHZ晶振,波特率为9600 TL1 = 0xFD; TR1 = 1; //开启定时器1 ES = 1; //开启串口中断 EA = 1; //开启全局中断 } /******************************************** 串口中断处理函数 *********************************************/ void Serial_Int() interrupt 4 { ES = 0; //关串口中断,防止中断嵌套 if(TI) //如果是发送中断,则不做任何处理 { TI = 0; //清除发送中断标志位 } if(RI) //如果是接送中断,则进行处理 { RI = 0; //清除接收中断标志位 Rec_Buf[i] = SBUF; //将接收到的字符串存到缓存中 i++; //缓存指针向后移动 if(i>100) //如果缓存满,将缓存指针指向缓存的首地址 { i = 0; } } ES = 1; //开启串口中断 } /******************************************** 发送字符(ASCII)函数 *********************************************/ void Send_ASCII(unsigned char *b) { ES = 0; //关串口中断 for (b; *b!=' |