#define uint8 unsigned char#define uint 16 unsigned int
_ _ configuration (wdtdis & Amplvpdis & Amphs & Amppwrtdis & Ampboldis); //Set the configuration bit
//WDTDIS: disable watchdog timer.
//LVPDIS: low voltage programming is disabled.
//HS: high-speed crystal/resonator
//PWRTDIS: disable power-on timer.
//BORDIS: disable power-off reset.
Static volatile bit dat @ (unsigned) portc * 8+2;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
const uint8 disp[] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x 90 }; //* * * Yang digital tube section code table
Volatile uint8temp _ data _ int//integer part of temperature
Volatile float temper _ data _ fla///Decimal part of temperature
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Void ShowTemper (invalid); //Display temperature
void Get _ temper(void); //Get temperature data
void Reset _ ds 18b 20(void); //Reset DS 18B20
uint 8 Read _ Temp _ Data(void); //Read temperature byte data
void delay us(uint 8 delay); //A slight delay
void delay ms(uint 16 delay); //millisecond delay
void Write _ 18b 20 _ Comd(uint 8 Comd); //Write DS 18B20 instruction
Void display (uint8 chosebit, uint8data); //Single 8-bit digital tube display
/*******************************************************************************
* function name: DelayUS(uint8 delay)
* Function: 20MHZ microsecond delay.
* Entry parameter: delay
* Return: None
*******************************************************************************/
Invalid delay (uint8 delay)
{
while(-delay);
}
/*******************************************************************************
* function name: delaymas (uint 16 delay)
* Function: 20MHZ millisecond delay.
* Entry parameter: delay
* Return: None
*******************************************************************************/
Invalid delay (uint 16 delay)
{
uint 16 I;
for(; Delay & gt0; Delay-)
for(I = 0; I & lt453; i++);
}
/******************************************************************************
* function name: display (uint8chosebit, uint8data)
* Function: Single digital tube display
* entrance parameters: chosebit selection bit, data font.
* Return: None
*******************************************************************************/
Empty display (uint8 selection bit, uint8 data)
{
adcon 1 = 0x8E; //Set PORTA0 as analog input and others as IO ports.
Teresa & = 0xc1;
TRISD = 0x00// The data line is configured as an output.
PORTA = 0xFF
PORTA & amp= ~(0x 02 & lt; & ltchose bit);
PORTD = data; //Send font
Drayus (200); //delay
PORTD = 0xFF// Turn off the monitor.
}
/******************************************************************************
* function name: Reset_DS 18B20(void)
* function: reset DS 18B20.
* Inlet parameters: None
* Return: None
*******************************************************************************/
Invalid reset _DS 18B20 (invalid)
{
Uint8 flag = 0; //Reset flag
while( 1)
{
dat = 0; //The data line is set to 0
Drayus (250);
Drayus (250); //Delay 500us
dat = 1; //The data line is set to 1.
Draeus (60); //Delay 60us
tris C2 = 1;
While(dat == 0) // If the data line is 0
{
Drayus (240); //delay 240us
If(dat) // If the data line is 1
{
flag = 1; //Reset succeeded, and set the success flag.
Break; //Jump out of the while(dat==0) statement.
}
other
Break;
}
If (logo)
{//If the reset is successful,
Drayus (240);
Drayus (240); //delay 480us
Break; //Jump out of the whole while( 1) statement.
}
tris C2 = 0;
}
}
/******************************************************************************
* function name: write _18b20 _ comd (uint8comd)
* function: DS 18B20 write instruction.
* entrance parameter: comd instruction
* Return: None
*******************************************************************************/
void Write _ 18b 20 _ Comd(uint 8 Comd)
{
uint8 i,temp
tris C2 = 0;
for(I = 0; I<8; I++) // A byte cycles 8 times.
{
temp = comd & amp0x 0 1; //Read the bottom bit of the instruction
comd & gt& gt= 1; //Instruct the data to shift one bit to the right.
dat = 0; //The data line is set to 0
Draeus (4); //delay 8us
If (temperature == 0)
{
dat = 0;
}
other
{
dat = 1;
}
Draeus (60); //delay 40us
dat = 1; //The data line is set to 1.
}
}
/******************************************************************************
* function name: read temporary data (void)
* Function: read the temperature sampling data of DS 18B20.
* Inlet parameters: None
* Return: data read.
*******************************************************************************/
Uint8 reads temporary data (void).
{
Uint8 i, data;
tris C2 = 0;
for(I = 0; I<8; I++) // Read a byte 8 times.
{
Data & gt& gt= 1; //The data is shifted one bit to the right.
dat = 0; //The data line is set to 0
Draeus (2); //Delay 2us
dat = 1; //The data line is set to 1.
Draeus (8); //delay 8us
tris C2 = 1;
Draeus (8); //delay 8us
If(dat) // If the read data is 1
Data | = 0x80// Highest position of data 1
Draeus (40); //delay 40us
tris C2 = 0;
}
To return (data); //Returns the read data.
}
/******************************************************************************
* function name: Get _ temple(void)
* Function: Read temperature data.
* Inlet parameters: None
* Return: None
*******************************************************************************/
void Get_temper(void)
{
uint8 temp,temp 1;
write _ 18b 20 _ Comd(0x cc); //write skip ROM instruction
write _ 18b 20 _ Comd(0x 44); //Write temperature conversion instruction
dat = 1; //The data line is set to 1.
Drayus (200);
Drayus (200);
reset _ ds 18b 20(); //Call the initialization reset function
write _ 18b 20 _ Comd(0x cc);
write _ 18b 20 _ Comd(0x be); //Write and read instructions
temp = Read_Temp_Data()。 //Read low storage temperature.
Temp & amp0x0f///take the decimal part and store it in temp _ data _ Fla
temp & gt& gt= 4; //Take the upper nibble of the lower byte.
temp 1 = Read_Temp_Data()。 //The read high bits are stored in temp 1.
temp 1 & lt; & lt= 4; //Take the lower half byte of the high byte
temp _ data _ int = temp 1 | temp; //high and low nibbles or temp _ data _ int
Tempering _ data _fla = Tempering _ data _ Fla *100/16; //Take the decimal part and store it in temp 1.
}
/******************************************************************************
* function name: show temple(void)
* Function: Display the detected temperature.
* Inlet parameters: None
* Return: None
*******************************************************************************/
Invalid display status (invalid)
{
Display(0,disp[temple _ data _ int/ 10]);
disp[temple _ data _ int % 10]& amp; 0x7F);
Display(2,disp[(uint 8)tempent _ data _ Fla/ 10]);
Display(3,disp[(uint 8)tempent _ data _ Fla % 10]);
}
/******************************************************************************
* Number of letters: main ()
* Function: Real-time detection and display of ambient temperature.
* Inlet parameters: None
* Return: None
*******************************************************************************/
void main()
{
tris C2 = 0;
while( 1)
{
reset _ ds 18b 20(); //Reset DS 18B20
get _ temple(); //Get the temperature value
show temple(); //Display temperature
}
}
Disaster prevention and mitigation class meeting summary 300 words 1
On the occasion of the X