Low-power baud rate generator

Abstract: A baud rate generator has a first oscillator that generates a first clock signal, and a second oscillator that generates a second clock signal with higher frequency but lower frequency accuracy. The first oscillator employs an external resonator but the second does not. The baud rate generator uses the first clock signal to measure the frequency of the second clock signal, then determines a division ratio by which to divide the frequency of the second clock signal, then divides the second clock signal by this division ratio to generate a clock signal for use in serial communication.

Description: BACKGROUND OF THE INVENTION This invention relates to a baud rate generator for generating a serial communication clock signal, more particularly to a baud rate generator suitable for integration into a low-power microcontroller. Microcontrollers (also referred to as single-chip microcomputers) serve as embedded controllers in products spanning the range from electronic toys to large-scale industrial equipment. Many microcontrollers communicate with other electronic devices by means of a built-in serial communication port. In the commonly-employed asynchronous communication mode, the port transmits and receives data at a standard rate such as 300, 600, 1200, 2400, or 9600 bits per second (bps), referred to as a baud rate. This requires the microcontroller to generate a clock signal, referred to as a serial clock, with a frequency preferably within one per cent of the desired baud rate. Microcontrollers that operate on a stable system clock generated by an external crystal resonating at a high frequency such as 10 MHz can easily generate the required serial clock by dividing the system clock frequency. For a 9600-bps baud rate, for example, it suffices to divide 10 MHz by 1042, obtaining 9597 Hz, which differs from 9600 Hz by only about 0.03%. Microcontrollers in some devices, however, must operate with very low current and power consumption, which entails running the microcontroller at a much slower system clock speed. The system clock of such a low-power microcontroller is typically generated by a crystal resonating at 32,768 Hz, the rate of the crystals employed in electronic wrist watches. This system clock frequency cannot be divided to obtain the higher baud rates mentioned above (1200, 2400, or 9600 bps) without incurring considerable frequency error. For example, the closest approximation to 9600 bps is obtained by dividing 32,768 Hz by three, but that gives 10,923 Hz, which is obviously unacceptable: the frequency error exceeds thirteen per cent. One possible solution to this problem is to use only a slower baud rate such as 300 or 600 bps, but this is too slow for many applications. Another possible solution is to use a high-frequency external crystal resonator, the frequency of which is divided to obtain both the necessary lower system clock frequency and the still-lower baud rate. Use of a high-frequency resonator, however, exacts a stiff power and current penalty: current consumption rises from several microamperes to several milliamperes. Yet another possible solution is to provide the microcontroller with two external crystals, one resonating at a low frequency to generate the system clock, and another resonating at a high frequency which is divided to generate the baud rate. The high-frequency crystal is activated only when serial communication is performed. This solution is unsatisfactory, however, because it increases the number of external components and adds significantly to the cost of the microcontroller system. EP-A-0 315 260 discloses a cellular telephone apparatus using one oscillator in combination with a synthesizer including a divider for generating first and second clock signals. SUMMARY OF THE INVENTION It is accordingly an object of the present invention to generate accurate baud rates for serial communication in a microcontroller running at a low system clock frequency, without restricting communication to low baud rates, drawing extra current and power, or requiring an extra external resonator. The invented baud rate generator employs a first oscillator circuit to generate a first clock signal (the system clock) and a second oscillator circuit to generate a second clock signal having a higher frequency, but lower frequency accuracy, than the first clock signal. The first clock signal is counted to determine a first interval of time. The second clock signal is counted during this interval to obtain a count value. The count value is then divided by a certain integer, preferably a power of two, to obtain a division ratio. The frequency of the second clock signal is divided by this division ratio to generate a clock signal for serial communication at the desired baud rate. BRIEF DESCRIPTION OF THE DRAWINGS Fig. 1 is a block diagram of the invented baud rate generator. Fig. 2 is a flowchart illustrating the operation of the central processing unit in Fig. 1. Fig. 3 is a timing diagram illustrating the operation of the first and second counters in Fig. 1. Fig. 4 is a timing diagram illustrating the operation of the programmable frequency divider in Fig. 1. Fig. 5 is a diagram illustrating a right shift. Fig. 6 is a schematic diagram of the second oscillator circuit in Fig. 1. Fig. 7 is a block diagram illustrating a modification of the invented baud rate generator. DETAILED DESCRIPTION OF THE INVENTION An embodiment of the invention will be described with reference to the attached drawings. These drawings illustrate the invention but do not restrict its scope, which should be determined solely from the appended claims. Fig. 1 shows a microcontroller 1 comprising a first oscillator 2, a central processing unit (CPU) 4, and a serial port 6. The first oscillator 2 is coupled to an external resonator 8 such as a crystal that resonates at an accurate frequency f 1 of 32,768 Hz. This enables the first oscillator 2 to generate a first clock signal CLK1 having the same frequency f 1 . This first clock signal CLK1 is a system clock signal that is supplied to the CPU 4 as a basic operating signal. The serial port 6 communicates with other devices (not shown in the drawing) by means of a serial input signal SI and a serial output signal SO. The bit rate of the signals SI and SO is equal to one of the standard frequencies used in serial communication. In the subsequent description this frequency f s will be assumed to be 9600 Hz; that is, the serial port 6 communicates at a baud rate of 9600 bps. To generate this baud rate, the microcontroller 1 also comprises a second oscillator 10, a first counter 12, a second counter 14, and a programmable frequency divider 16. The second oscillator 10 is a comparatively simple circuit such as a resistor-capacitor (RC) oscillator. A circuit diagram will be shown later. The second oscillator 10 oscillates at a higher frequency than the first oscillator 2, but with lower frequency accuracy and lower stability. It is not coupled to an external resonator. The second oscillator 10 is controlled by an enable signal (ENABLE) output by the CPU 4, and oscillates only when ENABLE is active. The output of the second oscillator 10 is a second clock signal CLK2 with a frequency f 2 , which is higher than f 1 . The first counter 12 is coupled to the first oscillator 2, from which it receives the first clock signal CLK1, and the CPU 4, from which it receives a reset signal (RESET). After being reset, the first counter 12 outputs an active gate signal (GATE) and begins counting cycles of CLK1. When it has counted a certain number N 1 of these CLK1 cycles, the first counter 12 deactivates the GATE signal. The first counter 12 can be configured as either an up-counter or a down-counter, with additional logic circuits as necessary to produce the GATE signal. The value of N 1 can be hard-wired into the first counter 12, or it can set by the CPU 4. Different N 1 values can be set for different baud rates. The number N 1 is preferably of the following form: N 1 = 2 x (f 1 /f s ) where x is a positive integer and f 1 and f s are as above. If x = 7, for example, then since 2 7 = 128, N 1 is: N 1 = 128 x 32,768/9600 = 436.90666... Since N 1 must be an integer it is rounded off to 437. The second counter 14 is an up-counter that is reset to zero by the RESET signal output by the CPU 4, receives the second clock signal CLK2 from the second oscillator 10, and counts CLK2 cycles while the GATE signal output by the first counter 12 is active. The second counter 14 is an n-bit binary counter, the n-bit value of which can be read by the CPU 4 via an n-bit signal line QA (here n is an appropriate positive integer). The count value left in the second counter 14 when the GATE signal becomes inactive will be denoted N 2 . The programmable frequency divider 16 receives two signals from the CPU 4: a division ratio N 3 which it receives as an m-bit binary signal via a signal line QB, and a load signal (LOAD). It also receives the second clock signal CLK2 from the second oscillator 10. When the LOAD signal becomes active, the programmable frequency divider 16 loads the value N 3 on the QB signal line into an internal register (not shown in the drawing). Thereafter it divides the frequency f 2 of the CLK2 signal by N 3 to generate a third clock signal CLK3 with a frequency of f 2 /N 3 . Next the operation will be explained with reference to Figs. 2, 3, and 4. Fig. 2 is a flowchart illustrating the operation of the CPU 4. In the first step 18, before beginning serial communication, the CPU 4 outputs a high ENABLE signal to start the second oscillator 10. Then in the next step 20 it waits a certain time for the second clock signal CLK2 to settle into steady oscillation. This time can be measured by a software timing loop running in the CPU 4, or by using the first counter 12 or another counter to count an appropriate number of cycles of the first clock signal CLK1. At the end of this time, in the next step 22 the CPU 4 generates a RESET pulse that resets the first and second counters 12 and 14. Then in the next step 24 the CPU 4 waits for the first counter 12 to count N 1 cycles of the first clock signal CLK1. The CPU 4 can learn when the first counter 12 has counted N 1 cycles by reading the count value in the first counter 12, or the first counter 12 can notify the CPU 4 by means of an interrupt, using a signal line not shown in the drawing. In the next step 26 the CPU 4 reads the contents N 2 of the second counter 14 to find out how many CLK2 cycles occurred while the first counter 12 was counting N 1 CLK1 cycles. Then in the next step 28 the CPU 4 calculates the division ratio N 3 by dividing N 2 by an integer N 4 . This integer N 4 is substantially equal to N 1 (f s /f 1 ) If N 1 is substantially equal to 2 x (f 1 /f s ) as described above, then N 4 should be 2 x . Division by a power of two can be carried out by a simple right-shift operation, as will be described later. In the next step 30 the CPU 4 places the calculated value N 3 on the QB signal line. Then in the last step 32 it outputs a LOAD pulse, causing the programmable frequency divider 16 in Fig. 1 to load the value N 3 . The programmable frequency divider 16 will then output a clock signal CLK3 with a frequency f 2 /N 3 substantially equal to the desired baud rate f s . The reason for this will be explained next. Let tg be the time during which the GATE signal is high. This time tg is the time taken by the first counter 12 to count N 1 CLK1 cycles having a frequency f 1 , from which it follows that f 1 = N 1 /tg . Similarly, tg is the time taken by the second counter 14 to count N 2 CLK2 cycles having a frequency f 2 , from which it follows that f 2 = N 2 /tg . Therefore, since N 4 = N 1 (f s /f 1 ), tg = N 1 /f 1 = N 2 /f 2 N 2 = N 1 (f 2 /f 1 ) N 3 = N 2 /N 4 = [N 1 (f 2 /f 1 )]/[N 1 (f s /f 1 )] = f 2 /f s f 2 /N 3 = f s The equalities in the last two lines above are only approximate, since N 4 is only approximately equal to N 1 (f s /f 1 ), but the approximation is very close. For example, if the second oscillator 10 oscillates at 1 MHz, then from the above equations: tg = 437/(32,768 Hz) = 0.01333612 seconds N 2 = 437 x (1,000,000/32,768) = 13,336 N 3 = 13,336/128 = 104 f 2 /N 3 = 1,000,000/104 = 9615 Hz The calculations in the last three lines above have been rounded off to integers by ignoring fraction digits. The 9615-Hz frequency of the third clock signal CLK3 differs from the desired 9600 Hz by only about 0.16%, which is well within the acceptable margin of error. In general, if f 2 is sufficiently high, the frequency error of the third clock signal CLK3 can be kept within any desired limit. Figs. 3 and 4 are timing charts illustrating the above operations. The ENABLE, RESET, GATE, and LOAD signals are assumed to be active high. In Fig. 3, the CPU 4 drives the ENABLE signal high, waits for CLK2 to stabilize, then outputs a high RESET pulse. The rise of the RESET signal resets the first and second counters 12 and 14. At the fall of the RESET signal the first counter 12 starts counting, the GATE signal goes high, and the second counter 14 also starts counting. When the first counter 12 has counted N 1 cycles of CLK1, at the end of the time interval tg, the GATE signal goes low and the second counter 14 stops counting. The count value N 2 of the second counter 14 can then be read by the CPU 4 on the QA signal line. In Fig. 4, having read N 2 , the CPU 4 calculates N 3 and places this value on the QB signal line, then outputs a LOAD pulse. The programmable frequency divider 16 then stores the N 3 value internally and uses it to divide the frequency of the second clock signal CLK2 to generate the third clock signal CLK3. The third clock signal CLK3 in Fig. 4 can be used by the serial port 6 for timing of the serial input and output signals SI and SO. When serial communication is completed, the second oscillator 10 can be shut down by deactivating the ENABLE signal, to conserve power. Fig. 5 is a diagram of the right-shift operation the CPU 4 uses to divide N 2 by N 4 when N 2 is a fifteen-bit binary value and N 4 is 2 x = 2 7 = 128. The bit positions in N 2 are numbered from 14 to 0, with bit 0 being the least significant bit. It is well known that a one-bit right shift of a binary number is equivalent to dividing the number by two, in the same way that shifting the digits of an ordinary base-ten number one place to the right is equivalent to dividing by ten. A seven-bit right shift is therefore equivalent to dividing by 2 7 = 128. To calculate N 3 = N 2 /N 4 the CPU 4 accordingly performs a seven-bit right shift: bit 7 of N 2 becomes bit 0 of N 3 , and bit 14 of N 2 becomes bit 7 of N 3 . Bits 0 to 6 of N 2 are discarded, which is equivalent to rounding the result to an integer by ignoring fraction digits. Dividing by a power of two to calculate N 3 is an important feature of the present invention. Many low-power microcontrollers have a four-bit CPU which lacks an explicit division instruction. A program to carry out a long division operation can be created, but the program itself is long: on the order of fifty to eighty instruction steps if the instruction word length is eight bits and the data length is twelve bits. Such a long division program would use up a significant amount of valuable space in the microcontroller's program memory, leaving less room for application programs. Performing a seven-bit right shift, however, is comparatively easy; it can be programmed in about ten instruction steps. Execution speed is also faster. Fig. 6 shows one possible circuit configuration of the second oscillator 10, comprising a two-input NAND gate 34, to the output side of which three inverters 36, 38, and 40 are coupled in series. The NAND gate 34 receives the ENABLE signal at its first input. The output of the first inverter 36 is coupled through a capacitor 42 of capacitance C to the second input of the NAND gate 34. The output of the second inverter 38 is coupled through a resistor 44 of resistance R to the second input of the NAND gate 34. The CLK2 signal is output from the third inverter 40. When ENABLE is low (inactive), the output of the NAND gate 34 is held at the high level, which is inverted by the three inverters 36, 38, and 40 to hold CLK2 at the low level. The second oscillator 10 is accordingly halted. The high output from the second inverter 38 charges the capacitor 42 through the resistor 44, making the second input to the NAND gate 34 high. When ENABLE goes high (active), both inputs to the NAND gate 34 are high, so its output goes low and CLK2 goes high. The output of the second inverter 38, however, is now low, and it discharges the capacitor 42 through the resistor 44. This brings the second input of the NAND gate 34 to the low level, so its output changes to the high state, and CLK2 goes low again. The circuit continues to oscillate in this way with a frequency f 2 approximately equal to 1/2RC. It is difficult to control f 2 accurately in this circuit, partly because of the difficulty of fabricating resistors and capacitors with accurate values, and partly because of the sensitivity of R and C to temperature variations. The point of the invention, however, is that f 2 does not have to be precisely controlled; the microcontroller itself will measure f 2 (by counting N 2 ) and determine the necessary division ratio to produce a close approximation to f s . An advantage of the circuit in Fig. 6 is that it is extremely inexpensive: all the circuit elements, including the resistor 44 and capacitor 42, can easily be integrated into the microcontroller chip; no external crystal or other external components are required. The circuit in Fig. 6 is suitable for integration into a microcontroller of the complementary metal-oxide-semiconductor (CMOS) type. The invention is not restricted to this circuit, however; it can obviously be practiced with other oscillator circuits. The programmable frequency divider 16 in Fig. 1 is a type of programmable counter. Since the second counter 14 and programmable frequency divider 16 do not have to operate at the same time, it is possible to replace both of them with a single programmable counter 46, as shown in Fig. 7. The programmable counter 46 is first programmed by signals from the CPU 4 to count CLK2 cycles, to obtain a count value N 2 . Next, after the CPU 4 calculates N 3 from N 2 , it programs the programmable counter 46 to divide CLK2 by N 3 and thereby generate the third clock signal CLK3. The CPU 4 uses a single n-bit signal line QC to read the count value N 2 from the programmable counter 46 and set the division ratio N 3 in the programmable counter 46. The first counter 12 in Fig. 1 can be replaced by a timing program running in the CPU 4, so that the CPU 4 outputs the GATE signal, as also indicated in Fig. 7. For example, a program that counts 437 CLK1 cycles can be constructed by creating a loop that executes in fifteen cycles, executing this loop twenty-nine times, then executing an instruction that executes in two CLK1 cycles. If the first counter is implemented in software in this way, the baud rate generator can have a particularly simple structure, shown in Fig. 7, comprising only the second oscillator 10, the programmable counter 46, and the timing and calculation facilities provided by the first oscillator 2 and CPU 4. The third clock signal CLK3 in the preceding description was a square-wave signal with a frequency substantially equal to the desired baud-rate frequency f s of 9600 Hz, but the invention is not restricted to this type of clock signal. For example, CLK3 can be a pulse signal with a frequency equal to a fixed multiple of the baud rate, which the serial port 6 divides to generate the baud rate. The baud rate can of course be other than 9600 bps. The frequency f 1 of the first clock signal can be other than 32,768 Hz. The integer N 4 used to calculate the division ratio need not be 2 7 but can be some other power of two, or in general any positive integer, although powers of two are preferable. Higher values of N 4 enable the frequency f 2 of the second oscillator 10 to be measured with greater accuracy. Although the invention has been described as part of a single-chip microcontroller, it can be applied in other devices as well. In place of the CPU 4 it can employ any processor unit capable of dividing N 2 by N 4 and generating the necessary control signals (e.g. ENABLE, LOAD, and RESET). Those skilled in the art will recognize that many further modifications can be made in the drawings and descriptions above without departing from the scope of the invention.

Claims: A method of generating a clock signal (CLK3) for serial communication from a first oscillator (2) and a second oscillator (10), said first oscillator (2) oscillating at a lower frequency than said second oscillator (10) but with higher frequency accuracy, comprising the steps of: generating a first clock signal (CLK1) from said first oscillator (2) employing an external resonator; generating a second clock signal (CLK2) from said second oscillator (10); counting said first clock signal (CLK1) to determine an interval (tg) of time; counting said second clock signal (CLK2) during said interval (tg) of time to obtain a count value (N 2 ); dividing said count value (N 2 ) by a certain integer (N 4 ) to obtain a division ratio (N 3 ); and dividing said second clock signal (CLK2) in frequency by said division ratio (N 3 ), thereby generating said clock signal (CLK3) for serial communication. The method of claim 1, wherein said first clock signal (CLK1) is a system clock signal of a microcontroller (1). The method of claim 2, wherein said first oscillator (2) employs a resonator (8) external to said microcontroller (1). The method of claim 2, wherein said second oscillator (10) is internal to said microcontroller (1) and employs no external resonator. The method of claim 4, wherein said second oscillator (10) is a resistor-capacitor oscillator. The method of claim 1, wherein said integer (N 4 ) is a power of two, and the step of dividing said count value is performed by a right shift operation. The method of claim 1, comprising the further steps of: activating said second oscillator (10) when serial communication is required; waiting for said second clock signal (CLK2) to stabilize before starting to count said first clock signal (CLK1) to determine said interval (tg) of time; and halting said second oscillator (10) when serial communication is not required. A baud rate generator for generating a clock signal (CLK3) for serial communication, comprising: a first oscillator (2) employing an external resonator (8), for generating a first clock signal (CLK1); a second oscillator (10), not employing an external resonator, for generating a second clock signal (CLK2); a first counter (12) coupled to count said first clock signal (CLK1), thereby determine an interval (tg) of time, and output a gate signal that is active only during said interval (tg); a second counter (14) coupled to count said second clock signal (CLK2) while said gate signal is active, thereby generating a count value (N 2 ); a programmable frequency divider (16) coupled to divide said second clock signal (CLK2) by a division ratio (N 3 ), thereby generating said clock signal (CLK3) for serial communication; and a processor unit (4) coupled to read said count value (N 2 ) from said second counter (14), divide said count value (N 2 ) by a certain integer (N 4 ), thereby calculating said division ratio (N 3 ), and load said division ratio (N 3 ) into said programmable frequency divider (16). The baud rate generator of claim 8, wherein said baud rate generator, except for said external resonator (8), is fabricated as part of a microcontroller (1), said processor unit (4) being a central processing unit of the microcontroller (1). The baud rate generator of claim 9, wherein said microcontroller (1) uses said first clock signal (CLK1) as a system clock signal. The baud rate generator of claim 8, wherein said processor unit (4) generates an enable signal that activates said second oscillator (10) when serial communication is required and halts second oscillator (10) at other times. The baud rate generator of claim 8, wherein said processor unit (4) generates a reset signal that resets said first counter (12) and said second counter (14). The baud rate generator of claim 8, wherein said integer (N 4 ) is a power of two, and said processor unit (4) is programmed to divide said count value (N 2 ) by said integer (N 4 ) by performing a right shift operation. The baud rate generator of claim 8, wherein said second oscillator (10) is a resistor-capacitor oscillator. The baud rate generator of claim 14, wherein said second oscillator (10) comprises: a logic gate (34) having a first input terminal, a second input terminal, and an output terminal; a plurality of inverters (36, 38, 40) coupled in series to said output terminal; a capacitor (42) for coupling output of one of said inverters (36) to said second input terminal; and a resistor (44) for coupling output of another of said inverters (38) to said second input terminal. The baud rate generator of claim 15, wherein said logic gate (34) is a NAND gate. The baud rate generator of claim 15, wherein said first input terminal receives an enable signal from said processor unit (4). The baud rate generator of claim 8, wherein said second counter (14) and said programmable frequency divider (16) are combined as a single programmable counter (46). The baud rate generator of claim 8, wherein said first counter (12) comprises a timing program running on said processor unit (4), said gate signal being output from said processor unit (4).

Inventors: TAKASHIMA SUMIHIRO, YAMAZAKI SEIICHI, TAKASHIMA, SUMIHIRO, YAMAZAKI, SEIICHI, Takashima, Sumihiro, c/o Oki Electric Ind. Co.,Ltd, Yamazaki, Seiichi, c/o Oki Electric Ind. Co., Ltd.
Applicants: OKI ELECTRIC IND CO LTD, OKI ELECTRIC INDUSTRY CO., LTD.
Application year: 1993
Main classification: H04L7
IPC codes: G06F1, H04L7, H03L7