1 Introduction
In order to give a summary of the PIC16F616 MCU for a while and to facilitate everyone's communication, I wrote this PIC MCU learning experience, which was organized in my own language after reading the manual and programming debugging. Or where there are doubts, I hope that everyone can give corrections and criticisms in a timely manner and offer valuable opinions.
2. Overview of PIC microcontroller
The PIC16F616 is a 14-pin, 8-bit CMOS microcontroller. With a reduced instruction set, there are only 35 instructions. Because of the Harvard bus structure, which uses a separate data bus and instruction bus, most of the instructions are single-cycle instructions except that a small number of instructions are not single-cycle. This is beneficial to improve the operating speed and execution efficiency of the microcontroller.
The PIC16F616 can be powered from 2V to 5.5V. It integrates an RC oscillator with a frequency of 8MHz or 4MHz. It can also be clocked by an external crystal. It integrates hardware modules such as AD conversion and comparator, and also has functions such as power-on reset, undervoltage reset, watchdog, and code protection. Three timers, PWM generators, etc. can be programmed by the user. Let me introduce these modules and functions about PIC microcontrollers.
3. Memory
The PIC16F616 is divided into program memory and data memory. The size of the program memory is 2048words, and the size of the data memory is 128bytes.
The address of 0000H in the program memory is the reset address. When the power is turned on or the watchdog timer is reset, it will cause the PC pointer to point to the reset address. Address 0004H is the interrupt address, and the PC pointer will point to this address whenever an interrupt occurs. The program can be transplanted at the address 0005H~07FFH.
The data memory is divided into two parts, namely bank0 and bank1, where bank0 has an address range of 00H-7FH and Bank1 has an address range of 80H-FFH. The general registers are placed inside. Bank0 and bank1 can be selected by the RP0 bit in the STATUSL register.
When programming, be aware that when you want to operate the register in bank0, you must first select bank0 (put the RP0 of the register STATUS to 0), and then operate on the register you want to operate, when you want to operate When the register is in bank1, the bank1 should be selected first.
If you want to define some variables, you can define the address at the beginning of data memory 20H. The defined address range is 20H-7FH. Generally enough is enough.
4.PIC input and output ports
In the learning of this part, I have encountered some problems. PIC microcontrollers have few pins, most of which are multiplexed pins, such as AD, IO, comparator, external crystal oscillator, etc., so when configuring the port, Be sure to know how each function can be set up. In this section, I am going to talk about the setup of the general IO port.
The PIC16F616 has 12 IO ports, but one pin (RA3) can only be used as an input pin. It cannot be used as an output. In addition, port A has a level change interrupt function, while port C does not. It must be designed. note.
When setting, generally do the following settings:
(1) Whether the setting port is an analog port or a digital port can be set by the register ANSEL. For example, if you want to use AD, you should set the corresponding pin as an analog input port.
(2) If you choose a digital port, the next step is to set the direction of the port, whether it is input or output (except RA3), which can be set by register TRISA (A port) or TRISC (C port).
(3) Set the output level of the port, which can be set by register PORTA (port A) or PORTC (port C).
This is a common setting for the IO port, but this is not the whole setting. The next setting depends on the A port or the C port. For port A, it has several special features: internal weak pull-up, interrupt-on-change, and edge interrupt on the RA2/INT pin. If you want these features, you need to set the corresponding registers.
Weak pull-up setting: weak pull-up is only effective when the pin is output. The weak pull-up of the corresponding pin can be set by the register WPUA. It is worth mentioning that if the weak pull-up is turned on, there will be excess current. Waste, this is not desirable for low-power design, but if you do some keyboard circuit design, for example, you can turn on the weak pull-up function, so you don't need to add a pull-up resistor to the keyboard circuit.
Setting of the level change interrupt: It can be set by the register IOCA, but first the corresponding pin is set to the digital port and is the input state. At the same time, the REIE bit of the INTCON register should be set to 1, and the total interrupt should be enabled (set the GIE bit of the INTCON register). If the corresponding pin has this function, when this pin level occurs, an interrupt will be generated. At the same time, some interrupt flag bits are set (the RAIF bit of INTCON is set), and the total interrupt GIE is set to 0. In the interrupt service routine, the software clears the RAIF bit and resets the GIE bit to continue the interrupt.
Interrupt setting of the RA2/INT pin: First, set the corresponding pin to the digital port and the input state. Set the INTF bit of INTCON to 1, which means that the external interrupt of the int pin is allowed. The INTEGD bit of the OPTION_REG register can be set to rise. Interrupted along the interrupt or falling edge. When an interrupt occurs, the INTF bit of INTCON is set to '1' and the GIE is cleared. In the interrupt service routine, software must clear the INTF bit and reset the GIE bit to continue the interrupt.
For the C port, the level change interrupt and the edge interrupt cannot be generated.
5. Timer
Timer is a very important part of the microcontroller, it can generate a lot of different timing time to meet the different needs of programming. PIC16F616 has three timers, respectively TImer0, TImer1, TImer2. Their usage is not the same, Let's talk about the usage and setting of these three timers separately.
(1) Timer0
Timer0 is an eight-bit counter with an eight-bit counter register TMR0, an eight-bit prescaler (shared with the watchdog), an internal or external clock source, and a counter overflow interrupt.
Timer0 can be used as a timer or counter. The registers associated with Timer0 are: TMR0, INTCON, OPTION_REG, TRISA.
When Timer0 is used as a timer, set the T0CS bit of OPTION_REG to 0, indicating that the internal clock is used. The value of TMR0 will increase every instruction cycle (when there is no prescaler), when TMR0 is assigned. At the time, there will be two instruction cycle delays. The prescaler can be shared with the watchdog and can be set by the PEA bit of OPTION_REG. When the PSA is 0, the divider selects Timer0. When the PSA is 1, the divider selects the watchdog. At the same time, the divider value of the divider can be set by the register OPTION_REG. The set value can be from 1:2 to 1:256. When the counter TMR0 of Timer0 counts from FFH to 00H, an overflow occurs and the overflow flag is generated. (T0IF bit in the INTCON register) is set (regardless of whether Timer0 interrupt is enabled). If the interrupt is enabled (T0IE in the INTCON register is set), then an overflow interrupt will be generated. The T0IF bit will need to be cleared by software. .
When Timer0 is used as a counter, it is necessary to use an external clock source (T0CS of OPTION_REG is set to 1). Each time the edge of the T0CK1 pin arrives, the TMR0 of Timer0 will increase by 1. The rising and falling edges can be set by the T0SE of OPTION_REG. To set it up. The interrupt is the same as when Timer0 is used as a timer. When we are programming, we can use Timer0 to time or generate timing information. Let me explain the calculation of the timer's timing time. Assuming that the clock source used by Timer0 is internal 4MHZ, the execution time of each instruction is 1us, the prescaler coefficient of Timer0 is 1:256, and the initial value of TMR0 is 6, then the timing time is:
256×(256-6)×1us=64ms
It should be noted during programming that the interrupt of Timer0 cannot wake up the MCU from the state of SLEEP.
(2) Timer1
Timer1 is a sixteen-bit counter. It has a count register pair (TMR1H:TMR1L). The clock source is also internal and external. It has a 2-bit prescaler that can be operated synchronously or asynchronously. It has an interrupt function, but the overflow interrupt can only be used on an external clock or asynchronous. Mode can wake up the microcontroller from SLEEP, Timer1 has capture/compare function, and some special event trigger function (ECCP), the output of the comparator can be synchronized with the clock of Timer1. Let's introduce these features one by one.
You can follow these steps when programming. Set the register T1CON, the clock source can select the external or internal clock source, the external clock source can select the LP crystal. When the internal clock is selected, Timer1 can run in the state of the timer. When the external clock is selected, it can run in the timer or It is the counter state. When working in the counter state, you can choose whether the threshold is high or low. These can all be set by register T1CON.
The following are the specific functions of each bit of T1CON: bit1: Whether Timer1 is enabled, when this bit is set to 1, Timer1 is enabled, when set to 0, Timer1 is turned off; bit2: Clock source selection bit, when set, select external clock (The rising edge of the T1CK1 pin). When this bit is set to 0, the internal clock is selected and matched with T1ACS (register CM2CON1). When the T1ACS bit is 0, the clock is FOSC/4. When the T1ACS bit is 1, The clock is FOSC.bit2: T1SYNC: Timer 1 external clock input sync bit. When TMR1CS bit is 1, T1SYNC bit is 1, timer 1 is set to be out of sync with external clock, T1SYNC bit is 0, timer 1 It is set to synchronize with external clock mode. Bit3: T1OSCEN: When this bit is 1, the clock of Timer1 selects LP. When it is 0, the LP crystal is turned off. Bit5-4: T1CKPS: Set the prescaler of the Timer1 clock. Bit means that Timer1 can be set to 1:1, 1:2, 1:4, 1:8 frequency division value. Bit6: TMR1GE: Only valid when TMR1ON bit is 1, when this bit is 1 When the Timer1 count is controlled by the threshold of Timer1, when this bit is 0, Timer1 counts normally. Bit7:T1GINV: When this bit is 1, Timer1 Threshold count is high, this bit is 0, Timer1 is the threshold count is low.
Timer1 interrupt programming: When the Timer1 timer overflows, an interrupt will be generated if the Timer1 interrupt is enabled. The interrupt can be set as such, Timer1 interrupt enable bit TMR1IE (in the PIE1 register) is set, the PEIE bit of register INTCON is set, and the total interrupt bit GIE (in register INTCON) is set to 1. When the timer generates an interrupt. The interrupt flag T1IF is set to 1 (located in register PIR1), and then the PC pointer points to the 0004H address. The T1IF bit must be cleared by software.
(3) Timer2
The function of Timer2 is different in Timer1. An eight-bit counter in Timer2 has an eight-bit counter register TMR2. Timer2 has the following functions: two dividers, one is the prescaler and the other is the postscaler. . The frequency division can be set by software. In addition, the clock source of Timer2 is the instruction time (FOSC/4), and Timer2 has a register PR2. The function of this register is to generate an interrupt when TMR2 is increased to the value of PR2. Of course, the interrupt must be Allow, then the value of PR2 will be changed back to 00H. Let's introduce the programming of Timer2:
Timer2's control register T2CON is used to set the division and closing factor of Timer2's on and off and before and after division. The TOUTPS "3:0" bit of the register T2CON is set to the division factor, which can be set to 1:1~1:16; When TMR2ON is 1, Timer2 is enabled. When it is 0, Timer2 is turned off. Bit T2CKPS "1: 0" can set the prescaler coefficient, which can be set to 1, 4, 16.
The Timer2 interrupt can be controlled by allowing the Timer2 interrupt bit, TMR2IE (in the PIE1 register), to be set. The Timer2 interrupt is enabled. When set, the Timer2 interrupt is disabled. The PEIE bit of register INTCON is set, and the total interrupt bit GIE (located in register INTCON) is set to 1. With the above settings, Timer2 can generate an interrupt. When the timer generates an interrupt, the interrupt flag T2IF is set to 1 (located in register PIR1), and then the PC pointer points to the 0004H address. The interrupt flag bit T2IF must be cleared by software.
6.AD module
The PIC16F616 has a ten-bit, eight-way AD converter. The reference voltage can be either the supply voltage VDD or the external reference voltage (VREF pin). When the AD conversion is completed, an interrupt can be generated. This interrupt can wake up the microcontroller from sleep. Let's introduce the programming method for AD conversion.
To use an ADC, there are a few things to do:
(1) Set the port. The port that needs to sample the analog signal must be set to the analog input state. If it is set to digital port, the conversion result will be incorrect. The analog input of the port can be configured by the register ANSEL. When the RA port is spoken, Said how to configure it.
(2) Channel selection, there are eight external channels and three internal channels. The channel selection can be set by the CHS "3:0" bit in the ADCON0 register.
(3) The reference voltage can be selected as VDD or external reference voltage, which can be set by the VCFG bit in the ADCON0 register. When VCFG=0, the reference voltage is VDD. When VCFG=1, the reference voltage Is the external reference voltage (from the VREF pin)
(4) ADC conversion format, the result of AD conversion is stored in a register pair: ADRESH and ADRESL, but the AD conversion result is only ten bits. Setting the AD conversion format can be selected by setting the ADFM bit of ADCON0, when ADFM=1 The lower eight bits of the 10-bit AD result are stored in ADRESL, and the upper two bits are stored in ADRESH; when ADFM=0, the upper eight bits of the 10-bit AD result are stored in ADRESH, and the lower two bits are stored in ADRESL.
(5) AD clock source selection, register ADCON1 specifically to set the AD clock source, ADCS "2:0" different combinations, you can set the AD clock source to a different frequency, can be FOSC/2, FOSC / 4, FOSC/8, FOSC/16, FOSC/32, FOSC/64 and FRC (internal RC).
(6) For the configuration of the AD interrupt, to use the interrupt function of AD, the AD interrupt can be enabled first, the ADIE bit is set to 1 (in register PIE1), the PEIE bit is set to 1 (in the INTCON register), and the total interrupt GIE bit is set. (in the INTCON register).
To start an AD conversion, first enable the ADC module, that is, set the ADON bit of the ADCON0 register, and then set the GO/DONE bit (in ADCON0) to start the AD conversion.
AD conversion takes time. It takes time to convert 1bit to Tad. Tad is related to the clock source of AD conversion and VDD. It takes 11 Tad time to convert ten bits. If the first AD conversion is completed, a second AD conversion is required. You must wait for 2*Tad to start. When an AD is completed, the GO/DONE bit will be set to 0. If the interrupt is enabled, an interrupt will be generated, and the interrupt flag bit, ADIF (within register PIR1), will be set, and AD can be converted in the AD interrupt routine. The result is read (read ADRESH and ADRESL) and the AD interrupt flag bit is cleared when needed.
The AD interrupt can wake up the microcontroller from sleep, but be aware that when using this function, the clock source must be set to FRC, otherwise the AD interrupt will not be generated during sleep.
power off protection connector,street light connector,Road Lamp Protection Connector
Guangdong Ojun Technology Co., Ltd. , https://www.ojunconnector.com