Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[AVR] 16-bits Timer/Counter (Atmega2560) and Proteus

Status
Not open for further replies.

47box

Newbie level 5
Joined
Jan 8, 2014
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
101
I'm having problems with code execution in Proteus.

The code has a delay time that is used to put the state of the output PORTB into 00000011 ( pin0 and pin1 "on") after the time set by the timer.

One time the button is pressed the output state is changed and after a delay of 2s the output state is automatically changed to (00000011).

The first stage the initial condition is set and after 2s automatically changed to 00000011 without the need to press the button.

After this stage the program will only respond with the button press, causing the output state to change (00000001) and after 2s (delay_loop call) it will return to state 00000011 automatically.

I finished the program after the button was pressed to determine the problem more easily. (after pressed ONCE the program goes to "rjmp PC")



The problems I'm having are:

* the program wait press a button.... button pressed >> goes to 00000001 ........then the delay_loop is called again (HERE looks like the delay_loop does not do nothing in Proteus, the output signal will suddenly go from 00000001 to 00000011 ).


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
.nolist
.include "M2560DEF.INC"
.list
 
.def temp                   = R17
.def leds                   = R18
.def RS                                         = R22
 
.cseg
.org 0
 
;Time Delay 2s
;Time Delay 2s
tick_MAX: .dw $127A   ;      int(0.5 + (Base_Clock/Prescaler) * Delay)   -->   int(0.5 * (1.000.000/64 * 2)   ==   31.250  ($7A12)  Prescaler Used: 64   
 
;Loading data from Program Memory (Flash - .cseg)
ldi ZH, high(tick_MAX*2)
ldi ZL, low(tick_MAX*2)
lpm R19, Z+
lpm R20, Z
 
;Defining 16-bits TOP value to compare
STS OCR1AH, R19
STS OCR1AL, R20
 
/*                                       Setting WGM Mode (CTC) & Prescaler (1024)
==================================================================================================================
TCCR1A – Timer/Counter 1 Control Register A
Bit       7     6       5      4     3      2      1     0
(0x80) COM1A1 COM1A0 COM1B1 COM1B0 COM1C1 COM1C0 WGM11 WGM10  -->  (TCCR1A)
==================================================================================================================
TCCR1B – Timer/Counter 1 Control Register B
Bit      7     6     5     4     3     2    1    0
(0x81) ICNC1 ICES1   –   WGM13 WGM12 CS12 CS11 CS10          -->   (TCCR1B)
==================================================================================================================
 
CTC Mode  (0100):    WGMn3 WGMn2 WGMn1 WGMn0       (WGMn3 & WGMn2   ->   TCCR1B)   (WGMn1 & WGMn0   ->   TCCR1A)
Prescaler (011) :    CS12  CS11  CS10              (CS12 CS11 CS10  ->   TCCR1B)
 
*/
ldi temp, 0b00000000
STS TCCR1A, temp
ldi temp, 0b00001011
STS TCCR1B, temp
 
;Data Direction OUTPUT   (DDRB - Output     DDRC - Input)
LDI temp, 0b00000011
OUT DDRB, temp
LDI temp, 0b00000000
OUT DDRC, temp
 
;Setting Initial Condition    S -->  HIGH      //      R   -->  LOW
ldi RS, 0b00000010
OUT PORTB,RS
CALL delay_loop
 
;+++++++++++++++++++++++++++  Wait Pess a Button  +++++++++++++++++++
waitpress:
in temp, PINC
cpi temp, 0b00000000 ;0x00 means none pressed
breq waitpress
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
ldi RS, 0b00000001
OUT PORTB,RS
 
;+++++++++++++++++++++++++++  Wait Release  +++++++++++++++++++++++++
waitrelease:
in temp, PINC
cpi temp, 0b00000001 ;0xFF means none pressed
breq waitrelease
 
CALL delay_loop
 
rjmp PC
 
;++++++++++++++++++++++++   Delay Loop   ++++++++++++++++++++++
delay_loop:
        in temp, TIFR1
        cpi temp, 0b00000000
        breq delay_loop
ldi temp, 1<<OCF1A
out TIFR1, temp
 
;After Loop       S -->  HIGH      //      R   -->  HIGH
ldi RS, 0b00000011
OUT PORTB,RS
ret
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



Capture.PNG
 

Hi
I changed the code:

Changes is in BOLD in a code

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
.nolist
.include "M2560DEF.INC"
.list
 
.def temp                   = R17
.def leds                   = R18
.def RS                     = R22
 
.cseg
.org 0
 
;Time Delay 2s
tick_MAX: .dw $127A   ;      int(0.5 + (Base_Clock/Prescaler) * Delay)   -->   int(0.5 * (1.000.000/64 * 2)   ==   31.250  ($7A12)  Prescaler Used: 64  
 
;Loading data from Program Memory (Flash - .cseg)
ldi ZH, high(tick_MAX*2)
ldi ZL, low(tick_MAX*2)
lpm R19, Z+
lpm R20, Z
 
;Defining 16-bits TOP value to compare
STS OCR1AH, R19
STS OCR1AL, R20
 
/*                                       Setting WGM Mode (CTC) & Prescaler (64)
==================================================================================================================
TCCR1A – Timer/Counter 1 Control Register A
Bit       7     6       5      4     3      2      1     0
(0x80) COM1A1 COM1A0 COM1B1 COM1B0 COM1C1 COM1C0 WGM11 WGM10  -->  (TCCR1A)
==================================================================================================================
TCCR1B – Timer/Counter 1 Control Register B
Bit      7     6     5     4     3     2    1    0
(0x81) ICNC1 ICES1   –   WGM13 WGM12 CS12 CS11 CS10          -->   (TCCR1B)
==================================================================================================================
 
CTC Mode  (0100):    WGMn3 WGMn2 WGMn1 WGMn0       (WGMn3 & WGMn2   ->   TCCR1B)   (WGMn1 & WGMn0   ->   TCCR1A)
Prescaler (101) :    CS12  CS11  CS10              (CS12 CS11 CS10  ->   TCCR1B)
 
*/
ldi temp, 0b00000000
STS TCCR1A, temp
ldi temp, 0b00001011
STS TCCR1B, temp
 
;Data Direction OUTPUT   (DDRB - Output     DDRC - Input)
LDI temp, 0b00000011
OUT DDRB, temp
LDI temp, 0b00000000
OUT DDRC, temp
 
;Setting Initial Condition    S -->  HIGH      //      R   -->  LOW
ldi RS, 0b00000010
OUT PORTB,RS
call delay_loop
 
;+++++++++++++++++++++++++++  Wait Pess a Button  +++++++++++++++++++
waitpress:
in temp, PINC
[B]andi temp, 0b00000001 ;test PC0[/B]
breq waitpress
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
ldi RS, 0b00000001
OUT PORTB,RS
 
;+++++++++++++++++++++++++++  Wait Release  +++++++++++++++++++++++++
waitrelease:
in temp, PINC
[B]andi temp, 0b00000001 ;test PC0[/B]
brne waitrelease
 
call delay_loop
 
rjmp PC
 
;++++++++++++++++++++++++   Delay Loop   ++++++++++++++++++++++
delay_loop:
        in temp, TIFR1
        cpi temp, 0b00000000
        breq delay_loop
 
        ldi temp, 1<<OCF1A
        out TIFR1, temp
        ;After Loop       S -->  HIGH      //      R   -->  HIGH
        ldi RS, 0b00000011
        OUT PORTB,RS
 
        ret
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



After the code has passed "waitrelease" the same should wait for 2s according to the call "delay_loop".
When I debug the code in AtmelStudio7, after the "waitrelease" the code goes to the delay loop ("delay_loop") before causing PORTB to load the "ON" condition for bits 0 and 1 (00000011).
When I run the code in Proteus , after "waitrelease" (when the button is released), the output status of the PORTB goes directly to "00000011" without waiting for the delay of 2s determined by delay_loop.
Is there some other modification that could causing error in the code?
 
Last edited by a moderator:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top