[PIC] PIC16f887 delay 99ms

Status
Not open for further replies.

Jorge Hernández Garay

Newbie level 2
Joined
Nov 9, 2014
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
52
Hi guys! firstly thank you everyone for this excellent forum and for the help i'll receive even before i get it!

i'm a newbie in coding Assambly, i have a really terrible teacher and most of the time we students need to figure it out by ourselves.. not fair at all...

anyway... i need to create a code for counter delay of 99mS and i have no idea in how to start! i did before a simple one of 7ms but this new one use 16 bits and tmr1h tmr1l etc....

could you please guys give me a hand? what i've done was this:


Code ASM - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
;SET CONFIG REGISTER
 
        __CONFIG _CONFIG1,_LVP_OFF& _FCMEN_ON& _BOR_OFF& _CPD_OFF& _CP_OFF& _MCLRE_ON& _PWRTE_ON& _WDT_OFF& _INTRC_OSC_NOCLKOUT
 
        __CONFIG _CONFIG2,_WRT_OFF& _BOR21V
 
        ERRORLEVEL -302
 
;MACROS
BANK0   MACRO                                   ;SET UP MACRO TO SWITCH TO BANK 0
                        BCF STATUS, RP0
                        BCF STATUS, RP1
                        ENDM
 
BANK1   MACRO
                        BSF STATUS, RP0
                        BCF STATUS, RP1
                        ENDM
 
BANK3   MACRO
                        BSF STATUS, RP0
                        BSF STATUS, RP1
                        ENDM
 
;***************        VARIABLE DEFINITIONS    **************************************************************
COUNT1  EQU     0X20            ;USED FOR DELAY LOOP
COUNT2  EQU     0X21            ;USED FOR DELAY LOOP
;*********************************************************************************************************
;PROGRAM START
 
        ORG     0X000           ;PROCESSOR RESET VECTOR
        GOTO    MAIN    ;GO TO BEGINNING OF PROGRAM
MAIN    CALL SETUP
                CALL DELAY_TEST
 
                
 
 
 
                
 
;       GOTO REPEAT
 
;*********************************************************************************************************
;**************************     SETUP PROCEDURE **************************************************************
;       Initial settings for Pic Ports and Peripherals'
;*********************************************************************************************************
 
 
SETUP
        BANK0           ;SWITCH BANK TO BANK 0
        CLRF PORTA
        CLRF PORTB      ;SET UP INITIAL PORTB O/P CONDITIONS
        CLRF PORTC      
        CLRF PORTD
        CLRF PORTE
 
        BANK1                   ;SWITCH TO BANK1
 
        MOVLW 0XF0
        MOVLW TRISA             ;SET TRISA TO ALL O/Ps
        CLRF TRISC              
        CLRF TRISD
        BSF  TRISE, 0
        MOVLW 0X07
        MOVWF OPTION_REG
 
 
 
 
        BANK3
        CLRF ANSEL
        CLRF ANSELH
 
        BANK0                   ;SWITCH BACK TO BANK 0
        RETURN
 
;********************************************************************************************************
;*********************  DELAY FOR APPROX 22mS ***********************************************************
; MAXIMUM DELAY AVAILABLE APPROX 65mS (4MHz PROCESSOR)
;********************************************************************************************************
 
DELAY_TEST
        BCF INTCON, T0IF
        MOVLW 0XAA                      ;SET UP DELAY COUNTERS
        MOVWF TMR0
XX  BTFSS INTCON, T0IF
        GOTO XX
        
 
RETURN 
 
;********************************************************************************************************
 
        END


thank you in advance~~!!!!!!
 


thank you mate! but how do we determine the 16 bits for TMR1H AND TMR1L?

I'm using the formula: 65536- (99ms X (1Mhz/4) so my result is 16036 which in HEX is: 3EA4 then split in 2 :

TMR1H 3E
TMR1L A4

but still no working. this is what i've done:

Code ASM - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
;PROGRAM START
 
        ORG     0X000           ;PROCESSOR RESET VECTOR
        GOTO    MAIN    ;GO TO BEGINNING OF PROGRAM
MAIN    CALL SETUP
                CALL DELAY_TEST_2
 
                
 
 
 
                
 
;       GOTO REPEAT
 
;*********************************************************************************************************
;**************************     SETUP PROCEDURE **************************************************************
;       Initial settings for Pic Ports and Peripherals'
;*********************************************************************************************************
 
 
SETUP
        BANK0           ;SWITCH BANK TO BANK 0
        CLRF PORTA
        CLRF PORTB      ;SET UP INITIAL PORTB O/P CONDITIONS
        CLRF PORTC      
        CLRF PORTD
        CLRF PORTE
        BTFSS PIR1, TMR1IF
        MOVLW 0X31
        MOVWF T1CON
 
        BANK1                   ;SWITCH TO BANK1
 
        MOVLW 0XF0
        MOVLW TRISA             ;SET TRISA TO ALL O/Ps
        CLRF TRISC              
        CLRF TRISD
        CLRF  TRISE
;       MOVLW 0X07
;       MOVWF OPTION_REG
        BSF PIE1, TMR1IE
 
 
        BANK3
        CLRF ANSEL
        CLRF ANSELH
 
        BANK0                   ;SWITCH BACK TO BANK 0
        RETURN
 
;********************************************************************************************************
;*********************  DELAY FOR APPROX 22mS ***********************************************************
; MAXIMUM DELAY AVAILABLE APPROX 65mS (4MHz PROCESSOR)
;********************************************************************************************************
 
DELAY_TEST
        BCF INTCON, T0IF
        MOVLW 0XAA                      ;SET UP DELAY COUNTERS
        MOVWF TMR0
XX  BTFSS INTCON, T0IF
        GOTO XX
        
 
RETURN 
;********************************************************************************************************
;*********************  DELAY FOR APPROX 22mS ***********************************************************
; MAXIMUM DELAY AVAILABLE APPROX 65mS (4MHz PROCESSOR)
;********************************************************************************************************
 
DELAY_TEST_2
        BTFSS PIR1, TMR1IF
        MOVLW 0X3E              ;SET UP DELAY COUNTERS
        MOVWF TMR1H
        MOVLW 0XA4
        MOVWF TMR1L
YY  BTFSS PIR1, TMR1IF
        GOTO YY
        
 
 RETURN 
 
;********************************************************************************************************
 
        END




Any guess? the result is like 396mS

thank you again!!
 
Last edited by a moderator:

An example.

1/(1MHz/128/256) = 32.768ms

1/(1MHz/128) = 128us

99 ms / 128 us = 773.4375

TMR1H = 0x03

TMR1L = 0x05

Use mikroe Timer Calculator.


Example 4 MHz PIC16F Timer 1 99 ms Timer Interrupt.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Timer1
//Prescaler 1:2; TMR1 Preload = 16036; Actual Interrupt Time : 99 ms
 
//Place/Copy this part in declaration section
void InitTimer1(){
  T1CON  = 0x11;
  TMR1IF_bit     = 0;
  TMR1H  = 0x3E;
  TMR1L  = 0xA4;
  TMR1IE_bit     = 1;
  INTCON     = 0xC0;
}
 
void Interrupt(){
  if (TMR1IF_bit){ 
    TMR1IF_bit = 0;
    TMR1H    = 0x3E;
    TMR1L    = 0xA4;
    //Enter your code here
  }
}



Convert this to ASM code.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…