EENG 383
Lab 6 - IR decoder
InLab 6
Some self guided
activities.
Lab 6 assignment
Thanks for Alex for putting together this
flowchart of the software organiaztion
required for this lab. This PDF also contains a few hints in addition
to the flowchart.
Software
There are two main components to this week's lab. First, modify the
transmit function of the inLab code to transmit a NULL terminated
string of character. Second, convert the EUSART2 receive function to
operate off an interrupt and store a string of characters in a global
array of characters.
EUSART2
Start by modifying the EUSART2 configuration in MCC and checking
"Enable EUSART Interrupts". Do not redirect EUSART2 to STDIO because
that is EUSART1's job (because EUSART1 is connected to the FT230 chip
which creates the virtual COM port on your PC). Make sure to regenerate
the MCC files. Back in main, make sure to include the following code
after your call to
SYSTEM_Initialize();.
EUSART2_SetRxInterruptHandler(myEUSART2ISR);
INTERRUPT_GlobalInterruptEnable();
INTERRUPT_PeripheralInterruptEnable();
Now you need to write the myEUSART2 interrupt service routine. This
function will check the incoming character from EUSART2 and append it
to a global array of characters called IRrecieveBuffer. The last
two characters of this array are the NULL character, '\0', followed
by a checksum. After the EUSART2 ISR receives the checksum,
the EUSART2 ISR should set a global flag, receiveNewMessage, to
true. This will alert main that a new message has arrived. The
following global variables and function declarations should be at
the top of your program.
void myEUSART2ISR(void);
char IRrecieveBuffer[MAX_BUFFER_SIZE];
uint8_t receiveBusy = false;
uint8_t receiveNewMessage = false;
Checksum
An 8-bit checksum for a string is the sum of the characters in the
string modulo 256. By "modulo 256", I mean that is the sum were to
get to 256, the sum rolls over to 0. You can calculate a sum modulo
256 as follows. If the sum is less than 256, leave it alone. If the
sum is greater than 255, subtract sets of 256 until the sum is less
than 256. This means that an 8-bit checksum will always
be in the range [0-255]. The nice thing about addition of uint8_t
datayped variables in the PIC microcontroller, is that any carry out
from the MSB during addition is thrown-away. This makes addition of
uint8_t variables modulo 256 by default without requiring any additional
work on your part.
For example, the sum of the characters in the null terminated string
"abc\0" is 97 + 98 + 99 + 0 = 294. However, if you computed this sum
in a uint8_t typed variable, then 97 + 98 + 99 + 0 = 294 - 256 = 38.
Transmit
The transmit function will be handled using the same procedure used
in the inLab. However, you will move the code from main into a
function transmitCharacterOverIR. In order to be compatible with
lab 7, make the array containing the transmitted string,
IRtransmitBuffer, global. The following declarations should be at
the top of your program.
char IRtransmitBuffer[MAX_BUFFER_SIZE];
void transmitCharacterOverIR(char letter, uint16_t baudRate);
In order to provide some level of fault detection in our communication
protocol, you will append an 8-bit checksum to the end of the
transmitted string (but after the NULL character).
A checksum for a string is the numerical sum of
the individual characters. For example, for the string "abc" the
checksum is 97+98+99 = 294. But since this sum is being computed
in an 8-bit variable, the check sum would actually be 294-256=38.
For a more complex message like "This is a test" the checksum is 245.
Note the terminating NULL character at the end of the string does not
contribute to the checksum because its ASCII value is 0.
User Interface
At start-up your program should present a splash screen. The splash screen
should also contain connection instruction for the development board (where
to install jumper wires). When you press "?" you should be greeted with the
following menu. Note that the "true" or "false" of receiveNewMessage should
be printed along with the current Baud rate - you can take this code from
the inLab.
-------------------------------------------------
1200 Baud
receiveNewMessage = true
-------------------------------------------------
?: help menu
o: k
Z: Reset processor
z: Clear the terminal
b: set the Baud rate of the sent characters
r: reset EUSART2
m: Enter in transmit Message
S: Send message in foreground
R: Receive message using EUSART2 ISR via IR decoder
-------------------------------------------------
- ?
Prints out the ever useful help menu.
- z
Clear the terminal using a bunch of new lines.
- z
Reset the processor so that we can see that splash screen.
- b
Uses the inLab code to set the Baud rate to one of five preset
speeds. This should configure both the receiving and transmitting
data rates.
- r
Reset EUSART2 by clearing RCSTA2bits.CREN then immediately setting
it. When everything is working well, this function should not be
needed, but it's nice to have in case you transmit some
corrupt data that puts EUSART into an error state. After resetting
EUSART2, print a message stating that "EUSART2 was just reset", or
something similar.
- m
This command prompts user to enter in a message with something like:
Enter a message, hit return when done.
>
As each character is read in by EUSART1, you should:
- Stored it into the IRtransmitBuffer,
- Echo it out to EUSART1 so the user can see what character
they just entered, and
- Add the character to the checksum (which should be a
uint8_t datatype).
The loop reading in characters terminates when the user hits Enter,
'\r', or the length of the message reaches MAX_BUFFER_SIZE-2 characters.
The NULL character, '\0', is placed just at the end of the characters
followed by the 8-bit checksum.
After the message is entered, your program should echo out the string
one more time and print out the checksum using a format that looks
something like:
Enter message, hit return when done.
>This is a test
Created
Message: This is a test
Checksum: 245
Hint: The EUSART1_Read()
function waits for the PIR1bits.RC1IF
flag to equal 0 before starting before reading RCREG1. I found that I
needed to while (EUSART1_DataReady);
before performing the
first EUSART1_Read. Seem unintuitive and I'll come back here to post
a better explaination when I figure this out.
- S
Uses the transmitCharacterOverIR function to transmit all the characters
of the IRtransmitBuffer over IR. Make sure that you transmit the NULL
and checksum characters. After transmitting all the characters, tell the
user what has just happened with a message something like:
Transmitted
Message: This is a test
Checksum: 245
- R
Print the contents of IRrecieveBuffer to the terminal when receiveNewMessage
is true and then set receiveNewMessage to false. If receiveNewMessage is
true then produce output something similar to the following.
Received
Message: This is a test
Checksum: 245
If receiveNewMessage is false, then tell the user that this is the case
by printing a message something like the following.
No message, receiveNewMessage = false
Turn-in
You may work with a single partner (or alone) to complete this lab.
Submit your main.c file on Canvas using the instructions posted
there. You should take note of the Rubric that will be used to evaluate
your assignment. Please form a group before submitting using the
instructions posted on Canvas. You will demonstrate your code at the
beginning of lab.