- Joined
- Nov 28, 2011
- Messages
- 8,393
OK. I don't want to give you a complete answer because this is obviously a school project.
You need to divide this task into several stages. These can be written as separate functions, to keep the organisation of the program clear and easy to understand. This will also impress your tutor. Structured programming is important for maintainability, i.e. it makes your program easy to understand and modify in the future.
First, let's write a function to determine whether the seat is occupied. This function simply reads the I/O port input that comes from the seat sensor, and returns either TRUE or FALSE according to whether there is a person sitting in the seat.
FALSE and TRUE are not defined in standard C but you can define them easily enough using a typedef or some #defines. For example:
Now you have definitions for FALSE and TRUE. These values, 0 and 1, are integers, so any variable or function parameter that is either FALSE or TRUE should be declared as an integer (int). (Standard C doesn't have a boolean data type.)
The first function should be something like this:
I've assumed that the seat pressure switch comes in on PINA bit 0. If it's on bit 1, you'll need to change the line in that code.
Next, you need another function that is almost exactly the same, but it tests the seat belt status. Let's name this function seat_belt_is_buckled_up() and have it return TRUE if the seat belt is buckled up, otherwise FALSE. This function looks the same as seat_is_occupied() but it tests the other I/O pin. You should be able to write this function for yourself.
Now we need to define states for the LCD. You can use an enum for this:
Now you need to write the code that uses the seat_is_occupied and seat_belt_is_buckled_up states to calculate what the LCD should display. This code should be the first thing in your while (1) loop inside main(). It needs to calculate the correct value for an integer variable called lcd_should_display, which should be defined within your main() function. This variable will contain one of the three LCD_SHOWING_... values listed in the enum above. The logic is pretty clear:
Now you've determined what the LCD should display, you can decide whether or not you need to update the LCD. For this, you need another variable that remembers what the LCD is displaying. Let's call it lcd_is_showing. It's an integer, and it contains one of the LCD_SHOWING_... values from the enum.
This variable needs to be initialised to LCD_SHOWING_UNKNOWN, because you don't actually know what the LCD is displaying when your main() function starts. Or you could assume that the LCD will be initially blank; in that case, you don't need a value for LCD_SHOWING_UNKNOWN because you can initialise lcd_is_showing to LCD_SHOWING_BLANK.
So now you've calculated what the LCD should be showing, you need to see whether that value is the same as what the LCD _is_ showing. If they're different, you need to update the LCD by sending it a command to display some text or blank the display, and you need to update the lcd_is_showing variable to reflect what the LCD is actually now displaying.
That's the end of your while (1) loop in main().
So the loop calculates what the LCD should be displaying (using seat_is_occupied() and seat_belt_is_buckled_up() to detect the state of your sensors), decides whether the LCD needs to be updated, then updates it if so.
I hope you can fill in the gaps and write the code. As I said, I don't want to give you the answer on a plate, because you are supposed to be learning how to convert ideas into code.
Good luck!
You need to divide this task into several stages. These can be written as separate functions, to keep the organisation of the program clear and easy to understand. This will also impress your tutor. Structured programming is important for maintainability, i.e. it makes your program easy to understand and modify in the future.
First, let's write a function to determine whether the seat is occupied. This function simply reads the I/O port input that comes from the seat sensor, and returns either TRUE or FALSE according to whether there is a person sitting in the seat.
FALSE and TRUE are not defined in standard C but you can define them easily enough using a typedef or some #defines. For example:
Code:
typedef enum {
FALSE = 0,
TRUE = 1
};
The first function should be something like this:
Code:
int seat_is_occupied(void)
{
if (bit_is_set(PINA, 0)) // Test input from seat pressure switch
return TRUE; // Pressure switch closed - seat is occupied
else
return FALSE; // Pressure switch open - seat is vacant
}
Next, you need another function that is almost exactly the same, but it tests the seat belt status. Let's name this function seat_belt_is_buckled_up() and have it return TRUE if the seat belt is buckled up, otherwise FALSE. This function looks the same as seat_is_occupied() but it tests the other I/O pin. You should be able to write this function for yourself.
Now we need to define states for the LCD. You can use an enum for this:
Code:
typedef enum {
LCD_SHOWING_UNKNOWN,
LCD_SHOWING_BLANK,
LCD_SHOWING_BUCKLE_UP,
LCD_SHOWING_THANK_YOU
};
Code:
if (seat_is_occupied() == TRUE)
{
if (seat_belt_is_buckled_up() == FALSE)
lcd_should_display = LCD_SHOWING_BUCKLE_UP; // Seat occupied but not buckled up
else
lcd_should_display = LCD_SHOWING_THANK_YOU; // Seat occupied and buckled up
}
else
lcd_should_display = LCD_SHOWING_BLANK; // Seat unoccupied - no message to display
This variable needs to be initialised to LCD_SHOWING_UNKNOWN, because you don't actually know what the LCD is displaying when your main() function starts. Or you could assume that the LCD will be initially blank; in that case, you don't need a value for LCD_SHOWING_UNKNOWN because you can initialise lcd_is_showing to LCD_SHOWING_BLANK.
So now you've calculated what the LCD should be showing, you need to see whether that value is the same as what the LCD _is_ showing. If they're different, you need to update the LCD by sending it a command to display some text or blank the display, and you need to update the lcd_is_showing variable to reflect what the LCD is actually now displaying.
That's the end of your while (1) loop in main().
So the loop calculates what the LCD should be displaying (using seat_is_occupied() and seat_belt_is_buckled_up() to detect the state of your sensors), decides whether the LCD needs to be updated, then updates it if so.
I hope you can fill in the gaps and write the code. As I said, I don't want to give you the answer on a plate, because you are supposed to be learning how to convert ideas into code.
Good luck!
Last edited: