View Single Post
  #7   Spotlight this post!  
Unread 23-03-2004, 19:27
DanL DanL is offline
Crusty Mentor
FRC #0097
Team Role: Mentor
 
Join Date: Jan 2002
Rookie Year: 2001
Location: Somerville, MA
Posts: 682
DanL is just really niceDanL is just really niceDanL is just really niceDanL is just really niceDanL is just really nice
Send a message via AIM to DanL
Re: Fading LEDs using a PWM signal from the Edubot

Quote:
Originally Posted by Alan Anderson
I don't know what the PWM frequency is, but I'd be very surprised if it were slow enough to detect with your eyes.
You can't detect it precisely (the human eye notices flashes of light only to about 25Hz-30Hz - coincidentally the framerate of television). However, some people can observe a flicker. Take your computer CRT - those are usually 70-75Hz. The problem with CRTs is this flicker can be detected and the subconcious detection of this flicker is one of the reasons prolonged exposure to a monitor leads to eye strain. The flicker is especially noticable from the corner of your eyes because the light detecting cells on the edges of the human eye are more sensitive to light vs. dark while the cells in the middle of the human eye are more sensitive to color. Proof: ever been stargazing and see a dim star in the corner of your vision but when you center your vision on it, it disapears? The corner of your vision is more sensitive to contrast while the center is more sensitive to color (if you think about this from an evolutionary standpoint, this also makes sense - our ancestors would have benefited from detecting moving objects in the corners of their vision). Anyways, the point of this extended tangent is that yes, the blinking is too fast to detect outright, but some people can observe the flicker much like the flicker of a computer monitor is observed.

Quote:
Originally Posted by Kevin Watson
Dan,

This can be done using the PIC's dedicated PWM hardware. Have a look at the beacon code and note where the 40KHz duty cycle is set to 50%. You can get the effect you want by changing this value. The beacon drive circuit can be used to drive an array of LEDs too. If this isn't clear, I'll write some example code and post it.

-Kevin
Okay, so I've examined the use of timers with the help of your beacon code and interrupt template from kevin.org... however, I still have some questions
Code:
// initialize timer 2, which generates the 40KHz IR carrier frequency
	PR2 = PWM_TOTAL_PERIOD;		// set PWM frequency (defined in beacon.h) [154]
	TMR2 = 0;					// clear timer2 register [142]	
	T2CONbits.T2OUTPS0 = 0;		// setup 1:1 postscale [141]
	T2CONbits.T2OUTPS1 = 0;
	T2CONbits.T2OUTPS2 = 0;
	T2CONbits.T2OUTPS3 = 0;
	T2CONbits.T2CKPS0 = 0;		// setup 1:1 prescale [141]
	T2CONbits.T2CKPS1 = 0;
	T2CONbits.TMR2ON = 1;		// start timer 2 [141]
So, with a 1:1 prescale, one timer tick is 1e-7 seconds, 100 ns. Assuming PWM_TOTAL_PERIOD is 250, that means the timer rollover handler is called every 25e-6 seconds, or 25 microseconds. 1 / 25e-6 results in the 40KHz frequency described in the comment. I can now see how to use a timer to generate the interrupt frequency needed. However, I don't see how this is used to generate the PWM signal

Code:
beacon.h:
// This sets the PWM high period, which should be 
// half of PWM_TOTAL_PERIOD for a 50% duty cycle.
// Set to 10000000*desired_high_period.
#define PWM_HIGH_PERIOD 125

beacon.c:
	// initialize PWM 1, which generates type 0 beacon waveforms
	TRISCbits.TRISC1 = 0;		// make sure CCP2/PWM1 is setup as an output [110]
	CCPR2L = PWM_HIGH_PERIOD;	// set CCP2/PWM 1 duty cycle (defined in beacon.h) [
I don't see any information about what CCPR2L is, or what timer 2 is used for. My problem is I found where you initialized timer 2 to create a 40KHz pulse, but I'm not seeing how it goes about creating the signal. What does timer 2 do (I don't see any interrupt handling routine)? What is CCPR2L and how does that relate to a duty cycle?
__________________
Dan L
Team 97 Mentor
Software Engineer, Vecna Technologies