typedef unsigned char uchar;
typedef unsigned int uint;
/**************Macro definition****************/
#define DataDir P4DIR
#define DataPort P4OUT
#define Busy 0x80
#define CtrlDir P3DIR
#define CLR_RS P3OUT&=~BIT0; //RS = P3.0
#define SET_RS P3OUT|=BIT0;
#define CLR_RW P3OUT&=~BIT1; //RW = P3.1
#define SET_RW P3OUT|=BIT1;
#define CLR_EN P3OUT&=~BIT2; //EN = P3.2
#define SET_EN P3OUT|=BIT2;
#define DQ1 P1OUT |= BIT6
#define DQ0 P1OUT &= ~BIT6
#define DQ_in P1DIR &= ~BIT6
#define DQ_out P1DIR |= BIT6
#define DQ_val (P1IN & BIT6)
uint tvalue;
uchar tflag;
uchar disdata[4];
/*******************************************
Function name: Delay5msFunction
: Delay about 5msParameter
: None
Return value: None
********************************************/
void Delay5ms(void)
{
uint i=40000;
while (i != 0)
{
i--;
}
}
/*******************************************
Function name: DelayNusFunction
: Achieve N microseconds of delay
Parameter: n--delay length
Return value: None
Description :The counting clock of timer A is 1MHz, and the CPU main frequency is 8MHz
, so extremely accurate
us-level delay can be obtained through timer delay
********************************************/
void DelayNus(uint n)
{
CCR0 = n;
TACTL |= MC_1; //Increase count to CCR0
while(!(TACTL & BIT0)); //Wait for
TACTL &= ~MC_1; //Stop counting
TACTL &= ~BIT0; //Clear interrupt flag
}
/*******************************************
Function name: WaitForEnableFunction
: Wait for 1602 LCD to complete internal operation
Parameter: None
Return value: None
********************************************/
void WaitForEnable(void)
{
P4DIR &= 0x00; //Switch P4 port to input
stateCLR_RS;
SET_RW;
_NOP();
SET_EN;
_NOP();
_NOP();
while((P4IN & Busy)!=0); //Detect busy flag
CLR_EN;
P4DIR |= 0xFF; //Switch P4 port to output state
}
/*******************************************
Function name: write_comFunction
: Write command to LCD module
Parameter: cmd--command,
chk--flag of whether to judge busy, 1: judge busy, 0: not judge
Return value: None
********************************************/
void write_com(uchar cmd)
{
WaitForEnable(); //Detect busy signal?
CLR_RS;
CLR_RW;
_NOP();
DataPort = cmd; //Write command word to data port_NOP
();
SET_EN; //Generate enable pulse signal_NOP
();
_NOP();
CLR_EN;
}
/*******************************************
Function name: write_dataFunction
: Write display data to the current address of LCD display
Parameter Number: data--display character data
Return value: None
********************************************/
void write_data( uchar data )
{
WaitForEnable(); //Wait for the LCD to be unbusy
SET_RS;
CLR_RW;
_NOP();
DataPort = data; //Write display data to data port_NOP
();
SET_EN; //Generate enable pulse signal_NOP
();
_NOP();
CLR_EN;
}
void zifuchuan(uchar *ch)
{
while(*ch!=0)
write_data(*ch++);
Delay5ms();
}
/*******************************************
Function name: LcdResetFunction
: Reset the 1602 LCD module
Parameter: None
Return value: None
********************************************/
void LcdReset(void)
{
CtrlDir |= 0x07; //Control line port is set to output
stateDataDir = 0xFF; //Data port is set to output
statewrite_com(0x38); //Specified reset operationDelay5ms
();
write_com(0x38);
Delay5ms();
write_com(0x38);
Delay5ms();
write_com(0x38); //Display mode settingwrite_com
(0x08); //Display offwrite_com
(0x01); //Display clear screenwrite_com
(0x06); //Write characters without moving the whole
write_com(0x0c); //Display on, cursor off, no flashing
}
/*******************************************
Function name: Init_18B20Function
: Reset DS18B20
Parameters: None
Return value: Initialization status flag: 1-failure, 0-success
****************************************/
uchar Init_18B20(void)
{
uchar Error;
DQ_out;
_DINT();
DQ0;
DelayNus(500);
DQ1;
DelayNus(55);
DQ_in;
_NOP();
if(DQ_val)
{
Error = 1; //Initialization failed
}
else
{
Error = 0; //Initialization successful
}
DQ_out;
DQ1;
_EINT();
DelayNus(400);
return Error;
}[page]
/***********************************************
Function name: Write_18B20Function
: Write one byte of data to DS18B20
Parameters Data: wdata--Written data
Return value: None
********************************************/
void Write_18B20(uchar wdata)
{
uchar i;
_DINT();
for(i = 0; i < 8;i++)
{
DQ0;
DelayNus(6); //Delay 6us
if(wdata & 0X01) DQ1;
else DQ0;
wdata >>= 1;
DelayNus(50); //Delay 50us
DQ1;
DelayNus(10); //Delay 10us
}
_EINT();
}
/***********************************************
Function name: Read_18B20Function
: Read one byte of data from DS18B20
Parameters: None
Return value: One byte of data read out
********************************************/
uchar Read_18B20(void)
{
uchar i;
uchar temp = 0;
_DINT();
for(i = 0;i < 8;i++)
{
temp >>= 1;
DQ0;
DelayNus(6); //Delay 6us
DQ1;
DelayNus(8); //Delay 9us
DQ_in;
_NOP();
if(DQ_val) temp |= 0x80;
DelayNus(45); //Delay 45us
DQ_out;
DQ1;
DelayNus(10); //Delay 10us
}
_EINT();
return temp;
}
/***********************************************
Function name: Skip
Function: Send skip reading product ID command
Parameter: None
Return value: None
********************************************/
void Skip(void)
{
Write_18B20(0xcc);
}
/*******************************************
Function name: ConvertFunction
: Send temperature conversion command
Parameter: None
Return value: None
********************************************/
void Convert(void)
{
Write_18B20(0x44);
}
/*******************************************
Function name: Read_SPFunction
: Send read ScratchPad command
Parameter: None
Return value: None
********************************************/
void Read_SP(void)
{
Write_18B20(0xbe);
}
/***********************************************
Function name: ReadTempFunction
: Read temperature conversion result from DS18B20 ScratchPad
Parameter: None
Return value: Read temperature value
********************************************/
uint ReadTemp(void)
{
uchar temp_low;
uint temp;
temp_low = Read_18B20(); //Read low bit
temp = Read_18B20(); //Read high bit
temp = (temp<<8) | temp_low;
return temp;
}
uint Do1Convert(void)
{
uchar i;
uchar temp_low;
uint temp;
do
{
i = Init_18B20();
}
while(i);
//i here is equal to the previous Error. If Error = 1, an infinite loop will appear, indicating that 18B20 may be broken.
Write_18B20(0xcc); //Send the command to skip reading the product ID number
. Write_18B20(0x44); //Send the temperature conversion command
for(i = 20;i > 0;i--)
DelayNus(60000); //Delay for more than 800ms
do
{
i = Init_18B20();
}
while(i);
//i here is equal to the previous Error. If Error = 1, an infinite loop will appear, indicating that 18B20 may be broken.
Write_18B20(0xcc); //Send the command to skip reading the product ID number.
Write_18B20(0xbe); //Send the command to read ROM.
temp_low = Read_18B20(); //Read low bit
temp = Read_18B20(); //Read high bit
temp = (temp<<8) | temp_low;
if(temp<0x0fff)
tflag=0;
else
{temp=~temp+1;
tflag=1;
}
tvalue=temp*(0.625);//Temperature value is enlarged 10 times, accurate to 1 decimal place
return tvalue;
}
void display(int dat)
{
disdata[0]=dat/1000;
disdata[1]=dat%1000/100;
disdata[2]=dat%100/10;
disdata[3]=dat%10;
write_com(0x80+0x40);
write_data(disdata[0]+0x30);
write_data(disdata[1]+0x30);
write_data(disdata[2]+0x30);
write_com(0x80+0x43);
write_data(0x2e);
write_data(disdata[3]+0x30);
write_data(0xdf); //Write the small circle of Celsius
write_data('C');
}
/*************************Main function****************************/
void main( void )
{
/*The following six lines of program close all IO ports*/
P1DIR = 0XFF;P1OUT = 0XFF;
P2DIR = 0XFF;P2OUT = 0XFF;
P3DIR = 0XFF;P3OUT = 0XFF; P4DIR = 0XFF;
P4OUT = 0XFF;
P5DIR = 0XFF;P5OUT = 0XFF;
P6DIR = 0XFF;P6OUT = 0XFF;
uchar i;
WDTCTL = WDTPW + WDTHOLD; //Turn off the dog
P6DIR |= BIT2;P6OUT |= BIT2; //Turn off the level conversion
/*------Select the system main clock as 8MHz-------*/
BCSCTL1 &= ~XT2OFF; //Turn on the XT2 high frequency crystal oscillatordo
{
IFG1
&= ~OFIFG; //Clear crystal failure flag
for (i = 0xFF; i > 0; i--); //Wait for 8MHz crystal to start oscillating
}
while ((IFG1 & OFIFG)); //Does the crystal failure flag still exist?
BCSCTL2 |= SELM_2 + SELS; //MCLK and SMCLK select high-frequency crystal oscillator
//Counting clock selects SMLK=8MHz, which is 1MHz after 1/8 division
TACTL |= TASSEL_2 + ID_3;
//Open global interrupt
_EINT();
LcdReset();
zifuchuan("Temperature is:");
//Loop reading display
while(1)
{
display(Do1Convert());
}
}
Previous article:IIC bus protocol written in 430: 24C02 power-off memory program (digital tube display)
Next article:MSP430 ADC12 1602 display
- Popular Resources
- Popular amplifiers
- MCU C language programming and Proteus simulation technology (Xu Aijun)
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Single-chip microcomputer technology and application - electronic circuit design, simulation and production (edited by Zhou Runjing)
- Principles and Applications of Single Chip Microcomputers and C51 Programming (3rd Edition) (Xie Weicheng, Yang Jiaguo)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Digital Circuits and Digital Systems: Experiments and Course Design Training Course
- AT91SAM7S256 serial port data loss
- Using DSP motor speed measurement and detailed explanation
- Low-power Qorvo chips enhance connectivity and reliability for new Luna smart home system
- Highlights of Bluetooth Mesh Technology
- ST NUCLEO-G071RB evaluation serial port printing and LED flashing
- What is an ISO Connector?
- B-L4S5I-IOT01A development board has WIFI, Bluetooth, NFC, microphone, distance measurement, magnetometer, thermometer and hygrometer.
- cc2640 data processing problem
- Summary of Common/Uncommon IOT Protocols