本帖最后由 dirtwillfly 于 2015-11-4 09:45 编辑
- #include <msp430.h>
- #include"config.h"
- #include"1838.h"
- #include"1602.h"
- #include"dh11.h"
- #include"ds3231.h"
- #include"ta.h"
- //#include"adc.h"
- int main(void)
- {
- extern_12m();
- hc164_init();
- LCD_init();
- h1838_init();
- init_TA();
- // I2cByteWrite(0xD0,0x0e,0);
- // I2cByteWrite(0xD0,0x0f,0);
- // ModifyTime(15,7,19,12,07,00);//初始化时钟,2007/11/20,12/59/00
- //小时采用24小时制
- get_show_time();
- get_show_Temperature();
- dh11_read();
-
- // adc_init();
- _EINT();
- while (1)
- {
- // LCD_disp_string(0,0,"I AM A BOY?");
- // LCD_disp_string(0,1,"YOU ARE A GIRL!");
- Red_Code();
- Key_Icode(key_code[2]);
- }
- }
- #include"config.h"
- uchar change;
- void extern_12m()
- {
- WDTCTL = WDTPW + WDTHOLD; // Stop WDT
- if (CALBC1_12MHZ==0xFF) // If calibration constant erased
- {
- while(1); // do not load, trap CPU!!
- }
- DCOCTL = 0; // Select lowest DCOx and MODx settings
- BCSCTL1 = CALBC1_12MHZ;
- DCOCTL = CALDCO_12MHZ; // Load 8MHz constants
- }
- void hc164_init()
- {
- P1OUT &= ~(BIT6+BIT7+BIT3); // Clear P1.0
- P1DIR |=BIT6+BIT7+BIT3; // P1.0 output
-
- P2OUT &= ~(BIT0+BIT1+BIT2); // Clear P1.0
- P2DIR |=BIT0+BIT1+BIT2; // P1.0 output
- }
- void LCD_delay_10us(uint n) // - - 10微秒的延时子程序
- {
- uint i,j;
- for(i=n;i>0;i--)
- for(j=4;j>0;j--);
- }
- void sendbyte(uchar byte)
- {
- uchar c,num;
- num=byte;
- for(c=0;c<8;c++)
- {
- P1OUT&=~0x80;
- CLK0;
- P1OUT|=num&0x80; //(0x80即十进制的128, 二进制的10000000 按位发送
- CLK1;
- num<<=1;
- }
- }
- // - - 向LCD1602写指令
- void LCD_write_command(uchar dat)
- {
- LCD_delay_10us(2);
- LCD_RS_LOW; // - - 指令
- //LCD_RW_LOW; // - - 写入
- sendbyte(dat);
- LCD_delay_10us(2);
- LCD_EN_HIGH; // - - 允许
- LCD_delay_10us(2);
- LCD_EN_LOW;
- }
- // - - 向LCD1602写数据
- void LCD_write_data(uchar dat)
- {
- LCD_delay_10us(2);
- LCD_RS_HIGH;// - - 数据
- //LCD_RW_LOW;// - - 写入
- sendbyte(dat);
- LCD_delay_10us(2);
- LCD_EN_HIGH;// - - 允许
- LCD_delay_10us(2);
- LCD_EN_LOW;
- }
- // - - 设置显示位置
- void LCD_set_xy(uchar x,uchar y)
- {
- uchar address;
- if(y==1)
- {
- address=0x80+x; // - - 第一行位置
- } else {
- address=0xc0+x; // - - 第二行位置
- }
- // LCD_delay_10us(1);
- LCD_write_command(address);
- }
- // - - 显示一个字符函数
- void LCD_disp_char(uchar x,uchar y,uchar dat) // - - LCD_disp_char(0,1,0x38); // - - 显示8
- {
- LCD_set_xy(x,y);
- // LCD_delay_10us(1);
- LCD_write_data(dat);
- }
- // - - 显示一个字符串函数
- void LCD_disp_string(uchar x,uchar y,char *s)
- {
- LCD_set_xy(x,y);
- // LCD_delay_10us(1);
- while(*s!=' ')
- {
- LCD_write_data(*s);
- s++;
- }
- }
- void LCD_init(void)
- {
- LCD_delay_10us(20);
- LCD_write_command(0x38); // - - 设置8位格式,2行,5x7
- LCD_delay_10us(5);
- LCD_write_command(0x0c); // - - 整体显示,关光标,不闪烁
- LCD_delay_10us(5);
- LCD_write_command(0x06); // - - 设定输入方式,增量不移位
- LCD_delay_10us(5);
- LCD_write_command(0x01); // - - 清除屏幕显示
- LCD_delay_10us(200);
- }
- #include"config.h"
- #define IR_DIR_IN P2DIR&=~BIT3//红外接收头P1.0设置为输入,
- #define RED_R (P2IN&BIT3)//红外接收头输入值
- void h1838_init()
- {
- IR_DIR_IN;
- }
- uchar KEY_VAL;
- uchar key_code[4],code_value;
- void Red_Code()
- {
- uchar i,j,k = 0;
- for(i = 0;i < 19;i++)
- {
- delay_us(400); //延时400us
- if(RED_R) //9ms内有高电平,则判断为干扰,退出处理程序
- {
- return;
- }
- }
- while(!RED_R); //等待9ms低电平过去
- for(i=0;i<5;i++) //是否连发码
- {
- delay_us(500);
- if(!RED_R)
- {
- return;
- }
- }
- while(RED_R); //等待4.5ms高电平过去
- for(i = 0;i < 4;i++) //接收4个字节的数据
- {
- for(j = 0;j < 8;j++) //每个字节的数据8位
- {
-
- while(!RED_R); //等待变高电平
- while(RED_R) //计算高电平时间
- {
- delay_us(100);
- k++;
- if(k >22) //高电平时间过长,则退出处理程序
- {
- return;
- }
-
- }
- code_value>>=1; //接受一位数据
- if(k >= 7)
- {
- code_value|=0x80; //高电平时间大于0.56,则为数据1
- }
- k = 0; //计时清零
- }
- key_code[i]=code_value; //四个字节的数据
- }
- // Key_Icode(); //调用赋值函数
- // Display_Key(KEY_VAL); //显示按键值
-
- // delay_ms(50);
- }
复制代码- #include <msp430.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define ulong unsigned long
- #define CLK0 P1OUT&=~BIT6
- #define CLK1 P1OUT|=BIT6
- #define LCD_EN_HIGH P2OUT|=BIT2 //拉高
- #define LCD_EN_LOW P2OUT&=~BIT2 //拉低
- #define LCD_RW_HIGH P2OUT|=BIT1 //拉高
- #define LCD_RW_LOW P2OUT&=~BIT1 //拉低
- #define LCD_RS_HIGH P2OUT|=BIT0 //拉高
- #define LCD_RS_LOW P2OUT&=~BIT0 //拉低
- #define CPU_F ((double)12000000) //外部高频晶振8MHZ
- //#define CPU_F ((double)32768) //外部低频晶振32.768KHZ
- #define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))
- #define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))
- #define KEY_0 0x16 //键0
- #define KEY_1 0x0C //键1
- #define KEY_2 0x18 //键2
- #define KEY_3 0x5E //键3
- #define KEY_4 0x08 //键4
- #define KEY_5 0x1C //键5
- #define KEY_6 0x5A //键6
- #define KEY_7 0x42 //键7
- #define KEY_8 0x52 //键8
- #define KEY_9 0x4A //键9
- #define prex 0x44 //电源
- #define nex 0x40 //模式
- #define play_pause 0x43 //静音
- #define ch_ 0x45 //模式
- #define ch 0x46
- #define cha 0x47 //模式
- #define __ 0x07
- #define _a 0x15
- #define eq 0x09
- #define _100a 0x19
- #define _200a 0x0d
- #define power 0x12 //power
- #define up 0x1b //up
- #define down 0x1a //down
- #define left 0x04 //left
- #define right 0x06 //right
- #define ok 0x05 //0k
复制代码- #include"config.h"
- //连接端口
- #define DHT11_Dir P1DIR
- #define DHT11_In P1IN
- #define DHT11_Out P1OUT
- #define DHT11_Bit BIT0
- char dh11_str[8];
- //8bit湿度+8bit湿度小数点
- //8bit温度+8bit温度小数点
- //数据位为0是高电平26~28us | 1高电平70us
- uchar DHT11_Data[5] = {0}; //从DHT11中读到的数据
- void read_DHT11(uchar *Data)
- {
- uchar re,m,n,Time_out;
- DHT11_Dir &= (~DHT11_Bit); //引脚为输入
- for(n = 0; n < 5; n++)
- {
- for(m = 0; m < 8; m++)
- {
- while( !(DHT11_In & DHT11_Bit) ); //每1bit以50us低电平时限开始,直到高电平到来
- delay_us(30);
- re = 0;
- if((DHT11_In & DHT11_Bit)) //30us后还是高电平说明是1
- {
- re = 1;
- }
- Time_out = 2;
- while((DHT11_In & DHT11_Bit) && (Time_out++ ));
- if(Time_out == 1) //超时处理
- {
- break;
- }
- *Data <<= 1;
- if(re)
- {
- *Data |= 0x01;
- }
- }
- Data++;
- }
- }
- //DHT11的握手协议模拟和数据的读取
- uchar DHT11_GetData(void)
- {
- uchar o,sum;
- DHT11_Dir |= DHT11_Bit;
- DHT11_Out &= ~DHT11_Bit; //拉低,发出开始信号
-
- delay_ms(18); //拉低18ms
- DHT11_Out |= DHT11_Bit; //拉高20us等待
- delay_us(20);
- DHT11_Dir &= (~DHT11_Bit);
- if( !(DHT11_In & DHT11_Bit) ) //DHT11响应
- {
- while( !(DHT11_In & DHT11_Bit) ); //80us低电平,等待DHT11的响应信号
- while( (DHT11_In & DHT11_Bit) ); //DHT11准备发送数据
- read_DHT11(DHT11_Data); //主机接收数据
- sum = 0;
- for(o = 0; o < 4; o++) //数据校验
- {
- sum += DHT11_Data[o];
- }
- if(sum != DHT11_Data[4])
- {
- return 0;
- }
- else
- {
- return 1;
- }
- }
- return 0;
- }
- //对温度湿度进行格式化(如果获取温度成功)
- void DHT11_format(char *Des_str)
- {
- Des_str[0] = DHT11_Data[0]/10 + 0x30;
- Des_str[1] = DHT11_Data[0]%10 + 0x30;
- Des_str[2] = '%';
- Des_str[3] = ' ';
- Des_str[4] = DHT11_Data[2]/10 + 0x30;
- Des_str[5] = DHT11_Data[2]%10 + 0x30;
- Des_str[6] = 'C';
- Des_str[7] = ' ';
- }
- void dh11_read()
- {
- if(DHT11_GetData())
- DHT11_format(dh11_str);
- LCD_disp_string(0,0,dh11_str);
- }
复制代码- #include"config.h"
- #define SDA_DIR_OUT P1DIR|=BIT4
- #define SDA_DIR_IN P1DIR&=~BIT4
- #define SCL_DIR_OUT P1DIR|=BIT5
- #define SDA_IN P1IN&BIT4
- #define SDA1 P1OUT|=BIT4
- #define SDA0 P1OUT&=~BIT4
- #define SCL1 P1OUT|=BIT5
- #define SCL0 P1OUT&=~BIT5
- uchar year,month,date,hour,minute,second;
- uchar const table[]={'0','1','2','3','4','5','6','7','8','9'};
- uchar const month_12[]={31,29,31,30,31,30,31,31,30,31,30,31};
- uchar clockset,clockon,clockoff[5],clock_on;
- uchar oclock[5][2];
- uchar BCD2HEX(uchar val) //BCD转换为Byte
- {
- uchar temp;
- temp=val&0x0f;
- val>>=4;
- val&=0x0f;
- val*=10;
- temp+=val;
-
- return temp;
- }
- uchar HEX2BCD(uchar val) //B码转换为BCD码
- {
- uchar i,j,k;
- i=val/10;
- j=val%10;
- k=j+(i<<4);
- return k;
- }
- void Start_I2C()
- {
- SDA_DIR_OUT;
- SCL_DIR_OUT;
- SDA1; //发送起始条件的数据信号
- delay_us(5);
- SCL1;
- delay_us(5); //起始条件建立时间大于4.7us,延时
-
- SDA0; //发送起始信号
- delay_us(5); // 起始条件锁定时间大于4μs
-
- // SCL0; //钳住I2C总线,准备发送或接收数据
- // delay_us(2);
- }
- void Stop_I2C()
- {
- SDA_DIR_OUT;
- SCL_DIR_OUT;
- SDA0; //发送结束条件的数据信号
- delay_us(5); //发送结束条件的时钟信号
- SCL1; //结束条件建立时间大于4us
- delay_us(5);
-
- SDA1; //发送I2C总线结束信号
- delay_us(1);
- }
- void SendByte(uchar c)
- {
- uchar BitCnt;
- SDA_DIR_OUT;
- SCL_DIR_OUT;
- SCL0;
- delay_us(10);
- for(BitCnt=0;BitCnt<8;BitCnt++) //要传送的数据长度为8位
- {
- if(c&0x80)
- SDA1; //判断发送位
- else
- SDA0;
- delay_us(1);
- SCL0; //置时钟线为高,通知被控器开始接收数据位
- delay_us(1); //保证时钟高电平周期大于4μs
- SCL1;
- delay_us(5);
- c=c<<1;
- SCL0; //置时钟线为高,通知被控器开始接收数据位
- delay_us(5);
- }
- SDA1; //8位发送完后释放数据线,准备接收应答位
- delay_us(1);
- SCL0;
- delay_us(1);
-
- SCL1;
- delay_us(1);
- SCL0;
-
- }
- uchar ReceiveByte(uchar b)
- {
- uchar i;
- uchar temp;
- uchar Dat=0;
- /*
- SDA_H;
- delay(5);
- SCL_L;
- delay(5);
- */
- SDA_DIR_IN;
- for(i=0;i<8;i++)
- {
- SCL1;
- delay_us(5);
- Dat=Dat<<1;
- delay_us(1);
- temp=SDA_IN;
- if(temp&0x10)
- Dat|=0x01;
- else
- Dat|=0x00;
- delay_us(1);
- SCL0;
- delay_us(5);
- }
- SDA_DIR_OUT;
-
- if(b)//每收一个数后发送ACK或nACK
- SDA1;
- else
- SDA0;
-
- delay_us(1);
- SCL1;
- delay_us(1);
- SCL0;
- delay_us(1);
- SDA1; //释放总线
- delay_us(1);
- return Dat;
- }
- /****************************************************/
- /***********将一个字节写入DS3231指定的地址***********/
- void I2cByteWrite(uchar device,uint addr,uchar bytedata)
- {
- Start_I2C();
- // delay_us(1);
- SendByte(device);
- // delay_us(1);
- SendByte(addr);
- // delay_us(1);
- SendByte(bytedata);
- // delay_us(1);
- Stop_I2C();
- }
- /****************************************************/
- /************从DS3231指定的地址读一个字节************/
- uchar I2cByteRead(uchar device,uint addr)
- {
- uchar Dat=0;
-
- Start_I2C();
- SendByte(device);
- // delay_us(1);
- SendByte(addr);
- // delay_us(1);
- Start_I2C();
- SendByte(0xd1);//读数
- // delay_us(1);
- Dat=ReceiveByte(1);//只接收一个字节,所以发nACK
- Stop_I2C();
- return Dat;
- }
- /***************************************************/
- /*********************读取时间**********************/
- void Readtime(void)
- {
- year=I2cByteRead(0xd0,0x06); //年
- month=I2cByteRead(0xd0,0x05); //月
- date=I2cByteRead(0xd0,0x04); //日
- hour=I2cByteRead(0xd0,0x02); //时
- minute=I2cByteRead(0xd0,0x01);//分
- second=I2cByteRead(0xd0,0x00);//秒
- }
- /***************************************************/
- /****************修改时间,BCD码输入*****************/
- void ModifyTime(uchar yea,uchar mon,uchar da,uchar hou,uchar min,uchar sec)
- {
- uchar temp=0;
-
- temp=HEX2BCD(yea);
- I2cByteWrite(0xD0,0x06,temp);//修改年
-
- temp=HEX2BCD(mon);
- I2cByteWrite(0xD0,0x05,temp);//修改月
-
- temp=HEX2BCD(da);
- I2cByteWrite(0xD0,0x04,temp);//修改日
-
- temp=HEX2BCD(hou);
- I2cByteWrite(0xD0,0x02,temp);//修改时
-
- temp=HEX2BCD(min);
- I2cByteWrite(0xD0,0x01,temp);//修改分
-
- temp=HEX2BCD(sec);
- I2cByteWrite(0xD0,0x00,temp);//修改秒
- }
- /****************************************************/
- /****************读取时间 计算 并显示****************/
- void get_show_time(void)
- {
- uchar temp1,temp2;
- char a[2];
- temp1=I2cByteRead(0xd0,0x06); //年
- temp2=BCD2HEX(temp1);
- a[0]=table[temp2/10];
- a[1]=table[temp2%10];
- LCD_disp_string(0,1,a);
- LCD_disp_char(2,1,'-');
- temp1=I2cByteRead(0xd0,0x05); //月
- temp2=BCD2HEX(temp1);
- a[0]=table[temp2/10];
- a[1]=table[temp2%10];
- LCD_disp_string(3,1,a);
- LCD_disp_char(5,1,'-');
- temp1=I2cByteRead(0xd0,0x04); //日
- temp2=BCD2HEX(temp1);
- a[0]=table[temp2/10];
- a[1]=table[temp2%10];
- LCD_disp_string(6,1,a);
- LCD_disp_char(8,1,'-');
- temp1=I2cByteRead(0xd0,0x03); //星期
- temp2=BCD2HEX(temp1);
- temp2=table[temp2];
- LCD_disp_char(9,1,temp2);
- LCD_disp_char(10,1,' ');
- LCD_disp_char(8,0,' ');
- if(clockset==0)
- {
- temp1=I2cByteRead(0xd0,0x02); //时 24小时制
- temp1&=0x3f;
- temp2=BCD2HEX(temp1);
- a[0]=table[temp2/10];
- a[1]=table[temp2%10];
- LCD_disp_string(8,0,a);
- LCD_disp_char(10,0,':');
- temp1=I2cByteRead(0xd0,0x01); //分
- temp2=BCD2HEX(temp1);
- a[0]=table[temp2/10];
- a[1]=table[temp2%10];
- LCD_disp_string(11,0,a);
- LCD_disp_char(13,0,':');
-
- temp1=I2cByteRead(0xd0,0x00); //秒
- temp2=BCD2HEX(temp1);
- a[0]=table[temp2/10];
- a[1]=table[temp2%10];
- LCD_disp_string(14,0,a);
- }
- if(clockset!=0)
- {
- LCD_disp_string(8,0," ");
- a[0]=table[oclock[clockset-1][0]/10];
- a[1]=table[oclock[clockset-1][0]%10];
- LCD_disp_string(8,0,a);
- LCD_disp_char(10,0,'-');
- a[0]=table[oclock[clockset-1][1]/10];
- a[1]=table[oclock[clockset-1][1]%10];
- LCD_disp_string(11,0,a);
- LCD_disp_string(13,0,"CK");
- LCD_disp_char(15,0,table[clockoff[clockset-1]]);
- }
- }
- //显示温度
- void get_show_Temperature(void)
- {
- if(change==0)
- {
- uchar Ttemp1,Ttemp2;
- char a[2];
- Ttemp1=I2cByteRead(0xd0,0x11); //温度 高字节
- Ttemp2=BCD2HEX(Ttemp1);
- // LCD_disp_string(9,1," ");
- // Ttemp3=I2cByteRead(0xd0,0x12); //温度低字节
- // Ttemp4=BCD2HEX(Ttemp3);
- a[0]=table[Ttemp2/10];
- a[1]=table[Ttemp2%10];
- LCD_disp_string(13,1,a);
- LCD_disp_char(15,1,'c');
- }
-
- if(change!=0)
- {
- LCD_disp_string(13,1," ");
- LCD_disp_char(15,1,table[change-1]);
- }
-
- if((clockset!=0)&&(change==0))
- {
- LCD_disp_string(13,1," ");
- LCD_disp_char(13,1,table[clockset]);
- LCD_disp_char(15,1,table[clockon]);
- }
-
- }
- void change_del()
- {
- uchar temp1,temp2,temp3,temp4;
- if(change>0)
- {
- temp1=I2cByteRead(0xd0,change-1);
- temp2=BCD2HEX(temp1);
- temp2-=1;
- if(change<3)
- {
- if(temp2>59)
- temp2=59;
- }
- if(change==3)
- {
- if(temp2>23)
- temp2=23;
- }
-
- if(change==4)
- {
- if(temp2==0)
- temp2=7;
- }
- if(change==5)
- {
- if(temp2==0)
- {
- temp3=I2cByteRead(0xd0,0x05);
- temp4=BCD2HEX(temp3);
- temp2=month_12[temp4-1];
- }
- }
- if(change==6)
- {
- if(temp2==0)
- temp2=12;
- }
-
- temp2=HEX2BCD(temp2);
- I2cByteWrite(0xD0,change-1,temp2);//修改年
- }
- }
- void change_add()
- {
- uchar temp1,temp2,temp3,temp4;
- if(change>0)
- {
- temp1=I2cByteRead(0xd0,change-1);
- temp2=BCD2HEX(temp1);
- temp2+=1;
- if(change<3)
- {
- if(temp2>59)
- temp2=0;
- }
- if(change==3)
- {
- if(temp2>23)
- temp2=0;
- }
-
- if(change==4)
- {
- if(temp2==8)
- temp2=1;
- }
- if(change==5)
- {
- temp3=I2cByteRead(0xd0,0x05);
- temp4=BCD2HEX(temp3);
- if(temp2>month_12[temp4-1])
- temp2=1;
- }
- if(change==6)
- {
- if(temp2==13)
- temp2=1;
- }
-
- temp2=HEX2BCD(temp2);
- I2cByteWrite(0xD0,change-1,temp2);//修改年
- }
- }
- void change_set()
- {
- ++change;
- if(change>=8)
- change=0;
- }
- void clock_set()
- {
- ++clockset;
- if(clockset>=6)
- clockset=0;
- }
- void clock_add()
- {
- change=0;
- ++clockon;
- if( clockon==3)
- clockon=0;
- }
- void clock_change()
- {
- if(clockon==1)
- {
- ++oclock[clockset-1][1];
- if(oclock[clockset-1][1]>59)
- oclock[clockset-1][1]=0;
- }
-
- if(clockon==2)
- {
- ++oclock[clockset-1][0];
- if(oclock[clockset-1][0]>23)
- oclock[clockset-1][0]=0;
- }
- }
- void Key_Icode(uchar dat)
- {
- LCD_disp_char(10,1,' ');
- // LCD_disp_string(10,1," ");
- switch(dat) //第3个字节是数据,第4个字节是反码
- { //为了更稳定,可以加上第4个字节数据的判断
- case KEY_0:KEY_VAL=0; LCD_disp_char(11,1,KEY_VAL+0x30); LCD_disp_char(12,1,' '); break;
- case KEY_1:KEY_VAL=1; LCD_disp_char(11,1,KEY_VAL+0x30); LCD_disp_char(12,1,' '); break;
- case KEY_2:KEY_VAL=2; LCD_disp_char(11,1,KEY_VAL+0x30); LCD_disp_char(12,1,' '); break;
- case KEY_3:KEY_VAL=3; LCD_disp_char(11,1,KEY_VAL+0x30); LCD_disp_char(12,1,' ');break;
- case KEY_4:KEY_VAL=4; LCD_disp_char(11,1,KEY_VAL+0x30); LCD_disp_char(12,1,' ');break;
- case KEY_5:KEY_VAL=5; LCD_disp_char(11,1,KEY_VAL+0x30); LCD_disp_char(12,1,' ');break;
- case KEY_6:KEY_VAL=6; LCD_disp_char(11,1,KEY_VAL+0x30); LCD_disp_char(12,1,' ');break;
- case KEY_7:KEY_VAL=7; LCD_disp_char(11,1,KEY_VAL+0x30); LCD_disp_char(12,1,' ');break;
- case KEY_8:KEY_VAL=8; LCD_disp_char(11,1,KEY_VAL+0x30); LCD_disp_char(12,1,' ');break;
- case KEY_9:KEY_VAL=9; LCD_disp_char(11,1,KEY_VAL+0x30); LCD_disp_char(12,1,' ');break;
- case prex:LCD_disp_string(11,1,"PR");clock_change();break;
- case nex :LCD_disp_string(11,1,"NX");clock_add();break;
- case play_pause:LCD_disp_string(11,1,"PS");clock_set();break;
- case ch_: LCD_disp_string(11,1,"C-");change_del();break;
- case ch: LCD_disp_string(11,1,"CH");change_set();break;
- case cha:LCD_disp_string(11,1,"C+");change_add();break;
- case eq: LCD_disp_string(11,1,"EQ");
- if(clockoff[clockset-1]==0)
- clockoff[clockset-1]=1;
- else clockoff[clockset-1]=0;
- break;
- case _a:LCD_disp_string(11,1,"V+");break;
- case __:LCD_disp_string(11,1,"V-");break;
- case _100a:LCD_disp_string(11,1,"1+");break;
- case _200a:LCD_disp_string(11,1,"2+");break;
-
- case power:LCD_disp_string(11,1,"PW");break;
- case up: LCD_disp_string(11,1,"UP");break;
- case down:LCD_disp_string(11,1,"DW");break;
- case left:LCD_disp_string(11,1,"LF");break;
- case right:LCD_disp_string(11,1,"RT");break;
- case ok:LCD_disp_string(11,1,"OK");break;
- default:KEY_VAL=' ';break;
- }
- key_code[2]=' ';
-
- }
-
- void oclockon()
- {
- uchar clock_flag[5];
- uchar temp1,temp2,temp3;
- temp1=I2cByteRead(0xd0,0x02); //时 24小时制
- temp1&=0x3f;
- temp2=BCD2HEX(temp1);
-
- temp1=I2cByteRead(0xd0,0x01); //分
- temp3=BCD2HEX(temp1);
- uchar i;
- for(i=0;i<5;i++)
- {
- if(temp2==oclock[i][0])
- {
- if(temp3==oclock[i][1])
- {
- if(clockoff[i]==1)
- clock_flag[i]=1;
- else clock_flag[i]=0;
- }
- else clock_flag[i]=0;
- }
- else clock_flag[i]=0;
- }
- clock_on=0;
- for(i=0;i<5;i++)
- {
- if(clock_flag[i]==1)
- clock_on=1;
- }
- }
复制代码- #include"config.h"
- uchar t;
- /***********TA初始化*********/
- void init_TA()
- {
- TACCTL0 = CCIE; // TACCR0 interrupt enabled
- TACCR0 = 60000; //5ms中断一次
- TACTL = TASSEL_2 + MC_1; // SMCLK, upmode
- }
- #pragma vector=TIMER0_A0_VECTOR
- __interrupt void Timer_A (void)
- {
- if(++t==200)
- {
- t=0;
- dh11_read();
- get_show_Temperature();
- get_show_time();
- oclockon();
- }
- if(clock_on==1)
- P1OUT ^= 0x08;
- }
复制代码
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
- /***************************************************************************/
- /* DEMO3231.C */
- /***************************************************************************/
- #include <stdio.h> /* Prototypes for I/O functions */
- #include <DS5000.h> /* Register declarations for DS5000 */
- /************************* bit definitions ****************************/
- sbit scl = P0^0; /* I2C pin definitions */
- sbit sda = P0^1;
- sbit E = P1^0; /* DCM LCD module control signal definitions */
- sbit RS = P1^1;
- sbit RW = P1^2;
- sbit CLK = P2^5; /* DS1267 control signal definitions */
- sbit RSTb = P2^6;
- sbit DQ = P2^7;
- sbit int0 = P3^2;
- /**************************** defines *******************************/
- #define ADDRTC 0xd0 /* DS3231 slave address (write) */
- #define ACK 0
- #define NACK 1
- /*********************** Function Prototypes **************************/
- void start();
- void stop();
- uchar i2cwrite(uchar d);
- uchar i2cread(char);
- void wr_dsp_dat(uchar);
- void wr_dsp_ins(uchar);
- uchar rd_dsp_ins();
- void hex2asc(uchar);
- void dsp_adj(uchar pos);
- void init_dsp();
- void writebyte();
- void initialize_DS3231();
- void disp_regs();
- void rd_temp();
- void frq_out_tog();
- void init_alrm();
- void comm_init();
- /************************* Global Variables ***************************/
- xdata uchar sec, min, hr, dy, dt, mn, yr;
- /**************************** functions ******************************/
- void start() /* --------- Initiate start condition ---------- */
- {
- sda = 1; scl = 1;
- sda = 0;
- }
- void stop() /* ---------- Initiate stop condition ----------- */
- {
- sda = 0; sda = 0;
- scl = 1; scl = 1; sda = 1;
- }
- uchar i2cwrite(uchar d) /* ----------------------------- */
- {
- uchar i;
- scl = 0;
- for (i = 0;i < 8; i++)
- {
- if (d & 0x80)
- sda = 1; /* Send the msbits first */
- else
- sda = 0;
- scl = 0;
- scl = 1;
- d = d << 1; /* do shift here to increase scl high time */
- scl = 0;
- }
- sda = 1; /* Release the sda line */
- scl = 0;
- scl = 1;
- i = sda;
- if (i) printf("Ack bit missing %02X
",(unsigned int)d);
- scl = 0;
- return(i);
- }
- uchar i2cread(char b) /* ----------------------------------- */
- {
- uchar i, d;
- d = 0;
- sda = 1; /* Let go of sda line */
- scl = 0;
- for (i = 0; i < 8; i++) /* read the msb first */
- {
- scl = 1;
- d = d << 1;
- d = d | (unsigned char)sda;
- scl = 0;
- }
- sda = b; /* low for ack, high for nack */
- scl = 1;
- scl = 0;
- sda = 1; /* Release the sda line */
- return d;
- }
- void wr_dsp_dat(uchar dat) /* -------- write one byte to the display --------- */
- {
- P0 = dat;
- RS = 1; /* data register */
- RW = 0; /* write */
- E = 1;
- E = 0; /* latch data in */
- }
- void wr_dsp_ins(uchar dat) /* ---- write one byte to the display instruction ----- */
- {
- P0 = dat;
- RS = 0; /* instruction register */
- RW = 0; /* write */
- E = 1;
- E = 0; /* latch data in */
- }
- uchar rd_dsp_ins() /* ---- read one byte from the instruction registers ----- */
- {
- uchar dat;
- P0 = 0xff; /* set up for read */
- RS = 0; /* instruction register */
- RW = 1; /* read */
- E = 1;
- dat = P0;
- E = 0;
- return(dat);
- }
- void init_dsp() /* -------- initialize DMC-16207 LCD display ------- */
- {
- while((rd_dsp_ins() & 0x80)); /* wait for display */
- wr_dsp_ins(0x38); /* Set 8-bit data, 2-line display, 5X7 font */
- while((rd_dsp_ins() & 0x80));
- wr_dsp_ins(0x0c); /* Display on, cursor off, blink off */
- while((rd_dsp_ins() & 0x80));
- wr_dsp_ins(0x06); /* Entry mode set: increment, no display shift */
- while((rd_dsp_ins() & 0x80));
- wr_dsp_ins(0x01); /* clear display */
- while((rd_dsp_ins() & 0x80));
- wr_dsp_ins(0x80); /* move to start of line 1 */
- while((rd_dsp_ins() & 0x80));
- wr_dsp_ins(0x50); /* Set CG RAM address */
- while((rd_dsp_ins() & 0x80));
- wr_dsp_dat(0x07); /* write 1st line of custom char to CG RAM */
- while((rd_dsp_ins() & 0x80));
- wr_dsp_dat(0x05); /* write 2nd line */
- while((rd_dsp_ins() & 0x80));
- wr_dsp_dat(0x07); /* write 3rd line */
- while((rd_dsp_ins() & 0x80));
- wr_dsp_dat(0x00); /* write 4th line */
- while((rd_dsp_ins() & 0x80));
- wr_dsp_dat(0x00); /* write 5th line */
- while((rd_dsp_ins() & 0x80));
- wr_dsp_dat(0x00); /* write 6th line */
- while((rd_dsp_ins() & 0x80));
- wr_dsp_dat(0x00); /* write 7th line */
- while((rd_dsp_ins() & 0x80));
- }
- void hex2asc(uchar hex) /* -- convert the upper and lower nibbles to 2 ascii characters -- */
- {
- if( ( (hex & 0xf0) >> 4) < 0x0a)
- wr_dsp_dat( ( (hex & 0xf0) >> 4) + 48 ); /* prints 0-9 */
- else
- wr_dsp_dat( ( (hex & 0xf0) >> 4) + 55 ); /* prints A-F */
- while((rd_dsp_ins() & 0x80));
- if( (hex & 0x0f) < 0x0a)
- wr_dsp_dat( (hex & 0x0f) + 48 );
- else
- wr_dsp_dat( (hex & 0x0f) + 55 );
- while((rd_dsp_ins() & 0x80));
- }
- void dsp_adj(uchar pos) /* ------ adjust contrast on LCD display ------- */
- {
- char inc;
- RSTb = CLK = 0; /* initialize for 1st pass */
- RSTb = 1; /* enable DS1267 */
- DQ = 0; /* write stack select bit */
- CLK = 1; /* toggle clk with data valid */
- CLK = 0;
- for (inc = 7; inc >= 0; inc--) /* write wiper 0 */
- {
- DQ = ((pos >> inc) & 0x01); /* shift x bits left */
- CLK = 0; /* toggle clk with data valid */
- CLK = 1;
- }
- for (inc = 7; inc >= 0; inc--) /* write wiper 1 */
- {
- DQ = ((pos >> inc) & 0x01); /* shift x bits left */
- CLK = 0; /* toggle clk with data valid */
- CLK = 1;
- }
- RSTb = 0; /* reset the 1267 (/RST & CLK low) */
- CLK = 0;
- }
- void writebyte() /* ----------------------------------------------- */
- {
- uchar Add, Data;
- printf("
ADDRESS (hex):"); /* Get Address & Data */
- scanf("%bx", &Add);
- printf("DATA (hex):");
- scanf("%bx", &Data);
- start();
- i2cwrite(ADDRTC);
- i2cwrite(Add);
- i2cwrite(Data);
- stop();
- }
- void initialize_DS3231() /* ----- set time & date; user data entry ------ */
- /* Note: NO error checking is done on the user entries! */
- {
- printf("
Enter the year (0-99): ");
- scanf("%bx", &yr);
- printf("Enter the month (1-12): ");
- scanf("%bx", &mn);
- printf("Enter the date (1-31): ");
- scanf("%bx", &dt);
- printf("Enter the day (1-7): ");
- scanf("%bx", &dy);
- printf("Enter the hour (1-23): ");
- scanf("%bx", &hr);
- /* hr = hr & 0x3f; /* force clock to 24 hour mode */
- printf("Enter the minute (0-59): ");
- scanf("%bx", &min);
- printf("Enter the second (0-59): ");
- scanf("%bx", &sec);
- start();
- i2cwrite(ADDRTC); /* write slave address, write 1339 */
- i2cwrite(0x00); /* write register address, 1st clock register */
- i2cwrite(sec);
- i2cwrite(min);
- i2cwrite(hr);
- i2cwrite(dy);
- i2cwrite(dt);
- i2cwrite(mn);
- i2cwrite(yr);
- i2cwrite(0x10); /* enable sqw, 1hz output */
- stop();
- }
- void disp_regs() /* --- display date/time on LCD display --- */
- {
- uchar age, prv_sec=99;
- while(!RI) /* Read & Display Clock Registers */
- {
- while(int0); /* loop until int pin goes low */
- start();
- i2cwrite(ADDRTC);
- i2cwrite(0x0f);
- i2cwrite(0); /* clear alarm flags */
- stop();
- start();
- i2cwrite(ADDRTC);
- i2cwrite(0);
- start();
- i2cwrite(ADDRTC | 1);
- sec = i2cread(ACK);
- min = i2cread(ACK);
- hr = i2cread(ACK);
- dy = i2cread(ACK);
- dt = i2cread(ACK);
- mn = i2cread(ACK);
- yr = i2cread(NACK);
- stop();
- wr_dsp_ins(0x80); /* move to start of line 1 */
- while((rd_dsp_ins() & 0x80));
- hex2asc(yr);
- wr_dsp_dat('/');
- while((rd_dsp_ins() & 0x80));
- hex2asc(mn);
- wr_dsp_dat('/');
- while((rd_dsp_ins() & 0x80));
- hex2asc(dt);
- wr_dsp_dat(' ');
- while((rd_dsp_ins() & 0x80));
- hex2asc(dy);
- wr_dsp_dat(' ');
- while((rd_dsp_ins() & 0x80));
- start();
- i2cwrite(ADDRTC);
- i2cwrite(0x10);
- start();
- i2cwrite(ADDRTC | 1);
- age = i2cread(NACK);
- stop();
- wr_dsp_dat(' ');
- while((rd_dsp_ins() & 0x80));
- hex2asc(age);
- wr_dsp_ins(0xc0); /* move to start of line 2 */
- while((rd_dsp_ins() & 0x80));
- hex2asc(hr);
- wr_dsp_dat(':');
- while((rd_dsp_ins() & 0x80));
- hex2asc(min);
- wr_dsp_dat(':');
- while((rd_dsp_ins() & 0x80));
- hex2asc(sec);
- rd_temp();
- }
- RI = 0; /* Swallow keypress to exit loop */
- }
- void rd_temp() /* -------- display temperature -------- */
- {
- char str[8];
- int itemp;
- float ftemp;
- do
- {
- start();
- i2cwrite(ADDRTC);
- i2cwrite(0x0e); /* address of control register */
- start();
- i2cwrite(ADDRTC + 1); /* send the device address for read */
- itemp = i2cread(NACK); /* get the control register value */
- stop();
- } while(itemp & 0x20); /* wait until CNVT bit goes inactive */
- start();
- i2cwrite(ADDRTC);
- i2cwrite(0x11); /* address of temperature MSB */
- start();
- i2cwrite(ADDRTC + 1); /* send the device address for read */
- itemp = ( (int) i2cread(ACK) << 5 );
- itemp += ( i2cread(NACK) >> 3);
- stop();
- if(itemp & 0x1000) itemp += 0xe000; /* if sign bit set, make 16 bit 2's comp */
- ftemp = 0.03125 * (float) itemp; /* convert to degrees C */
- /* ftemp = ftemp * 9 / 5 + 32; /* skip this if you don't want degrees F */
- sprintf(str, "%5.2f", ftemp);
- wr_dsp_ins(0xc9); /* go to line 2, column 10 */
- while((rd_dsp_ins() & 0x80));
- wr_dsp_dat(str[0]);
- while((rd_dsp_ins() & 0x80));
- wr_dsp_dat(str[1]);
- while((rd_dsp_ins() & 0x80));
- wr_dsp_dat(str[2]);
- while((rd_dsp_ins() & 0x80));
- wr_dsp_dat(str[3]);
- while((rd_dsp_ins() & 0x80));
- wr_dsp_dat(0x02); /* display programed 3rd char in CG RAM */
- while((rd_dsp_ins() & 0x80));
- }
- void frq_out_tog() /* --- toggle en32khz bit to enable/disable sqw --- */
- {
- uchar val;
- start();
- i2cwrite(ADDRTC);
- i2cwrite(0x0f); /* control/status reg address */
- start();
- i2cwrite(ADDRTC + 1); /* send the device address for read */
- val = i2cread(NACK);
- stop();
- val ^= 0x08; /* toggle en32khz bit */
- start();
- i2cwrite(ADDRTC);
- i2cwrite(0x0f); /* control/status reg address */
- i2cwrite(val);
- stop();
- }
- void init_alrm() /* --- enable alarm 1 for once-per-second --- */
- {
- start();
- i2cwrite(ADDRTC);
- i2cwrite(7); /* 1st alarm 1 reg address */
- i2cwrite(0x80); /* mask alarm register */
- i2cwrite(0x80);
- i2cwrite(0x80);
- i2cwrite(0x80);
- stop();
- start();
- i2cwrite(ADDRTC);
- i2cwrite(0x0e); /* control/status reg address */
- i2cwrite(0x05); /* enable interrupts, alarm 1 output */
- }
- void comm_init() /* ------ reset DS3231 comm interface ------ */
- {
- do /* because the DS3231 I2C interface is active for both supplies */
- { /* after a micro reset, we must get the comm into a known state */
- sda = 1; /* make sure master has released SDA */
- scl = 1;
- if(sda) /* if sda is high, generate a start */
- {
- sda = 0; /* The DS3231 will recognize a valid start */
- sda = 1; /* condition anywhere in a I2C data transfer */
- }
- scl = 0;
- } while(sda == 0); /* if the DS3231 is holding sda low, try again */
- }
- void main (void) /* ------------------------------------------------ */
- {
- uchar i, M, M1;
- dsp_adj(0x10); /* adjust contrast on LCD display */
- init_dsp(); /* initialize LCD display */
- comm_init();
- init_alrm(); /* enable alarm */
- disp_regs();
- while(1)
- {
- printf("
DS3231 build date: %s
", __DATE__);
- printf("I Init DS3231 S Show temp
");
- printf("R Read Time W Write Byte
");
- printf("F Frq out D Display adj
");
- printf("Enter Menu Selection: ");
- M = _getkey();
- switch(M)
- {
- case 'D':
- case 'd': printf("val: "); scanf("%bx", &M1);
- dsp_adj(M1); break;
- case 'F':
- case 'f': frq_out_tog(); break;
- case 'I':
- case 'i': initialize_DS3231(); break;
- case 'R':
- case 'r': disp_regs(); break;
- case 'S':
- case 's': rd_temp(); break;
- case 'W':
- case 'w': writebyte(); break;
- }
- }
复制代码一周热门 更多>