How to Implement 25us Delays in PIC16F84A PWM Code?

In summary, the table can be converted to 4 columns by duplicating the middle column, onto the right; 0 x x = 0 x x x = 0%1 0 0 = 1 x 0 x = 25%1 0 1 = 1 x 1 x = 50%1 1 0 = 1 x 1 x 1 = 75%1 1 1 = 1 x 1 x 1 = 100%
  • #1
Noob of the Maths
52
6
Hello how are you :D

I have some problems with my code, which is to control the speed of a DC motor by pwm with the pic16f84A.

I already have the combinations and the diagram of the signals, but I still need to implement the part in bold.

How would the delays be implemented so that they have 25 us, 50 us, 75 us according to the diagram? I do not understand that part ;(
The time per cycle is 100 microseconds

Captura de Pantalla 2022-05-16 a la(s) 17.50.57.png
Captura de Pantalla 2022-05-16 a la(s) 17.51.32.png
 
Technology news on Phys.org
  • #2
If this is free running code, without interrupts, then three of the states will need two delay loops.
Declare a register called State, read RA into state
State = State AND 0h07 mask high five, keep only three lowest bits
There are 5 cases.
If State(bit2) = 0 then turn off; wait 100u; end
If State = 00 then turn on; wait 25; turn off; wait 75; end
If State = 01 then turn on; wait 50; turn off; wait 50; end
If State = 10 then turn on; wait 75; turn off; wait 25; end
If State = 11 then turn on; wait 100u; (turn off?); end
Is PortB, bit 0 the output to the motor?
 
  • #3
Baluncore said:
If this is free running code, without interrupts, then three of the states will need two delay loops.
Declare a register called State, read RA into state
State = State AND 0h07 mask high five, keep only three lowest bits
There are 5 cases.
If State(bit2) = 0 then turn off; wait 100u; end
If State = 00 then turn on; wait 25; turn off; wait 75; end
If State = 01 then turn on; wait 50; turn off; wait 50; end
If State = 10 then turn on; wait 75; turn off; wait 25; end
If State = 11 then turn on; wait 100u; (turn off?); end
Is PortB, bit 0 the output to the motor?
Oh, i now understand the times. Yes, the output to the motor can be any bit of the PortB.
 
  • #4
I would write straight code with unconditional equal delays
Declare a register called State,
label Cycle; it will get back here every 100 usec
read PortA into state
State = State AND 0h07; mask high five, keep only three lowest bits
If state(bit2) = 1 then turn on motor
delay 25 usec
if state = 0b100 then turn off motor
delay 25 usec
if state = 0b101 then turn off motor
delay 25 usec
if state = 0b110 then turn off motor
delay 25 usec
turn off motor here only if needed?
Goto cycle
 
  • #5
Baluncore said:
I would write straight code with unconditional equal delays
Declare a register called State,
label Cycle; it will get back here every 100 usec
read PortA into state
State = State AND 0h07; mask high five, keep only three lowest bits
If state(bit2) = 1 then turn on motor
delay 25 usec
if state = 0b100 then turn off motor
delay 25 usec
if state = 0b101 then turn off motor
delay 25 usec
if state = 0b110 then turn off motor
delay 25 usec
turn off motor here only if needed?
Goto cycle
The code its like this?
 

Attachments

  • Captura de Pantalla 2022-05-17 a la(s) 7.27.33.png
    Captura de Pantalla 2022-05-17 a la(s) 7.27.33.png
    36 KB · Views: 101
  • #6
You did not understand the straight code with unconditional equal delays. There are 4 delays of 25 usec in the loop. The total delay is therefore the loop period of 100 usec. If active, the motor is turned on at the start, then turned off after the appropriate delay, depending on the state of portA.

There are a couple of tricks needed to keep it compact. Also, the 50% state might better be done with [on-off-on-off], rather than [on-on-off-off].

I will see if I can write some PIC16F84 code tomorrow.
 
  • #7
Attached is untested code for 100 usec state machine loop.
You will need to make the delay 25 usec routine.
 

Attachments

  • PIC16F84code_1.pdf
    18.3 KB · Views: 101
  • Like
Likes Noob of the Maths
  • #8
@Noob of the Maths

25 usec is probably too short to be done efficiently with a timer.
An in-line delay might be more appropriate, then you can trim the number of cycles.
With a 4 MHz clock the instruction time is 1 usec.

Cblock ; assign delay variable to next available register in file
count
endc

; this is the tight in-line loop that does not use a timer
; Delay(25) start with a number that will make up to the 25 usec
movlw 12; adjust the start count
movwf count
decfsz count, f
goto $-1
; count is now zero, pad with a NOP if you need 1 usec more

I have been thinking about the state conversion.

Your table of 3 bit codes;
0 x x = 0%
1 0 0 = 25%
1 0 1 = 50%
1 1 0 = 75%
1 1 1 = 100%
Can be converted to 4 columns by duplicating the middle column, onto the right;
0 x x = 0 x x x = 0%
1 0 0 = 1 0 0 0 = 25%
1 0 1 = 1 0 1 0 = 50%
1 1 0 = 1 1 0 1 = 75%
1 1 1 = 1 1 1 1 = 100%
Notice how these 4 bits are now arranged so the number of bits set, is correct for the duty cycle.
To generate the motor PWM you only need output those 3 bits at the appropriate 4 times.
It is simplest to select PortB, bit 1, as the motor output.

; The code then becomes something like this
Loop
movf PortA,w ; get state
movwf pattern ; save pattern
btfss pattern,2 ; test motor on/off
clf pattern ; if motor off

; Copy bit(2) to output
rrf pattern,w
movwf PortB
Delay(25)

; Copy bit(1) to output
movf pattern,w
movwf PortB
Delay(25)

Copy bit(0) to output
rlf pattern,w
movwf PortB
Delay(25)

; Copy bit(1) to output
movf pattern,w
movwf PortB
Delay(25)

Goto Loop
 

FAQ: How to Implement 25us Delays in PIC16F84A PWM Code?

How does a speed controller for a PWM motor work?

A speed controller for a PWM motor works by adjusting the duty cycle of the PWM signal being sent to the motor. This controls the amount of power that is being delivered to the motor, ultimately controlling its speed.

What is the role of a PIC16F84A in a speed controller for a PWM motor?

The PIC16F84A is a microcontroller that is responsible for generating the PWM signal and controlling the duty cycle. It also receives input from sensors or user inputs to adjust the speed of the motor.

Can I use any other microcontroller instead of a PIC16F84A for a speed controller?

Yes, there are many other microcontrollers that can be used for a speed controller, but the PIC16F84A is a popular choice due to its low cost and ease of use.

How do I program a PIC16F84A for a speed controller for a PWM motor?

To program a PIC16F84A, you will need a programming tool such as a PIC programmer and a programming language such as C or assembly. You will also need the datasheet for the PIC16F84A to understand its pin configuration and programming instructions.

Are there any pre-made libraries or code examples available for a speed controller for a PWM motor using a PIC16F84A?

Yes, there are many pre-made libraries and code examples available for a speed controller for a PWM motor using a PIC16F84A. These can be found online or through microcontroller forums and communities.

Similar threads

Replies
5
Views
4K
Replies
11
Views
1K
Replies
5
Views
1K
Replies
1
Views
1K
Replies
3
Views
1K
Replies
7
Views
1K
Replies
6
Views
5K
Back
Top