Yes, you are correct. The transitions at noon and midnight require specific attention in the software logic to handle the toggling of the AM/PM indicator correctly. Let me clarify and correct the transitions:
At noon, when the time advances from 11:59 AM to 12:00 PM, the hours should reset to 12, and the PM indicator should be enabled but not toggled (since 12:00 noon is PM). At midnight, when the time goes from 11:59 PM to 12:00 AM, the hours should again reset to 12, but this time the AM indicator should be enabled, marking the start of a new day.
The corrected pseudo-algorithm would reflect these transitions:
initialize system (setup pins, interrupts, and variables)
initialize time variables to 12:00 AM
on timer interrupt every second:
increment seconds
if seconds equal 60:
reset seconds to 0
increment minutes
if minutes equal 60:
reset minutes to 0
increment hours
if hours equal 12 and it is currently AM:
leave hours at 12 but switch to PM (12:00 noon)
else if hours equal 12 and it is currently PM:
reset hours to 1 and switch to AM (start of afternoon)
else if hours equal 11 and it is currently AM:
increment hours to 12 but leave it as AM (approaching noon)
else if hours equal 11 and it is currently PM:
increment hours to 12 but leave it as PM (approaching midnight)
else if hours exceed 12:
reset hours to 1
on button press (debounced):
if mode button is pressed:
toggle between setting hours and setting minutes
if increment button is pressed:
if setting hours:
increment hours, handle roll-over and AM/PM transition
if setting minutes:
increment minutes, handle roll-over
This updated logic now properly handles the transition between AM and PM at both noon and midnight. Here's further detail specifically for the changeover points:
- At 11:59 AM, when the time increments:
- Minutes roll over from 59 to 00.
- Hours increment to 12.
- AM stays as AM.
- When time advances to 12:00 PM (noon):
- Minutes roll over normally.
- If hours increment from 11 to 12, PM indicator is turned on (because it's noon).
- After 12:59 PM, hours reset to 1, and PM stays as PM.
- At 11:59 PM, when the time increments:
- Minutes roll over from 59 to 00.
- Hours increment to 12.
- PM stays as PM.
- When time advances to 12:00 AM (midnight):
- Minutes roll over normally.
- If hours increment from 11 to 12, AM indicator is turned on (because it's midnight).
- After 12:59 AM, hours reset to 1, and AM stays as AM.
These transitions are crucial for the clock to accurately represent the 12-hour time with an AM/PM indicator. And the software must be able to handle these distinct cases accordingly.