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.

Help needed! PWM speed increases but does not decreses

Status
Not open for further replies.

0killingsoul0

Junior Member level 2
Joined
Feb 8, 2012
Messages
20
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,432
I am trying to make a pwm using 8051. I am using external interrupt 0 (pin 3.1) to increase speed and interrupt 1 (pin 3.2) to decrease speed. My code is working fine for increasing speed but it does not work for decreasing speed. i.e interrupt 1 is not working properly.

Capture.PNG:**broken link removed**

**broken link removed**

[

Code C - [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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include<reg51.h>
 
 
 
 
unsigned char inc = 0;
 
void MSDelay(unsigned int);
void ex0_isr (void) interrupt 0
{
inc++;   // Increment the count
}
 
 
void ex1_isr (void) interrupt 1
{
inc=inc--;   // Deccrement the count
}
 
 
 
sbit pwm=P2^0;
sbit pwmn=P2^1;
sbit enable=P2^2;
 
 
 
 
 
void universal_delay(unsigned int);
 
void main( )
{     IT0 = 1;   // Configure interrupt 0 for falling edge on /INT0 (P3.2)
      IT1 = 1;   // Configure interrupt 1 for falling edge on /INT0 (P3.2)
      
      
      EX0 = 1;   // Enable EX0 Interrupt
      EX1 = 1;
      
      EA = 1;    // Enable Global Interrupt Flag
     
     enable=1;          
     
     pwm=0;       //  making outputs
     pwmn=0;
 
 
 
    
 
    while(1)
    {     
        universal_delay(inc);
    }
 
}
 
 
 
void universal_delay(unsigned int d)
{
            if (d==1)
            {   
                
                pwm=1;
                pwmn=0;
                
                MSDelay(50);      //  50 ms delay
                    
                pwm=0;
                pwmn=1;
 
                MSDelay(50);
                
            
            }
 
        else if (d== 2)
        
        {   
             pwm=1;
                pwmn=0;
                
                MSDelay(55);
                    
                pwm=0;
                pwmn=1;
 
                MSDelay(45);    
                    
            }
 
 
        else if (d== 3)
            {   
                 pwm=1;
                pwmn=0;
                
                MSDelay(60);    
                    
                pwm=0;
                pwmn=1;
 
                MSDelay(40);
        }
 
        
        else if (d==4)
            {    
                                pwm=1;
                pwmn=0;
                
                MSDelay(65);    
                    
                pwm=0;
                pwmn=1;
 
                MSDelay(35);
            }
 
            else if (d==5)
        
            {    pwm=1;
                pwmn=0;
                
                MSDelay(70);    
                    
                pwm=0;
                pwmn=1;
 
                MSDelay(30);
            
            }
 
        else if (d== 6)
        
            {   
                 pwm=1;
                pwmn=0;
                
                MSDelay(75);    
                    
                pwm=0;
                pwmn=1;
 
                MSDelay(30);
            }
 
        else if (d== 7)
              {
                 pwm=1;
                pwmn=0;
                
                MSDelay(75);    
                    
                pwm=0;
                pwmn=1;
 
                MSDelay(25);
                }
 
                    else if (d== 8)
              {
                 pwm=1;
                pwmn=0;
                
                MSDelay(80);    
                    
                pwm=0;
                pwmn=1;
 
                MSDelay(20);
                }
 
                else if (d== 9)
              {
                 pwm=1;
                pwmn=0;
                
                MSDelay(85);    
                    
                pwm=0;
                pwmn=1;
 
                MSDelay(15);
                }
                else if (d== 10)
              {
                 pwm=1;
                pwmn=0;
                
                MSDelay(90);    
                    
                pwm=0;
                pwmn=1;
 
                MSDelay(10);
                }
        
 
                    else if (d>10)
              {
                 pwm=1;
                pwmn=0;
                               
                MSDelay(100);   
                    
                pwm=0;
                pwmn=1;
 
                MSDelay(0);
                }
 
 
        
    
}
 
 
 
void MSDelay(unsigned int itime)
{
unsigned int i,j;
for (i=0;i<itime;i++)
for (j=0;j<1275;j++);
}

 
Last edited:

I have never used 8051 mcu so I may not be of much help but one thing I see is that the inc variable is not decalerd as volatile and you should always do that when you have a variable used in both main and isr and may be changed in isr.

You have also used inc = inc-- in isr1 , this should have the same result as inc-- so why don't you just use inc--.

Maybe you also want to use some kind of condition so that the variable doesn't go below 0 (which will result in 255), for example
Code:
if (inc>0)	inc--; // Decrement the count
maybe you should apply that in isr0too
Code:
if (inc<255)	inc++; // increment the count

I'm not sure why your code strucks where it does.

Alex
 

Thanks for the reply. Tired of removing this glitch but still no success :( .

I don't know why the second interrupt 1 is not working properly. As i have showed in the picture. That interrupt is not returning properly.

3.PNG
 

What compiler do you use?

I didn't worked in past period with 8051 devices, but I remember that you should specify also interrupt attribute (interrupt vector). Try the following code:


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
void ex0_isr (void) interrupt 0 using 0
{
inc++;   // Increment the count
}
 
 
void ex1_isr (void) interrupt 1 using 2
{
inc=inc--;   // Deccrement the count
}


Example how to define interrupts in Keil
 
Last edited by a moderator:

What compiler do you use?

I didn't worked in past period with 8051 devices, but I remember that you should specify also interrupt attribute (interrupt vector). Try the following code:


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
void ex0_isr (void) interrupt 0 using 0
{
inc++;   // Increment the count
}
 
 
void ex1_isr (void) interrupt 1 using 2
{
inc=inc--;   // Deccrement the count
}


Example how to define interrupts in Keil



Tried this but still no success..... :(.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top