Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

....fading leds....

Status
Not open for further replies.

timothyjackson

New Member
Trying to use a PIC to create a fading/dimming of 3 leds (well i want to fade 7 at different rates, but will start with 3)

I understand how to fade/dim on and off 1 LED using several levels of PWM, each having a slightly brighter effect.

But, fading 3 leds at different times?

I have an idea on how to do it, but, I think it is far to complex:

00000001 (send this to the LED port, turning on LED 0)
wait 5ms
00000000
wait25ms
repeat 16 times (PWM level 1 for LED 0)
00000001
wait 10ms
00000000
wait 20ms
repeat 16 times (PWM level 2)
00000001
wait 15ms
00000000
wait 15ms
repeat 16 times (PWM level 3)
00000101 (LED 2 now begins PWM level 1, and LED begins PWM level 4)
wait 5ms
00000001
wait 15ms
00000000
wait 10ms
00000101
wait 5ms
00000001
wait 15ms
etc etc etc

Continue assigning "XXXXXXXX" to the port to control the LED states, and call certain millisecond delays to control the invidual PWM's.

I think this is a little complex.?.
How many instruction lines are possible in a 16F628?

:? :?
 
Why not just use 3 separate timers, each controlling the pwn of one LED? All three timers could easily be configured to run at different speeds, and changing bits only takes up a single clock cycle, so I'm pretty sure it'll be more than sufficient to handle all thre..
 
Or if you want to save on timers, the logical choice would be to set the timer to run at 5ms interrupts, and determine the state of each led during the interrupt. For a PIC running at 20MHz, that's 25000 instructions per interrupt, definitely enough for you to do state transitions for each and every I/O pin the PIC has, and probably enough left over to do a host of other simple operations concurrently.
Another point to note is that at higher frequencies, the brightness of the LED is determined by it's duty cycle, not it's frequency.
 
You only need one timer.

pseudo code:
Code:
StartTimer();

Loop forever
    TimerValue = ReadTimer();
    if TimerOverflowFlag = 1 then
       LED1 = 0;
       LED2 = 0;
       LED3 = 0;
       TimerOverflowFlag = 0;
    end if;
    if TimerValue > LED1TurnOnTime then
       LED1 = 1;
    end if;
    if TimerValue > LED2TurnOnTime then
       LED2 = 1;
    end if;
    if TimerValue > LED2TurnOnTime then
       LED1 = 2;
    end if;
end loop;

Forgive the weird mixed syntax I've got both C and VHDL on my brain right now. The timer should have a flag that gets set when the timer overflows (goes from 255 to 0) check the datasheet. You can use this flag to reset the pwm signal on all your LEDs. You then just keep looking at the timer and when it reaches the turn on time for each LED you turn on that LED. This should give you 256 levels more or less.
 
bmcculla said:
You only need one timer.

pseudo code:
Code:
StartTimer();

Loop forever
    TimerValue = ReadTimer();
    if TimerOverflowFlag = 1 then
       LED1 = 0;
       LED2 = 0;
       LED3 = 0;
       TimerOverflowFlag = 0;
    end if;
    if TimerValue > LED1TurnOnTime then
       LED1 = 1;
    end if;
    if TimerValue > LED2TurnOnTime then
       LED2 = 1;
    end if;
    if TimerValue > LED2TurnOnTime then
       LED1 = 2;
    end if;
end loop;

Forgive the weird mixed syntax I've got both C and VHDL on my brain right now. The timer should have a flag that gets set when the timer overflows (goes from 255 to 0) check the datasheet. You can use this flag to reset the pwm signal on all your LEDs. You then just keep looking at the timer and when it reaches the turn on time for each LED you turn on that LED. This should give you 256 levels more or less.


I'm not as advanced as you in thinking. Can you explain this a little simpler please? I want to understand.
 
checkmate said:
Why not just use 3 separate timers, each controlling the pwn of one LED? All three timers could easily be configured to run at different speeds, and changing bits only takes up a single clock cycle, so I'm pretty sure it'll be more than sufficient to handle all thre..

I thought a timer just counted. Just went up or down.

How can a timer change the state of a portbit aswell?

How can you set off three timers to run at the same time?

:?
 
The Timer module not only counts. It can trigger interrupts after some condition is met. You can refer to the PIC mid-range manual on the section on timers. When this interrupt is triggered, you can add in code to do whatever processing you want, including setting I/O pins and checking states.

To set off three timers to run is the same as setting off one. Most PICs have at least 2-3 hardware timers for you to use that can be operated independently.
 
checkmate said:
The Timer module not only counts. It can trigger interrupts after some condition is met. You can refer to the PIC mid-range manual on the section on timers. When this interrupt is triggered, you can add in code to do whatever processing you want, including setting I/O pins and checking states.

So I could set the interupt condition as "after every 5ms" (for example), and, after each 5ms, I could pop in a new BCF/BSF according to the PWM.?.

checkmate said:
To set off three timers to run is the same as setting off one. Most PICs have at least 2-3 hardware timers for you to use that can be operated independently.

So, if a PIC has 3 hardware timers, this would limit the not of "different" fading rate LED's to three? If I wanted 5 different fading rate led's, I'd need 5 hardware timers?

How many instruction lines can I have when in the code(16f628)? a million????
 
timothyjackson said:
So, if a PIC has 3 hardware timers, this would limit the not of "different" fading rate LED's to three? If I wanted 5 different fading rate led's, I'd need 5 hardware timers?

No, you can do it all with interrupt code off a single timer, as 'bmcculla' previously mentioned.

There have been a couple of LED flasher/fader projects in EPE, you can download the software from their website.

How many instruction lines can I have when in the code(16f628)? a million????

The 628 has 2K of program memory, so basically 2048 instruction lines - this is a great deal of assembler!.
 
You only need to use one timer. An 8 bit timer counts up from 0 to 255. When it gets to 255 it "overflows" and goes back to 0 and continues to count up. When the timer overflows the timer hardware sets a special bit that can be read by the processor - its probably called something like Timer Overflow Interrupt Bit - Nigel should be able to help you out with the name (I'm not a PIC guy). You don't have to use interrupts to use this bit though you just have to have the processor read the bit (it'll be in one of the timer special function registers)and leave interrupts off.

The idea behind dimming LEDs is that you switch then on and off really fast: the longer the percentage of time they are on the brighter they look. If you start with the LEDs off and the timer at 0. You start the timer and keep reading it and comparing the value of the timer to the time you want the LED to come on. When you reach the time the LED should come on you simply set the LED bit, just like you would do normaly. When you see that the Overflow bit has been set you know that the timer has gone back to 0 and you need to turn the LEDs off and start over.

This method of timing doesn't use any delays. Because you are always checking the timer you know know how much time has passed.

Hope this is a bit clearer.
Brent
 
Nigel Goodwin said:
timothyjackson said:
So, if a PIC has 3 hardware timers, this would limit the not of "different" fading rate LED's to three? If I wanted 5 different fading rate led's, I'd need 5 hardware timers?

No, you can do it all with interrupt code off a single timer, as 'bmcculla' previously mentioned.

There have been a couple of LED flasher/fader projects in EPE, you can download the software from their website.

How many instruction lines can I have when in the code(16f628)? a million????

The 628 has 2K of program memory, so basically 2048 instruction lines - this is a great deal of assembler!.

Thanks Nigel. I've come across a bit of a problem.

I'm using (or will be) a programmer from Quasar electronics. Problem is, MPLAB doesnt work on the same PC that the programmer will be connected to (its running on win 95). So, is it ok to do my code writing/buidling simulating on one pc in MPLAB, and then, with a floppy disk, copy the .hex file to the other pc with the programmer???

Brent, Thanks for your clearer explanation. I understand the difference in not using delays, however, I don't see how it can control 5 for example, leds fading at a different rate. I'd still have to movlw 00001001 (for example) to the port to achieve the PWM of individual LED's?

Am checking EPE for some info.
 
You can manage each bit individualy using something called masking. Masking lets you change the state of only a few bits. I don't know PIC assembler so I'll just give you the basic idea. I'm only using 4 bits to keep the typing down, it'll be the same with 8 bits.

Say you have 0010 on a port and you want to change the first bit. You just use the OR operation: 0010 OR 1000 = 1010. Now if you want to change the first bit back to 0 you use the AND operation : 1010 AND 0111 = 0010. You still read and write the whole port at once but you change only the bits you want to before writing it back to the port.
 
Thanks Brent. I understand the or and and operations in this context now.

I'm going to summarise my understanding.....

The timer (counts from 0 to 255)....I should set this to last for 5ms? (becuase at every 5ms I want to change the state of an led somewhere.

So, when it resets (overflows), a reset flag will be set, and, at that point, I will know that 5ms has passed. Thats when I'll change the state of the bits of the port pins.

:?
 
You actually want to set the port pins while the timer is counting up. Say you want the LED to be 1/4 on over the 5ms timer period. For the counter 0-255 that would be 64 counts out of the total of 256 counts. Because you start with the LED off you need it to come on 3/4 of the way through the period that is 192 counts out of the 256. Your program watches the timer and when the timer goes over 192 you turn the LED on - you write to the port. If you average the power to the LED over that 5ms you get 1/4. If you want to change the power to the LED the next 5ms you change the numer of counts when you turn the LED on. The over flow flag just lets you know when you have to turn the LEDs off to start over. Because 5ms is too fast for the human eye to see it looks like the LED is changing brightness instead of flashing on for a variable length of time.
 
bmcculla said:
You can manage each bit individualy using something called masking. Masking lets you change the state of only a few bits. I don't know PIC assembler so I'll just give you the basic idea. I'm only using 4 bits to keep the typing down, it'll be the same with 8 bits.

Say you have 0010 on a port and you want to change the first bit. You just use the OR operation: 0010 OR 1000 = 1010. Now if you want to change the first bit back to 0 you use the AND operation : 1010 AND 0111 = 0010. You still read and write the whole port at once but you change only the bits you want to before writing it back to the port.

You don't have to this with a PIC, it's a micro-controller and designed for I/O operations, so you have two instuctions - BSF and BCF:

BSF PortB, 0 (which sets pin 0 of PortB)

BCF PortA, 3 (which clears pin 3 of PortA)

Likewise you can test single pins, using BTFSS and BTFSC.
 
Thanks Nigel. I've come across a bit of a problem.

I'm using (or will be) a programmer from Quasar electronics. Problem is, MPLAB doesnt work on the same PC that the programmer will be connected to (its running on win 95). So, is it ok to do my code writing/buidling simulating on one pc in MPLAB, and then, with a floppy disk, copy the .hex file to the other pc with the programmer???
 
timothyjackson said:
Thanks Nigel. I've come across a bit of a problem.

I'm using (or will be) a programmer from Quasar electronics. Problem is, MPLAB doesnt work on the same PC that the programmer will be connected to (its running on win 95). So, is it ok to do my code writing/buidling simulating on one pc in MPLAB, and then, with a floppy disk, copy the .hex file to the other pc with the programmer???

Yes, that's fine - it's no different to downloading a file off the net. Although I'm not sure why you can't get MPLAB to work on your other machine, as far as I know it should work on anything Win95 or later (but not on Win3.11 or earlier). The important part of MPLAB is the assembler, MPASM, you might try if that will work.
 
Nigel Goodwin said:
Yes, that's fine - it's no different to downloading a file off the net.

Woaw. I'm so excited in actually reaching the point of buying programmer, and having software ready, and code, and, y'know, just knowing a little more than before christmas.

Nigel Goodwin said:
Although I'm not sure why you can't get MPLAB to work on your other machine, as far as I know it should work on anything Win95 or later (but not on Win3.11 or earlier). The important part of MPLAB is the assembler, MPASM, you might try if that will work.

"MPLAB tools v7 requires Win 98 sE, Win mE, or NT, or 2000, or XP". So, I guess I'm stumped on that one. Unless, another version works with win 95, which can be downloaded elsewhere.

Off to order my programmer from quasar. Thanks Nigel. :D
 
timothyjackson said:
"MPLAB tools v7 requires Win 98 sE, Win mE, or NT, or 2000, or XP". So, I guess I'm stumped on that one. Unless, another version works with win 95, which can be downloaded elsewhere.

I suspect those requirements are for USB support, earlier OS's didn't support USB (at least not enough to make it usable). It's only the recent versions of MPLAB which provide USB support, with the introduction of their own USB programmers - if you're not using a USB connection (and you're not using the programmer side of MPLAB at all), it may well install and work OK.

Even if it won't run, if you can install it you should have the latest version of MPASM installed along with it - this is all you need to program PIC's. Personally I never use MPLAB, just the MPASM assembler.
 
Nigel Goodwin said:
I suspect those requirements are for USB support, earlier OS's didn't support USB (at least not enough to make it usable). It's only the recent versions of MPLAB which provide USB support, with the introduction of their own USB programmers - if you're not using a USB connection (and you're not using the programmer side of MPLAB at all), it may well install and work OK.

When the zip file is extracting into an installation to the PC, it wont progress even to the "custom" selection of components (i.e. disabling certain features/functions), it just stops and shouts, or laughs. Windows 95!

Nigel Goodwin said:
Even if it won't run, if you can install it you should have the latest version of MPASM installed along with it - this is all you need to program PIC's. Personally I never use MPLAB, just the MPASM assembler.

I remember you mentioned this before. Since then, have tried looking for MPASM to download. Again, no luck.

hey ho.
 
Status
Not open for further replies.

Latest threads

Back
Top