8051终极指南:C编程与实战解析

5星 · 超过95%的资源 需积分: 15 43 下载量 44 浏览量 更新于2024-07-30 收藏 1.28MB PDF 举报
"8051终极宝典,由Matthew Chapman撰写,中文版由周立功翻译,内容包括8051微控制器的详细介绍,涵盖硬件结构、存储区配置、位操作、寻址方式、处理器状态、电源控制、中断系统、定时器、UART、I²C、A/D转换、看门狗等特性,以及使用Keil C编程的指导,讨论了高级语言编程的优势、数据类型、存储类型、指针、中断服务、再入函数和编程最佳实践等。" 《THE_FINAL_WORD_ON_THE_8051》是Matthew Chapman关于8051微控制器的经典著作,中文版由周立功翻译并由Crafter进行重拍版排版。该书详尽地阐述了8051微控制器的基础知识和高级应用,是学习和理解8051芯片的宝贵资料。 在硬件部分,书中详细介绍了8051的存储区结构,包括CODE区、DATA区、特殊功能寄存器(SFR)、IDATA区和XDATA区。位操作和布尔逻辑部分解释了如何利用8051的位操作能力进行高效编程。寻址方式章节涵盖了直接寻址、间接寻址、立即寻址等多种方式。处理器状态和电源控制部分讲解了如何管理和优化8051的运行状态和能耗。 中断系统是8051的重要特性,书中详细解析了中断优先级寄存器、中断使能寄存器和中断延迟机制,以及外部中断信号的处理。对于内置的定时/计数器,书里列举了四种工作模式,包括模式0、1、2和3。内置UART(通用异步收发传输器)部分则详细描述了模式0、1和2的工作原理。此外,还涉及了I²C总线和A/D转换器的使用,以及看门狗定时器的功能。 在软件开发方面,书籍强调了使用高级语言如C进行8051编程的优势,探讨了C语言的关键概念,如结构、联合、指针和类型定义。针对Keil C,书中解释了数据类型与SFR的映射,存储类型(如DATA、BDATA、IDATA、PDATA和XDATA),以及如何使用指针。中断服务和再入函数的实现是嵌入式编程中的关键,书中提供了相关指导。最后,列出了使用Keil C编程时的一些建议和注意事项,如合理选择变量类型、避免浮点指针和优化内存使用等。 通过这本书,读者不仅可以深入了解8051的硬件架构,还能掌握有效的编程技巧,为实际项目开发提供坚实基础。无论是初学者还是有经验的工程师,都能从中受益。

You are required to write a C program to: • Initialize GPIO peripherals • Initialise UART peripheral for receiving ASCII characters ‘A’ to ‘Z’ at baud 9600 • Initialise an internal array to hold 10 characters with head and tail: CharBuff • Repeat the following:o When data is received on the serial communication port, read ASCII character X, o If received character X is a capital letter add it to CharBuff, else ignore. o While CharBuff is not empty, transmit the morse code of the oldest stored character by blinking the LED (code provided for you). o When CharBuff is full, disable UART RX. o If UART RX is disabled, pushing the button P_B1 will activate it; otherwise, pushing the button does not affect your programme. You are recommended to use interrupt to control UART receiving data and coordinate the operation between CharBuff and P_LD2. 在我的代码基础上完成以上任务#include <platform.h> #include <gpio.h> #include "delay.h" #include "uart.h" #include <stm32f4xx.h> /* ***************NOTE*********************** YOU CAN USE THE IN-UILT FUNCTION delay_ms(HOW_LONG) TO CAUSE A DELAY OF HOW_LONG MILLI SECONDS ******************************************* */ //placeholder /*void uart_rx_isr(uint8_t rx){ }*/ #define MAX 10 int uart_rx_enabled = 1; char CharBuff[MAX]; int head = 0; int tail = 0; int is_full() { return (tail + 1) % MAX == head; } int is_empty() { return head == tail; } void add_to_buffer(char c) { if (!is_full()) { CharBuff[tail] = c; tail = (tail + 1) % MAX; } else { uart_rx_enabled = 0; //uart_disable(); } } void uart_rx_isr(uint8_t c){ if (c >= 'A' && c <= 'Z') { if (!is_full()) { CharBuff[tail] = c; tail = (tail + 1) % MAX; } else { uart_rx_enabled = 0; //uart_disable(); } } } char remove_from_buffer() { char c = CharBuff[head]; head = (head + 1) % MAX; if (uart_rx_enabled == 0 && !is_full()) {//The buffer is not full after removing a char uart_rx_enabled = 1;//enable the Uart RX uart_enable(); } return c; } int main(void) { // Initialise GPIO. gpio_set_mode(P_LD2, Output); gpio_set_mode(P_B1, PullUp); // hardware/peripheral initialisation uart_init(9600); uart_enable(); uart_set_rx_callback(uart_rx_isr);//This callback function is triggered when data is received through the serial port while(1){ if(!is_empty()){ gpio_set(P_LD2, LED_ON); char c = remove_from_buffer(); } } } // ******* ARM University Program Copyright © ARM Ltd 2016 ****************** // ******* Queen Mary University of London Copyright Matthew Tang 2021 ******

2023-06-01 上传