#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
#include "stdio.h"
//from the lcd display program
char Esc_seq[2]={
0x1b,'[' };
int dummy;
char sendtime[9];
unsigned int temptime;
short hr, min, sec, incsec, startupdate;
long time;
//when connecting to PMODRS232, TXD1 (from HC12) connects to
//RXD on PMOD and RXD1 (from HC12) connects to TXD on PMOD
//void MCU_init(void); /* Device initialization function declaration */
void TERMIO_PutChar(char ch); // send character to serial port
char TERMIO_GetChar(void); // get character from serial port
void serial_port_sel(char sel);
void send_nibble(short hr);
char sel,temp[20];//temp;
int dummy;
//setup generic serial port register definitions
unsigned char *SCI_SR1,*SCI_DRL; //* tells to get the value out of the location that is stored in variable
void MCU_init(void); /* Device initialization function declaration */
void main(void) {
MCU_init(); /* call Device Initialization */
/* put your own code here */
startupdate=0;
// code from the long int example
sel = 1; serial_port_sel(sel);
dummy = printf("%s1e",Esc_seq);//turn on display,no back light
dummy = printf("%s2c",Esc_seq);//cursor on blinking
dummy = printf("%s0h",Esc_seq);//wrap around after 16 chars
dummy = printf("%sj",Esc_seq); //clear screen and home cursor
dummy = printf("Waiting");//send chars
sel = 0; serial_port_sel(sel);
dummy=printf("\n\rPlease enter the time (HMS),no leading 0: ");
dummy=scanf("%li", &time); /*use %li (long int) to input a long*/
/*break long into hr,min,sec */
temptime= (char)(time/10000); /*isolate hr*/
hr=temptime;
hr=(temptime/10)*16+temptime%10; /*change hr into BCD number*/
temptime= (char)((time/100)%100); /*isolate min*/
min=temptime;
min=(temptime/10)*16+temptime%10; /*change min into BCD number*/
temptime= (char)(time%100); /*isolate sec*/
sec=temptime;
sec=(temptime/10)*16+temptime%10; /*change sec into BCD number*/
send_nibble(hr); //lcd print function
//dummy = printf("%i:%i:%i",hr,min,sec);
startupdate=1;
while(1)
{
if (incsec) //bcd clock section
{
incsec=0;
if ((sec & 0b00001001) == 0b00001001) //checks to make sure lower nibble is less than A
{
sec = sec + 0b0111; //if lower is nine adds one to upper nible
}
else
{
sec = sec + 1; //incrememnts seconds
}
if (sec == 0b01100000)//checks to see if counter is at 60
{
sec = 0b00000000; //clears seconds
min = min + 1; //increment minutes
if ((min & 0b00001010) == 0b00001010) //this section does same as seconds, but with minutes
{
min = min + 0b0110; //adds 6 instead of eight becaus checking for 0xA instead of 9 due to the way C goes linearly
}
if (min == 0b01100000) ///checks for 60 minutes
{
min = 0b00000000; //clears minutes
hr = hr + 1; //increments hours
if ((hr & 0b00001010) == 0b00001010) //this section does same as seconds, but with hours and 24 not 60
{
hr = hr +0b0110;
}
if (hr == 0b00100100) //checks for 24 bcd
{
hr = 0b00000000; //clears hours, all values are 0 at this point
}
}
}
send_nibble(hr); //lcd print function
}
}//ends while loop
for(;
{
/* _FEED_COP(); by default, COP is disabled with device init. When enabling, also reset the watchdog. */
} /* loop forever */
/* please make sure that you never leave main */
}
void serial_port_sel(char sel)
{
if (sel) //selects the second serial port
{
SCI_SR1 = &SCI1SR1; //& means wants the address, not the content
SCI_DRL = &SCI1DRL;
}
else ////selects the primary serial port
{
SCI_SR1 = &SCI0SR1;
SCI_DRL = &SCI0DRL;
}
}
//redefine functions used by scanf and printf
void TERMIO_PutChar(char ch)
{
while ((*SCI_SR1 & 0x80) == 0)//reads the transmit buffer empty status flag
;
*SCI_DRL = ch; // * is a pointer in this case its placing data to send
}
char TERMIO_GetChar(void)
{
while ((*SCI_SR1 & 0x20) == 0); ///reads the recieve buffer full bit
return *SCI_DRL;
}
void send_nibble(short hr)
{
sendtime[0] = (char)((hr / 16)+ 0b00110000); // shifts upper bits right four places the adds a "3" on for ascii
sendtime[1] = (char)((hr & 0b00001111) + 0b00110000); // clears upper bits adds a "3" on for ascii
sendtime[2] = 0x3a; // colon
sendtime[3] = (char)((min / 16)+ 0b00110000); // shifts upper bits right four places the adds a "3" on for ascii
sendtime[4] = (char)((min & 0b00001111) + 0b00110000); // clears upper bits adds a "3" on for ascii
sendtime[5] = 0x3a; // colon
sendtime[6] = (char)((sec / 16)+ 0b00110000); // shifts upper bits right four places the adds a "3" on for ascii
sendtime[7] = (char)((sec & 0b00001111) + 0b00110000); // clears upper bits adds a "3" on for ascii
sendtime[8] = (char)(00); // tells printf done
sel = 1; serial_port_sel(sel);
dummy = printf("%sj",Esc_seq); //clear screen and home cursor
dummy = printf("%s",sendtime); //sends array
}