您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > 基于MSP430的多功能数字时钟C程序设计
硬件连接图源程序如下://主程序/****************************************功能:1、KEY1循环选择设置时间模式和设置闹钟模式。选择设置时间模式时P2.3口灯亮,选择设置闹钟模式时P2.7口的灯亮。KEY2按下退出模式选择,同时进入时间/闹钟设置,按KEY1小时位加,按KEY2分钟位加。2、KEY3按下退出时间/闹钟设置,同时显示温度,循环按KEY3可切换温度与时间的显示。3、未进入设置模式状态时,KEY2可循环选择闹钟的开启和关闭。注:在闹钟设置状态下,时钟并没有停止,是不停的走动的。且在时间走动显示过程中,小时和分钟之间的分隔符号每秒闪烁次。小时进制。****************************************/#includemsp430x14x.h#includelcd1602.h//===========================DS18B20端口========================================#defineOUT_DS18B20_0P6OUT&=~BIT7//输出#defineOUT_DS18B20_1P6OUT|=BIT7//输出#defineDS18B20_INP6DIR&=~BIT7//方向设置为输入#defineDS18B20_OUTP6DIR|=BIT7//方向设置为输出//============================DS18B20寄存器指令=================================#defineDS18B20_READ_ROM0x33#defineDS18B20_MATCH_ROM0X55#defineDS18B20_SKIP_ROM0XCC#defineDS18B20_SEARCH_ROM0XF0#defineDS18B20_ALARM_SEARCH_ROM0XEC#defineDS18B20_WRITE_RAM0X40#defineDS18B20_READ_RAM0XBE#defineDS18B20_COPY_RAM0X48#defineDS18B20_CONVERT_TEM0X44#defineDS18B20_EECALL_EEPROM0XB8#defineDS18B20_READ_POWER_SUPPLY0XB4//===========================DS18B20相关变量====================================chartemp_data[2]={0x00,0x00};chardispaly[32];///////////////////////////////////////////////charp_key1=255;charn_key1=255;charp_key2=255;charn_key2=255;charp_key3=255;charn_key3=255;charcount1=0;charcount2=0;charmode=0;#definepush_key11#definepush_key22#definepush_key33#definekey1_in(P1IN&BIT0)#definekey2_in(P1IN&BIT1)#definekey3_in(P5IN&BIT0)#definekey_buffsize8charkey_buff[key_buffsize];charkey_indexW=0;charkey_indexR=0;charkey_count=0;voidkey_inbuff(charkey);chargetkey();voidscan_io();voiddelay_ms(unsigneddly){inti,j;for(i=0;idly;i++)for(j=0;j1000;j++);}//////////////////////初始化#defineled1_1P2OUT|=BIT3;#defineled1_0P2OUT&=~BIT3;#defineled2_1P2OUT|=BIT7;#defineled2_0P2OUT&=~BIT7;#defineled3_1P6OUT|=BIT1;#defineled3_0P6OUT&=~BIT1;#defineled4_1P6OUT|=BIT2;#defineled4_0P6OUT&=~BIT2;#defineBeep_0P6OUT&=~BIT3;#defineBeep_1P6OUT|=BIT3;charhour=0;charmin=0;charsec=0;charCLKhour=0;charCLKmin=0;charclkmode=0;chardispmode=0;unsignedinttime=0;voidinit_sysclk(){unsignedchari;BCSCTL1&=~XT2OFF;do{IFG1&=~OFIFG;for(i=0;i0xee;i++);}while(IFG1&OFIFG);BCSCTL2|=SELM_2;//MCLK8MDS18B20的转换需要该时钟频率BCSCTL2|=SELS;;//SMCLK8M}voidinit_TB(){TBCTL=TBCLR;TBCTL|=TBSSEL_2+MC_1+ID_3;//1MTBCCTL0=CCIE;TBCCR0=1000;//1msint}voidio_set(){P6DIR|=BIT1+BIT2+BIT3;P2DIR|=BIT3+BIT7;P1DIR&=~(BIT0+BIT1);P5DIR&=~BIT0;}voiddisplay(){P4OUT=0X80;order();if(hour12){write_char('a');write_char('m');}if(hour=12){write_char('p');write_char('m');}write_char('');lcd1602_disp_byte(hour);if(count2==0){write_char(':');}if(count2==1){write_char('');}lcd1602_disp_byte(min);lcd1602_disp_space();}voiddisplayCLK(){P4OUT=0X80;order();if(CLKhour12){write_char('a');write_char('m');}if(CLKhour=12){write_char('p');write_char('m');}write_char('');lcd1602_disp_byte(CLKhour);write_char(':');lcd1602_disp_byte(CLKmin);lcd1602_disp_space();}//////////////////////voidkey_inbuff(charkey)//键盘部分程序{if(key_count=key_buffsize)return;_DINT();key_count++;key_buff[key_indexW]=key;if(++key_indexW=key_buffsize)key_indexW=0;_EINT();}chargetkey(){charkey;if(key_count==0)return(0);_DINT();key_count--;key=key_buff[key_indexR];if(++key_indexR=key_buffsize)key_indexR=0;_EINT();return(key);}voidscan_io(){p_key1=n_key1;n_key1=key1_in;p_key2=n_key2;n_key2=key2_in;p_key3=n_key3;n_key3=key3_in;if((p_key1==0)&&(n_key1!=0)){key_inbuff(push_key1);}if((p_key2==0)&&(n_key2!=0)){key_inbuff(push_key2);}if((p_key3==0)&&(n_key3!=0)){key_inbuff(push_key3);}}#pragmavector=WDT_VECTOR__interruptvoidWDT_ISR(void){scan_io();}////////////////////////////////==============================================================================voidDS18B20_IO_set(void){P6SEL&=0x7f;}//===========================DS18B20延时========================================voidDS1820_delay(inti){while(i--);}//========================================长延时================================voidDelay(ints){unsignedinti,j;for(i=0;is;i++);for(j=0;js;j++);}//==========================DS18B20复位=========================================voidds18b20_reset(void){DS18B20_OUT;//OUT_DS18B20_1;OUT_DS18B20_0;DS1820_delay(3000);//500usOUT_DS18B20_1;DS18B20_IN;//设置为输入DS1820_delay(40);while(P6IN&BIT7);DS18B20_OUT;OUT_DS18B20_1;DS1820_delay(300);}//========================DS18B20读操作函数=====================================chards18b20_read(void){chari;chartemp=0;for(i=0;i8;i++){DS18B20_OUT;OUT_DS18B20_0;temp=1;OUT_DS18B20_1;DS18B20_IN;if(P6IN&BIT7){temp|=0x80;}DS1820_delay(4);DS18B20_OUT;DS1820_delay(50);}return(temp);}//========================DS18B20写操作函数=====================================voidds18b20_write(charDate){chari;DS18B20_OUT;for(i=0;i8;i++){OUT_DS18B20_0;//DS1820_delay(6);if(Date&0x01){OUT_DS18B20_1;}else{OUT_DS18B20_0;}DS1820_delay(40);OUT_DS18B20_1;Date=1;}DS1820_delay(8);}//======================DS18B20温度转换函数=====================================voidDs18b20Convert(void){ds18b20_reset();ds18b20_write(0xCC);//DS18B20_SKIP_ROMds18b20_write(0x44);//DS18B20_CONVERT_TEMDelay(200);ds18b20
本文标题:基于MSP430的多功能数字时钟C程序设计
链接地址:https://www.777doc.com/doc-4697807 .html