Docsity

Lab: LCD Screen Interface with Microcontroller - ADC & PWM - Prof. Timothy S. Stombaugh, Lab Reports of Engineering

Toggle sidebar
bg1
BAE 305
Analog-To-Digital Conversion And Pulse Width Modulation Lab
Familiarize yourself with the LCD screen
One major problem when working with microcontrollers is that there is no way to
visually view data that are inside the microcontroller. We need to connect an external
device to display the data. LCD screens are commonly used for this purpose. With most
LCD screens, 6 I/O pins are required to interface it to a microcontroller. Fortunately for
us, we have purchased serial LCD screens which only require one input line. The LCD
screen accepts data from a serial input signal and displays the desired characters on the
screen.
Hook up the LCD screen to your microcontroller by applying 5 volts to the red wire,
ground to the white (or black) wire, and connect the remaining wire (blue or green) to the
serial output pin. You could use any pin to send the serial data to the screen, but we
suggest that you use PORTB. as the output pin.
In order to send data to the LCD screen, we’ll need to use the SEROUT2 command.
Reading the SEROUT2 description on page 137 of the PicBasic Pro manual indicates that
there are several variables that must be defined. These include, DataPin, Mode, and Item.
The DataPin is simply the output pin we selected above (B7). The mode is a number that
specifies the values of the serial baud rate and other parameters such as parity, data bits,
and stop bits. We’ll not go into detail on these parameters, but know that a mode number
of 16416 is appropriate for this particular application.
The Item value can be one of two forms. It can either be a command to the LCD screen
or it can be a data value to display on the LCD screen. Commands are sent in two to four
bytes. The table below lists some of the commands that we can use.
Description Command
Clear Display [254,88]
Set Contrast [254,80,"contrast"]
Backlight On [254,66,"minutes"]
Backlight Off [254,70]
Set Text To Top Left [254,72]
Set Text Insertion Point [254,71,"row","col"]
where,
"contrast" ranges from 0-255
"minutes" is the length the backlight will stay lit, 0 = indefinitely
"row" and "col" are the space at which text will begin, range ={[1,1],[4,17]}
1
pf3
pf4
pf5

Partial preview of the text

Download Lab: LCD Screen Interface with Microcontroller - ADC & PWM - Prof. Timothy S. Stombaugh and more Lab Reports Engineering in PDF only on Docsity!

BAE 305

Analog-To-Digital Conversion And Pulse Width Modulation Lab

Familiarize yourself with the LCD screen One major problem when working with microcontrollers is that there is no way to visually view data that are inside the microcontroller. We need to connect an external device to display the data. LCD screens are commonly used for this purpose. With most LCD screens, 6 I/O pins are required to interface it to a microcontroller. Fortunately for us, we have purchased serial LCD screens which only require one input line. The LCD screen accepts data from a serial input signal and displays the desired characters on the screen.

Hook up the LCD screen to your microcontroller by applying 5 volts to the red wire, ground to the white (or black) wire, and connect the remaining wire (blue or green) to the serial output pin. You could use any pin to send the serial data to the screen, but we suggest that you use PORTB. as the output pin.

In order to send data to the LCD screen, we’ll need to use the SEROUT2 command. Reading the SEROUT2 description on page 137 of the PicBasic Pro manual indicates that there are several variables that must be defined. These include, DataPin, Mode, and Item. The DataPin is simply the output pin we selected above (B7). The mode is a number that specifies the values of the serial baud rate and other parameters such as parity, data bits, and stop bits. We’ll not go into detail on these parameters, but know that a mode number of 16416 is appropriate for this particular application.

The Item value can be one of two forms. It can either be a command to the LCD screen or it can be a data value to display on the LCD screen. Commands are sent in two to four bytes. The table below lists some of the commands that we can use.

Description Command Clear Display [254,88] Set Contrast [254,80,"contrast"] Backlight On [254,66,"minutes"] Backlight Off [254,70] Set Text To Top Left [254,72] Set Text Insertion Point [254,71,"row","col"]

where,

"contrast" ranges from 0- "minutes" is the length the backlight will stay lit, 0 = indefinitely "row" and "col" are the space at which text will begin, range ={[1,1],[4,17]}

The item field can also be text values to display to the screen. Text strings can be displayed by encapsulating the desired string in quotation marks. Microcontroller variables can also be displayed by setting the Item equation to the desired variable. The term DEC must be placed in front of the variable to output the decimal form of the number. To fix the number of decimal characters, an integer number may be included along with the DEC term to indicate the number of digits. If only specific digits of a decimal number are required, they can displayed using the combination of the DEC term followed by the variable and a DIG "X" term where X is the digit. DIG is a zero based command. Therefore, the DIG 0 returns the least significant figure.

Examples:

'Clear the screen SEROUT2 PORTB.7, 16416, [254, 88]

'Clear the screen and place the cursor at the home location SEROUT2 PORTB.7, 16416, [254, 88, 254, 72]

'Place the cursor at home and display HELLO SEROUT2 PORTB.7, 16416, [254, 72, "HELLO"]

'Place the cursor at home, display VALUE = VAR1 where VAR is an 'internal microcontroller variable SEROUT2 PORTB.7, 16416, [254, 72, "VALUE = ", DEC VAR1]

'Repeat the previous problem, but display the variable on the second line SEROUT2 PORTB.7, 16416, [254, 71, 1, 2, "VALUE = ", DEC VAR1]

'Repeat the previous problem, but display the variable as a 3 digit number SEROUT2 PORTB.7, 16416, [254, 71, 1, 2, "VALUE = ", DEC VAR1]

'Repeat the previous problem, but only display the 3rd digit of the variable SEROUT2 PORTB.7, 16416, [254, 71, 1, 2, "VALUE = ", DEC VAR1 DIG 2]

NOTE: When outputting multiple commands/strings in a row, it is necessary to pause long enough to allow the serial LCD screen to process the previous command. A minimum of 100 ms should be allowed for each full line of text.

Pulse Width Modulation

Now for the hardware pulse width modulation (HPWM) section: This command is very simple within PicBasic Pro. The following single line of code is all that is needed to turn on the HPWM.

HPWM CHANNEL, DUTY CYCLE, FREQUENCY

Channel indicates which channel (pin) the PWM will be sent to. For our chip there is only one channel of HPWM so this value will always be 1. The output pin for the pulse train will be C2.

Duty Cycle indicates the percent on and off time of the pulse train. This number can range from 0 (0%) to 255 (100%). A 50% duty cycle would require the number 127 in this parameter.

Frequency indicates the output frequency of the PWM. For our particular oscillator the range of this parameter is 1500 hz to 30,000 hz. The motor control applications we are implementing will work best at 2-3kHz.

Also remember that we must set the TRISC register to output.

If you are using the HPWM command inside a loop, place PAUSE commands in the loop also to slow down the program and create smooth transitions from one duty cycle to another.

Example:

HPWM 1,50,2000 'Output a 2000Hz square wave at 50% 'duty cycle using HPWM

Procedures & Questions

  1. Setup an LCD screen for serial input from the P18F2585.
  2. Send the names of your group members to the LCD screen. Use both lines on the screen, and center the names on the screen.
  3. Configure the analog to digital converter on the P18F2585 and output the conversion value (counts) to the LCD screen. Also display the expected voltage based on the theoretical conversion accuracy considering that this is an 8-bit converter with a 5V full scale range. Display the voltage to at least 2 decimal places.
  4. Calibrate the ADC so you know how the ADC output relates to input voltage. Is this calibration as you expected? Comment on the significance of each digit.
  5. Change the ADC to 10 bit conversion by adding "DEFINE ADC_BITS 10" to the beginning of your program. Replace the ADCON2 register with ADCON2 = 149. Repeat step 4 with a 10 bit conversion.

NOTE: You will need something larger than a byte to store a 10-bit A/D result.

  1. Output a pulse train using the hardware pulse width modulation on the P18F2585. Check the output signal on the oscilloscope. How accurate is the signal?
  2. Use the analog to digital converter and a potentiometer to vary the duty cycle of the hardware pulse width modulation. Make sure you change your settings back to 8-bit analog to digital conversion. Check your results on the oscilloscope. Does the system work?
  3. Connect the PWM channel through a relay to a 12V muffin fan. Using an optical tachometer, calibrate the speed of the fan with respect to duty cycle of the control pulse. Watch out for hysteresis!
  4. Include all program code in your lab report and make sure to comment each line so you can understand the code at a later time.