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.

PIC16F877A LED lights PWM effect Question

Status
Not open for further replies.

thilan d

Newbie level 3
Newbie level 3
Joined
Jan 15, 2014
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
47
Hi friends...

am trying to do some PWM effected LED setup with portc and tmr0.. but i can only light up 3 leds each time
can some one help me to light up other leds that connected to port c please

thx a lot


""


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
#include <htc.h>
#include <stdlib.h>
#include <pic.h>
#define _XTAL_FREQ 40000000
 
 
#define BLUE      RC0
#define RED       RC7
#define GREEN     RC5
#define BROWN     RC3
#define BLACK     RC4
#define YELLOW    RC2
#define ORANGE    RC6
#define ASH       RC1
 
 
#define ON              1
#define OFF             0
 
 
unsigned char pwmCounter = 0;
unsigned char redActual, redTarget, greenActual, greenTarget, blueActual, blueTarget; 
unsigned char brownActual, brownTarget, blackActual, blackTarget, yellowActual, yellowTarget;
unsigned char orangeActual, orangeTarget, ashActual, ashTarget;
 
unsigned char fade = 0;
 
void main(void) {
    TRISC   = 0;
    PORTC   = 0;
    T0CS    = 0;        // Internal Mode (Fosc/4)
    PSA     = 0;        // Prescaler assigned to TMR0
    PS2     = 1;
    PS1     = 0;
    PS0     = 0;        // Prescaler ratio 1:32
    T0IE    = 1;        // TMR0 overflow int. enable
    GIE     = 1;        // Global int. enable
    TMR0    = 131;      // (256-125=131) for 4mS time
 
 
//Set up initial LEDs off
redActual = 0;
greenActual = 0;
blueActual = 0;
brownActual = 0;
blackActual =0;
yellowActual = 0;
orangeActual =0;
ashActual = 0;
redTarget = 0;
greenTarget = 0;
blueTarget = 0;
brownTarget = 0;
blackTarget = 0;
yellowTarget = 0;
orangeTarget = 0;
ashTarget = 0;
 
        while (1) {
                redTarget       =   rand() % 60;
                greenTarget     =   rand() % 60;
                blueTarget      =   rand() % 60;
                brownTarget     =   rand() % 60;
                blackTarget     =   rand() % 60;
                yellowTarget    =   rand() % 60;
                orangeTarget    =   rand() % 60;
                ashTarget       =   rand() % 60;
 
                
//Can't do delay higher than 99ms, so chain multiple together
                for (int delay=0;delay<100;delay++) {
                        __delay_ms(10);
                }
        }
}
 
void interrupt timer0(void) {
        if (T0IF == 1) {
                fade++;
                
                if (fade == 64) {
                        if (redTarget > redActual)
                                redActual++;
                        else if (redTarget < redActual)
                                redActual--;
 
                        if (greenTarget > greenActual)
                                greenActual++;
                        else if (greenTarget < greenActual)
                                greenActual--;
 
                        if (blueTarget > blueActual)
                                blueActual++;
                        else if (blueTarget < blueActual)
                               blueActual--;
 
                        if (brownActual > brownActual)
                                brownActual ++;
                        else if (brownActual < brownActual)
                                brownActual --;
 
                        if (blackActual > blackActual)
                                blackActual ++;
                        else if (blackActual < blackActual)
                                blackActual --;
 
                        if (yellowActual > yellowActual)
                                yellowActual ++;
                        else if (yellowActual < yellowActual)
                                yellowActual --;
 
                        if (orangeActual > orangeActual)
                                orangeActual ++;
 
                        else if (orangeActual < orangeActual)
                                orangeActual --;
 
                        if (ashActual > ashActual)
                                ashActual ++;
                        else if (ashActual < ashActual)
                                ashActual --;
 
 
 
                }
 
               if (redActual > pwmCounter) {
                        RED = ON;
                        } else {
                        RED = OFF;
               }
 
               if (greenActual > pwmCounter) {
                        GREEN = ON;
                        } else {
                       GREEN = OFF;
                }
 
               if (blueActual > pwmCounter) {
                        BLUE = ON;
                        } else {
                      BLUE = OFF;
                }
                if (brownActual > pwmCounter) {
                        BROWN = ON;
                        } else {
                        BROWN = OFF;
 
                }
                if (blackActual > pwmCounter) {
                        BLACK     = ON;
                        } else {
                        BLACK     = OFF;
                }
 
                if (yellowActual > pwmCounter) {
                        YELLOW    = ON;
                        } else {
                        YELLOW    = OFF;
 
                }
                if (orangeActual > pwmCounter) {
                        ORANGE    = ON;
                        } else {
                        ORANGE    = OFF;
                }
 
                if (ashActual > pwmCounter) {
                        ASH       = ON;
                        } else {
                        ASH       = OFF;
 
                }
 
 
                pwmCounter++;
                if (pwmCounter > 80) {
                        pwmCounter = 0;
                }
                
                T0IF = 0;
        }
}

""
 
Last edited by a moderator:

I think you are doing it the wrong way around. At the moment you are continuously generating new targets then trying to reach them in an interrupt routine. The targets are being changed faster than you can reach them!

You should initially generate all targets ONCE then only when that target has been reached, generate a new target only for that color.

I do something almost identical in a product randomly fading 16 channels of different shades or red, orange, yellow and white to simulate a flame, also using a 16F877A.

Brian.
 
dear betwixt,,, could you please advice me how i can make it work.. ?? can you change the code for me ? some how i can only light up 3 leds randomly and continuously
thank a lot
 

I don't use the HTC compiler so there is no point in me trying out your code but this is how the flow should go:

In main()
1. Initialize all the pins etc. I think you have all the done correctly.
2. Generate all the random target values (as in lines 60 - 67)
3. In a while(1) loop
4. check if actual = target, if it is, generate a new random target.
5. check if actual > target, if it is, decrement 'actual' so it drops in value towards the target, if if is less than target, add one to it instead so it increases in value towards the target.
6. close the while loop
7. close main()

Then in the interrupt routine you produce the 'PWM' pulse length according to the 'actual' value.

It is essentially two programs, one running in main() to create random targets and move the 'actual' value towards it, the other in the interrupt routine to create the power to the LEDs according to the 'actual' value.

I cannot post the program for my 16 channel version for contractual reasons but I have attached a 10 channel version. It's in assembly language and compiles in MPLAB but you should be able to follow the way it works.
Ignore the 'C' code in the zip file, it was an experimental C version but was never completed.

Brian.
 

Attachments

  • Flame.zip
    30.2 KB · Views: 91
betwixt am only able to light up 3 leds in port c
that is the issue am haveing with this code

do you have any idea to slove it ?
 

Do you notice something different between this code:
Code:
                        if (redTarget > redActual)
                                redActual++;
                        else if (redTarget < redActual)
                                redActual--;
 
                        if (greenTarget > greenActual)
                                greenActual++;
                        else if (greenTarget < greenActual)
                                greenActual--;
 
                        if (blueTarget > blueActual)
                                blueActual++;
                        else if (blueTarget < blueActual)
                               blueActual--;

and this code:

Code:
if (brownActual > brownActual)
                                brownActual ++;
                        else if (brownActual < brownActual)
                                brownActual --;
 
                        if (blackActual > blackActual)
                                blackActual ++;
                        else if (blackActual < blackActual)
                                blackActual --;
 
                        if (yellowActual > yellowActual)
                                yellowActual ++;
                        else if (yellowActual < yellowActual)
                                yellowActual --;
 
                        if (orangeActual > orangeActual)
                                orangeActual ++;
 
                        else if (orangeActual < orangeActual)
                                orangeActual --;
 
                        if (ashActual > ashActual)
                                ashActual ++;
                        else if (ashActual < ashActual)
                                ashActual --;

With the exception of the first three colors you are asking if the same variable is greater than itself and less than itself - that doesn't make sense!

Brian.
 
thank you Brian.
i was copy and paste the text
in word file before i compile
its the mistake that i done

how i can make it as a chaser now ?
can you please advice me ?
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top