Programming based on single chip microcomputer application AVR 18B20

Publisher:码农闲散人Latest update time:2020-01-28 Source: elecfansKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#include //Includes model header file #include //Includes "bit" operation header file #include //Standard input and output header file #include //Includes custom constant header file #include "SMG.C" //Includes digital tube display function /********************************************

#include//Include model header file

#include //Include "bit" operation header file

#include //Standard input and output header files

#include《AVR_PQ1A.h》//Includes custom constant header file

Programming based on single chip microcomputer application AVR 18B20

#include "SMG.C" //Includes digital tube display function

/*******************************************

Function name: B20_init

Function: Reset DS18B20

Parameters: None

Return value: None

/************************************************/

void B20_init(void)

{

DDRA|=BIT(DS18B20); //Configure as output

PORTA|=BIT(DS18B20);

Delayus (10);

PORTA&=~BIT(DS18B20); //pull low

Delayus(750); //Wait for 600 microseconds

PORTA|=BIT(DS18B20); //Release the bus

Delayus(60); //Wait for 60 microseconds

DDRA&=~BIT(DS18B20); //Configure as input

while(( PI NA & (BIT(DS18B20)))); //Wait for DS18B20 to pull low

while(!(PINA&(BIT(DS18B20)))); //Wait for DS18B20 to release the bus

}

/*******************************************

Function name: B20_readB

Function: Read one byte of data

Parameters: None

Return value: retd - one byte of data returned

/************************************************/

ucharB20_readB(void)

{

uchari,retd=0;

for (i = 0; i < 8; i ++) // bit count value

{

retd》》=1; //Shift right, ready to receive new data bits

DDRA|=BIT(DS18B20); //Configure as output

PORTA&=~BIT(DS18B20); //Pull low to start reading data bit

PORTA|=BIT(DS18B20); //Release the bus

Delayus(5); //Wait for 5 microseconds

DDRA&=~BIT(DS18B20); //Configure as input, start reading data bits

if (PINA & BIT (DS18B20)) // Is the bit high?

{

retd|=0x80; //Yes, set this position high

}

Delayus(50); //Wait for 50 microseconds

}

returnretd; //Return the read byte

}

/*******************************************

Function name: B20_write B

Function: Write one byte of data

Parameter: wrd--data to be written

Return value: None

/************************************************/

void B20_writeB(ucharwrd)

{

uchari;

for (i = 0; i < 8; i ++) // bit count value

{

DDRA|=BIT(DS18B20); //Configure as output

PORTA&=~BIT(DS18B20); //Pull low to start writing data bit

Delayus(1); //Wait for 1 microsecond

if (wrd & 0x01) // Is this bit data high?

{

PORTA|=BIT(DS18B20); //If it is high, pull the single bus high

}

else

{

PORTA&=~BIT(DS18B20); //If it is low, pull the single bus low

}

Delayus(50); //Wait for 50 microseconds

PORTA|=BIT(DS18B20); //Release the bus

wrd》》=1; //Shift right to prepare for writing new data bits

}

Delayus(50); //Wait for 50 microseconds

}

/*******************************************

Function Name: Read_temp

Function: Read temperature value

Parameters: None

Return value: rettemp--returned temperature value

/************************************************/

uintRead_temp(void)

{

uchartempl,temph;

uinttemp;

B20_init(); // Initialization, each write command starts from initialization

B20_writeB(0xCC); //skip ROM

B20_writeB(0x44); //Start temperature conversion

B20_init(); // Initialization, each write command starts from initialization

B20_writeB(0xcc); //skip ROM

B20_writeB(0xbe); //read register

templ=B20_readB(); //read temperature low byte

temph=B20_readB(); //read temperature high byte

temp=templ+temph*256; //Organize the temperature into a 16-bit variable

returntemp; //Return 16-bit variable

}

/*******************************************

Function name: Num_BCD

Function: Convert a one-byte integer into a three-digit BCD code

Parameter: num - the integer to be converted

Return value: chr - three-digit BCD code array pointer

/************************************************/

uchar* Num_BCD(uint num)

{

uchari,chr[3];

uchar*rept;

rept=&(chr[0]); //Return pointer to BCD code array

for (i=0;i<3;i++)

{

chr[2-i]=num%10; //Remainder of 10 (actually modulus, but for positive numbers, remainder and modulus are equal)

num/=10; // Divide by 10 to prepare for taking out the next digit

}

returnrept; //Return pointer

}

/*******************************************

Function name : main

Function: Complete temperature reading and display

Parameters: None

Return value: None

/************************************************/

voi dma in (void)

{

uintt;

uchar*temppt;

uchari, temp[3];

Board_init(); //Initialize the development board

while(1)

{

t=Read_temp(); //Read temperature value

t*=0.625; //Convert to 10 times the actual temperature

while(t==850)

{

t=Read_temp(); //Read temperature value

t*=0.625;

}

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 is the same as the digits, add a decimal point (the decoding array of the digital tube has changed, the reader can see the SMG.C file in the 10_DS18B20 folder)

}

One_smg_display(temp[i],i+1);

Delayms(5);

}

}

}


Keywords:MCU Reference address:Programming based on single chip microcomputer application AVR 18B20

Previous article:AVR high voltage parallel programmer manufacturing method
Next article:How to Design a Solar Cell Controller Using an AVR Microcontroller

Recommended ReadingLatest update time:2024-11-16 11:47

Based on 51 single chip microcomputer + SG90 servo
basic introduction Classification of servos They can be roughly divided into the following three types, but I have only used three types now. 180° limited position (can only rotate 180 degrees without power on, true rotation is 180°) 180° unlimited position (can rotate 360° without power on, but the actual rotation
[Microcontroller]
Based on 51 single chip microcomputer + SG90 servo
51 MCU Study Notes [0] - Basic Knowledge
1. Three major resources of MCU 1. Flash - program storage space It is equivalent to the hard disk of a computer and can ensure that data is not lost after a power outage. 2. RAM - Data storage space It is equivalent to the computer's memory, used to store data generated and needed during program execution. The data
[Microcontroller]
51 MCU Study Notes [0] - Basic Knowledge
ATMEGA8 microcontroller drives stepper motors
#include #include #defineucharunsignedchar #defineuintunsignedint ucharnp; //Stepper motor operation data table constucharmotortb[]={0x11,0x99,0x88,0xCC,0x44,0x66,0x22,0x33}; voiddelay_nms(uintms) //delay subroutine for each step { swimming; for(i=0;i_delay_loop_2(8*250); } void a_step (uchard, uchart) //stepper motor
[Microcontroller]
ATMEGA8 microcontroller drives stepper motors
Course Design of Single Chip Microcomputer 00 to 99 Stopwatch
This is the MCU stopwatch course design I made last month. This course design is to realize a stopwatch from 00 to 99 seconds, and it has start and reset functions. It took a week. The attachment contains the MCU source program of the stopwatch, the proteus simulation circuit diagram and the course design document. I
[Microcontroller]
Course Design of Single Chip Microcomputer 00 to 99 Stopwatch
Wireless terminal temperature test system based on single chip microcomputer design
  As people's living standards improve, food safety and hygiene are receiving more and more attention. Every year, the technical supervision department conducts random inspections on food in cold storages throughout the city. After inspection, it is found that a large part of the agricultural products and other perisha
[Microcontroller]
Wireless terminal temperature test system based on single chip microcomputer design
51 MCU Experiment 5: Dynamic Digital Tube
The circuit diagram of the digital tube module of the development board is as follows: Due to the characteristics of the 74ls138 decoder, that is, only one digital tube can have a cathode level of 0, it is obviously impossible to make multiple digital tubes work at the same time. If you want to get the effect of mul
[Microcontroller]
51 MCU Experiment 5: Dynamic Digital Tube
MCU button switches to the next song
#include "at89x51.h" #define uchar unsigned char #define uint unsinged int sbit speaker = P1^5; //Buzzer. The default pin for the button is P3.2 (external interrupt 0) uchar t0h,t0l,time,flag; //-------------------------------------- //The microcontroller crystal oscillator uses 12MHz // Frequency-half-cycle data ta
[Microcontroller]
MCU PWM Timer
S3C2410 has 5 timers in total Among them, 0, 1, 2, and 3 have PWM functions, and each has an output pin, which can be controlled by a timer to periodically change the high and low levels of the pin; The timer has no output pins; 1. Timer clock source The clock source of the timing component is PCLK, which is fir
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号