Programming PIC16F876A. We assemble a circuit with a smoothly flashing LED

    This article is aimed at newbies in programming PIC16 microcontrollers from Microchip . In our case, I chose a slightly superior microcontroller for such tasks, namely the PIC16F876A. We will program the microcontroller in the MPLAB IDE .

    Purpose of work: to assemble a circuit that will flash an LED, attention, using PWM .

    And so, the goal of the task is indicated. Now let's move on to the implementation of our plans.

    Part 1. Iron.


    First of all, we will need components from which we will assemble the circuit. Namely:
    • Microcontroller PIC16F876A
    • Loaf to him
    • Light-emitting diode
    • Bread board
    You can take the LED to your liking whichever you like best.
    It is desirable to have a prototype board available.
    The concept of “looseness” includes such details as: a pair of capacitors for quartz and a capacitor to the output of the CPP module (in order to smooth out the ripple).

    The assembled circuit is as follows:



    This is a typical inclusion of a microcontroller, I did not come up with 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 development:



    Part 2. Software.


    We connect the LED to the 1st CPP module (PWM). The CPP1CON register is responsible for configuring the module in the microcontroller . For the module to work with us, you must first initialize the timer. For PWM mode, the TMR2 timer is used . The T2CON register is responsible for its configuration . Initialization:
    movlw  .0     
    bcf   STATUS, 5

    movwf  T2CON ; Помещаем в регистр T2CON - 0
    bsf   T2CON, 0 ; Устанавливаем бит T2CKPS0 (Предделитель)
    bsf   T2CON, 2 ; Включаем таймер TMR2 битом TMR2ON
    bsf   T2CON, 3 ; Устанавливаем бит TOUTPS0 (Постделение)

    This completes the initialization of the timer. Now, when the controller is turned on, it will serve as a source for our PWM module.

    The initialization of the PWM module is as follows:
    movlw   00101111b ; Подготавливаем конфигурацию
    movwf   CPPCON ; Конфигурируем ШИМ
    bsf    CPPCON, 2 ; Включаем модуль ШИМ
    That's it, the initialization is over. Now we can put a number from 0 to 255 in the CPP1L register, thereby changing the duty cycle of the output pulses.

    Full source code for the firmware of our microcontroller:

    STATUS   equ     03h    
    TRISC    equ     07h                
    CPPCON   equ      17h
    CPP1L    equ      15h
    T2CON    equ      12h
    counter  equ      23h
    tmp      equ      25h    
          org     0     
          goto    start    
    start   
      bsf     STATUS, 5  
                     
      movlw    .0     
      movwf    TRISC   
      bcf      STATUS, 5
      movwf    T2CON
      bsf      T2CON, 0
      bsf      T2CON, 2
      bsf      T2CON, 3
      movlw    00101111b
      movwf    CPPCON
      bsf     CPPCON, 2
      movlw    .0
      movwf    CPP1L
      movlw    .255
      movwf    tmp
      decfsz  tmp, 1
        goto $+2
        goto $+4
          call delay10mS
          incf CPP1L, 1
          goto $-5
      movlw    .255
      movwf    tmp
        decfsz  tmp, 1
        goto $+2
        goto $+4
          call delay10mS
          decf CPP1L, 1
          goto $-5
      goto $-16
    delay10mS
      movlw  .50
      movwf  counter
    loop
      call  delay200uS
      decfsz  counter
      goto  loop
      return
    delay200uS
      movlw  .100  
      addlw  -1
      btfss  STATUS,2
      goto  $-2
      return  
    end  
    A brief note on the commands used in the program.
    equ - Assigning a name to a specific address.
    goto - Transition of the program to the label or a specific
    call line - Call of the subroutine
    movlw - Place the number W in the register W;
    movwf - Move the number bsf from the W
    register - Set the bit in the register to state 1
    bcf - Set the bit in the register to state 0
    addlw - Add the number
    btfss to the W register - Check the bit in the register for 1
    incf - Incriminate the register (add 1)
    decf - Decriminalize the register (subtract 1)
    decfsz- Subtract 1 from the register + check for 0

    Delays in the program are calibrated to the frequency of the quartz resonator at 8 MHz.

    The principle of the program.
    At the beginning, the registers are initialized, then the internal modules of the microcontroller are configured.

    In the variable tmp we can set the duty cycle, thereby changing the maximum brightness of the LED.

    Next, the part of the program that is responsible for the LED itself blinking is implemented, taking into account the use of PWM. First, by incriminating CPP1L to the tmp value, we make the LED slowly light up, and then do the reverse operation.

    Part 3. Final


    Before flashing your controller, you need to set the microcontroller configuration bits. Without them, nothing will work.

    And so:
    1) WDT - turn off. This is a watchdog. 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 supplies the MCLR 13B.
    3) Oscillator: In this case, we have 8 MHz. So we set the value of XT.

    Part 4. Bonus.


    Video for those who have not programmed / assembled circuits yet, but really want to see the result:



    Part 5. Information.


    Microchip official website - www.microchip.com
    Quoted sources - www.wikipedia.org
    Russian documentation for PIC microcontrollers - www.microchip.ru/lit/?mid=1x0

    Also popular now: