在学习MSP430,将以前学习的常用模块移植过来,由于MSP430F149没有iic接口,自己模拟了一个,用到的模块为:DS1302、DS18B20、OLED(IIC接口)。可通过按键切换AD及温度显示(红外程序也包含其中,有兴趣的朋友可以稍微改一下就能实现红外控制) 制作出来的实物图如下:
- #define main_c
- #include "msp430x14x.h"
- #include"main.h"
- #include"iic.h"
- #include"ds1302.h"
- #include "ds18b20.h"
- #include"adc12.h"
- #include "IrRed.h"
- #include"key.h"
- struct sTime BufTime;
- struct sTime SetBuf;
- uchar ddd=0x55;
- uchar Temp_TH=35,Temp_TL=25;
- void System_Init()
- {
- uchar i;
- BCSCTL1&=~XT2OFF; //打开XT2振荡器
- do
- {
- IFG1&=~OFIFG; //清除故障标志
- for(i=0xff;i>0;i--); //延时
- }while(IFG1&OFIFG); //等待无故障
- BCSCTL2|=SELM_2+SELS; //主时钟及子系统时钟使用XT2
- }
- void TimeA_Init()
- {
- TACTL|= TASSEL_1+ ID_3+ MC_1; //时钟选择ACLK 8分频 增计数模式
- CCTL0|= CCIE; //开中断
- CCR0=103; //0.25s中断
- }
- void Get_Time() //使用结构体 获取及显示
- {
- static uchar s=0;
- Get_DS1302_Time(&BufTime);
- OLED_ShowString(0, 2, "WEEK:",16);
- OLED_ShowString(0, 0, "20",16);
- OLED_ShowChar(33, 0, '-',16);
- OLED_ShowChar(57, 0, '-',16);
- OLED_ShowChar(78, 2,':',16);
- OLED_ShowChar(102, 2,':',16);
- if(ddd!=BufTime.sec)
- {
- OLED_DS1302_Num(17,0,BufTime.year,16);
- OLED_DS1302_Num(41,0,BufTime.mon,16);
- OLED_DS1302_Num(65,0,BufTime.day,16);
- OLED_DS1302_Num(62,2,BufTime.hour,16);
- OLED_DS1302_Num(86,2,BufTime.min,16);
- OLED_DS1302_Num(110,2,BufTime.sec,16);
- OLED_ShowChar(41,2,(BufTime.week&0x0f)+0x30,16);
- OLED_ShowBattery(110,0,s);
- s++;
- if(s>4)s=0;
- ddd=BufTime.sec;
- }
- }
复制代码- void DS1302_Time()
- {
- static uchar s=0;
- 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';
- OLED_ShowString(0, 0, str,16);
- OLED_ShowBattery(110,0,s);
- s++;
- if(s>4)s=0;
- str[0] = (time[5] & 0x0F) + 0x30;
- str[1] = '\0';
- OLED_ShowString(0, 2, "WEEK:",16);
- OLED_ShowString(41, 2, str,16);
- 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';
- OLED_ShowString(62, 2, str,16);
- psec = time[0];
- }
- }
复制代码- void DS18b20_Temp() //获取温度及oled显示
- {
- uchar ack;
- uchar num0,num1,num2;
- int temp=0;
- ack=GetTemp(&temp);
- if(ack==0)
- {
- num1=temp>>4;
- num2=temp&0x0f;
- num0=num1/10+0x30;
- num1=num1%10+0x30;
- num2=num2%10+0x30;
- OLED_ShowHz(0,5,2,16);
- OLED_ShowChar(32,5,':',16);
- OLED_ShowChar(40,5,num0,16);
- OLED_ShowChar(48,5,num1,16);
- OLED_ShowChar(56,5,'.',16);
- OLED_ShowChar(64,5,num2,16);
- OLED_ShowChar(72,5,' ',16);
- }
- else
- {
- OLED_ShowString(40,5,"ERROR",16);
- }
- DS_Start();
- }
- void AD_Oled() //ad转换显示
- {
- OLED_ShowString(16, 4, "AD1",16);
- OLED_ShowString(78, 4, "AD2",16);
- //OLED_ShowString(45, 4, "℃",16);
- HexDec(10,6,average0,buffer0);
- HexDec(72,6,average1,buffer1);
- }
- #pragma vector= TIMERA0_VECTOR
- __interrupt void Time_A()
- {
- //T250MSfg=1;
- Key_Scan();
- T25MSfg++;
- if(T25MSfg>20)
- {
- T250MSfg=1;
- flash++;
- T25MSfg=0;
- }
- }
复制代码- #pragma vector =ADC12_VECTOR
- __interrupt void ADC_Get()
- {
- static uchar index=0;
- uchar i;
- result0[index++]=ADC12MEM0; //保存数据,中断标志清零
- result1[index++]=ADC12MEM1;
- if(index==NUM)
- {
- for(i=0;i<NUM;i++)
- {
- average0+=result0;
- average1+=result1;
- }
- index=0;
- average0>>=3;
- average1>>=3;
- }
- }
- #pragma vector =PORT1_VECTOR //红外接收程序
- __interrupt void Ir_Get()
- {
- if(P1IFG&BIT5)
- {
- switch(Ir_Num)
- {
- case 0:
- Ir_Num=1;
- Start_time();
- P1IFG&=~BIT5;
- break;
- case 1:
- Stop_time();
- timer=TBR;
- Start_time();
- if(timer>13000)
- {
- Ir_Num=2;
- //P2OUT^=0x01;
- }
- else
- {
- Ir_Num=0;
- }
- P1IFG&=~BIT5;
- break;
- case 2:
- Stop_time();
- timer=TBR;
- Start_time();
- if((timer>=800)&&(timer<=1500))
- {
- data=data<<1;
- count++;
- }
- else if((timer>=1600)&&(timer<=3000))
- {
- data=(data<<1)|0x0001;
- count++;
- }
- else
- {
- Ir_Num=0;
- count=0;
- data=0;
- P1IFG&=~BIT5;
- break;
- }
- if(count==32)
- {
- Ir_ok=1;
- Ir_Num=0;
- count=0;
- //P2OUT^=0x08;
- }
- P1IFG&=~BIT5;
- break;
- default:
- Ir_Num=0;
- break;
- }
- }
- }
复制代码
基于MSP430的OLED实时时钟.zip
(93.51 KB, 下载次数: 1)
|