STC89C52单片机,用C语言编温度报警器的程序

kuaidi.ping-jia.net  作者:佚名   更新日期:2024-07-04
我需要stc89c52单片机ds18b20温度控制的c语言程序,要求可显示当前温度,并且在达到温度上限时会产生报警

我这里刚好有,不过是用的串口现实的,能正确测试温度
********************************************************/
#include
#include
sbit DQ=P1^0;
void usart_init();
void send_str(char *read_data);
void delayms(unsigned char ms);
void send_char(unsigned char txd);
unsigned char readbata[8];
unsigned char readTem[4];
/*******************************************************/
/*****经过测试:timer=6*i+11 *****/
/*******************************************************/
void delay(unsigned char i) //ds18b20的延时程序
{
while(i--);
}

unsigned char resetds() //DS18B20初始化
{
unsigned char i=0;
unsigned char flag=0;
DQ=0; //拉低总线
delay(81); //精确延时 497us
DQ=1; //释放总线
delay(20);
flag=DQ;
delay(60); //
return flag;
}

unsigned char re_one_byte()//读取一个字节的数据?
{
unsigned char i=0;
unsigned char dat=0;
for(i=8;i>0;i--)
{
dat>>=1;//让从总线上读到的位数据,依次从高位移动到低位。
DQ=0;//总线拉低
_nop_();
_nop_();
DQ=1; //释放总线,此后DS18B20会控制总线,把数据传输到总线上
delay(0);
if(DQ)//控制器进行采样
{
dat|=0x80;//若总线为1,即DQ为1,就把dat的最高位置1;若为0,则不进行处理,保持为0
}
delay(10);//此延时不能少,确保读时序的长度60us。
DQ=1; //释放总线
delay(1);
}
return(dat);
}
void wr_one_byte(unsigned char dat)
{
unsigned char i=0;
for(i=8;i>0;i--)
{
DQ=0; //拉低总线
_nop_();
_nop_(); //2us
DQ=dat&0x01;//从字节的最低位开始传输
//指令dat的最低位赋予给总线,必须在拉低总线后的15us内,
//因为15us后DS18B20会对总线采样。
delay(10);//必须让写时序持续至少60us 65us
DQ=1;//释放总线
dat>>=1;
delay(1);
}
}

void read_rom()
{
unsigned char i=0;
while(resetds());
wr_one_byte(0x33);
for(i=0;i<8;i++)
{
readbata[i]=re_one_byte();
}
}

void read_temp()/*璇诲彇娓╁害鍊煎苟杞崲*/
{
unsigned char a1,b1;
unsigned int avalue;
resetds();
wr_one_byte(0xcc);
wr_one_byte(0x44);
delay(1);
resetds();
wr_one_byte(0xcc);//*璺宠绷璇诲簭鍒楀佛*/
wr_one_byte(0xbe);//*璇诲彇娓╁害*/
a1=re_one_byte();
//send_char(a1);
b1=re_one_byte();
//send_char(b1);
avalue=(b1<<8);
avalue+=a1;
avalue=avalue*0.0625*10; //保留一位小数
readTem[0]=avalue/1000; //百位
readTem[1]=avalue%1000/100;//十位
readTem[2]=avalue%100/10;//各位
readTem[3]=avalue%10;//小数位

}
void main()
{
unsigned char i=0;
delay(9);
while(resetds());
usart_init();
while(1)
{
//read_rom();
//for(i=0;i<8;i++)
//send_char(readbata[i]);
// for(i=0;i<6;i++)
// send_char(readbata1[i]);
read_temp();
for(i=0;i<4;i++)
send_char(readTem[i]);
delayms(1000);
}
}

#include //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义
#include
#include

#define uchar unsigned char
#define uint unsigned int;
/******************************************************************/
/* 定义端口 */
/******************************************************************/
sbit seg1=P2^0;
sbit seg2=P2^1;
sbit seg3=P2^2;
sbit DQ=P1^3;//ds18b20 端口
sfr dataled=0x80;//显示数据端口
/******************************************************************/
/* 全局变量 */
/******************************************************************/
uint temp;
uchar flag_get,count,num,minute,second;
uchar code tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
//7段数码管段码表共阳
uchar str[6];
/******************************************************************/
/* 函数声明 */
/******************************************************************/
void delay1(uchar MS);
unsigned int ReadTemperature(void);
void Init_DS18B20(void);
unsigned char ReadOneChar(void);
void WriteOneChar(unsigned char dat);
void delay(unsigned int i);
/******************************************************************/
/* 主函数 */
/******************************************************************/
main()
{
unsigned char TempH,TempL;
TMOD|=0x01;//定时器设置
TH0=0xef;
TL0=0xf0;
IE=0x82;
TR0=1;
P2=0x00;
count=0;

while(1)
{
str[5]=0x39; //显示C符号
str[1]=tab[TempH/100]; //十位温度
str[2]=tab[(TempH%100)/10]; //十位温度
str[3]=tab[(TempH%100)%10]|0x80; //个位温度,带小数点
str[4]=tab[TempL];
if(flag_get==1) //定时读取当前温度
{
temp=ReadTemperature();
if(temp&0x8000)
{
str[0]=0x40;//负号标志
temp=~temp; // 取反加1
temp +=1;
}
else
str[0]=0;
TempH=temp>>4;
TempL=temp&0x0F;
TempL=TempL*6/10;//小数近似处理
flag_get=0;
}
}
}


/******************************************************************/
/* 定时器中断 */
/******************************************************************/
void tim(void) interrupt 1 using 1//中断,用于数码管扫描和温度检测间隔
{
TH0=0xef;//定时器重装值
TL0=0xf0;
num++;
if (num==50)
{num=0;
flag_get=1;//标志位有效
second++;
if(second>=60)
{second=0;
minute++;
}
}
count++;
if(count==1)
{P2=0;
dataled=str[0];}//数码管扫描
if(count==2)
{P2=1;
dataled=str[1];}
if(count==3)
{ P2=2;
dataled=str[2];
}
if(count==4)
{ P2=3;
dataled=str[3];
}
if(count==5)
{ P2=4;
dataled=str[4];
}
if(count==6)
{ P2=5;
dataled=str[5];
count=0;}
}
/******************************************************************/
/* 延时函数 */
/******************************************************************/
void delay(unsigned int i)//延时函数
{
while(i--);
}
/******************************************************************/
/* 初始化 */
/******************************************************************/
void Init_DS18B20(void)
{
unsigned char x=0;
DQ = 1; //DQ复位
delay(8); //稍做延时
DQ = 0; //单片机将DQ拉低
delay(80); //精确延时 大于 480us
DQ = 1; //拉高总线
delay(10);
x=DQ; //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
delay(5);
}

/******************************************************************/
/* 读一个字节 */
/******************************************************************/
unsigned char ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ = 0; // 给脉冲信号
dat>>=1;
DQ = 1; // 给脉冲信号
if(DQ)
dat|=0x80;
delay(5);
}
return(dat);
}

/******************************************************************/
/* 写一个字节 */
/******************************************************************/
void WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
delay(5);
DQ = 1;
dat>>=1;
}
delay(5);
}

/******************************************************************/
/* 读取温度 */
/******************************************************************/
unsigned int ReadTemperature(void)
{
unsigned char a=0;
unsigned int b=0;
unsigned int t=0;
Init_DS18B20();
WriteOneChar(0xCC); // 跳过读序号列号的操作
WriteOneChar(0x44); // 启动温度转换
delay(200);
Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
a=ReadOneChar(); //低位
b=ReadOneChar(); //高位

b<<=8;
t=a+b;

return(t);
}

温度报警就用外部中断不就行了?和18b20连接
万年历就是利用定时器就可以了吧。在加一块液晶屏用来显示。
具体的应该网上都有你可以搜一下。

我帮您搜了一个万年历的程序
一、 阳历算法
具体算法见函数void get_solar_day_date(void),这样阳历日历的星期排法就确定了。
表1:
变量定义:
Public:
Unsigned int temp_total_day;
Unsigned char gc_solar_calendar_year;
Unsigned char gc_solar_calendar_month;
Unsigned char gc_solar_calendar_date;
Unsigned char gc_lunar_calendar_year;
Unsigned char gc_lunar_calendar_month;
Unsigned char gc_lunar_calendar_date;
Unsigned char start_day_of_week;
说明:函数get_solar_day_date(void)的输入变量:gc_solar_calendar_year和 gc_solar_calendar_month
输出变量:start_day_of_week和temp_total_day
Void get_solar_day_date(void)
{
unsigned char temp01;
/*------calculate what day is the day of the current month and year. Mon~Sun?---*/
/*条件初始化二次,减少运算数据量. temp_total_day 是int型变量*/
start_day_of_week = 2; temp_total_day = 0;calculate_temp = 1;
if(gc_solar_calendar_year > 99)
{start_day_of_week = 6;calculate_temp = 100;}
for(temp01 = calculate_temp; temp01<gc_solar_calendar_year; temp01++)
{ if(temp01%4 == 0){start_day_of_week +=2;temp_total_day += 366; }
else {start_day_of_week +=1;temp_total_day += 365;}}
for(temp01 = 1;temp01<gc_solar_calendar_month;temp01++)
{ switch(temp01)
{case 1,3,5,7,8,10,12: start_day_of_week +=3;temp_total_day +=31;break;
case 2: if(((gc_solar_calendar_year%4) == 0)&&(gc_solar_calendar_year != 200))
{start_day_of_week +=1; temp_total_day +=29;}
else {start_day_of_week +=0;temp_total_day +=28;} break;
case 4,6,9,11: start_day_of_week +=2; temp_total_day +=30; break;}}
start_day_of_week %=7;
/*-end of calculate what day is the day(Mon~Sun?) and total day --*/
}

二、 阴历算法
200年需要200 × 2 = 400个字节,构成阴历压缩数据表lunar_calendar_month_table[]如下
const char lunar_calendar_month_table[]={ //从阴历年1900年到2100年
/*the total day of each month pointer */
月份 1 2 3 4 5 6 7 8 9 10 11 12
闰年 31 29 31 30 31 30 31 31 30 31 30 31
非闰年 31 28 31 30 31 30 31 31 30 31 30 31

/* from 1901~2100*/
/* (0110)110000001001 (0110)leap month,110000001001
lunar month total day: 1:29 0:30*/
0x00,0x04,0xad,0x08,0x5a,0x01,0xd5,0x54,0xb4,0x09,0x64,0x05,0x59,0x45,
0x95,0x0a,0xa6,0x04,0x55,0x24,0xad,0x08,0x5a,0x62,0xda,0x04,0xb4,0x05,
0xb4,0x55,0x52,0x0d,0x94,0x0a,0x4a,0x2a,0x56,0x02,0x6d,0x71,0x6d,0x01,
0xda,0x02,0xd2,0x52,0xa9,0x05,0x49,0x0d,0x2a,0x45,0x2b,0x09,0x56,0x01,
0xb5,0x20,0x6d,0x01,0x59,0x69,0xd4,0x0a,0xa8,0x05,0xa9,0x56,0xa5,0x04,
0x2b,0x09,0x9e,0x38,0xb6,0x08,0xec,0x74,0x6c,0x05,0xd4,0x0a,0xe4,0x6a,
0x52,0x05,0x95,0x0a,0x5a,0x42,0x5b,0x04,0xb6,0x04,0xb4,0x22,0x6a,0x05,
0x52,0x75,0xc9,0x0a,0x52,0x05,0x35,0x55,0x4d,0x0a,0x5a,0x02,0x5d,0x31,
0xb5,0x02,0x6a,0x8a,0x68,0x05,0xa9,0x0a,0x8a,0x6a,0x2a,0x05,0x2d,0x09,
0xaa,0x48,0x5a,0x01,0xb5,0x09,0xb0,0x39,0x64,0x05,0x25,0x75,0x95,0x0a,
0x96,0x04,0x4d,0x54,0xad,0x04,0xda,0x04,0xd4,0x44,0xb4,0x05,0x54,0x85,
0x52,0x0d,0x92,0x0a,0x56,0x6a,0x56,0x02,0x6d,0x02,0x6a,0x41,0xda,0x02,
0xb2,0xa1,0xa9,0x05,0x49,0x0d,0x0a,0x6d,0x2a,0x09,0x56,0x01,0xad,0x50,
0x6d,0x01,0xd9,0x02,0xd1,0x3a,0xa8,0x05,0x29,0x85,0xa5,0x0c,0x2a,0x09,
0x96,0x54,0xb6,0x08,0x6c,0x09,0x64,0x45,0xd4,0x0a,0xa4,0x05,0x51,0x25,
0x95,0x0a,0x2a,0x72,0x5b,0x04,0xb6,0x04,0xac,0x52,0x6a,0x05,0xd2,0x0a,
0xa2,0x4a,0x4a,0x05,0x55,0x94,0x2d,0x0a,0x5a,0x02,0x75,0x61,0xb5,0x02,
0x6a,0x03,0x61,0x45,0xa9,0x0a,0x4a,0x05,0x25,0x25,0x2d,0x09,0x9a,0x68,
0xda,0x08,0xb4,0x09,0xa8,0x59,0x54,0x03,0xa5,0x0a,0x91,0x3a,0x96,0x04,
0xad,0xb0,0xad,0x04,0xda,0x04,0xf4,0x62,0xb4,0x05,0x54,0x0b,0x44,0x5d,
0x52,0x0a,0x95,0x04,0x55,0x22,0x6d,0x02,0x5a,0x71,0xda,0x02,0xaa,0x05,
0xb2,0x55,0x49,0x0b,0x4a,0x0a,0x2d,0x39,0x36,0x01,0x6d,0x80,0x6d,0x01,
0xd9,0x02,0xe9,0x6a,0xa8,0x05,0x29,0x0b,0x9a,0x4c,0xaa,0x08,0xb6,0x08,
0xb4,0x38,0x6c,0x09,0x54,0x75,0xd4,0x0a,0xa4,0x05,0x45,0x55,0x95,0x0a,
0x9a,0x04,0x55,0x44,0xb5,0x04,0x6a,0x82,0x6a,0x05,0xd2,0x0a,0x92,0x6a,
0x4a,0x05,0x55,0x0a,0x2a,0x4a,0x5a,0x02,0xb5,0x02,0xb2,0x31,0x69,0x03,
0x31,0x73,0xa9,0x0a,0x4a,0x05,0x2d,0x55,0x2d,0x09,0x5a,0x01,0xd5,0x48,
0xb4,0x09,0x68,0x89,0x54,0x0b,0xa4,0x0a,0xa5,0x6a,0x95,0x04,0xad,0x08,
0x6a,0x44,0xda,0x04,0x74,0x05,0xb0,0x25,0x54,0x03,};
确定阳历日和阴历日的对应关系的算法:
对于其他任何一个阳历日和阴历日的对应关系,都可以通过以下算法求得结果。具体算法由如get_lunar_day(void)实现:
说明:函数get_lunar_day(void)的输入变量:gc_solar_calendar_year和gc_solar_calendar
输出变量:gc_lunar_calendar_year、gc_lunar_calendar_month和 gc_lunar_calendar_date
void get_lunar_day(void)/*计算出输入阳历年、阳历月,对应该阳历月第一天对应阴历时间年、月、日*/
{unsigned char temp_leap_month;
unsigned char temp_flag;
unsigned char calculate_temp;
unsigned char mc_tpumenus_temp_loop;
unsigned char mc_tpumenus_temp_01;
temp_leap_month = 0;temp_flag = 1;
//条件初始化二次,减少运算数据量.
if(gc_solar_calendar_year > 99)
{gc_lunar_calendar_year = 99;gc_lunar_calendar_month = 11;
gc_lunar_calendar_date = 25;temp_total_day += 25;calculate_temp = 100;}
else
{gc_lunar_calendar_year = 0;gc_lunar_calendar_month = 11;
gc_lunar_calendar_date = 11;temp_total_day += 11;calculate_temp = 1;}
if(gc_solar_calendar_year >calculate_temp||gc_solar_calendar_month>1)
{ for(mc_tpumenus_temp_loop = 1;mc_tpumenus_temp_loop>0;){
temp_total_day -=calendar_calculate_lunar_month_total_day();
temp_leap_month = tpumenus_lunar_calendar_month_table[2*gc_lunar_calendar_year +
temp_leap_month = (temp_leap_month>>4)&0x0F;
if(gc_lunar_calendar_month == temp_leap_month)
{switch(gc_lunar_calendar_year)
{case 6,14,19,25,33,36,38,41,44,52,55,79,117,
136,147,150,155,158,185,193:
if(temp_total_day<31){gc_lunar_calendar_date = temp_total_day;
mc_tpumenus_temp_loop = 0;temp_flag = 0;}
else temp_total_day -= 30;
break; //current month:temp_leap_month
default:
if(temp_total_day < 30)
{gc_lunar_calendar_date = temp_total_day;mc_tpumenus_temp_loop = 0;
temp_flag = 0; /*current month:temp_leap_month*/ }
else temp_total_day -= 29; break;}}
if(temp_flag){gc_lunar_calendar_month++;
if(gc_lunar_calendar_month == 13){gc_lunar_calendar_month = 1;
gc_lunar_calendar_year++;}
If(temp_total_day < 61) //if temp_total_day>60,ignore compare
{mc_tpumenus_temp_01 = calendar_calculate_lunar_month_total_day();
if(temp_total_day < (mc_tpumenus_temp_01 + 1))
{mc_tpumenus_temp_loop = 0;gc_lunar_calendar_date = temp_total_day;
} }
} } }
gc_lunar_leap_month = (temp_flag<<4)|temp_leap_month;/*set leap_month flag*/
说明:函数calendar_calculate_lunar_month_total_day(void)根据输入变量gc_lunar_calen和 gc_lunar_calendar_month,结合压缩数据表lunar_calendar_month_table计算出对应阴历天数。
unsigned char calendar_calculate_lunar_month_total_day(void)
{
unsigned char mc_tpumenus_temp_01;
unsigned char mc_tpumenus_temp_02;
if(gc_lunar_calendar_month < 9)
{mc_tpumenus_temp_01 = lunar_calendar_month_table[2*gc_lunar_calendar_year];
mc_tpumenus_temp_02 = gc_lunar_calendar_month - 1;}
else{
mc_tpumenus_temp_01 = lunar_calendar_month_table[2*gc_lunar_calendar_year + 1];
mc_tpumenus_temp_02 = gc_lunar_calendar_month - 9;}
if((mc_tpumenus_temp_01>> mc_tpumenus_temp_02)&0x01)return(29);
else return(30);}
阳历日和农历节气的对应关系
压缩节气数据表:
根据规律可以得到四个数据表(每个阳历月有两个节气,每个节气需要两个数据表):
const unsigned char calendar_solar_term_table_01[12][33] = {
{7,6,6,6,6,6,6,6,6,5,6,6,6,5,5,6,6,5,5,5,5,5,5,5,5,4,5,5}, //month 1
{5,4,5,5,5,4,4,5,5,4,4,4,4,4,4,4,4,3,4,4,4,3,3,4,4,3,3,3}, //2
{6,6,6,7,6,6,6,6,5,6,6,6,5,5,6,6,5,5,5,6,5,5,5,5,4,5,5,5,5}, //3
{5,5,6,6,5,5,5,6,5,5,5,5,4,5,5,5,4,4,5,5,4,4,4,5,4,4,4,4,5}, //4
{6,6,6,7,6,6,6,6,5,6,6,6,5,5,6,6,5,5,5,6,5,5,5,5,4,5,5,5,5}, //5
{6,6,7,7,6,6,6,7,6,6,6,6,5,6,6,6,5,5,6,6,5,5,5,6,5,5,5,5,4,5,5,5,5}, //6
{7,8,8,8,7,7,8,8,7,7,7,8,7,7,7,7,6,7,7,7,6,6,7,7,6,6,6,7,7}, //7
{8,8,8,9,8,8,8,8,7,8,8,8,7,7,8,8,7,7,7,8,7,7,7,7,6,7,7,7,6,6,7,7,7}, //8
{8,8,8,9,8,8,8,8,7,8,8,8,7,7,8,8,7,7,7,8,7,7,7,7,6,7,7,7,7}, //9
{9,9,9,9,8,9,9,9,8,8,9,9,8,8,8,9,8,8,8,8,7,8,8,8,7,7,8,8,8}, //10
{8,8,8,8,7,8,8,8,7,7,8,8,7,7,7,8,7,7,7,7,6,7,7,7,6,6,7,7,7}, //11
{7,8,8,8,7,7,8,8,7,7,7,8,7,7,7,7,6,7,7,7,6,6,7,7,6,6,6,7,7}, //12
}; //这个数据表表示了每个月第一个节气出现的规律
const unsigned char calendar_solar_term_year_01[12][9] = {
{13,49,85,117,149,185,201,250,250}, //month 1
{13,45,81,117,149,185,201,250,250}, //2
{13,48,84,112,148,184,200,201,250}, //3
{13,45,76,108,140,172,200,201,250}, //4
{13,44,72,104,132,168,200,201,250}, //5
{5 ,33,68,96 ,124,152,188,200,201}, //6
{29,57,85,120,148,176,200,201,250}, //7
{13,48,76,104,132,168,196,200,201}, //8
{25,60,88,120,148,184,200,201,250}, //9
{16,44,76,108,144,172,200,201,250}, //10
{28,60,92,124,160,192,200,201,250}, //11
{17,53,85,124,156,188,200,201,250}, //12
}; //这个数据表表示了每个月第一个节气出现规律对应的阳历年份范围
const unsigned char calendar_solar_term_table_02[12][29] = {
{21,21,21,21,21,20,21,21,21,20,20,21,21,20,20,20,20,20,20,20,20,19,20,20,20,19,19
{20,19,19,20,20,19,19,19,19,19,19,19,19,18,19,19,19,18,18,19,19,18,18,18,18,18,18
{21,21,21,22,21,21,21,21,20,21,21,21,20,20,21,21,20,20,20,21,20,20,20,20,19,20,20
{20,21,21,21,20,20,21,21,20,20,20,21,20,20,20,20,19,20,20,20,19,19,20,20,19,19,19
{21,22,22,22,21,21,22,22,21,21,21,22,21,21,21,21,20,21,21,21,20,20,21,21,20,20,20
{22,22,22,22,21,22,22,22,21,21,22,22,21,21,21,22,21,21,21,21,20,21,21,21,20,20,21
{23,23,24,24,23,23,23,24,23,23,23,23,22,23,23,23,22,22,23,23,22,22,22,23,22,22,22
{23,24,24,24,23,23,24,24,23,23,23,24,23,23,23,23,22,23,23,23,22,22,23,23,22,22,22
{23,24,24,24,23,23,24,24,23,23,23,24,23,23,23,23,22,23,23,23,22,22,23,23,22,22,22
{24,24,24,24,23,24,24,24,23,23,24,24,23,23,23,24,23,23,23,23,22,23,23,23,22,22,23
{23,23,23,23,22,23,23,23,22,22,23,23,22,22,22,23,22,22,22,22,21,22,22,22,21,21,22
{22,22,23,23,22,22,22,23,22,22,22,22,21,22,22,22,21,21,22,22,21,21,21,22,21,21,21
}; //这个数据表表示了每个月第二个节气出现的规律
const unsigned char calendar_solar_term_year_02[12][8] = {
{13,45,81,113,149,185,201},{21,57,93,125,161,193,201},{21,56,88,120,152,188,200,20
{21,49,81,116,144,176,200,201},{17,49,77,112,140,168,200,201},
{28,60,88,116,148,180,200,201},{25,53,84,112,144,172,200,201},
{29,57,89,120,148,180,200,201},{17,45,73,108,140,168,200,201},
{28,60,92,124,160,192,200,201},{16,44,80,112,148,180,200,201},
{17,53,88,120,156,188,200,201},};
//这个数据表表示了每个月第二个节气出现规律对应的阳历年份范围
每个阳历月对应的两个农历节气出现的日期,可根据条件规律算法,分别由以下两个函数实现
unsigned char calendar_calculate_solar_term_1(void)
{
zpage unsigned char done_index;
zpage unsigned char solar_term;
done_index = 0;
while(gc_solar_calendar_year >= calendar_solar_term_year_01[gc_solar_calendar_mon
[done_index]) {done_index++;}
solar_term = calendar_solar_term_table_01[gc_solar_calendar_month - 1][4*done_ind
gc_solar_calendar_year%4];
if((gc_solar_calendar_year == 121)&&(gc_solar_calendar_month == 4))solar_term = 5
if((gc_solar_calendar_year == 132)&&(gc_solar_calendar_month == 4))solar_term = 5
if((gc_solar_calendar_year == 194)&&(gc_solar_calendar_month == 6))solar_term = 6
return(solar_term);
} //计算阳历月对应的第一个节气
unsigned char calendar_calculate_solar_term_2(void)
{
zpage unsigned char done_index;
zpage unsigned char solar_term;
done_index = 0;
while(gc_solar_calendar_year >= calendar_solar_term_year_02[gc_solar_calendar_mon
[done_index]){done_index++;}
solar_term = calendar_solar_term_table_02[gc_solar_calendar_month - 1][4*done_ind
gc_solar_calendar_year%4];
if((gc_solar_calendar_year == 171)&&(gc_solar_calendar_month == 3))solar_term = 2
if((gc_solar_calendar_year == 181)&&(gc_solar_calendar_month == 5))solar_term = 2
return(solar_term);
} //计算阳历月对应的第二个节气
以上就是万年历的完整算法。它首先计算出对应阳历月第一天对应是星期几,然后根据数据压定,确定对应的阴历日期;而阴历节气,则有条件规律算法实现。

这个可以帮你做!不过我还是要建议你一下,这样的题目课程设计最好是自己编写编写练习的好,这对以后的工作很有帮助,如果实在是不知道怎么做,那来找我也可以可以的!!!

温度用硬件实现,二极管测温,比较器比较后进入MUC

  • 问: 求用C语言写一个简单的STC89C52单片机程序
    答:include<reg52.h>sbit Motor_L=P1^0;sbit Motor_R=P1^1;sbit K0=P0^0;sbit K2=P0^2;sbit K1=P0^1;sbit K3=P0^3;unsigned char time2s;/*定时器0初始化函数*/void InitTimer0(){ TMOD%=0xF0; TMOD|=0x01;//定时器0,方式一 TH0=(65536-50000)/256; TL0=(65536-...
  • 求一段STC89C52单片机c语言程序,要求能通过串口通信由电脑向单片机发送...
    答:include "reg52.h"define uchar unsigned char define uint unsigned int //--- sbit led = P1^0;sbit key = P0^0;sbit out1= P0^1;sbit out2= P0^2;uint run_cnt;uchar temp;//--- void send_one_byte(unsigned char tad ){ TI=0;SBUF=tad;//发送数据 os_wait2...
  • 单片机STC89C52显示0-99的C语言程序
    答:include <reg52.h> include <stdio.h> include <intrins.h> define uchar unsigned char define uint unsigned int sbit sm1= P3^7;sbit sm2=P3^6;sbit sm3=P3^5;sbit sm4=P3^4;sbit led=P2^7;unsigned char table1[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90 }...
  • 用stc89c52的单片机编写c语言的键盘扫描程序,为什么键盘扫描不成功呢...
    答:sbit p=P2;这个能这样定义吗!!应该改为 define p P2。sbit是定义一个可寻址位的,P2不是一个位地址,而是一个字节地址,不能那样定义的,虽然编译通过,但是是错的,你先试试看,我这没硬件电路 查到一处了 "case 0xee" 这个应该改为 "case 0xe0",因为前面temp=temp&0xf0;已经将temp的...
  • 求STC89C52的单片机跑马灯C语言程序
    答:int runlinght(){int i,j;j=1;for(i=0;i<8;i++){P[i]=(j<
  • 求stc89c52单片机c语言ds18b20温度控制
    答:uchar code wendu[]="0123456789";//利用一个温度表解决温度显示乱码 sbit DQ = P2^1; //定义ds18B20总线IO void delay(uchar z) //延时子函数 { uchar x,y;for(x=50;x>1;x--)for(y=z;y>1;y--);} void write_com(uchar com) //向液晶写入命令函数 { lcdrs=0;lcdrw=0...
  • stc89c52单片机C语言编程:8个LED登先左循环,再闪烁3次,最后右循环
    答:include<reg52.h> include <intrins.h> define uint unsigned int define uchar unsigned char void delay(uint);void main(){ uchar i,j,k;uchar temp;temp=0xfe;while(1){ for(j=1;j>0;j--){ for(i=7;i>0;i--){ P1=temp;delay(200);temp=_crol_(temp,1);} P1=0xff;for(...
  • STC89C52单片机数字温度传感器设计 (温度传感器是DS18B20) 寻高手帮...
    答:include<reg52.h> include <intrins.h> define uchar unsigned char define uint unsigned int define upzero 1 define downzero 0 uchar code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};uchar code seg[]={0xfe,0xfd,0xfb}; //加了PNP的情况下的段选 sbit DQ=...
  • STC89C52单片机,用C语言编温度报警器的程序
    答:{ //对于11.0592MHz时钟, unsigned int型的i, 作一个i++操作的时间大于8us unsigned int i;ds = 0;i = 100; //拉低约800us, 符合协议要求的480us以上 while(i>0) i--;ds = 1; //产生一个上升沿, 进入等待应答状态 i = 4;while(i>0) i--;} void dsWait(){ unsigned...
  • 求STC89C52单片机运算及共阴4位八段数码管显示运算结果的C语言程序...
    答:这个程序我编了一个上午,希望能对你有用。运行时按下INT0开始计时,再按下INT1得到速度 由于我只有公阳极数码管,(因为要调试)P2为片选端因为我加了三极管,所以也是低电平有效,你是共阴极,片选端可以直接连P2上,你自己把数字取反再调试看看 速度的单位是厘米每秒,定义的L是1米,由于用浮点数...