The memory function of TV channels, the setting of traffic light countdown time, and the memory function of outdoor LED advertisements may all use storage devices such as EEPROM. The advantage of this type of device is that the stored data can not only be changed, but also the data is saved and not lost after power failure, so it is widely used in various electronic products.
The example program in this class is a bit like an advertising screen. After power-on, the first line of 1602 displays 16 characters starting from address 0x20 of EEPROM, and the second line displays 16 characters starting from address 0x40 of EEPROM. We can change the data inside EEPROM through UART serial communication, and at the same time change the content displayed by 1602. The next time we power on, it will directly display the updated content.
All the relevant contents of this program have been mentioned before. However, this program is reflected in a comprehensive application ability. This program uses the comprehensive application of multiple functions such as 1602 LCD, UART serial communication, EEPROM read and write operations. It is very simple to write a small light, but if we want to really learn the microcontroller well, we must learn the application of this comprehensive program and realize the simultaneous participation of multiple modules. Therefore, students, you must carefully build the project, write the program line by line, and finally consolidate it.
/********************************I2C.c file program source code*******************************/
(Omitted here, please refer to the code in the previous section)
/***************************Lcd1602.c file program source code********************************/
(Omitted here, please refer to the code in the previous section)
/****************************eeprom.c file program source code********************************/
(Omitted here, please refer to the code in the previous section)
/*********************************Uart.c file program source code********************************/
(Omitted here, please refer to the code in the previous section)
/********************************main.c file program source code******************************/
#include
unsigned char T0RH = 0; //T0 high byte of reload value
unsigned char T0RL = 0; //Low byte of T0 reload value
void InitShowStr();
void ConfigTimer0(unsigned int ms);
extern void InitLcd1602();
extern void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str);
extern void E2Read(unsigned char *buf, unsigned char addr, unsigned char len);
extern void E2Write(unsigned char *buf, unsigned char addr, unsigned char len);
extern void UartDriver();
extern void ConfigUART(unsigned int baud);
extern void UartRxMonitor(unsigned char ms);
extern void UartWrite(unsigned char *buf, unsigned char len);
void main(){
EA = 1; // Enable general interrupt
ConfigTimer0(1); //Configure T0 timing 1ms
ConfigUART(9600); //Configure the baud rate to 9600
InitLcd1602(); //Initialize LCD
InitShowStr(); //Initial display content
while (1){
UartDriver(); //Call the serial port driver
}
}
/* Process the initial display content of the LCD screen */
void InitShowStr(){
unsigned char str[17];
str[16] = '\0'; //Add the string terminator at the end to ensure that the string can end
E2Read(str, 0x20, 16); //Read the first line of string, the E2 start address is 0x20
LcdShowStr(0, 0, str); //Display on LCD screen
E2Read(str, 0x40, 16); //Read the second line of string, the E2 starting address is 0x40
LcdShowStr(0, 1, str); //Display on LCD screen
}
/* Memory comparison function, compares whether the memory data pointed to by two pointers are the same.
ptr1-pointer 1 to be compared, ptr2-pointer 2 to be compared, len-length to be compared
Return value - if the two memory segments are exactly the same, it returns 1, if they are different, it returns 0 */
bit CmpMemory(unsigned char *ptr1, unsigned char *ptr2, unsigned char len){
while (len--){
if (*ptr1++ != *ptr2++){ //Return 0 immediately when encountering unequal data
return 0;
}
}
return 1; //If all lengths are equal, return 1
}
/* Arrange a string into a fixed length string of 16 bytes, and fill the missing part with spaces
out- output pointer of the sorted string, in- pointer of the string to be sorted*/
void TrimString16(unsigned char *out, unsigned char *in){
unsigned char i = 0;
while (*in != '\0'){ //Copy the string until the end of the input string
*out++ = *in++;
i++;
if (i >= 16){ //When the copy length reaches 16 bytes, force the loop to exit
break;
}
}
for ( ; i<16; i++){ //If less than 16 bytes, fill with spaces
*out++ = ' ';
}
*out = '\0'; //Finally add the end character
}
/* Serial port action function, executes the response action according to the received command frame
buf- received command frame pointer, len- command frame length*/
void UartAction(unsigned char *buf, unsigned char len){
unsigned char i;
unsigned char str[17];
unsigned char code cmd0[] = "showstr1"; //The first line of characters displays the command
unsigned char code cmd1[] = "showstr2"; //The second line of characters displays the command
unsigned char code cmdLen[] = { //Command length summary table
sizeof(cmd0)-1, sizeof(cmd1)-1,
};
unsigned char code *cmdPtr[] = { //Command pointer summary table
&cmd0[0], &cmd1[0],
};
for (i=0; i if (len >= cmdLen[i]){ //First, the length of the received data must be no less than the command length if (CmpMemory(buf, cmdPtr[i], cmdLen[i])){ // Exit the loop when the comparison is the same break; } } } switch (i){ //Execute the corresponding command according to the comparison result case 0: buf[len] = '\0'; //Add a terminator to the received string TrimString16(str, buf+cmdLen[0]); //Trimmed into a 16-byte fixed-length string LcdShowStr(0, 0, str); //Display string 1 E2Write(str, 0x20, sizeof(str)); //Save string 1, starting address is 0x20 break; case 1: buf[len] = '\0'; //Add a terminator to the received string TrimString16(str, buf+cmdLen[1]); //Trimmed into a 16-byte fixed-length string LcdShowStr(0, 1, str); //Display string 1 E2Write(str, 0x40, sizeof(str)); //Save string 2, starting address is 0x40 break; default: //When no matching command is found, a "wrong command" prompt is sent to the host UartWrite("bad command.\r\n", sizeof("bad command.\r\n")-1); return; } buf[len++] = '\r'; //After the effective command is executed, add it after the original command frame buf[len++] = '\n'; //The carriage return and line feed characters are returned to the host computer, indicating that the execution has been completed. UartWrite(buf, len); } /* Configure and start T0, ms-T0 timing time*/ void ConfigTimer0(unsigned int ms){ unsigned long tmp; //temporary variable tmp = 11059200 / 12; //Timer counting frequency tmp = (tmp * ms) / 1000; //Calculate the required count value tmp = 65536 - tmp; //Calculate the timer reload value tmp = tmp + 33; //Compensate for the error caused by interrupt response delay T0RH = (unsigned char)(tmp>>8); //Timer reload value is split into high and low bytes T0RL = (unsigned char)tmp; TMOD &= 0xF0; // Clear the control bit of T0 TMOD |= 0x01; //Configure T0 to mode 1 TH0 = T0RH; //Load T0 reload value TL0 = T0RL; ET0 = 1; // Enable T0 interrupt TR0 = 1; //Start T0 } /* T0 interrupt service function, execute serial port receiving monitoring and buzzer driving*/ void InterruptTimer0() interrupt 1{ TH0 = T0RH; //Reload the reload value TL0 = T0RL; UartRxMonitor(1); //Serial port receiving monitoring } When we were learning UART communication, we also used IO ports to simulate the UART communication process at the beginning, and finally realized communication with the computer. Then, because STC89C52 has a UART hardware communication module inside, we can easily realize the UART communication of the microcontroller by configuring the registers. In the same way, for I2C communication, if there is a hardware module inside the microcontroller, the microcontroller can directly and automatically realize I2C communication, and we don’t need to simulate the start, send, and end of the IO port. After configuring the registers, the microcontroller will do all these tasks. However, our STC89C52 microcontroller does not have an I2C hardware module, so if we use STC89C52 for I2C communication, we must use the IO port to simulate it. Using the IO port to simulate I2C is actually more conducive to our thorough understanding of the essence of I2C communication. Of course, by learning IO port simulation communication, if you encounter a microcontroller with an internal I2C module in the future, it should be easy to handle it. Using the internal hardware module can improve the execution efficiency of the program.
Previous article:MCU EEPROM multi-byte read and write operation timing
Next article:MCU EEPROM page writing
- Popular Resources
- Popular amplifiers
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- 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)
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
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- Using FPGA to realize accurate time keeping when GPS is out of step
- From millimeter wave to low frequency band, what you should know about 5G basic technology
- 125KHz low frequency wake-up wireless receiver chip - Si3933
- Remember! Don’t take these nine types of microcontroller projects!
- Take a break and watch a movie to refresh yourself
- Solving the IoT Landing Dilemma - Alibaba Cloud Hardware Access Best Practices
- [Domestic RISC-V Linux Banfang·Starlight VisionFive trial report] Completed the storage and reading of the sampled person’s data
- Characteristics of embedded software development, design process, and structure of embedded software
- EEWORLD University ---- FPGA Course Basics (Intel Official FPGA Tutorial Series)
- [GD32L233C-START Review] 3. GPIO, EXTI