顯示具有 單片機 標籤的文章。 顯示所有文章
顯示具有 單片機 標籤的文章。 顯示所有文章

星期六, 8月 01, 2009

MICROCONTROLLERS IN PRACTICE

內容大綱

Introduction

1. Resources of Microcontrollers 

1.1 In this Chapter 

1 1.2 Microcontroller Architectures

1 1.3 The Memory Map

1.4 CPU Registers

1.4.1 The CPU Registers of HC11 

1.4.2 The CPU Registers of AVR

1.4.3 The CPU Registers of 8051 

1.5 The Peripheral Interfaces

1.6 The Interrupt System

1.6.1 General Description of the Interrupt System

1.6.2 Distinctive Features of the Interrupt System of HC11

1.6.3 Distinctive Features of the Interrupt System of AVR

1.6.4 Distinctive Features of the Interrupt System of 8051

1.7 Expanding the Resaurces of Microcontrollers

1.7.1 HC11 Operating with External Bus

1.7.2 AT90S8515 Operating with External Bus

1.7.3 8051 Operating with External Bus

1.8 Exercises

2 Using the Digital I/O Lines

2.1 In this Chapter 

2.2 Overview of the Parallel I/O System

2.3 Electrical Characteristics of the I/O Lines

2.4 Controlling the I/O Lines by Software

2.5 Exercises

3 Using the Asynchronous Serial Interface

3.1 In this Chapter 

3.2 Synchronous vs. Asynchronous Communication

3.3 Error Detection in Asynchronous Communication

3.4 The General Structure of the Asynchronous Serial Communication Interface 

3.5 The Serial Communication Interface of 68HC11F1

3.6 The Asynchronous Serial Communication Interface of AVR Microcontrollers 

3.7 The Asynchronous Serial Interface of 8051

3.8 Programming the Asynchronous Serial Interface

3.8.1 Programming the SCI of HC11 

3.8.2 Programming the UART of AT90S8535

3.8.3 Programming the UART of 8051

3.9 Hardware Interfaces for Serial Communication

3.9.1 The RS232 Interface

3.9.2 Differential Communication. The Interfaces RS422 and RS485

3.9.3 The Current Loop Interface 

3.10 Basic Principles of Networking with Microcontrollers, Using the Asynchronous Serial Interface

3.11 Exercises

4 Using the Synchronous Serial Interface SPI

4.1 In this Chapter 

4.2 General Description of the SPI

4.3 The SPI of HC11 Microcontrollers

4.4 The SPI of the AVR Microcontrollers

4.5 Examples of Using The SPI

4.5.1 Using the SPI To Connect Two Microcontrollers

4.5.2 Expanding the I/O Space Using the SPI

4.6 Exercises

5 Using The I2C Bus

5.1 In this Chapter 

5.2 The Principles of Implementation of the I2C Bus

5.2.1 The Start Transfer Condition

5.2.2 The Data Transfer on the I2C Bus

5.2.3 The ACK Bit

5.2.4 The STOP Condition

5.3 A Software Implementation of the I2C Protocol

5.4 Accessing 24C256 Memory Devices

5.5 Exercises

6 Using the MCU Timers

6.1 In this Chapter 

6.2 The General Structure and Functions of the Timer System

6.3 Distinctive Features of the General-Purpose Timer of HC11

6.3.1 The Control and Status Registers of the HC11 Timer

6.3.2 Exercises Regarding the Use of the General-Purpose Timer of HC11

6.4 Distinctive Feature of the Timer of the AVR Microcontrollers

6.4.1 The 8-Bit Timer/Counter Timer0

6.4.2 The 16-Bit Timer/Counter Timer1

6.4.3 Synopsis of the Timer I/O Registers of AT90S8115

6.4.4 Summary of the Unique Features of the AVR Timer

6.4.5 Exercises Regarding the Use of AVR Timers

6.5 Distinctive Features of the Timer System of the 8051 Microcontrollers 

6.5.1 The Control and Status Registers of the Timer

6.5.2 Description of the Timer Operating Mode 0

6.5.3 Description of the Timer Operating Mode 1

6.5.4 Description of the Timer Operating Mode 2

6.5.5 Description of the Timer Operating Mode 3

6.5.6 Using Timer1 as a Baud Rate Generator

6.5.7 Exercises for Programming the 8051 Timer

6.6 PWM Timers. Principles of Operation

6.7 Watchdog Timers

6.7.1 The Watchdog of HC11

6.7.2 The Watchdog of AT90S8515 

7 Interfacing to Analog Signals

7.1 In This Chapter 

7.2 The Analog Comparator

7.3 The General Structure of the A/D Converter Subsystem

7.4 The A/D Converter of the HC11 Family of Microcontrollers

7.5 Exercises on Programming the A/D Converter of HC11

7.6 The A/D Converter of the AVR Microcontrollers

7.7 Exercises on Programming the A/D Converter AT90S8535

7.8 Digital-to-Analog Converters 

7.8.1 The Principles of the D/A Conversion

7.8.2 Exercise on Using MX7224 

8 Using the Internal EEPROM Memory

8.1 In this Chapter 

8.2 Overwiew of the EEPROM Subsystem

8.3 The EEPROM Memory and the CONFIG Register of HC11

8.3.1 The Registers Controlling the EEPROM of HC11

8.3.2 Software Routines to Erase and Write the EEPROM

8.3.3 The CONFIG Register

8.4 The EEPROM Memory of the AVR Microcontrollers

8.4.1 The Registers of the Interface with the EEPROM Memory

8.4.2 Software Routines to Read and Write EEPROM 

9 HC11 Development Board 

9.1 In this Chapter 

9.2 Description of the Hardware Module

9.3 Assembling and Testing the Module

9.4 Description of the Software Components

9.5 Exercises

10 AVR Development Board 

10.1 In this Chapter 

10.2 The Hardware

10.3 Testing the Circuit

10.4 The Software

10.5 Exercises

11 8051 Development Board 

11.1 In this Chapter 

11.2 Hardware

11.3 The Software

11.3.1 Installing the Cross-Assembler

11.3.2 Writing and Testing Simple 8051 Programs

11.3.3 Loading and Executing Programs in the External Ram Memory

11.4 Exercises

12 Digital Voltmeter with RS232 Interface

12.1 In this Chapter 

12.2 The Hardware

12.3 The Software

12.4 Exercises

13 Simple RS485 Network with Microcontrollers

13.1 In this Chapter 

13.2 The Hardware

13.2.1 The RS232-to-RS485 Converter

13.2.2 The Digital Input Module 

13.2.3 The Analog Input Module 

13.2.4 Using the AVR Development Board to Emulate The SLD and SLA Modules

13.3 The Software

13.3.1 Description of the Communication Protocol

13.3.2 The Software for the SLD Module

13.3.3 The Software for theMASTER Device

13.4 Exercises

14 PI Temperature Controller

14.1 In this Chapter 

14.2 Basic Concepts

14.3 Hardware Implementation of a Microcontroller-Based Temperature Controller

14.4 Software Implementation of a PI Temperature Controller

15 Fuzzy Logic Temperature Controller

15.1 In this Chapter 

15.2 The Principles of Fuzzy Control

15.3 A Microcontroller Implementation of a Fuzzy Controller

16 Remote Relay Controller over Telephone Lines

16.1 In this Chapter 

16.2 Description of the Hardware Solution

16.3 Description of the Software 

APPENDICES

A.1 Glossary of Terms 

A.2 Description of the Registers of 68HC11F1

A.3 HC11 Instruction Set

A.4 An Example of Expanded Structure with HC11

A.5 Using HC11 in Bootstrap Mode 

A.6 The Registers of AT90S8535 

A.7 AVR Instruction Set

A.8 AT90S8515 Operating with External RAM

A.9 In-system Programming the AVR AT90S8535

A.10 The Special Function Registers of 8051

A.11 8051 Instruction Set

A.12 An Example of 8051 Operating with External Bus

A.13 Programming the Internal Memory of 8051

A.14 SPI Seven-Segment Display Units

A.15 Description of the Software Utility ASMEDIT

B.1 Contents of the Accompanying CD

B.2 Recommended Readings and Web References

網路上找到的Ebook鏈結

如果覺得內容不錯請購買正本

淺談單片機程式設計中的“分層概念”

        隨便寫下的一點東西,本來打算去發表,不過想想還是算了,不是什麼重要的東西,不過這個東西確實很有用。文章懶得去整理和修改了,隨便看看吧。

        軟體分層的概念,並不是什麼神秘的東西,事實上很多程式開發的工程師本身自己也會在用。看了不少文章都發現沒有提及這個東西,然而分層結構確是很有用的東西,參透後會有一種恍然大悟的感覺。如果說我不懂LCD怎麼驅動,那好辦,看一下datasheet,參考一下別人的程序,很快就可以做出來。但是如果不懂程序設計概念的話,會給你程式開發的過程中帶來很多很多的困惑。

        參考了市面上各種各樣的嵌入式書籍,MCS-51,AVR ,ARM 等都有看過,但是沒有發現有哪本是介紹設計思想的,就算有也是鳳毛麟角。寫程式不難,但是程式怎麼樣才能寫的好,寫的快,那是需要經驗的積累。結構化模組化的程序設計概念,是程式開發者最基本的要求。然而怎麼將這個抽象的概念運用到實際的程式開發當中?那需要在程式開發的過程中經歷不少磨難,將一些東西總結出來,抽象昇華為理論,對經驗的積累和技術的傳播都大有裨益。所以在下出來獻醜一下,總結一些東西。

        就我個人的經驗而談,有兩個程式設計概念是非常重要的。

        第一個就是“polling I/O的設計思想”,這個對實際中解決MultiTask問題非常有用,通常可以用這個東西來判斷一個人是單片機學習者,還是一個單片機工程師。這個必須掌握。由於網上介紹這個的文章也不少,所以這裏就不多說了。

"這個部分應該是提到 scheduling time slices的概念,利用time slices將時間切割,排程器可以利用polling的方式將資源分配給多項task使用,達到多工的目的。"


       第二個就是我今天想說的主題“分層屏蔽的設計思想”。下面用掃描鍵盤程序例子作為引子開始,引出今天說的東西。

問題的提出

       單片機學習板一般為了簡單起見,將按鍵分配的很好,例如整個 4*4 的鍵盤矩陣分配到 P1 口上面,剛好8條控制線。這樣的話程序也非常好寫。只需要簡單的表示如下:

KEY_DAT = P1;

鍵盤端口的數據就讀進來了。

        當然,現實中沒有這麼好的事情。在實際的應用當中,單片機腳位的使用相當多元,這跟那些所謂的單片機學習板就有很大的差別了。

        另外一個原因,一般設計來說,是“軟件配合硬件”的設計流程,簡單點就是說,先確定好硬體線路圖,硬體線路走線,最後才是軟件的開發,因為硬件修改起來比較麻煩,相對來說軟件修改的時候比較好改。這個就是中國傳統的陰陽平衡哲學原理。硬件設計和軟件設計本來就是魚與熊掌的關係,兩者不可兼得。方便了硬體設計,很可能給寫軟件帶來很大的麻煩。反過來說,方便了軟件設計,硬件設計也會相當的麻煩。如果硬件設計和軟件設計同時方便了,那只有兩種可能,一是這個設計方案非常簡單,二是設計師已經達到了一個非常高的境界。我們不考慮那麼多情況,單純從常用的實際應用的角度來看問題。

        硬體為了走線的方便,很多時候會可能將IO口分配到不同的腳位上面,例如上面說的4*4鍵盤,8根線分別分配到 P0 P1 P2 P3 上面去了。那麼,開發板的那些掃描鍵盤程序可以去見鬼了。怎麼掃按鍵?我想起了我剛開始學習的時候,分成3段非常相似的程序,一個一個按鍵的掃描的經歷......

        或許有人不甘心,“那些東西我花了很長時間學習的,也用的好好的,怎麼能說一句不用就不用?”雖然有點殘忍,但是我還是想說“兄弟,接受現實吧,現實是殘酷的......”

       不過,人跟低等動物的差別,就是人會創造,在碰到困難的時候會想辦法解決,於是我們開始了沉思......

       最後我們引入初中數學學的“映射”的概念來解決問題。基本思想就是,將不同腳位端口的按鍵映射到相同控制程序上面。

       這樣按鍵掃描程序就分成3個層次了。

1)最底層的是硬件層,完成硬體腳位掃描,20ms延時消抖,將腳位的數據映射到一個KEY_DAT暫存器上面,KEY_DAT作為對上層驅動層的一個接口。

2)中間的一層是驅動層,驅動層只對 KEY_DAT 暫存器的數值進行操作。簡單點說,我們無論底層的硬件是怎麼接線的,在驅動層都不需要關心,只需要關心 KEY_DAT 這個暫存器的數值是什麼就可以了。這樣出來的間接效果就是“屏蔽了底層硬件的差異”,所以驅動層寫的程序就可以通用了。

       驅動層的另外一個功能是為上層提供訊息的接口。我們用了類似window程序的訊息的概念。這裏可以提供一些按鍵消息,例如:按下訊息,鬆開訊息,長按鍵訊息,長按鍵的時候的步進訊息,等等。

3)應用層。這裏就是根據項目的不同分別寫按鍵功能程序,屬於最上層的程序。它使用的是驅動層提供的訊息接口。在應用層寫程序的思想就是,我不管下層是怎麼工作的,我只關心按鍵訊息。有按鍵訊息來的時候我就執行功能,沒有訊息來的時候,我就什麼也不做。

       下面用一個簡單的常用的例子,說明我們這個設計思想的用法。

       秒表調整時間的時候,要求按著某個按鍵不放,時間能連續的向上增加。這個東西很實用,實際的家電中用途很廣泛。

       在看下面的東西之前,大家可以想一下,這東西難嗎?相信大家都會很響亮的回答,“不難!!”,然而我再問:“這東西麻煩嗎?”我相信很多人肯定會說“很麻煩!!” 這不禁讓我想起開始學單片機的時候寫這種按鍵的那程序,亂七八糟的結構。如果不相信的話,可以自己用51寫一下哦,那樣就更加能體會本文說的分層結構的優越性。

項目要求:

      兩個按鍵,分別分配在P10 和P20,分別是“加”“減”按鍵,要求長按鍵的時候實現連續加和連續減的功能。

實戰:

假設:

     按鍵上拉,沒有按鍵的時候高電位,有按鍵的時候低電位,另外,為了突顯問題,這裏沒有將延時消抖的程序寫上去,在實際項目中應該加上。C語言函數參數的傳遞多種多樣,這裏作為例子,用了最簡單的全域變數來傳遞參數,當然你也可以用 unsigned char ReadPort(void) 返回一個讀鍵結果,甚至還可以 void ReadPort(unsigned char *pt) 用一個指針變量傳遞地址而達到直接修改變量的目的。方法是多種多樣的,這個決定於每個人的程序風格。

1)開始寫硬件層程序,完成映射

#define KYE_MIN 0X01

#define KEY_PLUS 0X01

unsigned char KeyDat;

void ReadPort(void)
{
if (P1 & KEY_PLUS == 0 ){
KeyDat |= 0x01 ;
}

if (P2 & KEY_MIN == 0 ){

KeyDat |= 0x02 ;}

}

      C語言應該很容易看懂吧?如果 KEY_PLUS 按下,P10口讀到低電位,則 P1 & KEY_PLUS 的結果為 0 ,滿足if 的條件,進入KeyDat |= 0x01 是將 KeyDat 的bit0 設定為一,也就是說,將 KEY_PLUS 映射到 KeyDat 的 bit0

KEY_MIN 是同樣的道理映射到 KeyDat 的 bit1,如果 KeyDat 的 bit0 為 1 ,則說明 KEY_PLUS 按下,反則亦然。

      不需要想的很神秘,映射就是這麼一回事。如果還有其他按鍵的話,用同樣辦法,將他們全部映射到 KeyDat 上面。

2)驅動層程序編寫

      如果將 KeyDat想象成 P1 口,那麼這個跟學習板那標準的掃描程序不就是一樣了嗎?對的,這個就是底層映射的目的了。


3)應用層程序編寫

根據訊息

      硬件層是必須分離出來,然而驅動層和應用層的要求就不那麼嚴格了,事實上一些簡單的項目沒有必要將這兩層分離開來,根據實際應用靈活應對就可以了。其實這樣寫程序是很方便移植的,根據板子的不同而適當的修改一下硬件層那個 ReadPort 函數就完成了,驅動層和應用層很多代碼可以不經過修改直接用,很能提高開發效率的。當然這個按鍵程序會存在一定的問題,特別是遇到常閉按鍵和點觸按鍵的混合使用的場合。這個留給大家自己去想了,反正問題總是能找到解決辦法的,盡管方法有好有壞。

結束語

      以按鍵為媒介,介紹了程序設計當中的“分層屏蔽”的思想的原理和應用,按鍵只是一個例子,其實分層的思想普遍存在著程序設計當中。細心留意一下的話發現其實window,linux,網路的tcp/ip 結構全部都是分層的。這東西不是繡花枕頭,而是實際用在工程上面的,只是平時不多見文章的介紹,或者沒有人特意這樣來總結,又或者是有經驗的工程師作為藏在心中的法寶吧,這個就不得而知。不過好東西應該共享,菜鳥應該共勉,一起來學飛吧。

轉載自