201107970 六、發明說明: 【發明所屬之技術領域】 本發明係有關於記憶體’尤其更有關於微控制器所使 【先前技術i 微控制器單元(Micro con_ Unit,MCU)是一種將中央 處理器、記憶體、計數器及輸出/入介面等元件通通匯集 在同-塊積體電路晶片上之微型電腦。而目 、 式2統(Embedded System),例如收音機、電冰箱、冷^機 等家電,皆係使用微控制器單元來實現。 傳統的微控制器單元,係使用一唯讀記憶體 Only Memory,_來儲杨體(Firmware)及系統程式,並 透k P返機存取5己憶體(Rand〇m ACCess Mem〇ry,膽)來在 微控制器單元使用過程中所產生之資料。唯,隨著消 需求的改變,各式系統慢慢複雜化’原本用以暫存資料之 疆的空間早已不敷使用。再者,因麵係為-種揮發性 yolatUe)之記憶體,於微控制器單元斷電後,所暫存之 貧料即會消失’因此,這種使用模式雖然較為節省成本, 但因難以滿足消費者之需求,&已漸漸的被市場所淘汰。 如上所述,例如一使用者透過一收音機收聽廣播電台 時’可使用收音機内部微控制器單元所設置之疆來^ 喜歡之電纟鮮。但如果㈣收音機之插職除,或 除該收音機之電池後,所儲存之電台頻率即隨著消失。如 201107970 此-來’使用者必須於重新開啟收音機後,再次設置所有 電台頻率’並一一重新儲存’實為不便。也因此,目前已 慢慢淘汰此種做法,而係另外設置一非揮發性 (non-volatile)之記憶體,來儲存需持續保存之資料。 而使用非揮發性記憶體來儲存資料,目前-般的做法 。:為透過-斷電偵測系統及一斷電延遲系統來與微控制器 單元互相配合。更詳細而言,目前做法係利用該斷電制 系統來伯測斷電狀況之發生(例如谓測電子裝置拔除電源 的瞬間),並於斷電後,由該斷電延遲系統提供該微控制 益早疋持續動作一短暫時間。如此一來,該微控制器單元 即可於此段時間内,將斷電前欲儲存之資料儲存至該非揮 發性之記憶體中。藉以令該微控制器單元重新啟動後 讀取斷電前儲存之資料,保存使用者所需之資訊。 唯,上述作法雖然可成功於斷電後儲存資料,但使用 此-做法之系統,係需額外配置該斷電伯測系統及該斷電 延遲系統,對產品開發廉商來說,係需付出相當程度之額 外成本。再者,該斷電延遲系統係僅可 控制器單元動作非常短暫的時間(原則上為一至=鐘f ,於該段時間内該微控制器單元可完錢存之資料實相+ 有限。因此’該種作法實際上是田 低之作法,實有改良之必要。 成本同但達成功效 “例如由與本發明之相同發明人所提出之另一申請案, 台灣申請案號為098125118之智慧型燈光控制模組與方法。 係可於燈具之開關電源完全關閉後(不持續耗費電源), I201107970 丨! m關之啟閉_欠數及燈具之亮度等資料,藉以依該些資 «進行燈具之智慧型㈣。例如,依_之狀次數資料 ,、自動判斷此次開關開啟後,係要進行燈光亮度之調整, 或是要設定燈具之自動開啟/關閉時間等等。然而’燈具 身之裝le即扃一疋之生產成本,因此若以上述使用斷電 、、'J系、.先及斷電延遲系統之方法來實現,可能會使得產品 =生產成本過於昂貴。再者,若能提供—種新穎之技術, J微控制器單元於斷電前能夠儲存之資料數量增加,則亦 #錢該申請案中之燈具,具有更多延伸之功能與應用。 【發明内容】 f發明之主要目的,在於提供—種微控制器使用之記 憶體存取方法,係可於微控制器動作時持續將資料寫入記 憶體’令資料於完全斷電後仍然持續存在 重新啟動後,得以隨時讀取所需之任一筆資料。 •〜為達上述目的’本發明係於微控制器動作時,持續將 =料^非揮發性記憶體中,以便於斷電之後,微控制 以“保留之資料仍能繼續存在,而於讀取時,將資料儲 存於非揮發性5己憶體中之位址錯存至微控制器中之一暫存 記憶體’藉以令微控制器可以隨時讀取所儲存之任一筆資 料。 對照先前技術,本發明所能達成之功效在於,不需額 外設置斷電_系統與斷電延遲系統來配合微控㈣,而 係於使用時即持續將資料寫入非揮發性記憶體,藉此以低 201107970 成本之手段,達成令微控制器斷電後,資料仍然存在之目 的,且因資料為持續寫人記憶體,而非於偵測到斷電後才 寫入部分資料,因此可儲存之資料量亦可大幅增加。 【實施方式】 ,茲就本發明之一較佳實施例,配合圖式,詳細說明如 後。 請參閱第一圖A及第一圖B,分別為本發明之二具 體實如例之方塊圖。如圖所示,本發明之微控制器,主要 係包括一中央處理單元11、一系統記憶體12及一暫存記憶 體13,並且包括一用以儲存資料之資料記憶體14。而視該 資料記憶體14為内建或外部連接而定,該微控制器之晶^ 係可分為第一圖A中之微控制器1〇(内建)或第一圖β中 之微控制H 1G’(外部連接)。而為方便說明,下面將統 一使用第-圖A中,内建該資料記憶體14之該微控制器1〇 晶片來舉例說明。 該系統記憶體12主要用以儲存該微控制器1〇動作所需 之物體及系統程式,因此為使韌體與系統程式永遠存在, 且為保護其不被任意更動而造成系統執行之錯誤,主要係 使用唯讀δ己憶體(Read Only Memory,ROM)來實現。該暫存記 憶體13則用以儲存該微控制器1〇運作過程中所產生之不需 持續保存之資料,該些資料於該微控制器1〇斷電再開啟後 並不會再度被使用,因此該暫存記憶體13主要係以隨機存 取記憶體(Random Access Memory,RAM)來實現。唯此僅為本 201107970 發明之一較佳實施例,不可以此為限。 該資料記憶體14係可為該微控制器10晶片所内建或由 外部連接,主要用以儲存該微控制器會持續使用’或有 價值之資料,且需於該微控制器1〇斷電後仍然存在’以便 該微控制器10重新啟動後能隨時讀取使用。因此’係以一 非揮發性(non-volati le)之記憶體來實現’且較佳者可為一 電子可抹除可規劃唯讀記憶體(Electrically-Erasable Programmable Read-Only Memory,EEPROM),或一快閃記憶體 φ (Flash Memory),但不以此為限。而上述之該三記憶體,係 透過該微控制器10中之該中央處理單元11,藉由一位址匯 流排(Address Bus)bl、一資料匯流排(Data Bus)b2及一控制 匯流排(Control Bus)b3來進行記憶體之控制,以及資料之 搜尋與存取。 接著請參閱第二圖’為本發明之一較佳具體實施例之 資料儲存流程圖,如圖所示,本發明之資料儲存流程,可 φ分為資料儲存前之抹除步騍S21 ,及抹除後之資料儲存步 驟S22 °為節省開發成本,並控制整個積體電路之面積不 會過大’因此一般微控制器1〇所内建或外部連接之記憶體 容量,並不會隨著技術之進步而無止盡的增大。如此一來 ,用以儲存資料之該資料記憶體14的儲存空間,並不一定 足夠該微控制器1〇持續寫入資料。因此,於資料儲存前, 4微控制器ίο中之該中央處理單元η,可先控制該資料記 憶體14進行步驟S21之抹除動作。首先,係設定該 資料記 憶體14之-第-起始位址(步驟s21〇),藉以令該中央處 201107970 理單元11得到該資料記憶體14中所儲存資料之源頭。接著 ,再自該第一起始位址開始抹除該資料記憶體14中所儲存 之部分或全部資料(步驟S212)。 子 而於該步驟S21中抹除該資料記憶體14後,隨即進行 儲存資料之該步驟S22。該微控制器10欲寫入資料時,^ 先設定該資料記憶體14寫入資料之一第二起始位址(步驟 S220),令该中央處理單元1丨得以指向該資料記憶體μ中 可寫入>料之第一個空白位址。而因已事先抹除該資料記 憶體14之内容,因此通常該第二起始位址即為該資料記憶 體14之源頭位址,但不以此為限。接著,透過該中央處理 早疋11,將資料寫入所指向之該資料記憶體14中之位址( ッ驟S222),並於將資料寫入後,將所指向之位址加一( 步驟S224) ’待下一筆資料寫入該資料記憶體14之下一個 空白位址。 接著,該中央處理單元11係判斷於該步驟S222中寫入 之資料,疋否為所需儲存之最後一筆資料(步驟S226)。 右疋,即完成儲存動作,開始執行其他待執行之程式(步 驟S228),同時等待下一個資料儲存命令。而若於該步驟 S226中’判斷該所寫人之資料並非最後-筆需儲存之資料 則回到s玄步驟S222,繼續寫入下一筆資料,直到所需儲 存之資料全部儲存完畢為止。 欠接下來凊參閱第三圖,為本發明之一較佳具體實施例 之貝料讀取流程圖。首先’係於該微控制器1〇斷電後重新 啟動’並第一次讀取該資料記憶體14之内容時,先行設定 201107970 遠資料把憶體14之—第三起始位址(步驟S31〇),該第三 起始位址可為該資料記憶體14之源頭位址,但不以此為限 ,令該中央處理單元指向該資料記憶體14中,第一筆資 料所儲存之位址。接著,讀取目前所指向之該資料記憶體 14位址之内容(步驟S312),並於讀取後,判斷該所指向 之位址内容_,是否存有資料(步驟S314)。若有,則將 5亥筆貝料於該資料記憶體14中所儲存之位址,寫入該暫存 記憶體13中之-指定位址(步驟S316),接著,將所指向 •之該資料記憶體Μ位址加-(步驟S318),令該中央處理 單元11指向該資料記憶體14位址加一之位址,並回到該步 驟%12,讀取該資料記憶體14位址加一的位址中之内容。 而執行完上述之讀取迴圈後,若於該步驟S314中發現 所指向之該資料記憶體14位址中已沒有資料,即,該中央 處理單元11指向該資料記憶體14中資料尾端之空白位址時 ’即將所指向之該資料記憶體14位址,設為該暫存記憶體 • 内之該指定位址中所儲存之内容(步驟S320)。即,令 •玄中央處理單元Η指向該資料記憶體14中所儲存之最後一 ,資料之位址。最後,再讀取該資料記憶體14中所儲存之 最後一筆貢料(步驟S322),並於讀取結束之後,叙行其 它待執行之程式(步驟S324),並等待下一個讀取資料= 命令。 :於上述第三圖所示之流程中,當該資料記憶體14中所 儲存之貝料第一次被讀取後,該暫存記憶體13内之該指定 位址中,即存有該資料記憶體14中所儲存之最後一筆資料 201107970 之位址。因此,若該微控制器10欲透過該中央處理單元n 項取該資料記憶體14中之任何一筆資料時,即可讀取該暫 存記憶體13内之該指定位址,藉以得到最後一筆資料於該 f料記憶體14中所儲存之位址。接著,藉由該位址往回推 算,即可讀取所想要的任何一筆資料。例如,可透過該中 央處理單元11指向該位址’藉以讀取儲存於該資料記憶體 14中之最後-筆資料,判斷是否為該微控制器職需讀取 之"貝料。若答案為否定的,則該中央處理單元丨丨可將所指 向之该資料記憶體14位址減一,讀取上一筆資料,判斷是 否為所需之資料。藉以’往回尋找直至找到所需資料,或 找到第-筆資料為止。f ’上述僅為舉例說明,並非用以 限定本發明之記憶體讀取方法。 以上所述僅為本發明之較佳具體實例,非因此即揭限 本發月之專利範圍’故舉凡運用本發明内容所為之等效 化’均同理皆包含於本發明之範圍内,合予陳明。 【圖式簡單說明】 第-圖A係本發明之—較佳具體實施例之方塊圖。 =-圖B係本發明之另—較佳具體實施例之方塊圖。 係本發明之—較佳具體實施例之資料儲存流程圖 圖係本發明之—錢具體實施例之資料讀取流程圖 【主要元件符號說明】 1〇、10’…微控制器 201107970 11…中央處理單元 12…系統記憶體 13…暫存記憶體 14…資料記憶體 bl…位址匯流排 b2…資料匯流排 b3…控制匯流排 S21 、S22、S210〜S228、S310〜S324 …步驟201107970 VI. Description of the Invention: [Technical Field of the Invention] The present invention relates to a memory device that is particularly relevant to a microcontroller. [Prior Art i Micro-Unit Unit (MCU) is a central processing unit. Components such as devices, memories, counters, and output/input interfaces are integrated into a microcomputer that is integrated on a chip of the same-block integrated circuit. The purpose of the "Embedded System", such as radio, refrigerator, cold machine and other appliances, are implemented using a microcontroller unit. The traditional microcontroller unit uses a read-only memory, Only Memory, _ to store the memory and system programs, and accesses the 5 memory (Rand〇m ACCess Mem〇ry). Bold) to produce the data generated during the use of the microcontroller unit. Only with the change of demand, various systems are gradually becoming more complicated. The space used to temporarily store data has long been insufficient. Furthermore, because the surface is a kind of volatile yolatUe) memory, after the microcontroller unit is powered off, the temporarily stored poor material will disappear. Therefore, although this mode of use is more cost-effective, it is difficult To meet the needs of consumers, & has gradually been eliminated by the market. As mentioned above, for example, when a user listens to a radio station through a radio, it can be used by the radio's internal microcontroller unit. However, if (4) the radio is removed, or the battery of the radio is removed, the stored radio frequency disappears. For example, 201107970 - the user must re-set the radio frequency after turning the radio back on and reset it one by one. As a result, this practice has been slowly phased out, and a non-volatile memory is additionally provided to store data that needs to be saved. The use of non-volatile memory to store data is currently the usual practice. : Interacting with the microcontroller unit for the pass-to-power detection system and a power-off delay system. More specifically, the current practice uses the power-off system to detect the occurrence of a power-off condition (for example, the moment when the electronic device is unplugged), and after the power is turned off, the power-down delay system provides the micro-control. Yi early and continue to move for a short time. In this way, the microcontroller unit can store the data to be stored before the power failure in the non-volatile memory in this period of time. After the microcontroller unit is restarted, the data stored before the power failure is read, and the information required by the user is saved. However, although the above method can successfully store data after power failure, the system using this method requires an additional configuration of the power-off test system and the power-off delay system, which is required for product development. A considerable amount of additional cost. Moreover, the power-off delay system can only operate the controller unit for a very short period of time (in principle, one to = clock f, during which the microcontroller unit can complete the data of the real phase + limited. 'This kind of practice is actually the practice of Tian Low, which is necessary for improvement. The cost is the same but the effect is achieved. For example, another application filed by the same inventor as the present invention, Taiwan Application No. 098125118 The lighting control module and method can be used after the switching power supply of the lamp is completely turned off (does not continue to consume power), I201107970 丨! m off the opening and closing _ the number of the light and the brightness of the lamp, etc. Wisdom type (4). For example, according to the number of times of data, automatically determine that the switch is turned on, it is necessary to adjust the brightness of the light, or to set the automatic on/off time of the lamp, etc. The cost of production is the same, so if the above-mentioned use of power outage, 'J system, first and power failure delay system' to achieve, it may make the product = production cost is too expensive. If the novel technology can be provided, the number of data that the J microcontroller unit can store before power-off increases, and the lamps in the application have more extended functions and applications. 】 f The main purpose of the invention is to provide a memory access method for the microcontroller, which can continuously write data to the memory when the microcontroller is in operation, so that the data will continue to restart after the power is completely cut off. After that, you can read any of the required data at any time. • For the above purpose, the present invention is used in the operation of the microcontroller, and will continue to be in the non-volatile memory, in order to facilitate the power-off, micro-control "The retained data can still exist, and when reading, the address stored in the non-volatile 5 memory is stored in the temporary memory of the microcontroller" is used to make the microcontroller Any stored data can be read at any time. Compared with the prior art, the invention can achieve the effect that no additional power-off system and power-off delay system are needed to cooperate with the micro-control (4), but when used That is, the data is continuously written into the non-volatile memory, thereby achieving the purpose of making the data still exist after the microcontroller is powered off by means of the low cost of 201107970, and the data is continuous writing memory instead of detecting After the power is turned off, part of the data is written, so that the amount of data that can be stored can be greatly increased. [Embodiment] A preferred embodiment of the present invention will be described in detail with reference to the drawings. Figure A and Figure B are respectively block diagrams of a second embodiment of the present invention. As shown, the microcontroller of the present invention mainly includes a central processing unit 11, a system memory 12, and A temporary memory 13 is included, and includes a data memory 14 for storing data. Depending on whether the data memory 14 is built-in or externally connected, the crystal system of the microcontroller can be divided into the first figure. The microcontroller 1 in A (built-in) or the micro-control H 1G' (external connection) in the first figure β. For convenience of explanation, the microcontroller 1 晶片 chip in which the data memory 14 is built in the first embodiment will be exemplified below. The system memory 12 is mainly used for storing the objects and system programs required for the operation of the microcontroller, so that the firmware and the system program are always present, and the system execution error is caused to protect them from being arbitrarily changed. Mainly using the Read Only Memory (ROM) to achieve. The temporary storage memory 13 is configured to store data generated during the operation of the microcontroller 1 without continuous storage, and the data is not used again after the microcontroller 1 is powered off and then turned on. Therefore, the temporary storage memory 13 is mainly implemented by a random access memory (RAM). This is only a preferred embodiment of the present invention, which is not limited thereto. The data memory 14 can be built in or externally connected to the microcontroller 10 chip, and is mainly used to store the microcontroller to continue to use 'or valuable data, and needs to be disconnected from the microcontroller 1 It still exists after the electric power so that the microcontroller 10 can be read and used at any time after restarting. Therefore, 'the implementation is implemented in a non-volati- ing memory, and the preferred one is an electrically-erasable Programmable Read-Only Memory (EEPROM). Or a flash memory φ (Flash Memory), but not limited to this. The three memories are transmitted through the central processing unit 11 in the microcontroller 10, by an address bus bl, a data bus b2, and a control bus. (Control Bus) b3 for memory control and data search and access. 2 is a data storage flowchart of a preferred embodiment of the present invention. As shown in the figure, the data storage process of the present invention can be divided into the erasing step S21 before data storage, and The data storage step S22 ° after erasing saves the development cost and controls the area of the entire integrated circuit not to be too large. Therefore, the memory capacity of the built-in or external connection of the general microcontroller 1 does not follow the technology. Progress has never stopped growing. As a result, the storage space of the data memory 14 for storing data is not necessarily sufficient for the microcontroller to continuously write data. Therefore, before the data is stored, the central processing unit η of the 4 microcontrollers can control the data memory 14 to perform the erasing operation of step S21. First, the -first start address of the data memory 14 is set (step s21 〇), so that the central office 201107970 unit 11 obtains the source of the data stored in the data memory 14. Then, part or all of the data stored in the data memory 14 is erased from the first start address (step S212). After the data memory 14 is erased in the step S21, the step S22 of storing the data is performed. When the microcontroller 10 wants to write data, the data storage unit 14 first sets a second start address of the data to be written (step S220), so that the central processing unit 1 can be pointed into the data memory μ. The first blank address that can be written to > Since the content of the data memory 14 has been erased in advance, the second start address is usually the source address of the data memory 14, but is not limited thereto. Then, the data is written into the address in the data memory 14 pointed to by the central processing block 11 (step S222), and after the data is written, the address pointed to is added by one (step S224) 'The next data is written to a blank address below the data memory 14. Next, the central processing unit 11 determines whether the data written in the step S222 is the last data to be stored (step S226). Right, the storage operation is completed, and other programs to be executed are started (step S228) while waiting for the next data storage command. If it is determined in the step S226 that the data of the written person is not the last data to be stored, the process returns to the step S222, and the next data is continued until the required data is completely stored. Referring to the third figure, a flow chart for reading a beaker according to a preferred embodiment of the present invention is shown. Firstly, 'after the microcontroller 1 is powered off and then restarted' and the content of the data memory 14 is read for the first time, the first data is set to 201107970. The remote data is the first starting address of the memory 14 (step S31〇), the third start address may be the source address of the data memory 14, but not limited thereto, the central processing unit is pointed to the data memory 14, and the first data is stored. Address. Next, the content of the data memory 14 address pointed to by the current address is read (step S312), and after reading, it is determined whether the pointed address content_, whether or not the data is stored (step S314). If yes, the address stored in the data memory 14 is written to the designated address in the temporary memory 13 (step S316), and then the pointed The data memory Μ address is added - (step S318), the central processing unit 11 is pointed to the address of the data memory 14 plus one address, and the process returns to the step %12 to read the data memory 14 address. Add one of the contents of the address. After the reading loop is performed, if there is no data in the address of the data memory 14 pointed to in the step S314, the central processing unit 11 points to the end of the data in the data memory 14. In the blank address, the data memory 14 address to be pointed to is set as the content stored in the specified address in the temporary memory (step S320). That is, the meta-central processing unit Η points to the last address of the data stored in the data memory 14. Finally, the last tribute stored in the data memory 14 is read again (step S322), and after the end of the reading, the other programs to be executed are described (step S324), and the next read data is waited for = command. In the process shown in the third figure, when the bedding stored in the data memory 14 is read for the first time, the designated address in the temporary storage memory 13 is stored in the specified address. The address of the last data stored in the data memory 14 201107970. Therefore, if the microcontroller 10 wants to retrieve any data in the data memory 14 through the central processing unit n, the specified address in the temporary memory 13 can be read, thereby obtaining the final The information is stored in the address stored in the memory 14. Then, by pushing back the address, you can read any data you want. For example, the central processing unit 11 can point to the address to read the last-pen data stored in the data memory 14 to determine whether it is the "content" read by the microcontroller. If the answer is negative, the central processing unit can decrement the address of the data memory 14 pointed to by one, and read the previous data to determine whether it is the required data. Use it to look back until you find the information you need, or find the first piece of information. The above is merely illustrative and is not intended to limit the memory reading method of the present invention. The above is only a preferred embodiment of the present invention, and thus it is not intended to limit the scope of the patents of the present invention. All the equivalents of the present invention are included in the scope of the present invention. To Chen Ming. BRIEF DESCRIPTION OF THE DRAWINGS Fig. A is a block diagram of a preferred embodiment of the present invention. =- Figure B is a block diagram of another preferred embodiment of the present invention. The data storage flow chart of the preferred embodiment of the present invention is a flow chart of reading the data of the present invention. [Main component symbol description] 1〇, 10'...microcontroller 201107970 11...Central Processing unit 12...system memory 13...temporary memory 14...data memory bl...address bus b2...data bus b3...control bus S21, S22, S210~S228, S310~S324 ...step