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] Please help to solve the program error

Status
Not open for further replies.

chirag2239

Member level 3
Joined
Jul 29, 2011
Messages
64
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
2,048
Hello,

Here I have attached the schematic and Program that I have developed. When I have run this program on my hardware, it doesn't work.
First, I have tried on development board, where the switch gives high logic to controller pin, so it works. But in my hardware, switch closes, it gives zero logic to controller pin. And does not work.

So can anyone please help me to solve out this issue?

Thanks.
Chirag


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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
 * PWM.c
 *
 * Created: 04-11-2016 02:13:49
 *  Author: Chirag
 */ 
 
 
#include <avr/io.h>
#include <util/delay.h>
 
int main()
{
    int brightness,count;
    DDRA = 0x00;
    DDRB = 0xFF;
    PORTA=0xFF;
    TCCR0=0x69;
    OCR0=255;
    while(1)
    {
        TCCR0=0x69;
        PORTB=0xFF;
        count = 0;
        brightness=0;
        
        count0:     
        while(count==0)
        {
            OCR0=255;
            if (PINA & 0b00000001)
            {
                while(PINA & 0b00000001);
                count=1;
                break;
            }
            if (PINA & 0b00000010)
            {
                while(PINA & 0b00000010);
                brightness=1;
                break;              
            }
        }
        
        while (brightness==1)
        {
            OCR0=130;
            if (PINA & 0b00000010)
            {
                while(PINA & 0b00000010);
                brightness=2;
                break;
            }
            if (PINA & 0b00000001)
            {
                while(PINA & 0b00000001);
                count=1;
                break;
            }
        }
        while (brightness==2)
        {
            OCR0=65;
            if (PINA & 0b00000010)
            {
                while(PINA & 0b00000010);
                brightness=3;
                break;
            }
            if (PINA & 0b00000001)
            {
                while(PINA & 0b00000001);
                count=1;
                break;
            }
        }
        while (brightness==3)
        {
            OCR0=25;
            if (PINA & 0b00000010)
            {
                while(PINA & 0b00000010);
                goto count0;
                break;
            }
            if (PINA & 0b00000001)
            {
                while(PINA & 0b00000001);
                count=1;
                break;
            }
        }
        
        count1:
        while(count==1)
        {
            OCR0=255;
            _delay_ms(300);
            OCR0=0;
            _delay_ms(300);
            if (PINA & 0b00000001)
            {
                while(PINA & 0b00000001);
                count=2;
                break;
            }
            if (PINA & 0b00000010)
            {
                while(PINA & 0b00000010);
                brightness=4;
                break;
            }
        }
        
        while (brightness==4)
        {
            OCR0=130;
            _delay_ms(300);
            OCR0=0;
            _delay_ms(300);
            if (PINA & 0b00000010)
            {
                while(PINA & 0b00000010);
                brightness=5;
                break;
            }
            if (PINA & 0b00000001)
            {
                while(PINA & 0b00000001);
                count=2;
                break;
            }
        }
        while (brightness==5)
        {
            OCR0=65;
            _delay_ms(300);
            OCR0=0;
            _delay_ms(300);
            if (PINA & 0b00000010)
            {
                while(PINA & 0b00000010);
                brightness=6;
                break;
            }
            if (PINA & 0b00000001)
            {
                while(PINA & 0b00000001);
                count=2;
                break;
            }
        }
        while (brightness==6)
        {
            OCR0=25;
            _delay_ms(300);
            OCR0=0;
            _delay_ms(300);
            if (PINA & 0b00000010)
            {
                while(PINA & 0b00000010);
                goto count1;
                break;
            }
            if (PINA & 0b00000001)
            {
                while(PINA & 0b00000001);
                count=2;
                break;
            }
        }       
        
        count2:
        while(count==2)
        {
            OCR0=255;
            _delay_ms(150);
            OCR0=0;
            _delay_ms(150);
            if (PINA & 0b00000001)
            {
                while(PINA & 0b00000001);
                count=3;
                break;
            }
            if (PINA & 0b00000010)
            {
                while(PINA & 0b00000010);
                brightness=7;
                break;
            }
            
        }
        while (brightness==7)
        {
            OCR0=130;
            _delay_ms(150);
            OCR0=0;
            _delay_ms(150);
            if (PINA & 0b00000010)
            {
                while(PINA & 0b00000010);
                brightness=8;
                break;
            }
            if (PINA & 0b00000001)
            {
                while(PINA & 0b00000001);
                count=3;
                break;
            }
        }
        while (brightness==8)
        {
            OCR0=65;
            _delay_ms(150);
            OCR0=0;
            _delay_ms(150);
            if (PINA & 0b00000010)
            {
                while(PINA & 0b00000010);
                brightness=9;
                break;
            }
            if (PINA & 0b00000001)
            {
                while(PINA & 0b00000001);
                count=3;
                break;
            }
        }
        while (brightness==9)
        {
            OCR0=25;
            _delay_ms(150);
            OCR0=0;
            _delay_ms(150);
            if (PINA & 0b00000010)
            {
                while(PINA & 0b00000010);
                goto count2;
                break;
            }
            if (PINA & 0b00000001)
            {
                while(PINA & 0b00000001);
                count=3;
                break;
            }
        }
        
        count3:
        while(count==3)
        {
            OCR0=255;
            _delay_ms(50);
            OCR0=0;
            _delay_ms(50);
            if (PINA & 0b00000001)
            {
                while(PINA & 0b00000001);
                count=0;
                break;
            }
            if (PINA & 0b00000010)
            {
                while(PINA & 0b00000010);
                brightness=10;
                break;
            }
        }
        
        while (brightness==10)
        {
            OCR0=130;
            _delay_ms(50);
            OCR0=0;
            _delay_ms(50);
            if (PINA & 0b00000010)
            {
                while(PINA & 0b00000010);
                brightness=11;
                break;
            }
            if (PINA & 0b00000001)
            {
                while(PINA & 0b00000001);
                count=0;
                break;
            }
        }
        while (brightness==11)
        {
            OCR0=65;
            _delay_ms(50);
            OCR0=0;
            _delay_ms(50);
            if (PINA & 0b00000010)
            {
                while(PINA & 0b00000010);
                brightness=12;
                break;
            }
            if (PINA & 0b00000001)
            {
                while(PINA & 0b00000001);
                count=0;
                break;
            }
        }
        while (brightness==12)
        {
            OCR0=25;
            _delay_ms(50);
            OCR0=0;
            _delay_ms(50);
            if (PINA & 0b00000010)
            {
                while(PINA & 0b00000010);
                goto count3;
                break;
            }
            if (PINA & 0b00000001)
            {
                while(PINA & 0b00000001);
                count=0;
                break;
            }
        }
        
    }
}

 

Attachments

  • Schematic _12.pdf
    22.8 KB · Views: 114
Last edited by a moderator:

Have you choosen R1 value and LED voltage in Q2 circuit to eliminate base resistor and to provide 5V directly to Q2 base ? Are they correct ? What is the actual problem you are facing ? Switch button code has to be reversed that is button press detect have to be reversed.

Code:
if (PINA & 0b00000001)

has to be replaced by

Code:
if (!(PINA & 0b00000001))

Similar for the other button.

Code is very badly written using goto statements. The code is jumping here and there. Please write structured code using state machine. If all you want to do is control the brightness of Led using PWM then there is a very simple and 5 or 6 lines code.
 
Last edited:

Can you please guide me? I am a beginner in this. So I need your assistance. Will you guide me how to finish this in 5-6 line code?

- - - Updated - - -

The problem I am facing is that, while pressing the switch, Output does not change. It remains ON with full intensity. I changed the controller but it did not worked but for simple function, it works. So problem is in software, not in hardware.

The other thing regarding to resistor, I did not get you exactly, but yes, I want to make you inform that, the base of the transistor is connected to the MCU pin 43 directly without resistor. And for simple function,like press the switch and LED glow, is working very well.


And yes, this logic is not working.
Can you show me a single loop? I mean a single stage? I have done it in multiple stages. But I expect single stage. For brightness and blinking. If we start, LED is not blinking and glow with full brightness. When we press 1st switch, it starts blinking, then second time we press the same speed, blinking speed increases. and so on.. While second switch will be pressed, the intensity will be decreased and the same follows for pressing the switch. Both the functions will be done for 3 times and for the fourth time, we press the switch, it comes to the start of the program and repeat the process. Both the switch will be pressed randomly.
 

I have tried on development board, where the switch gives high logic to controller pin, so it works
But in my hardware, switch closes, it gives zero logic to controller pin. And does not work
You depicted different beahaviours in different scenarios, so there isn't effectively any comparision on that statement. Furthermore, what do you mean by development board and my hardware, are both assembled in real world, or is one of them running in simulation environment ? Are both running exactly with the same hardware ( Vcc=3v3, Xtal=8Mhz, etc... ) ?
 

I never said there is problem is in hardware. I was not able to understand your hardware working from the code because I didn't not read it fully. All I understood is you are trying to control the brightness of the Led using PWM. I can't guide you but I can give you a fully working code if you explain in detail how the device has to work.

You button press is not being detected because code execution will be stuck in some loop or executing some blocking delays. So, If is better to use Timer Interrupt and PWM to make it work.

Can you show me a single loop? I mean a single stage?

There will be no loops and delays and any blocking codes in my code. Delays will be handled by Timer Interrupt. You please mention what is your actual requirement and I will write a code for it. Explain the control logic.
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top