void delay(int ms)
{
while(ms--)
{
uchar i;
for(i=0;i<250;i++)
{
; ; ; ;
}
}
}
/*******************************************************************
Function name: lcd_busy()
Function: Detect LCD busy status.
Input parameters: None
Return value: result When result is 1, busy waiting; when result is 0, idle, can write instruction data
***********************************************************************/
bit lcd_busy()
{
bit result;
LCD_RS = 0;
LCD_RW = 1;
LCD_EN = 1;
delay_ms(1);
result = (bit)(LCD_data&0x80);
LCD_EN = 0;
return(result);
}
/***************************************************************************/
/*Write command data to LCD */
/*RS=L, RW=L, E=high pulse, D0-D7=instruction code. */
/***************************************************************************/
void lcd_wcmd(uchar cmd)
{
while(lcd_busy());
LCD_RS = 0;
LCD_RW = 0;
LCD_EN = 0;
delay_ms(1);
LCD_data = cmd;
delay_ms(1);
LCD_EN = 1;
delay_ms(1);
LCD_EN = 0;
}
/***************************************************************************/
/*Write display data to LCD */
/*RS=H, RW=L, E=high pulse, D0-D7=data. */
/***************************************************************************/
void lcd_wdat(uchar dat)
{
while(lcd_busy());
LCD_RS = 1;
LCD_RW = 0;
LCD_EN = 0;
LCD_data = dat;
delay_ms(1);
LCD_EN = 1;
delay_ms(1);
LCD_EN = 0;
}
/***************************************************************************/
/* LCD initialization settings */
/***************************************************************************/
void lcd_init()
{
LCD_PSB = 1; //Parallel port mode
lcd_wcmd(0x34); //Extended instruction operation
delay_ms(5);
lcd_wcmd(0x30); //Basic instruction operation
delay_ms(5);
lcd_wcmd(0x0C); //Display on and off cursor
delay(5);
lcd_wcmd(0x01); //Clear the LCD display content
delay(5);
}
/****************************************************************/
/* Set the display position X: number of rows Y: number of columns */
/****************************************************************/
void lcd_pos(uchar X,uchar Y)
{
uchar pos;
if (X==0)
{X=0x80;}
else if (X==1)
{X=0x90;}
else if (X==2)
{X=0x88;}
else if (X==3)
{X=0x98;}
pos = X+Y ;
lcd_wcmd(pos); //display address
}
/****************************************************************/
/* Display the character (string) at the set position */
/****************************************************************/
void zifu_dis (uchar X,uchar Y,uchar *dis)
{
uchar i;
lcd_pos(X,Y);
i = 0;
while(dis[i] != '')
{ //Display characters
lcd_wdat(dis[i]);
i++;
}
}
/**************dis_12864.h***************/#ifndef __LCD12864_H__#define __LCD12864_H__
#include "delay.h" #include /*Function declaration*/ void delay(int ms); void lcd_init(); void beep(); void dataconv(); void lcd_pos(uchar X,uchar Y); //Determine the display position void zifu_dis (uchar X,uchar Y,uchar *dis); #endif 3. ds1302 clock Just give the program directly, and you can search for the corresponding information online. /********************************************************************************** THE REAL TIMER DS1302 DRIVER LIB COPYRIGHT (c) 2005 BY JJJ. -- ALL RIGHTS RESERVED -- File Name: DS1302.h Author: Jiang Jian Jun Created: 2003/7/21 Modified: NO Revision: 1.0 re *******************************************************************************/ #include "ds1302.h" /******************************************************************************* Function name: DS1302InputByte(unsigned char d) Function: Write a byte to the real-time clock (internal function) Input parameter: d data to be written Return value: None *******************************************************************************/ void DS1302InputByte(unsigned char d) { unsigned char i; ACC = d; for(i=8; i>0; i--) { DS1302_IO = ACC0; //equivalent to RRC in assembly DS1302_CLK = 1; DS1302_CLK = 0; ACC = ACC >> 1; } } /******************************************************************************* Function Name: DS1302OutputByte(void) Function: Real-time clock reads a byte (internal function) Input parameters: None Return value: data read by ACC *******************************************************************************/ unsigned char DS1302OutputByte(void) { unsigned char i; for(i=8; i>0; i--) { ACC = ACC >> 1; // Equivalent to RRC in assembly ACC7 = DS1302_IO; DS1302_CLK = 1; DS1302_CLK = 0; } return(ACC); } /******************************************************************************* Function name: Write1302(unsigned char ucAddr, unsigned char ucDa) Function: Write data to the specified address of the real-time clock Input parameter: ucAddr address to write data ucDa Data to be written Return value: None *******************************************************************************/ void Write1302(unsigned char ucAddr, unsigned char ucDa) { DS1302_RST = 0; DS1302_CLK = 0; DS1302_RST = 1; DS1302InputByte(ucAddr); // address, command DS1302InputByte(ucDa); // Write 1Byte data //DS1302_CLK = 1; DS1302_RST = 0; } /******************************************************************************* Function name: Read1302(unsigned char ucAddr) Function: Read data from a certain address of ds1302 Input parameter: ucAddr The address of the data to be read Return value: ucData read data *******************************************************************************/ unsigned char Read1302(unsigned char ucAddr) //Read data from a certain address of ds1302 { unsigned char ucData; DS1302_RST = 0; DS1302_CLK = 0; DS1302_RST = 1; DS1302InputByte(ucAddr|0x01); // address, command ucData = DS1302OutputByte(); // Read 1Byte data //DS1302_CLK = 1; DS1302_RST = 0; return(ucData); } /******************************************************************************* Function name: DS1302_SetProtect(bit flag) Function: Write protection or not Input parameter: flag Return value: None Others: When flag is 1, the highest bit of the control register corresponding to 0x8E is 1, and write protection is enabled *******************************************************************************/ void DS1302_SetProtect(bit flag) //Write protection or not { if(flag) Write1302(0x8E,0x80); else Write1302(0x8E,0x00); } /******************************************************************************* Function Name: DS1302_SetTime(unsigned char Address, unsigned char Value) Function: Write time to the specified register Input parameter: Address register address Value The time to be written (hex code) Return value: None Others: You can use macros to define the addresses of year, month, hour, etc. *******************************************************************************/ void DS1302_SetTime(unsigned char Address, unsigned char Value) // Set time function { DS1302_SetProtect(0); Write1302(Address, ((Value/10)<<4 | (Value%10))); //Convert hex code to BCD code } /******************************************************************************* Function name: DS1302_GetTime(SYSTEMTIME *Time) Function: Read the date and time and store them in the Time structure Input parameter: *Time Address of the structure to store date and time Return value: None *******************************************************************************/ void DS1302_GetTime(SYSTEMTIME *Time) { unsigned char ReadValue; ReadValue = Read1302(DS1302_SECOND); Time->Second = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F); //Convert octal to decimal ReadValue = Read1302(DS1302_MINUTE); Time->Minute = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F); ReadValue = Read1302(DS1302_HOUR); Time->Hour = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F); ReadValue = Read1302(DS1302_DAY); Time->Day = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F); ReadValue = Read1302(DS1302_WEEK); Time->Week = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F); ReadValue = Read1302(DS1302_MONTH); Time->Month = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F); ReadValue = Read1302(DS1302_YEAR); Time->Year = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F); } /******************************************************************************* Function name: DateToStr(SYSTEMTIME *Time) Function: Convert the read date into a character form that is easy to display Input parameter: *Time structure to store characters Return value: None *******************************************************************************/ void DateToStr(SYSTEMTIME *Time) { Time->DateString[0] = Time->Year/10+0x30; //·Separate the ones and tens Time->DateString[1] = Time->Year%10+0x30; Time->DateString[2] = '-'; Time->DateString[3] = Time->Month/10+0x30; Time->DateString[4] = Time->Month%10+0x30; Time->DateString[5] = '-'; Time->DateString[6] = Time->Day/10+0x30; Time->DateString[7] = Time->Day%10+0x30 ; //When using LCD to display, it needs to be converted into ASCII code, so 0x30 is added. If it is displayed with a digital tube, it does not need to be added Time->DateString[8] = '';
Previous article:51 MCU (Part 9). 51 MCU simple project - perpetual calendar and temperature acquisition
Next article:51 single chip perpetual calendar
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- How to change the inertia ratio of Panasonic servo drive
- What is the inertia ratio of the servo motor?
- Is it better for the motor to have a large or small moment of inertia?
- What is the difference between low inertia and high inertia of servo motors?
- What will happen if the servo motor inertia is insufficient?
- How to select parameters for servo motor inertia size
- The difference between the servo motor moment of inertia and the load moment of inertia
- How to calculate the inertia of servo motor and reducer
- What is the difference between a servo press and a normal press?
- CGD and Qorvo to jointly revolutionize motor control solutions
- Help others and achieve self-realization——EEWORLD Q&A List (11th issue)
- China's chip manufacturing: After ten years of drinking ice, the passion is hard to cool down
- [Shanghai Hangxin ACM32F070 development board + touch function evaluation board] 03. Different LCD display implementation methods
- Simple MicroPython UASYCIO buzzer driver
- [Topmicro Intelligent Display Module] Part 2: Continuous picture display evaluation, YYDS!
- Flow measurement using ultrasonic technology
- ST Motor Review_bygyp1
- TI's product pins have copper leakage
- Problem of assigning initial value to timer of C51 MCU
- ZTE Hardware Written Test