Menu English Ukrainian russian Home

Free technical library for hobbyists and professionals Free technical library


ENCYCLOPEDIA OF RADIO ELECTRONICS AND ELECTRICAL ENGINEERING
Free library / Schemes of radio-electronic and electrical devices

Beginner PIC microcontroller programmers. Encyclopedia of radio electronics and electrical engineering

Free technical library

Encyclopedia of radio electronics and electrical engineering / Beginner radio amateur

Comments on the article Comments on the article

[an error occurred while processing this directive]

Based on my own experience of starting to study microcontroller programming, I will try to give some practical advice on writing programs in assembler. All the programming examples below are given in relation to Pic controllers of the medium Microchip family, as the most suitable for starting development, due to their relatively simple architecture and a simple system of assembler commands.

The proposed programs can be used in the form of ready-made macros (complete subroutines). They are not tied to a specific controller, so the data from the datasheets should be taken into account when applying.

1. Application of timer overflow interrupts TMR0 (RTCC)

Let's take the clock frequency - Ftact. = 4,096 MHz (standard quartz). Then the cycle time will be tc = 1 / Ftact. * 4 = 0,97656 µs

INI_TMR ; initialization of interrupt mode from RTCC
bsf STATUS,RP0 ; choose bank 1
movlw b'00000100'
movwf OPTION ; prescaler for RTCC 1 : 32
bcf STATUS,RP0 ; bank 0
movlw b'10100000'
movwf INTCON ; RTCC interrupt enabled
movlw .96 ; uploading preliminary number 96 to RTCC
movwf TMR0

Get the interrupt time:
ti = tc * 32 * (256 - 96 = 160)
ti = 0,97656 * 32 * 160 = 5 µs = 000 ms

Now, if you enter an infinite loop into any program (the so-called interrupt waiting loop), and translate the end of the program to this loop, we will get a time reference of 5 ms. And after the interruption, the program will return to the address indicated by the interrupt vector (more often it is 04h ).What it can be used for - see below.

So:

;
0
START ; program start after
; power on
org 04h ; and this is the address of the interrupt vector, at which
main ; the main program will run
;
START ; this is usually where the mandatory ini-
INI_TMR ; cialization of ports, modes, registers, etc.
INI_PORTS
loop
goto-loop; and this is an endless loop
; ---------------------------------------------------- -

main
; Next comes the body of the main program,
; in which it is necessary to create an interrupt service program from RTCC,
; called by the CALL command:

ServTMR
btfsc INTCON,RTIF ; check the RTCC interrupt trigger flag and
call SET_TMR ; if yes, then initialize TMR0 again
return; if "no" - return to the place of ServTMR call in
; main program main
;
SET_TMR movlw .96
movwf TMR0 ; upload number 96 again
bcf INTCON,RTIF ; reset the trigger flag
retfie ; return with interrupt enable to ServTMR, and
; then to the main program main

An example of using an RTCC interrupt to receive a second pulse on one of the outputs, say, port B - RB0 : Use the Rsec register, which must be previously declared in the address field of the working registers.

Thus, at the output of the RB0 port, the signal level will change from '0' to '1' every second.

In the registers of the controller, information is usually in binary form (in binary code). But it is often necessary to obtain information in binary - decimal form (BCD - code), say, to control a seven-digit seven-segment indicator.

Let's consider examples of conversions of the binary code b2 to binary-decimal BCD and vice versa.

In an 8-bit register, you can write a binary number from 0 to 255 ( from b'00000000' to b'11111111' ). Let's convert a binary number into three digits of a binary - decimal code - "hundreds", "tens" and "ones". To do this, we will use the following registers, which must be declared in advance in the address field of working registers:

Rbin - register for storing a number in binary code b2
Rhan - register "hundreds" of BCD code
Rdec - register "tens" of the BCD code
Rsim - BCD code "one" register

Transformations are carried out using the operations of subtracting the numbers 100, and then 10, counting the number of positive subtractions.

FORM_1S ; in each cycle, and it lasts for the RTCC interrupt
incf Rsec,w ; 5 ms, increase the Rsec register by 1 to the number 200
xorlw .200 ; (5ms * 200 = 1 sec)
btfsc STATUS,z
goto OUT_PORT ; with Rsec = 200 flag z = '1' and transition to control
; pin RB0 of port B
return; return to the main program main
;
OUT_PORT btfss PORTB,0 ; check the status of the output RB0
goto OUT_ON ; if RB0 ='0' then set to '1'
bcf PORTB,0 ; otherwise - set to '0'
goto main ; return to main program
;
OUT_ON bsf PORTB,0 ; set RB0 = '1'
goto main

CON_100 movlw .100 ; subtract 100 from Rbin and check that
subwf Rbin,w ; the result is not negative. Flag 'c' = 1 when
btfss STATUS,c ; result > or = 0, and 'c' = 0 when < 0
goto CON_10
incf Rhan,f ; counting "hundreds"
movwf Rbin ; the result of the subtraction is first stored in a register
goto CON_100 ;battery and only then return to Rbin
; so as not to lose the remainder with a negative
; subtraction result.
CON_10 movlw .10 ; similarly define "tens"
subwf Rbin,w
btfss STATUS,c
goto end_con
incf Rdec,f
movwf Rbin
goto CON_10;
end_con
movf Rbin,w
movwf Rsim ; after subtractions, we enter the remainder in "units"
;continue program execution

Reverse conversion of BCD - code to b2. We use the same registers Rhan, Rdec, Rsim where the number is located in the BCD code, the registers RbinH are the high order and RbinL - the low order for numbers (> 255) in the b2 code and auxiliary registers RM1 - "multiplier", RM2 - "multiplier" .To convert BCD to b2, you need to multiply "hundreds" by 100, "tens" by 10 and add everything together with "units" and taking into account the transfer to the high order if necessary. For multiplication, we use the addition operation.

B2X_100 movlw .99 ; converting "hundreds"
movwf RM2 ; multiplier = number of additions (100) minus one
movf Rhan,w
movwf RM1 ; multiplicand = "hundreds"
loopX100 addwf RM1,w btfsc STASTUS,c ; check the transfer to the highest digit
incf RbinH,f ; if there is a transfer
decfsz RM2,f ; control the number of additions
goto loopX100
movwf RbinL ; the result of the addition is entered in the ml register. discharge
;
B2X_10 movlw .9 ; converting "tens"
movwf RM2 ; multiplier = number of additions (10) minus one
movf Rdec,w
movwf RM1 ; multiplicand = "tens"
loopX10 addwf RM1,w ; here the transfer can be omitted, because result
decfsz RM2,f ; always < 255
goto loopX10
addwf RbinL,f ; add the result of converting "tens"
btfsc STATUS,c ; taking into account the possible transfer in digits
incf
RbinH,f
movf Rsim,w
addwf Rbin,f ; add "units" taking into account the possible transfer
btfsc STATUS,c
incf RbinH,f

End of transformations and further execution of the program. In registers RbinL and RbinH got 16 - bit number in b2 code.

To perform the arithmetic operation of division, by analogy with the multiplication discussed above, the subtraction operation is used. Suppose we need to divide the number located in the registers RHsum (higher digits) and RLsum (lower digits) - by a divisor (let's take a divisor not > 255) located in the Rdel register.

The result will be entered into the registers RHrez and RLrez (high and low digits, respectively):

OP_DEL
movf Rdel,w
subwf Rlsum,w
btfss STATUS,c ; check if the result is negative?
goto DEF_carry ; if "yes", then we make a loan from art. discharge
incf RLrez,f ; count the number of subtractions, taking into account
btfsc STATUS,c ; possible promotion to senior level
incf RHRez,f
movwf RLsum ; restore the rest so as not to lose
goto OP_DEL ; with a negative result of subtraction
;
DEF_carry
movlw 0h
xorwf RHsum,w ; did everyone take from the senior category to the junior?
btfsc STATUS,z ; if "yes", i.e. RHdel = 0 and negated in OP_DEL
goto OUT_DEL ; cat. result - end of division and exit
decf RHsum,f ; if "no" - a loan from the senior category and pro-
incf RLrez,f ; we must go on
btfsc STATUS,c ; checking the need to transfer to the senior category
incf RHRez,f
goto OP_DEL

Author: Vladimir D., degvv@mail.ru; Publication: cxem.net

See other articles Section Beginner radio amateur.

Read and write useful comments on this article.

<< Back

Latest news of science and technology, new electronics:

Machine for thinning flowers in gardens 02.05.2024

In modern agriculture, technological progress is developing aimed at increasing the efficiency of plant care processes. The innovative Florix flower thinning machine was presented in Italy, designed to optimize the harvesting stage. This tool is equipped with mobile arms, allowing it to be easily adapted to the needs of the garden. The operator can adjust the speed of the thin wires by controlling them from the tractor cab using a joystick. This approach significantly increases the efficiency of the flower thinning process, providing the possibility of individual adjustment to the specific conditions of the garden, as well as the variety and type of fruit grown in it. After testing the Florix machine for two years on various types of fruit, the results were very encouraging. Farmers such as Filiberto Montanari, who has used a Florix machine for several years, have reported a significant reduction in the time and labor required to thin flowers. ... >>

Advanced Infrared Microscope 02.05.2024

Microscopes play an important role in scientific research, allowing scientists to delve into structures and processes invisible to the eye. However, various microscopy methods have their limitations, and among them was the limitation of resolution when using the infrared range. But the latest achievements of Japanese researchers from the University of Tokyo open up new prospects for studying the microworld. Scientists from the University of Tokyo have unveiled a new microscope that will revolutionize the capabilities of infrared microscopy. This advanced instrument allows you to see the internal structures of living bacteria with amazing clarity on the nanometer scale. Typically, mid-infrared microscopes are limited by low resolution, but the latest development from Japanese researchers overcomes these limitations. According to scientists, the developed microscope allows creating images with a resolution of up to 120 nanometers, which is 30 times higher than the resolution of traditional microscopes. ... >>

Air trap for insects 01.05.2024

Agriculture is one of the key sectors of the economy, and pest control is an integral part of this process. A team of scientists from the Indian Council of Agricultural Research-Central Potato Research Institute (ICAR-CPRI), Shimla, has come up with an innovative solution to this problem - a wind-powered insect air trap. This device addresses the shortcomings of traditional pest control methods by providing real-time insect population data. The trap is powered entirely by wind energy, making it an environmentally friendly solution that requires no power. Its unique design allows monitoring of both harmful and beneficial insects, providing a complete overview of the population in any agricultural area. “By assessing target pests at the right time, we can take necessary measures to control both pests and diseases,” says Kapil ... >>

Random news from the Archive

Transistors of the MDMESH V family 30.06.2010

MDMESH V transistors are the best transistors in the world in terms of open channel resistance in the operating voltage range of 500...650 V.

For example, transistors of the STW77N65M5 series from the MDMESH V family have a maximum Rdson value of 650 ohms for an operating voltage of 0,033 V and a maximum static current of 69 A. At the same time, the gate charge of such a transistor is only 200 nK. The STL21N65M5 is the first MDMESH V transistor in a PowerFlat package.

With an operating voltage of 650 V, the STL21N65M5 transistor has an open channel resistance of 0,190 ohms and a maximum static current of 17 A, while its gate charge is 50 nK.

Other interesting news:

▪ Himiway electric bikes Pony, Rambler and Rhino models

▪ Optimization of magnetic resonance imaging

▪ Drones can smell

▪ cockroach cure

▪ Winters will be cold

News feed of science and technology, new electronics

 

Interesting materials of the Free Technical Library:

▪ website section Television. Article selection

▪ article What kind of stop is this, Bologoe or Popovka? Popular expression

▪ article Which shoe company gives away another pair of shoes to poor children for every pair of shoes they sell? Detailed answer

▪ article Omezhnik water. Legends, cultivation, methods of application

▪ article Breadboard for microcircuits. Encyclopedia of radio electronics and electrical engineering

▪ article Norms of acceptance tests. Capacitors. Encyclopedia of radio electronics and electrical engineering

Leave your comment on this article:

Name:


Email (optional):


A comment:





All languages ​​of this page

Home page | Library | Articles | Website map | Site Reviews

www.diagram.com.ua

www.diagram.com.ua
2000-2024