最近学习MSP430,把以前学习的DS1302和DB18B20移植过来了,把时序搞清楚还是比较简单的,温度显示做得比较粗糙,分享一下(PS:后续有时间添加按键设置及其他功能)
单片机源程序如下:
- #include<msp430x14x.h>
- #include"lcd1602.c"
- #include"ds1302.c"
- #include"ds18b20.c"
- //typedef unsigned char uchar;
- //typedef unsigned int uint;
- //uchar str1[]="hongchao12345678";
- //uchar str2[]="hongchao12345678";
- //uchar LcdMove[16+sizeof(str1)+16];
- uchar index=0,T500MSfg=0,flash=0;
- uchar psec=0xaa;
- uchar time[8],str[12];
- //uchar second=10,minute=20,hour=11;
- /*
- void Lcd_Move()
- {
- uchar i;
- for(i=0;i<16;i++)
- {
- LcdMove[i]=' ';
- }
- for(i=0;i<sizeof(str1);i++)
- {
- LcdMove[16+i]=str1[i];
- }
- for(i=16+sizeof(str1);i<sizeof(LcdMove);i++)
- {
- LcdMove[i]=' ';
- }
- }
- */
- void Time0_Init()
- {
- TACTL=TASSEL_1+ ID_3+ MC_1;
- CCTL0=CCIE;
- CCR0=1024;
- }
- /*
- void clock()
- {
- second++;
- if(second>59)
- {
- second=0;
- minute++;
- if(minute>59)
- {
- minute=0;
- hour++;
- }
- }
- }
- void Display()
- {
- uchar s0,s1;
- uchar m0,m1;
- uchar h0,h1;
- s0=second/10;
- s1=second%10;
- m0=minute/10;
- m1=minute%10;
- h0=hour/10;
- h1=hour%10;
- s0+=0x30;
- s1+=0x30;
- m0+=0x30;
- m1+=0x30;
- h0+=0x30;
- h1+=0x30;
- LcdShowStr1(4,0,h0);
- LcdShowStr1(5,0,h1);
- LcdShowStr1(7,0,m0);
- LcdShowStr1(8,0,m1);
- LcdShowStr1(10,0,s0);
- LcdShowStr1(11,0,s1);
- }
- */
- void DS1302_Time()
- {
- //for(index=0; index<7; index++)
- //{
- //time[index] = DS1302SingleRead(index);
- DS1302BurstRead(time);
- //}
- if(psec != time[0])
- {
- str[0] = '2';
- str[1] = '0';
- str[2] = (time[6] >> 4) + 0x30;
- str[3] = (time[6] & 0x0F) + 0x30;
- str[4] = '-';
- str[5] = (time[4] >> 4) + 0x30;
- str[6] = (time[4] & 0x0F) + 0x30;
- str[7] = '-';
- str[8] = (time[3] >> 4) + 0x30;
- str[9] = (time[3] & 0x0F) + 0x30;
- str[10] = '\0';
- LcdShowStr(0, 0, str);
- str[0] = (time[5] & 0x0F) + 0x30;
- str[1] = '\0';
- LcdShowStr(11, 0, "WEEK");
- LcdShowStr(15, 0, str);
- str[0] = (time[2] >> 4) + 0x30;
- str[1] = (time[2] & 0x0F) + 0x30;
- str[2] = ':';
- str[3] = (time[1] >> 4) + 0x30;
- str[4] = (time[1] & 0x0F) + 0x30;
- str[5] = ':';
- str[6] = (time[0] >> 4) + 0x30;
- str[7] = (time[0] & 0x0F) + 0x30;
- str[8] = '\0';
- LcdShowStr(8, 1, str);
- psec = time[0];
- }
- }
- void DS18b20_Temp()
- {
- uchar ack;
- uchar num0,num1;
- int temp=0;
- // ack=DS_Ready();
- ack=GetTemp(&temp);
- if(ack==0)
- {
- num1=temp>>4;
- // num2=temp<<4;
- // num2=num2>>4;
- num0=num1/10+0x30;
- num1=num1%10+0x30;
- // num2=num2%10+0x30;
- LcdShowStr1(0,1,'T');
- LcdShowStr1(1,1,'H');
- LcdShowStr1(2,1,' ');
- LcdShowStr1(3,1,num0);
- LcdShowStr1(4,1,num1);
- // LcdShowStr1(5,1,'.');
- // LcdShowStr1(6,1,num2);
- }
- else
- {
- // LcdShowStr1(0,1,'E');
- // LcdShowStr1(1,1,'R');
- // LcdShowStr1(2,1,'R');
- // LcdShowStr1(3,1,'0');
- // LcdShowStr1(4,1,'R');
- LcdShowStr(0,1,"ERR0R");
- }
- DSStart();
- }
- void main()
- {
- /*下面六行程序关闭所有的IO口*/
- P1DIR = 0XFF;P1OUT = 0XFF;
- P2DIR = 0XFF;P2OUT = 0XFF;
- P3DIR = 0XFF;P3OUT = 0XFF;
- P4DIR = 0XFF;P4OUT = 0XFF;
- P5DIR = 0XFF;P5OUT = 0XFF;
- P6DIR = 0XFF;P6OUT = 0XFF;
- // uchar i;
- WDTCTL = WDTPW + WDTHOLD;
- InitLcd1602();
- // TACTL=TASSEL_2;
- // Lcd_Move();
- Time0_Init(); //定时器使用32768,250ms
- DS1302_Init();
- DSStart();
- _EINT();
- // LcdShowStr(0,1," CLOCK TIME ");
- // LcdShowStr1(6,0,':');
- // LcdShowStr1(9,0,':');
- // LcdShowStr(0,0," : : ")
- while(1)
- {
- // LcdShowStr(1,0,"w23123123!");
- // LcdShowStr(0,1,"hongchao 123");
- if(flash)
- {
- flash=0;
- DS1302_Time();
- DS18b20_Temp();
- // LcdShowStrLen(1,0,LcdMove+index,16);
- // index++;
- //if(index>sizeof(LcdMove))
- // {
- // index=0;
- // }
- // Display();
- }
- }
- }
- #pragma vector =TIMERA0_VECTOR
- __interrupt void TIME_A()
- {
- // T500MSfg++;
- flash=1;
- //if(T500MSfg>2)
- // {
- // T500MSfg=0;
- // flash=1;
- // clock();
- // }
- }
复制代码
全部资料51hei下载地址:
demo.zip
(54.25 KB, 下载次数: 46)
|