Programming the PIC16F876A. We collect the circuit with a smoothly flashing LED
This article is aimed at newcomers to Microchip PIC16 microcontroller programming. In our case, I chose a slightly superior microcontroller for such tasks, namely the PIC16F876A. Microcontroller programming will be done in MPLAB IDE .
The purpose of the work: to assemble a circuit that will flash the LED, attention, using PWM .
And so, the purpose of the task outlined. We now turn to the implementation of our plans.
Part 1. Iron.
First of all, we need components, from which we will assemble the circuit. Namely:
PIC16F876A microcontroller
Crumbly to it
Light-emitting diode
Bread board
The LED can be taken any taste you like. Development board is desirable to have in stock. The concept of “crumbling” includes such details as: a pair of capacitors for quartz and a capacitor at the output of the CPP module (In order to smooth out the pulsations). ')
The assembled circuit is as follows:
This is a typical inclusion of the microcontroller, I did not invent anything new here.
Also, for programming the microcontroller, I use the ICD2 debugger programmer. It connects to a computer via USB and works great on both Windows and GNU / Linux. In our case, we will use the native MPLAB IDE in Windows. Screen in progress:
Part 2. Software.
The LED is connected to the 1st CPP module (PWM). The register CPP1CON is responsible for setting the module in the microcontroller. In order for the module to work, we first need to initialize the timer. For PWM mode, the timer TMR2 is used . For its configuration is responsible register T2CON . Initialization:
A brief note on the commands used in the program. equ - Assign a name to a specific address. goto - Move a program to a label, or a specific string. call - subroutine call movlw - Put in register W, number movwf - Move a number from register W bsf - Set the bit in the register to state 1 bcf - Set the bit in the register to 0 addlw - Add a number to the register W btfss - Check bit in register for 1 incf - Incriminate the register (add 1) decf - De -register register (subtract 1) decfsz - Subtract 1 from register + check for 0
Delays in the program are calibrated to a quartz resonator frequency of 8 MHz.
The principle of the program. In the beginning, the registers are initialized, then the internal microcontroller modules are configured.
In the variable tmp we can set the duty cycle, thereby changing the maximum brightness of the LED.
Next, a part of the program is implemented that is responsible for the LED blinking itself, taking into account the use of PWM. First, by incriminating the CPP1L to the tmp value, we make the LED slowly start to glow, and then we do the reverse operation.
Part 3. Final
Before flashing your controller, you need to set the microcontroller configuration bits. Without them, nothing will work.
So: 1) WDT - turn off. This is a watchdog timer. Designed for hardware reset of the microcontroller at the time of an unexpected hang. 2) LWP - turn off. This is low voltage programming. We use a normal programmer that feeds on the MCLR 13B. 3) Oscillator: In this case, we have 8 MHz. So set the value of XT.
Part 4. Bonus.
Video for those who have not yet programmed / did not collect the scheme, but really wants to see the result: