File: DS 18B20. C
Environment: compiled as ICC AVR6.3 1A and simulated as AVR Studio4. 10.
Hardware: ATMEGA 16 chip.
Date: August 8, 2008
/******************************************/
# include// contains the model header file.
# include// The header file containing the bit operation.
# include// standard input/output header file
# include// Header file containing custom constants
# include "SMG. c "// contains the display function of digital tube.
/*******************************************
Function name: B20_init
Function: Reset DS 18B20
Parameter: None
Return value: None
/********************************************/
Void B20_init (invalid)
{
DDRA | = BIT(ds 18b 20); //configured as output
PORTA & amp= ~ BIT(ds 18b 20); //drop down
Drayus (600); //Wait 600 microseconds
PORTA | = BIT(ds 18b 20); //Release the bus
Draeus (60); //Wait 60 microseconds
DDRA & amp; = ~ BIT(ds 18b 20); //configured as input
And ((Pina &; (BIT(ds 18b 20))); //Wait for DS 18B20 to be pulled low.
And (! (PINA & amp; (BIT(ds 18b 20))); //Wait for DS 18B20 to release the bus.
}
/*******************************************
Function name: B20_readB
Function: Read one byte of data.
Parameter: None
Return value: retd-Returns one byte of data.
/********************************************/
uchar B20_readB(void)
{
uchar i,retd = 0;
for(I = 0; I<8; I++)// bit count value
{
retd & gt& gt= 1; //Move to the right, ready to accept new data bits.
DDRA | = BIT(ds 18b 20); //configured as output
PORTA & amp= ~ BIT(ds 18b 20); //Pull low to start reading data bits.
PORTA | = BIT(ds 18b 20); //Release the bus
Draeus (5); //Wait for 5 microseconds
DDRA & amp; = ~ BIT(ds 18b 20); //is configured to input and start reading data bits.
If (pina &; Bit(ds 18b 20))// Is this bit high?
{
Retd | = 0x80// Yes, set this bit high.
}
Draeus (50); //Wait 50 microseconds
}
Return retd// Returns the bytes read.
}
/*******************************************
Function name: B20_writeB
Function: Write one byte of data.
Parameter: wrd-data to be written.
Return value: None
/********************************************/
void B20_writeB(uchar wrd)
{
Ucal I;
for(I = 0; I<8; I++)// bit count value
{
DDRA | = BIT(ds 18b 20); //configured as output
PORTA & amp= ~ BIT(ds 18b 20); //Pull low to start writing data bits.
Drayus (1); //Wait 1 microsecond
If (wrd & amp0x001)/is this bit high?
{
PORTA | = BIT(ds 18b 20); //If it is high, pull up the single bus.
}
other
{
PORTA & amp= ~ BIT(ds 18b 20); //If it is low, the single bus will be pulled low.
}
Draeus (50); //Wait 50 microseconds
PORTA | = BIT(ds 18b 20); //Release the bus
wrd & gt& gt= 1; //Move to the right, ready to write new data bits.
}
Draeus (50); //Wait 50 microseconds
}
/*******************************************
Function Name: Reading Temperature
Working energy: read the temperature value.
Parameter: None
Return value: rettemp-the returned temperature value.
/********************************************/
Uintrade _ temp (empty)
{
uchar templ,temph
Unit temperature;
b20 _ init(); //Initialize. Every time you write a command, it starts with initialization.
b20 _ writeB(0x cc); //skip ROM
b20 _ writeB(0x 44); //Start temperature conversion
b20 _ init(); //Initialize. Every time you write a command, it starts with initialization.
b20 _ writeB(0x cc); //skip ROM
b20 _ writeB(0x be); //Read register
templ = B20 _ readB(); //Read temperature low byte
temph = B20 _ readB(); //Read temperature high byte
temp = templ+temph * 256; //Sort the temperature as a 16-bit variable.
Return temperature; //Returns a 16-bit variable.
}
/*******************************************
Function name: Num_BCD
Function: Convert a byte integer into a three-bit BCD code.
Parameter: num-integer to be converted.
Return value: chr-three BCD code array pointer.
/********************************************/
Uchar* Num_BCD (company number)
{
uchar i,chr[3];
Uchar report;
rept = & amp(chr[0]); //Returns a pointer to an array of BCD codes
for(I = 0; I<3; i++)
{
chr[2-I]= num % 10; //take the remainder of 10 (in fact, it is to find the modulus, but for positive numbers, taking the remainder is equal to finding the modulus)
num/= 10; //divided by 10, ready to take out the next bit.
}
Return the report; //Returns a pointer
}
/*******************************************
Function name: main
Function: Read and display the temperature.
Parameter: None
Return value: None
/********************************************/
Invalid master (invalid)
{
uint t;
uchar * temppt
uchar i,temp[3];
board _ init(); //Initialize the development board
while( 1)
{
t = Read _ temp(); //Read the temperature value
T * = 0.625// is converted into 10 times of actual temperature.
temppt = Num _ BCD(t); //Convert 10 times the actual temperature into BCD code.
for(I = 0; I<3; I++)// Store the converted BCD code in the display array.
{
temp[I]= *(temppt+I);
}
for(I = 0; I<3; I++)// Display temperature
{
if(i== 1)
{
temp[ 1]+= 16; //If it's you, add decimal point (digital tube decoding)
}
One_smg_display(temp[i],I+ 1);
Delems (5);
}
}
}
/*******************************************
Document: SMG. C
Environment: compiled as ICC AVR6.25A and simulated as AVR Studio4. 10.
Hardware: ATMEGA 16 chip.
Date: 12, 2006
Function: Drive the digital tube on the development board and scroll to display 1, 2, 3, 4, 5, 6.
Remarks: Refer to C language programming and application examples of AVR series single chip microcomputer.
/******************************************/
# include// contains the model header file.
# include// The header file containing the bit operation.
# include// standard input/output header file
#pragma data: code
const SEGMENT[26]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x7 1,
0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,
0xff,0x ef };
/*******************************************
Function name: One_smg_display
Function: Specify the digital tube to display the specified content.
Parameter: data display content (0- 15)
Digital assigned nixie tube (1-6)
Return value: None
/********************************************/
Void One_smg_display(uchar data, uchar number)
{
PORTB | = 0x3F// Output bit selection
PORTA | = BIT(BITLK); //Update bit selection
Draeus (50); //Adjust the timing
PORTA & amp= ~ BIT(BITLK); //latch bit selection
PORTB = segment [data]; //Output segment selection
PORTA | = BIT(seg lk); //Update segment selection
Draeus (50); //Adjust the timing
PORTA & amp= ~ BIT(se glk); //Latch segment selection
Number=~BIT (number-1); //Adjust position selection (1~6 when specified and 0~5 when operated)
PORTB = number; //Output bit selection
PORTA | = BIT(BITLK); //Update bit selection
Draeus (50); //Adjust the timing
PORTA & amp= ~ BIT(BITLK); //latch bit selection
}
/*******************************************
Function name: All _ smg _ monitor
Function: 6 digital tubes display the specified content.
Parameter: * pdata- Displays the address of the content array.
Return value: None
/********************************************/
void All _ SMG _ display(uchar * pdata)
{
Ucal I;
for(I = 0; I<6; i++)
{
One_smg_display(pdata[i],I+ 1);
delay ms( 1);
}
}
/*******************************************
Function Name: Cyclic Display
Function: Six digital tubes scroll from right to left to display the specified content (once).
Parameter: * pdata- Displays the address of the content array.
Return value: None
/********************************************/
void Cycle _ SMG _ display(uchar * pdata)
{
uchar i,j;
for(I = 0; I & lt240; I++)/* * * Scroll 6 times, and each time the content is displayed for 40 cycles.
{
for(j = 0; j & lt6; J++)// Scan 6 digital tubes
{
one _ SMG _ display(pdata[(I/40+j)% 6],j+ 1); //Display
Drayus (2000); //Adjust the display time and brightness (the longer the time, the higher the brightness, but the longer the digital tube flashes)
}
}
}