Writing an Embedded Controller
It usually consists of two parts – Initialization – A main loop More challenging than HW3 because HW3 is stateless, the microcontroller has states. Experience: – The main loop usually has to deal with many things. It is important NOT to stay in any job for too long. You should process an event and almost immediately return to the main loop.
A Simple Program Write a program which – Prints out “everything is fine” every 3 seconds – Print out the ASCII of any key you have pressed immediately. Note: – Cannot sleep for 3 seconds and then print out the sentence – Must break away from the wait periodically to look for input (not using interrupt here)
.data msg_tvworking:.asciiz "everything is fine\n".text.globl main main: li $s0, 300 mainloop: # 1. get external input, and process it lui $t0, 0xFFFF lw $t1, 0($t0) andi $t1, 1 beq $t1, $0, mainloopnext1 # call the process_input function here lw $a0, 4($t0) li $v0, 1 syscall mainloopnext1: # 2. delay for 10ms jal delay_10ms # 3. print status addi $s0, $s0, -1 bne $s0, $0, mainloopnext4 li $s0, 300 la $a0, msg_tvworking li $v0, 4 syscall mainloopnext4: j mainloop li $v0,10 # exit syscall delay_10ms: li $t0, 3000 delay_10ms_loop: addi $t0, $t0, -1 bne $t0, $0, delay_10ms_loop jr $ra
HW4
A TV controller Let’s use this lecture to start to develop a simple TV controller together.
TV controller It accepts simple commands like vol+/-, channel +/-, channel digit input (0-99), sleep timer, and power on/off. When there is no input, roughly every 3 seconds, print out the current state of the TV, in the format of – Current second, Power On (of Off), Channel (current channel watching), volume (current volume), Sleep timer remaining. – For example, “At 100 second, Power On, Channel 10, Volume 30, sleep timer 120 sec remaining.” – If power is off, only output “At 100 sec, Power off.”
TV controller I didn’t find a timer facility in SPIM, so the time is approximated using delay loops.
Specifications Initialization. When the code started to run, the initial state is – Power on – Channel 0 (0-99) – Volume 50 (0-99) – Sleep timer off
Specifications Power key (should be ‘p’ on the key board) – When power key is pressed, if the power is currently off, turn on the TV immediately by printing “Power On!” – When power key is pressed, if the power is currently on, turn on the TV immediately by printing “Power Off!”
Specification Channel +/-. Should use ‘u’ as + and ‘d’ as -. Once pressed, change the current watching channel accordingly (but not out of range of 0- 99), and print out, for example, “Channel 30.” Allows wraparound.
Specifications Vol +/-. Should use ‘l’ as + and ‘k’ as -. Once pressed, change the current volume accordingly (but not out of range of 0-99), and print out, for example, “Volume 30.” Do not allow wraparound.
Specification Digit input. Use 0-9 key on key board. Used to change channels. – Once a digit key is pressed, print out the intended channel immediately. For example, `1’ is pressed, print out “1-” immediately. The controller should be expecting another digit input for another 2 seconds. If any other key is pressed during these two seconds, abort the digit input. For example, if `1’ is pressed at 1000 second and `channel +’ is pressed at 1001 second, the controller should perform the channel + function and “forget” about the digit input. If no other key is pressed during the two seconds, the controller switches to channel 1. If another digit key is pressed within the two seconds, for example, `2’ is pressed, the controller switches to channel 12 immediately.
Specifications Sleep timer. Should use ‘s’ key on the keyboard. – When hitting the sleep timer key, at the first time, do not change the status of the sleep timer and just show the current status. If the sleep timer key is pressed again within 3 seconds, then starts to modify the status of the sleep timer. That is, if the sleep timer is off, set the sleep timer to be 5 sec. If the sleep timer is on, increment the sleep timer by 5 sec. If the sleep timer exceeds 200 sec after the increment, set the sleep timer to be off. If the sleep timer is on, it decrements by 1 every second. Once it becomes 0, turn the TV off immediately and print out “Power off due to sleep timer!”
View history The “View history” key. – When pressed, print out the most watched 5 channels in a descending order. The channel is ordered according to the total amount of seconds this channel is watched since the TV is turned on. The time must be stored as single precision floating number.
Extra credits -- “Go-back” key The “Go-back” key (the `b’ key on the keyboard). When pressed, the TV should automatically switch back to current channel after 10 seconds. That is, if you pressed the “Go-back” key when watching channel 10 at time 1000 sec, at time 1010 sec, the TV should automatically go back to channel 10, regardless of what channel you are in.
Extra credit – Keyboard input with Interrupt Use the interrupt to implement the keyboard input function. Basically, you may use the interrupt to capture the input key value and write it to a register only for this purpose. In this assignment, let’s reserve $s7 for this. $s7 should be initialized with some impossible value such as You main loop should be constantly checking $s7, and if it is storing some value that is within 0-255, it means that the interrupt has written a value to it. The main loop should then reset it to be the impossible value.
Structure of the main loop Check for new key board input and process the input. If power is on, check if sleep timer hits 0. Loop for 0.01 second. Print out the status if 3 seconds elapsed since last status print. Jump to the beginning of the loop.