



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




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
NOTE: You will need something larger than a byte to store a 10-bit A/D result.