Name:______

Lecture 20 generates a pulsing of the green LED by providing a sinusoidal varying PWM wave to the LED. Since the update rate of the LED is higher than our eye’s ability to detect the switching on and off, our eye perceives the average level of illumination. We call this the persistence of vision effect.

On line 27 of lec20-parta.c, in main, there is an assignment statement: duty = sin(angle>2); The value of dutyplays a large role in shaping the perceived illumination level of the LED as we will see below. There are two items to note:

  • The anglevariable is an int8 (line 16) and has a range of [0,255]
  • The angle variable is incremented during every loop in line 39.
  • The sin function is defined in the file lec20-partb.c on lines 30-34 expects an input between [0,63].
  • Consequently, the call to the sinfunction on line 27 in lec20-parta.c, dividesangleby 4 to produce an input in the range for the sinfunction.

Your first step in understanding the code is to plot the values of duty (line 27 of lec20-parta.c) over all the possible values ofangle. Do this in the figure below paying special attention to fill in the values of angle for the min, max and zero crossings.

The main point of today’s program is to illuminate the green LED with a varying duty cycle. The dutyvariable (assigned a value in line 27) serves to provide the duty cycle of the LED illumination on each iteration of the while(1)(infinite) loop on lines 22-46. In this infinite loop, it’s the code on lines 30-32 and line 35-36, that turn the LED on and off. Your next task is to understand how this code accomplishes this. In the graph below plot the logic level of P1.6 for all the values of cnt. The dutyvariable has a value between 0 and 255.

In addition to pulsing the LED, this program is supposed to toggle the red LED whenever the push button P1.3 is pressed. Unfortunately, something is wrong with the code as the red LED only toggles some of the time. Your first job is to go in and fix this error – it should only require one line of code on line 43.

Your fix:______

Try writing a subroutine to substitute in for the sin function, called linear. The linear function takes as input a value between [0,255] and outputs a value between [0,63]. The output should be the input divided by 4 (shift right by 2 bits). Add this code to lec20-partb.c. After you define the function, edit line 27 of lec20-parta.c to call your function. Compare the new pulsing to the sinusoidal pulsing.

Observation here: ______

You are never supposed to put function declarations ina .h file because the function will be defined in every file that uses that includes that .h file. To test this out, declare thebogus function below in the lec20.h file. Try to compile the program and note the error (pink highlighted text in the Console).

voidfoo(void) {// This is a bogus function that only

// exists to create an error with the

}// linker.

Error here:______

In order to slow down the pulse rate of the green LED you could make the angle variable a 16-bit integer. Then instead of shifting angleby 2 bits in line 27, you could shift it by 3 or more bits. Try this out and observe the pulsing. In your opinion does the pulse spend more time over-illuminated or under-illuminated?

Opinion here:______

In order to fix this problem of an ill-illuminated LED you could change the values of the sinLUT array defined in lec20-partb.c. You can do this by downloading the lec20.xlsx file and adjusting the formula for the sin function. Before modifying the function, write down the function in cell B1.

Excel sin function:______

How might you redefine the sin function in the excel spreadsheet to correct the problem you identified above?

New excel sin function:______

If you have time, recompile your pulsing LED with your improved sin function and sit back and enjoy.

Note enjoyment level:______

ECE 382Lecture #20 – C coding Page 1