Complete the implementation of the setup and process functions. The hash-tags fo
ID: 3720045 • Letter: C
Question
Complete the implementation of the setup and process functions. The hash-tags for this exercise are: #cab202, and #cab202CycleContrast.
In this exercise you will get further practice getting user input by reading the state of a pair of switches, and use direct write to the LCD display to adjust the contrast of the display. These operations are directly relevant to microcontroller programming in a wide range of fields.
Briefly, the programming task is as follows:
In setup:
Set your Teensy up to receive input from the left button and right button.
Turn on the LCD display using the default contrast level.
Create a global variable called Contrast as indicated in the code skeleton.
In process:
If the left button is closed, Contrast should increase by 3, wrapping to zero if it exceeds the maximum permitted contrast value.
If the right button is closed, Contrast should decrease by 3, wrapping to the maximum value if it becomes less than 0.
Regardless of whether the value of Contrast has changed, before leaving process the LCD contrast should be updated to reflect the current value of Contrast.
To complete the program, follow the instructions detailed in the in-line comments in the skeleton code below.
#include <avr/io.h>
#include <util/delay.h>
#include <cpu_speed.h>
#include <macros.h>
#include <graphics.h>
#include <lcd.h>
#include "lcd_model.h"
void setup( void ) {
set_clock_speed(CPU_8MHz);
// (a) Enable input from the left button and right button.
// (b) Initialise the LCD display using the default contrast setting.
// (c) Use one of the functions declared in "graphics.h" to display
// your student number, "n10099077", using the foreground colour,
// positioning the left edge of the text at 18 and the nominal top
// of the text at 16.
// (d) Use one of the functions declared in "graphics.h" to copy the contents
// of the screen buffer to the LCD.
}
// (e) Declare a global variable of type int named Contrast and
// initialise it to the default LCD contrast.
void process(void) {
// (f) Test pin corresponding to right button. If closed,
// decrement Contrast by 3. If the result is less than zero,
// set Contrast to the maximum permitted LCD contrast value.
// (g) Test pin corresponding to left button. If closed,
// increment Contrast by 3. If the result is greater
// then the maximum permitted LCD contrast value, set Contrast
// to zero.
// (h) Send a sequence of commands to the LCD to enable extended
// instructions, set contrast to the current value of Contrast,
// and finally retore the LCD to basic instruction mode.
}
int main(void) {
setup();
for ( ;; ) {
process();
_delay_ms(100);
}
return 0;
}
Explanation / Answer
#include <avr/io.h>
#include <util/delay.h>
#define MrLCDsCrib PORTB
#define DataDir_MrLCDsCrib DDRB
#define MrLCDsControl PORTD
#define DataDir_MrLCDsControl DDRD
#define LightSwitch 5
#define ReadWrite 7
#define BiPolarMood 2
void Check_IF_MrLCD_isBusy(void);
void Peek_A_Boo(void);
void Send_A_Command(unsigned char command);
void Send_A_Character(unsigned char character);
void Send_A_String(char *string);
int main(void)
{
DataDir_MrLCDsControl |= 1<<LightSwitch | 1<<ReadWrite | 1<<BiPolarMood;
_delay_ms(15);
Send_A_Command(0x01); //Clear Screen 0x01 = 00000001
_delay_ms(2);
Send_A_Command(0x38);
_delay_us(50);
Send_A_Command(0b00001110);
_delay_us(50);
Send_A_Character(0x4E); //N
Send_A_Character(0x65); //e
Send_A_Character(0x77); //w
Send_A_Character(0x62); //b
Send_A_Character(0x69); //i
Send_A_Character(0x65); //e
Send_A_Character(0x48); //H
Send_A_Character(0x61); //a
Send_A_Character(0x63); //c
Send_A_Character(0x6B); //k
Send_A_Character(0x2E); //.
Send_A_Character(0x63); //c
Send_A_Character(0x6F); //o
Send_A_Character(0x6D); //m
Send_A_String("Patrick");
while(1)
{}
}
void Check_IF_MrLCD_isBusy()
{
DataDir_MrLCDsCrib = 0;
MrLCDsControl |= 1<<ReadWrite;
MrLCDsControl &= ~1<<BiPolarMood;
while (MrLCDsCrib >= 0x80)
{
Peek_A_Boo();
}
DataDir_MrLCDsCrib = 0xFF; //0xFF means 0b11111111
}
void Peek_A_Boo()
{
MrLCDsControl |= 1<<LightSwitch;
asm volatile ("nop");
asm volatile ("nop");
MrLCDsControl &= ~1<<LightSwitch;
}
void Send_A_Command(unsigned char command)
{
Check_IF_MrLCD_isBusy();
MrLCDsCrib = command;
MrLCDsControl &= ~ ((1<<ReadWrite)|(1<<BiPolarMood));
Peek_A_Boo();
MrLCDsCrib = 0;
}
void Send_A_Character(unsigned char character)
{
Check_IF_MrLCD_isBusy();
MrLCDsCrib = character;
MrLCDsControl &= ~ (1<<ReadWrite);
MrLCDsControl |= 1<<BiPolarMood;
Peek_A_Boo();
MrLCDsCrib = 0;
}
Hope this helps