• No results found

LITERATURE REVIEW

2.9 The Key Scanning Concept

3.2.3 Programming Code

random colour setting, permanent data storage and menu option.

For the port, there are certain things which need to be highlighted are the mode and hardware connection. The number must be declared based on the hardware connection. The ports for column signal of the keypad are set as the output pins 14 – 17. In addition, the 4 ports of the keypad column, pins 32, 34, 36 and 38 are set to input mode for the microcontroller to control the RGB. Meanwhile the 2 ports of keypad row (pins 28, 30) are set as output to send signal to the column ports if the button has been pressed.

As the digital potentiometer is used, the SPI is initialized for it to communicate with the microcontroller. In order to decide the style of communication in SPI, a register named (SPCR) is set. In the program, the SPCR is set as 0b01010000 where the SPE and the MSTR are set to “high”. By referring to Figure 3.16, SPE (bit 6) is set high to enable the SPI and a “high” in MSTR indicates the master mode. The method of data sending is where the most significant is the DORD (bit 5) is set to “low”. As the CPOL (bit 3) is equal to low, the data clock will idle when low meanwhile samples data occurs at the rising edge as CPHA (bit 2) is set to low. As shown in Figure 3.17, the data will be sample and send at the rising edge of the clock. During the falling edge, the new second bit data (new cycle) starts and it will be send during the coming rising edge. The last 2 bits, SPR1 and SPR0 are set to zero to have the fastest SPI speed which is equal to 4 MHz. During the data transferring, the SPDR register is stored with the information to be sent and the program is checking for SPIF flag inside the SPI Status Register (SPSR). Before

proceeding to the next instruction, the program will wait until the SPIF is set as it indicates the transmission is done.

Figure 3.16 : SPI control register (SPCR) and the setting condition

Figure 3.17 : Data transferring mode for SPI

The button scanning loop is a program that will keep on repeating for scanning the signal from the keypad if any. It is located inside the function called

“keyinprocess()” as shown in the programming document in the CD. This loop will only start when the user tries to login on any account. This scanning loop is a nested loop where it is divided into the column and row for the keypad signal as displayed in Figure 3.18. The loop went through every column in the first row before it moves to the second row. It read the signal from each column in a certain row to check whether the button in the column has been pressed. As the row and column is separated by a diode and the corresponding row is given a high signal (5V) for every row looping, the signal will reach the column port if the button is pressed. The

microcontroller will receive a “high” signal from column to indicate the pressed button. After every column in a row is scanned and before moving to the next row, the microcontroller will send a “low” signal to that row’s pin. By this method, the microcontroller is able to detect the pressed button on the keypad. Once the column signal showed a “high”, a counter is increased to indicate the times of pressing. As each button has its own colour code during the scanning, the program stored the code in a specific variable. Once the third button is pressed, the program will compare the 3 stored codes with the code set of that particular account in the memory. If the set of entered code does not match with the real code, the wrong counter will increase by 1.

The loop will stop after the wrong counter showed 3 and it will step in a delay mode for a certain period.

Figure 3.18: Program flowchart of key scanning part

The random colour programming part is executed once, before going into the key scanning loop. The program called a function named “random_button[]” to randomly allocate a color from the 10 colours which are white, yellow, purple, pink, red, green, blue, light green, light blue and orange. Each colour has its own number which is from 1 to 10. The function will be call every 250 times of the key scanning loop if there are no key is pressed within this period. The interval between colour

stored the resistance value for red, green and blue pin of each RGB LED in a set of

Table 3.1 : Resistance value for the potentiometer set Colour Resistance Value memory that can be used to keep nonvolatile data where the data does not fade out even during the power off period. Therefore, the library entitled “avr/eeprom.h” is initialised in order to call the read and write function for the EEPROM memory. The

“eeprom_read_block ((void*)&structure, (void*)0, sizeof (structure));” function is to read all the structure parameter from the EEPROM. Meanwhile, the

“eeprom_write_block((const void*)& structure, (void*)0, sizeof(structure));”

function is to write or stored the structure into the EEPROM.

The menu option is done through the programming of the GLCD shield. A library for this GLCD has been downloaded and included in the program to make the coding easier. With the library, the SPI setting for the communication between the

shield and board is ignored. Therefore, the focused was only on how to read the input and to create the menu page and the option. The program kept on looping for scanning for the pressed joystick as demonstrated in Figure 3.19. Once the joystick is pressed, the program will fall into the corresponding case to execute the instruction accordingly. There is a 5 degree of freedom on the joystick where 4 of them are for direction indications and the center-down is used as select or the enter function.

Besides, a function “lcd.backlight(ON/OFF)” is used to turn on or off the back light for the user to read the screen in a dark environment. It is programmed to be on for only a certain period to reduce the energy consumption.

Figure 3.19: Programming concept for joystick input of GLCD shield

CHAPTER 4

Related documents