Created
December 5, 2023 16:00
-
-
Save ajarmst/4c6864737fd6b163d42ef0384782250a to your computer and use it in GitHub Desktop.
Basic demo of using the I2C library.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///////////////////////////////////////////////////////////////////////////// | |
// HC12 Program: I2C Demo | |
// Processor: MC9S12XDP512 | |
// Bus Speed: 20 MHz (Requires Active PLL) | |
// Author: AJ Armstrong | |
// Details: Demonstration of the I2C Driver Libraries | |
// Date: Dec 2023 | |
// Revision History : | |
///////////////////////////////////////////////////////////////////////////// | |
#include <stdio.h> // For sprintf, etc | |
#include <ctype.h> // For character utilities | |
#include <string.h> // Various string utilities | |
#include <hidef.h> // Common defines and macros | |
#include "derivative.h" // Derivative-specific definitions | |
#include "pll.h" // Phase-locked loop clock control | |
#include "swled.h" // GPIO switches and LEDs | |
#include "segs.h" // 7-Segment Displays | |
#include "timer.h" // Enhanced capture timer | |
#include "portj.h" // Interrupt buttons | |
#include "pit.h" // Periodic Interrupt timer | |
#include "rti.h" // Realtime interrupts | |
#include "lcd.h" // LCD Display | |
#include "sci.h" // Serial communications interface (EIA/TIA-232) | |
#include "i2c.h" // Carlos Estay's I2C Driver Libraries | |
///////////////////////////////////////////////////////////////////////////// | |
// Local Prototypes | |
///////////////////////////////////////////////////////////////////////////// | |
///////////////////////////////////////////////////////////////////////////// | |
// Global Variables | |
///////////////////////////////////////////////////////////////////////////// | |
///////////////////////////////////////////////////////////////////////////// | |
// Constants | |
///////////////////////////////////////////////////////////////////////////// | |
///////////////////////////////////////////////////////////////////////////// | |
// Main Entry | |
///////////////////////////////////////////////////////////////////////////// | |
void main(void) | |
{ | |
// Variables | |
int test = 0; // For capturing return values | |
char reg = 0; // For capturing single byte results | |
RTC_Read clockTime; // Type for getting RTC data (see i2c.h) | |
///////////////////////////////////////////////////////////////////////////// | |
// One-Time Initializations | |
///////////////////////////////////////////////////////////////////////////// | |
_DISABLE_COP(); // No watchdog | |
PLL_To20MHz(); // Configure the main clock to 20 MHz (implications for timers) | |
SWL_Init(); // Standard initialization for switches and LEDs | |
SEG_Init(); // Standard initializaton for 7-seg displays | |
RTI_Init(); // Initialize RTI timer | |
///////////////////////////////////////////////////////////////////////////// | |
// Main Program Loop | |
///////////////////////////////////////////////////////////////////////////// | |
EnableInterrupts; // Ensure our board will respond to interrupt signals. | |
LCD_Init(); // Current LCD drivers need RTI and interrupts. //FIXME | |
LCD_StringRC(0,6,"I2C Demo"); | |
LCD_StringRC(1,6,"--------"); | |
// This demo will use the M41T81S already built into our board and attached | |
// at address 0x68 (RTC_ADD in header) | |
// First, initialize the I2C at fast (400 kbps) speed | |
I2C0_InitBus(I2CBus400); | |
// Test for STOP flag by trying to read SECONDS | |
// register from the RTC. If the special bit | |
// 0x80 (STOP, RTC_SECONDS_ST) is set in the return, | |
// then is not running. | |
test = I2C0_RegRead(®, RTC_ADD, RTC_SECONDS); | |
if (reg & RTC_SECONDS_ST) // Stopped | |
{ | |
//Yellow LED means it was stopped. | |
SWL_SetLEDs(eYellow); | |
//Ok. Let's start it by rewriting the value with | |
//the stop bit reset | |
I2C0_RegWrite(RTC_ADD, RTC_SECONDS, reg & ~RTC_SECONDS_ST, IIC0_STOP); | |
} | |
//Also, if it was halted, that shows up in the AlarmHour register | |
//as the special bit 0x40 (RTC_AL_HOUR_HT) | |
//This is probably because the board was shut off or had a power | |
//failure. | |
test = I2C0_RegRead(®, RTC_ADD, RTC_AL_HOUR); | |
if (reg & RTC_AL_HOUR_HT) // Halted | |
{ | |
//Red LED means it was halted. | |
SWL_SetLEDs(eRed); | |
//Throw the seconds register onto the upper right 7Segs | |
//to show the seconds currently stored (time of failure) | |
test = I2C0_RegRead(®, RTC_ADD, RTC_SECONDS); | |
SEG_8H(2,reg); //Hex because seconds is BCD on timer | |
// Throw the minutes recorded for the halt to upper left 7segs | |
test = I2C0_RegRead(®, RTC_ADD, RTC_MINUTES); | |
SEG_8D(0,reg & 0x0F); //Decimal and mask because...well, look at datasheet | |
//Unhalt it by rewriting the value with the bit reset | |
I2C0_RegWrite(RTC_ADD, RTC_AL_HOUR, reg & ~RTC_AL_HOUR_HT, IIC0_STOP); | |
//IS THIS NOT BETTER THAN FLASHING 1200? | |
} | |
for (;;) | |
{ | |
//Show the current minutes/seconds from the RTC on the lower segs | |
if(rtiCount % 250) | |
{ | |
test = I2C0_RegRead(®, RTC_ADD, RTC_SECONDS); | |
SEG_8H(6,reg); //Hex because RTC seconds is BCD | |
test = I2C0_RegRead(®, RTC_ADD, RTC_MINUTES); | |
SEG_8D(4,reg & 0x0F); //Decimal and mask because...well, look at datasheet | |
} | |
asm WAI; | |
} | |
} | |
///////////////////////////////////////////////////////////////////////////// | |
// Functions | |
///////////////////////////////////////////////////////////////////////////// | |
///////////////////////////////////////////////////////////////////////////// | |
// Interrupt Service Routines | |
///////////////////////////////////////////////////////////////////////////// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment