十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
#include stdio.h
网站建设哪家好,找创新互联公司!专注于网页设计、网站建设、微信开发、小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了吴堡免费建站欢迎大家使用!
void main()
{
float F;
printf("输入华氏温度: ");
scanf("%f",F);
printf("对应的摄氏温度: %.1f",(F-32)/1.8);
}
#include stdio.hint main(){ double c, f; printf ("请输入华氏温度:"); scanf ("%lf", f); c=5.0/9*(f-32); printf ("%g华氏温度是%.2f摄氏温度\n", f, c); return 0;}
扩展资料:
#includeiostream
using namespace std;
int main()
{
float f,c;
cout"请输入华氏温度:";
cinf;
c=5*(f-32)/9;
cout"摄氏温度为:"cendl;
return 0;
}
C语言CPU测试温度程序
// Target : M8
// Crystal: 8.0000Mhz
/*******************************************************************************
* 不可在RESET时做LCD显示,因为DS18B20的复位回应时间只有80us,显示一个字符的时间
远大于这个值
*
*******************************************************************************/
#include iom8v.h
#include macros.h
#include "1602Driver.h"
#define Set_DQ1 DDRC |= 0x08; PORTC |= 0x08; //总线拉高
#define Set_DQ0 DDRC |= 0x08; PORTC = 0xf7; //总线置低
#define Read_DQ PINC0x08 //读总线
#define MatchROM 0xcc //匹配ROM
#define WriteMode 0x4e //写模式
#define TH 0x64 //设置温度上限100
#define TL 0x8a //设置温度下限-10
#define MatchTemp 0x7f //写温度匹配寄存器,12bit
#define ConverTem 0x44 //DS18B20温度转换命令
#define Get_Value 0xbe //读取温度寄存器值
char DelayMs = 0;
void port_init(void)
{
DDRD = 0xff;
PORTD = 0xff;
DDRB = 0xff;
PORTB = 0xff;
DDRC = 0xff;
PORTC = 0xff;
}
//TIMER1 initialize - prescale:1
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 15uSec
// actual value: 14.875uSec (0.8%)
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0xFF; //setup
TCNT1L = 0x89;
OCR1AH = 0x00;
OCR1AL = 0x77;
OCR1BH = 0x00;
OCR1BL = 0x77;
ICR1H = 0x00;
ICR1L = 0x77;
TCCR1A = 0x00;
TCCR1B = 0x01; //start Timer
}
#pragma interrupt_handler timer1_ovf_isr:9
void timer1_ovf_isr(void)
{
//TIMER1 has overflowed
TCNT1H = 0xFF; //reload counter high value
TCNT1L = 0x89; //reload counter low value
if (DelayMs 0)
{
DelayMs --;
}
}
void Delay_15us(unsigned int n)
/*---Ms延时函数---*/
{
DelayMs=n;
while(DelayMs 0);
}
/***************************************************************
* 功能:主机向总线写0 *
***************************************************************/
void Writr0(void)
{
Set_DQ1;
Set_DQ0;
Delay_15us(5);
Set_DQ1;
Delay_15us(1);
}
/***************************************************************
* 功能:主机向总线写1 *
***************************************************************/
void Writr1(void)
{
Set_DQ1;
Set_DQ0;
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
Set_DQ1;
Delay_15us(2);
}
/***************************************************************
* 功能:向总线写一个字节 *
* 输入:需要写的字 *
***************************************************************/
void WriteBits(char Byte)
{
char i = 0;
for (i=0;i8;i++)
{
if (Byte 0x01)
{
Writr1();
}
else
{
Writr0();
}
Byte=1;
}
}
/***************************************************************
* 功能:DS18B20复位程序 *
* 返回:总线复位成功,返回1 *
***************************************************************/
char Reset1820(void)
{
static char CheckTimes = 0;
static char CheckValue = 1;
Delay_nms(1);
Set_DQ1;
Set_DQ0; //拉低总线480us
Delay_15us(34);
Set_DQ1;
DDRC = 0xf7; //设置端口为输入状态,读取数据
PORTC |= 0x08;
while(PINC0x08); //等待,直至确认复位成功
CheckValue = Read_DQ;
Delay_15us(32);
return CheckValue;
}
void Init1820(void)
{
if (Reset1820() == 0x08)
{
LCD_Write_String(0,0,"1820 Not Detect!");
LCD_Write_String(0,1," ");
}
else
{
LCD_Write_String(0,0,"Init DS18B20 OK!");
LCD_Write_String(0,1," ");
}
WriteBits(MatchROM);
WriteBits(WriteMode);
WriteBits(TH);
WriteBits(TL);
WriteBits(MatchTemp);
}
/***************************************************************
* 功能:从总线中读取数据位 *
* 返回:读取值 *
***************************************************************/
char ReadBit(void)
{
char i = 0;
char Read_Value = 0;
Set_DQ1;
Set_DQ0;
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
Set_DQ1;
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
DDRC = 0xf7; //设置端口为输入状态,读取数据
PORTC |= 0x08;
asm("nop");
Read_Value = Read_DQ;
Delay_15us(3);
return Read_Value;
}
/***************************************************************
* 功能:从总线读取一个字节 *
* 返回:读取到的字符 *
***************************************************************/
char ReadBits(void)
{
char i = 0;
char b = 0;
char ReadBits_Value = 0;
for (i=8;i0;i--)
{
ReadBits_Value = ReadBits_Value1;
b = ReadBit();
if(b)
{
ReadBits_Value = ReadBits_Value|0x80;
}
}
//DEC_Num_Disp(0,1,ReadBits_Value,5);
return ReadBits_Value;
}
/***************************************************************
* 功能:获得温度值 *
* 返回:温度值--摄氏温度 *
***************************************************************/
char Get_Temperature(void)
{
static int TempLow = 0;
static int TempHi = 0;
static char Temp2 = 0;
static long final = 0;
static Sign = 0; //温度的符号位
Reset1820();
WriteBits(MatchROM);
WriteBits(ConverTem);
//Delay_15us(6);
Reset1820();
WriteBits(MatchROM);
WriteBits(Get_Value);
Delay_15us(5);
TempLow = ReadBits(); //温度低位
Delay_15us(5);
Temp2 = ReadBits();
Sign = Temp20xf8; //符号位取高5位
TempHi = Temp20x07; //温度高位
if (Sign == 0) //正温的数据处理
{
final = (((TempHi)8)|TempLow)*62.5;
LCD_Write_String(0,1," ");
}
else
{
final = (~(((TempHi)8)|TempLow)+1)*62.5;
LCD_Write_String(0,1,"-");
}
if (final = 37400)
{
LCD_Write_String(10,1,"Fever!");
}
if ((final 37400)(final36000))
{
LCD_Write_String(10,1,"Normal");
}
if (final=36000)
{
LCD_Write_String(10,1,"Low! ");
}
Point_Disp(1,1,final,6,3);
return final;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer1_init();
LCD_Init();
LCD_Write_Char(0x01,0); //清屏
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x04; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
void main(void)
{
init_devices();
Delay_nms(5);
LCD_Write_String(0,0,"1820 Not Detect!");
LCD_Write_String(0,1,"Reset after sure");
Init1820();
Delay_nms(100);
LCD_Write_String(0,0,"Body Temp. Test:");
LCD_Write_String(0,1," C ");
while(1)
{
Get_Temperature();
}
}
不是所有的设备都有温度传感器的,只有 cpu 和硬盘是比较固定的,显卡有可能有,但是不一定能够有对外的接口。
硬盘温度走 atapi 协议,你可以试试解析
CPU 的话,目前最新的 Intel 的有固定的 MSR 可以读取
此外,还可以通过主板上的 Super Io 或者笔记本的EC来读取。 Super IO 的话,比较容易搞,你可以看spec,但是不同厂家差别很大。EC 的话,没有规范,基本上不可能了
总而言之,最简单的方法是使用第三方或者开源的软件来获取。