Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

delay for RS232

Status
Not open for further replies.

ulot

New Member
I need a bit more clarity of delay routines.

how does this code implement a 104uS delay for RS232 serial data transfer using 4Hz clock?

Bit_Delay MOVLW 0x18
MOVWF Delay_Count
Bit_Wait NOP
DECFSZ Delay_Count , f
GOTO Bit_Wait
RETURN
 
I need a bit more clarity of delay routines.

how does this code implement a 104uS delay for RS232 serial data transfer using 4Hz clock?

By taking 104uS to execute all those instructions I imagine.

Assuming it's a 4Mhz clock, not 4Hz:confused: then the instruction cycle time is 1uS. Remember that GOTO, RETURN and the SZ part of the DECFSZ all take two cycles while anything else takes one. The loop executes 0x18 times which is decimal 24 times. From that it's quite easy to work out how many instructions cycles it will take the whole lot to execute and return.
 
Last edited:
I need a bit more clarity of delay routines.

how does this code implement a 104uS delay for RS232 serial data transfer using 4Hz clock?

Bit_Delay MOVLW 0x18
MOVWF Delay_Count
Bit_Wait NOP
DECFSZ Delay_Count , f
GOTO Bit_Wait
RETURN

Bit_Delay ; when called, this takes 2 instructions!

MOVLW 0x18 ; Moves literal value into Working reg. (1 instruction)
MOVWF Delay_Count ; Moves working reg contents into Variable (1 instruction)

Bit_Wait

NOP ; no operation. (1 instruction)
DECFSZ Delay_Count , f ; decrement 'Delay_Count (18Hex, 24 decimal) (1 instruction)
GOTO Bit_Wait ; goto bit wait. (2 instructions as its a program branch).
RETURN ; when done, go back to code. (1 instruciton I 'think').

So, the first part of it, under 'Bit_Delay' takes 2 cycles.

The second part is a loop, which is 4 cycles long (1, NOP + 1 for decfsz + 2 for goto). 'Bit_Wait' is executed until Delay_count is zero. So it loops 24 times (because 18hex is 24 in dec).

So..the loop is...4 * 24 = 96. Cycles long.

We 'call' the delay routine, which is a program branch and so takes 2 cycles. There are two further cycles at the start (under 'bit_Delay). So thats 4 at the start.

I think the 'return' part takes two instructions, maybe 1. We'll say 2 for now.

so..

Call = 2.
Pre-loop stuff = 2
Loop = 96
Return = 2

2+2+96+2 = 102. Not spot on but not bad. With a 4MHz osc, each instruction is 1us. Which makes delay calculations easy. So thats 102us. 104us is the period of a bit with a baud of 9600. (or half bit delay in 4800, used for the reciever). So the error in timing *IF* I'm correct is 102/104 = 1%. Thats fine.

Someone correct me if I'm wrong....its been a few months since I did PIC assembly, its all C over here now.

Blueteeth
 
So the error in timing *IF* I'm correct is 102/104 = 1%. Thats fine.

If the delay is used within a serial TX/RX routine it will come with some code to shift bits in/out of an I/O port which will end up stretching that delay several cycles over the 104 anyway.
 
Wow thanks. case closed on delay.

just a thought. i saw an online delay generator software, can i lay my hands on a downloadable version 'cos that one was embeded in the webpage?
 
Status
Not open for further replies.

Latest threads

Back
Top