Fast avr schematic. Restoring the configuration of the fuse-bits of Tiny AVR microcontrollers (HVSP). Connecting microcontroller pins in high-voltage serial programming mode

The universal development board FastAVR for beginners in programming MK ATMEL was developed on the basis of an analysis of about a dozen of similar designs. The board is a reasonable compromise between the redundant functionality of most of them or the overly primitive features of others. Having some experience in working with different microprocessor systems, below I will comment on my thoughts - how this or that board node was conceived. It is up to you to agree with them or not, but maybe they will be partially useful for those mastering AVR in the future ...

The design is based on the developments and application notes of Microelectronics (http://www.mikroe.com/ru/). But EasyAVR boards (and not only ones) contain too many components that attract newcomers to microprocessor technology by their diversity and quality of workmanship; in fact, half of them become unnecessary after successfully compiling 5-6 examples of programs and gaining experience. Judge for yourself - why do you need a bunch of LEDs and buttons connected to each port on the board? All this is relevant until you learn how to control the pins of the port and blink the indicators, but this will happen very quickly ;-) In this case, the board has 4 LEDs and a switch, for a start it is enough ...

So, the capabilities of the debug board:

  • basic set of peripherals to support ATmega: RS-232 converter, beeper, SPI EEPROM, LCD and LED indicators, built-in clock generator + quartz, PS-2 keyboard, ADC tester, logic testers;
  • possibility of repetition at home, single-sided printed circuit board optimized for laser-ironing technology, small size;
  • the use of all components only in DIP packages - allows you to easily replace them during experiments or program the processor itself for other circuits (for example, for JTAG);
  • full functional sufficiency for creating simple prototypes of devices and their debugging;
  • connector of the standard SPI programmer STK-200 with selective power supply to the programmer, the possibility of external JTAG connection;
  • the ability to turn on the board in JTAG ICE mode by simple re-switching;
  • the possibility of independent switching of on-board peripherals in any combination due to the linear technology of the location of all ports of the MC;
  • the ability to easily connect any external peripherals and use 100% of the ATmega resources in the DIP-40, all ports are bit by bit accessible, moreover, all the internal peripherals of the board can be used for external devices (for example, a clock generator or LED indicators;

All this does not require board modifications or soldering. That. at the initial stage of mastering MK, the capabilities of FastAVR are quite enough. Those who decide to move on, after creating their own software, will be able to independently decide what they need specifically and make a prototype of a design with their own set of peripherals. I repeat once again - the board was created to start studying the AVR MK, no more goals were pursued. The appearance of the documentation itself is associated with the interest shown by many beginners to master this type of controllers or are still thinking about where to start. Naturally, you need to start with the test board ;-)

MK AVR-self-sufficient controllers, but the processor is not the whole system. "Cubes" serving it or controlled by it, in themselves, can be considered as separate blocks of future structures. By combining them on the main board all together, you can combine them to get the desired results. At first, the board was conceived on the basis of the ATmega8, since it is cheap and has almost all the features of an AVR. However, having heeded the advice, I decided not to save money and put the microcontroller at the maximum available in a DIP package - ATmega16 or 32. The pinout of both MCUs is identical. The cost of such a solution is repaid a hundredfold by the number of I / O ports to which you can connect at least for the duration of debugging. Bottom-up compatibility for all AVR generations allows you to write and debug programs using a more powerful chip, and then compile for the target chip. A sufficient flash resource allows you not to "get hung up" on the limitations in the Mega's reprogramming capabilities, especially since you can, with enough exercise, send the MK to a working structure by flashing it for the last time (JTAG ICE is the first contender)

To manufacture the board, you will need a little non-scarce computer "junk", which is enough in the closets of any electronics engineer. Most of the components are used from old or failed IBM PC motherboards or near-computer equipment; recently, this kind of iron is increasingly becoming unusable and thrown away or lying around without use. Because With SMD small things, practically nothing can be done (sort out the problem, and the time ...), I solder such devices entirely either with a construction hairdryer or on an electric stove.

The basic description of FastAVR is blockwise below:

Nutrition. The built-in 78 (M) 05 regulator allows the board to be powered from widespread 9-12V adapters taken from other equipment, which are usually idle. In a normal connection, this is enough (Mega-16/32 + LCD + RS232 + TXO), when using an on-board 7-segment LED, or a highly voracious external periphery (complex programmer), the stabilizer is already very hot. Connecting an external stabilized voltage + 5V is possible through pin-3 X1 (a connector from the motherboard coolers is used). You must first disable the JP1-JP2 jumpers of the VCC_SEL group. X1 of this type was chosen for several reasons, the main one is that you almost always have to make an adapter to power the board from different adapters that are available or a laboratory power supply unit. Pass-through ferrite chokes (balun) FB1, FB2 filter impulse noise and high-frequency interference. The diodes VD1, VD2 are protected against "over-plus". Jumpers VCC_EXT and GND_EXT are installed in several places on the board. Through them, it is quite easy to remove the supply voltage and common "ground" when connecting the peripherals.

External memory is implemented on a standard I2C EEPROM 24cXXX. Although the AVR itself contains its own non-volatile memory, in many designs an external chip may be preferable due to size or resource. The switching circuit is standard, the crystal address is 0x01.

Linear LED indicators of the status of the HL2-HL5 ports are made on 4 discrete LEDs. To start experimenting with AVR, this is enough, I consider more of them not justified and rather a decoration. The LEDs turn on when writing to the log port "1", thus. port status is displayed without signal inversion, which is convenient and clear.

Connection to the LCD indicator board is made through 2 connectors, it is possible to use both 8-bit and 4-bit modes. The first of them - 34-pin X2 (from 3.5 "drives) allows you to use standard repressed cables from drives of the required length, respectively, on the indicator itself it is better to unsolder the pin jumper strip (pin block), this allows you to quickly change different indicators without fear The connection to the ports of the AVR is carried out through the pin-block X10, so in addition to the LCD connection mode itself, you can flexibly select the MK pins.This design allows you to easily adapt to the free ports of the controller, even "dial" them one by one from different groups ports, which may be necessary when matching a specific prototype being debugged, or a newly designed printed circuit board turns out to be more convenient in wiring.


In many cases, the use of an LCD indicator may not be justified in terms of price, size, or reliability. For example, in the simplest charger or timer, a 2-digit LED indicator may well work. The dual 7-segment displays of the common type that I have in my possession with a sign height of 14 mm turned out to be both with a common anode and with a common cathode (decommissioned cash registers and system blocks of 486 computers). I had to use 2-stroke keys on VT1-VT4 to connect indicators of any type and, accordingly, a socket for the indicator itself, so as not to rack my brains with circuitry in the future.

All peripheral connections to the ATmega ports, as noted earlier, are via line pin blocks X3-X6. Basically, on debugged boards, I observed the use of IDC-10 (2x5) connectors. Their only advantage in this case is the presence of a "key", so as not to confuse the loop in places when connecting. This is where the advantages of this method end and the disadvantages begin - it is even visually inconvenient to work with 8-bit ports, tk. the pins are not arranged in a row, it is impossible to connect the built-in peripherals except for a loop. The use of pin-blocks gives exactly the opposite result, in addition, through a standard jumper-jumper it is easy to control any signals from above, for example, with a logic probe or an oscilloscope, you do not need to poke around and read on the port legs for fear of accidentally "shorting" the pins. Add to this the maximum cheapness and reusability of this connection, because it is much easier to replace a cable or jumper than a connector soldered to the board. Moreover, now on sale, even in our outback, you can find such mating parts of connectors (or use from old system units), which allows you to easily and quickly combine connectors (Fig):


For sound, a common beeper with a resistance of about 80 ohms from the motherboard is used. The signal is not very loud, but sufficient for monitoring (R23 is already selected at the limit). I did not install a separate key, those who wish can unsolder it in the places for prototyping, designated as TEMP. A little advice - when working with sound, do not forget to put a command at the end of the signal generation procedure, which resets the PD7 output to the log "0", otherwise, after the generation stops, there may be "1" left and the current through the speaker will continue to flow, which is not good though would be for reasons of general consumption AVR.

A 4-bit DIP-switch SW4 is used to assemble a master of logical signals for ports. Here the situation with the quantity is similar to LEDs. Because The avr inputs have internal connected pool-up resistances, so there is no need to set "pullups" to the power supply. On resistors R18-R21, protection against errors of accidental switching on of the MK ports to the output is assembled. In board revision 1.03 and higher, the dip-switch can be replaced with jumpers if it is missing. Recently I needed to quickly make a JTAG ICE from a board. In this connection, in rev 1.4 introduced the resistor matrix RN1, which allows the hardware to form a ligical "1" on several controller inputs. If you don't need it, you can skip installing RN1.

The MC clock is selected by the CL_SEL pin group and can be carried out from an external crystal resonator Z1 (only JP37, JP38 are installed), an integral crystal oscillator G1 (16 MHz), or from a divider by: 2 and: 4. That. in addition to quartz, the processor can be clocked at frequencies of 16, 8, 4 MHz. You can easily estimate the speed of the program being debugged, or get a standard clock frequency with a soldered special. quartz. In principle, in the absence of TXO, any other generator up to 16 MHz can be used at this frequency. The generator can also be useful to you when "raising" the MK due to incorrectly stitched fuses of the microcontroller, in this case the clock frequency does not matter.

The RS-232 to UART serial level converter is an invariable feature of most AVR-based systems. There is no need to "reinvent the wheel" here, the standard MAX232 is enough. Only the RX-TX signals are involved, which is sufficient for most applications. It is practically possible to connect CTS-RTS for hardware flow control without reworking the board, using flexible wires on the JP31-JP32 from the track side. The circuit tested m / c Maxim MAX232, TI MAX232 and SIPEX SP3232 - put any analogs compatible in pinout.

An external matrix keyboard can be made on a separate board and connected with loops to the MK (I decided to use a mouse from the manipulators, as a rule, 2 mikriks are always working there). The debug board itself has a dual PS-2 connector block. A standard IBM PC keyboard can be connected without any hardware modifications, naturally with the appropriate software support from the AVR. The second connector is free, use at your discretion. As a rule, the keyboard is a very specific thing, depending on the prototype being debugged, so after some thought I decided not to put even the simplest buttons on the board. I'll post my board options after wiring and testing them.

The HL7 indicator is set for experiments with the built-in hardware PWM controller.

The connector for in-circuit serial programming X7 is made in accordance with STK-200. Power to the programmer can be selectively selected via JP43. In my case, the simplest programmer from PonyProg is used on the 74ALS (LS, F) 244 buffer with an LPT connection. Everything was tested on a Core2Duo + i965chipset running XP SP2, no problems arose. The programmer is powered through the connector from the debug board and is easy to use, because buffers in normal mode "go" to the Z-state and do not interfere with FastAVR operation at all. Connecting a JTAG adapter for in-circuit programming and real-time debugging is also possible without modifying the board via the corresponding linear pin block of port C.

It remains to mention a few more necessary elements:

The external Reset circuit, which is quite simple for the AVR. It can be turned off via JP42, although operation in conjunction with the programmer does not interfere at all. The reset input can be reprogrammed via the fuse as a standard I / O port and used for peripherals, but remember that in this case it is no longer possible to reprogram the crystal via X7.

The variable resistor R27, turned on by a potentiometer, is a voltage setting for experiments with a built-in ADC, the output from it can be fed to any of the analog inputs of the MK. A small note - pay attention, if you do not install this resistor for any reason, be sure to put a jumper (in the figure with a dotted line) for the normal passage of the common GND bus!

A little about the PCB itself and the design. As already noted, the board is one-sided. So far, I have checked 2 copies made using laser-ironing technology (one when printing on photographic paper from inkjet, the other based on self-adhesive), thus. if you wish, everything should work out ;-) If you think about using a photographic method, great! The jumper bars are separated taking into account the "forbidden zones" and the use of standard 16-pin loops (straps from GAME-PORT), even when connected with one extreme row. In the absence of ferrite chokes (I use from old 286 motherboards or burned monitors), you can safely put jumpers. I recommend that you put panels under all the chips so that you do not have to chip the board later. Do not forget 2 jumpers from the keys to the HL6 indicator.

And this is how the interface for checking TTL / CMOS logic looks like, as far as possible, I will try to tell you what came of it.

In general, the timer has a comparison register OCR ** and when the value in the timer matches the value of the compare register OCR ** 2 things can happen:

  • Interrupt
  • Changing the state of an external comparison pin OC **

We can now customize PWM when the counter counts to the value OCR ** voltage on the leg we have chosen OC **change from 5 to 0. When the timer counts to the end and starts counting, first we change the voltage from 0 to 5, at the output we will have rectangular pulses

There are 3 operating modesPWM

STS(reset on match) - This can be called CHIM a pulse-frequency simulated signal when the timer counts to a value OCR ** it resets and changes the value OC **to the opposite. Thus, the duty cycle PWM always the same.

This is used when you need to count exact periods, or generate interrupts after a certain time.

Fast PWM(fast PWM) - the counter counts from 0 to 255, after which it is reset to 0.

When the timer value is the same as OCR ** the corresponding output is reset to 0, when zeroed, it is set to 1.

Most often used as normal PWM.

Phase Correct PWM(PWM with precise phase) - in this mode, the counter counts from 0 to 255, and then counts back to zero. On first match with OCR ** the output is reset to 0, on 2 matches (when the counter goes back), 1 is set.

They are used so that the phase does not go astray when the duty cycle changes.



If we want to work with the output OC1A put the bits in COM1A1 COM1A0
Generally "/" means OR. TCNT1= OCR1A for PWM on withdrawal OC1A

Timer / Counter Mode of Operation- timer / counter operating mode.

Top- TCNT1 value at which the output value is switched OC **.

TOV1 Flag Set on- at what values ​​the GIFR register bit is set

We select the mode that we need from the last table, do not look at Top. From the 2 tables, select any of the last 2 options. It remains only to arrange the necessary bits in the registers.

#define F_CPU 8000000UL #include #include int main () (DDRD = 0xFF; OCR1A = 0xC0; // Compare with this value OCR1B = 0x40; // PWM and timer settings TCCR1A | = (1<CS10 set 1, he counts with the frequency of the MK about how to set the timer frequency

One of the best Basic-like language compilers for the AVR 8-bit microcontroller series.

The FastAVR development environment is characterized by a convenient, well-thought-out interface and includes an editor, an assembler, a compiler and a microcontroller programmer. The application has command highlighting, a friendly interface, and many other solutions designed to facilitate the process of creating and debugging programs. FastAVR uses eight-bit rather than sixteen-bit addressing and compares favorably with other compilers by creating extremely compact code (especially for controllers with less than 256 bytes of RAM). In addition, additional functions of a character generator for LCD, calculator of timers, terminal are built into the program. AVR's list of supported microcontroller devices includes: 2313, 2323, 2333, 4433, 8515, 8535, ATiny13, ATiny26, ATmega163, Atmega8, Atmega16, Atmega32, ATmega64, ATmega128 and many more.

The language used in FastAVR consists of familiar Basic commands, significantly enhanced with additional useful functions (I2C, 1-Wire, LCD and some others). The compiler supports structured programming to improve program readability. This application compiles the program located in the active editor window into standard AVR assembly code using the free Atmel assembler (included in the suite of programs). As a result of compilation and if there are no errors in the source text, a file with the * .asm extension is created. The programming environment outputs the generated code in assembly language, which is useful for experienced developers.

FastAVR can use virtually any of the existing in-circuit programmers for serial or parallel AVR controllers. The source code of the program is typed like ordinary text in the form of symbols in the built-in editor of this software. In addition to displaying the source text, the FastAVR editor independently corrects the program, highlights commands, variables and other data with color. It also supports a switchable ability to align blocks of text. The program code can be viewed and edited in any conventional editor. Nevertheless, when writing a program, you must follow a number of rules set by the FastAVR syntax. For example, in each program, the compiler needs to specify the type of controller used, the clock speed, and the size of the software stack. Such special commands (metadirectives) are not converted to assembly code.

Since the AVR family of microcontrollers stores data and code in different areas of memory, when working with the FastAVR program, developers need to specify in which memory location the data will be located. The compiler places the program code automatically.

FastAVR software was created by Bojan Ivancic from Slovenia and is a product of MicroDESIGN. Unfortunately, the author abandoned his project, his site does not work, and support phones do not answer. The application itself has not been updated for a long time and, as a result, the compiler does not support the latest models of Atmel controllers.

The FastAVR program was paid. Without entering the registration code, the development environment is launched only in demo mode, which has significant restrictions. The app can currently be downloaded from the link below. The presented assembly includes the latest working version of the program, help files in English, examples.

This product was written in English. There is no Russifier for it.

FastAVR runs under Microsoft Windows. The application functions correctly on 98SE, NT4, 2000 and XP operating systems.

This device - Attiny fusebit doctor - allows you to restore the configuration of the Fuse-bits (factory settings, according to the technical description) of Atmel Tiny microcontrollers. Supports all microcontrollers that have a High Voltage Serial Programming Interface (HVSP):

  • in an 8-lead package:,,;
  • in a 14-lead package:,;
  • in a 20-lead package with special adapter:,.

If you need a device to restore the fuse-bits configuration of ATtiny microcontrollers, check out the project:.

The device is very simple to manufacture and does not contain expensive components. The basis is a microcontroller, as well as several resistors and transistors (,), a +5 V voltage regulator (7805T). It is worth noting that a stabilized power supply with an output voltage of +12 V is required to power the device (which is important for initializing the high-voltage programming mode).

When programming the Fuse-bits of the microcontroller it should be borne in mind that an internal 4 MHz oscillator is used without a divider by 8. And you can also enable the "fast rising power" option.

Schematic diagram

Printed circuit board

Board with installed components

Connecting microcontroller pins in high-voltage serial programming mode

Restoration of the microcontroller (patient) configuration starts by pressing the Start button. To indicate the status, two LEDs are provided, the states of which indicate:

  • green LED is on- Fuse bit configuration restored. If the Lock-bits are set, then only the compliance of the current bit configuration with the factory settings is checked, and if it matches, the green LED turns on;
  • red LED is on- error while reading the microcontroller signature: it is impossible to read, there is no microcontroller in the socket, or the signature does not match those in the device database;
  • green LED flashes- the signature is correct, the configuration of the Fuse bits is incorrect. Lock bits are set, Flash erase is required;
  • flashing red LED- the signature is correct, the lock bits are not set, but for some reason the Fuse bits cannot be written, the check fails after 10 attempts.

The fuse bit recovery device operates according to the high voltage serial programming protocol. Initially, when the process is started, the restored microcontroller (patient) switches to the high-voltage memory programming mode, then the signature of the chip is read and the device's ability to work with it is checked. After that, an erase operation is performed, if the user has specified it. The next step is to read the lock bits and, if they are not set, then the "patient" receives a new configuration of Fuse bits corresponding to the model of the microcontroller-patient. After that, the Fuse-bit setting (verification) is checked and, if the test is successful, the device ends its work. Otherwise, the device repeats the write-verification Fuse-bits cycle 10 times.

The board has two jumpers (jumpers) "chip erase" and "unknown signature":

  • chip erase- enables the operation of erasing the entire Flash-memory of the chip. This is necessary if the lock bits are set, i.e. there is no way to fix the fuse bits until the lock bits are cleared. Jumper on - erase operation is allowed.
  • unknown signature- unknown chip signature - a very rare phenomenon, but it still happens that the chip has erased its signature. Signature, calibration bytes and other data cannot be permanently stored in the chip structure, they can be accidentally damaged (erased) in case of unstable power supply during programming. Usually, in such cases, the obtained signature values ​​are FF FF FF, but the chip is working fine, the Flash memory can be read and written. If the read signature does not match any of the device's database (including the FF FF FF and 00 00 00 values), then when this jumper is turned on, the device will write the universal Fuse-bit configuration. Universal configuration means that the ISP (turning on the SPIEN bit) and functionality of the Reset pin (turning off the RSTDISBL bit) of the microcontroller will be restored, the oscillator options will not be affected. With such actions, the microcontroller will be able to further recover, but with the help of a conventional SPI programmer.

Attention! Do not use the "unknown signature" option with ATtiny11 or ATtiny15 microcontrollers.

Application:

  1. Project files (Eagle 5.4.0 format), schematic diagram and printed circuit board drawing (pdf, png), .hex file and .bin file for microcontroller programming -.
  2. Updated firmware for the microcontroller (Version 2): source code (BASCOM v.1.11.9.0), .hex file and .bin file for microcontroller programming -.

Quite often, it becomes necessary to use the control of any device (be it an incandescent light bulb, engine, heating element or a simple LED) by means of PWM.

Probably there is no need to explain what it is and what is the beauty of PWM control, there is already a lot of information on the Internet, and I’m unlikely to be able to chew on this topic better. Therefore, let's get down to business right away, namely, we will launch PWM on Attiny2313 using Bascom-AVR tools.

PWM in AVR microcontrollers works on timers-counters, in Tiny2313 microcontrollers there are only 2 such timers: 8-bit Timer0 counting up to 255 and 16-bit Timer1 capable of counting up to 65535. Each timer controls two PWM channels, so everything can be done in hardware implement as many as 4 PWM channels.

Information about the number of PWM channels and the bit width of each channel can be found on the datasheet pages on the microcontroller.

So, on board the Attiny2313 there are two 8-bit PWM channels powered by Timer0 and two more channels controlled by the Timer1 timer have a programmable bit width from 8 to 10 bits. In the datasheet, these legs are signed as follows:

To configure Timer1 to generate PWM in Bascom, just write the following line:

Config Timer1 = Pwm, Pwm = 8, Compare A Pwm = Clear Up, Compare B Pwm = Clear Down, Prescale = 64

Pwm = 8 PWM bit width is selected, for Timer1, as it was written above, it can also be Pwm = 9 or Pwm = 10.

Compare A / B Pwm = Clear Up / Clear Down here we configure the active state for each PWM channel (A and B).

Prescale = 64 - the already familiar timer configuration line, which is responsible for the pre-division of the timer overflow frequency, in this case the divider will set the PWM frequency. We can change at our discretion Prescale = 1 | 8 | 64 | 256 | 1024


The duty cycle of the generated signal is determined by the value that we write to the comparison registers OCR1A and OCR1B (we have two PWM channels on one timer, here one register per channel A and B). The value of the counting register is constantly compared with the values ​​that lie in these registers (it is copied there from the timer), when they match, the leg μ switches to the active state, and the counting register continues to count to its maximum value. Having counted to the maximum, the timer starts counting in the opposite direction, and upon reaching the moment when the values ​​of the counting register and the comparison register coincide again, a reverse switching will occur on the microcontroller leg (see the figure below)



For us, the comparison registers OCR1A and OCR1B are just variables in which we can put some value. For example, like this:


OCR1A = 100
OCR1B = 150


For convenience, the Bascom provides another name for these registers: PWM1A and PWM1B, so the previous lines will be equivalent to the following:


PWM1A = 100
PWM1B = 150

Now let's see how the Clear Up / Clear Down active state configuration affects what happens at the PWM output, depending on the value of the compare register.

When the output is configured as Compare A Pwm = Clear Down, the active state of the output is high and as the value of the OCR (PWM) register increases, the proportional voltage on that leg will increase. Quite the opposite, everything will happen if the output is configured as Compare A Pwm = Clear Up. All this is well illustrated in the picture below.



The values ​​that these comparison registers can take depend on what bit width of the PWM channel we have chosen. With PWM = 8 (8-bit PWM), a value from 0 to 255 is possible; at PWM = 9 from 0 to 511; with PWM = 10 from 0 to 1023. Here I think everything is clear.


Now a small example: connect the LEDs to the microcontroller as shown in the diagram (power supply is not indicated in the diagram)


And let's write a small program:


$ crystal = 4000000

Config Timer1 = Pwm, Pwm = 9, Compare A Pwm = Clear Down, Compare B Pwm = Clear Up, Prescale = 8
Config PORTB.3 = Output
Config PORTB.4 = Output

Incr Pwm1a "smoothly increase the value of the comparison register OCR1A
Incr Pwm1b "smoothly increase the value of the comparison register OCR1B

Waitms 20 "add a delay

Loop

End

After compiling and flashing the program into the controller, one of the LEDs (D1) will smoothly gain brightness, and the other (D2) will smoothly go out


If we now poke an oscilloscope at the PWM outputs, then we can see such a picture with a changing duty cycle (blue signal on OC1A, red on OC1B):


Timer0 configuration for PWM generation is practically the same, except that timer0 is an 8-bit timer, and therefore the PWM generated by this timer will always have a bit width of 8. Therefore, when configuring this timer, the PWM bit width is not indicated:

Config Timer0 = Pwm, Compare A Pwm = Clear Up, Compare B Pwm = Clear Down, Prescale = 64



Now a similar example with LEDs, but now we will generate a pwm using Timer0:


$ regfile = "attiny2313.dat"
$ crystal = 4000000

Config Timer0 = Pwm, Compare A Pwm = Clear Down, Compare B Pwm = Clear Up, Prescale = 8
Config PORTB.2 = Output
Config PORTD.5 ​​= Output

Incr Pwm0a " smoothly increase the value of the register OCR0A
Incr Pwm0b ​​" smoothly increase the value of the register OCR0B

Waitms 20 "add a delay

Loop

End

Connect the LEDs to the Timer0 PWM output, as shown in the diagram:

Everything is the same here: the first LED (D1) will gradually gain brightness, and the second (D2) will smoothly go out.


Counting the PWM Generation Frequency

If you want to know the frequency of PWM generation, then this is not difficult to do. Look at the formula below:

PWM frequency = (quartz frequency / prescaler) / (count register size * 2)

For example, let's count several values:

1. Quartz frequency = 4000000 Hz, prescaler = 64, PWM width 10 bits => count register size = 1024

PWM frequency = (4000000/64) / (1024 * 2) = 122 Hz

2. Quartz frequency = 8,000,000 Hz, prescaler = 8, PWM width 9 bits => count register size = 512

PWM frequency = (8000000/8) / (512 * 2) = 976.56 Hz

3. The frequency of quartz is 16000000 Hz, the prescaler = 1, the width of the PWM is 8 bits => the size of the counting register = 256

PWM frequency = (16000000/1) / (256 * 2) = 31250 Hz

Did you like the article? To share with friends: