📄 main.c
字号:
#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
#include <string.h> /* include peripheral declarations */
#define ON 1
#define OFF 0
#define TOP 0
#define LEFT 1
#define UP 2
#define RIGHT 3
#define DOWN 4
#define BOTTOM 5
#define AsmStop __asm STOP;
volatile unsigned char state = 0;
volatile unsigned char sec = 5;
volatile unsigned char tsec = 5;
volatile unsigned char mins = 9;
volatile unsigned char tmins = 5;
volatile unsigned char hour = 3;
volatile unsigned char thour= 2;
volatile unsigned char Result = 0;
volatile unsigned char C_x = 0;
volatile unsigned char C_y = 0;
volatile unsigned char C_z = 0;
extern char ascii[];
extern void SetAlpha(char alphanum, char data);
extern void ClearDisplay(void);
extern void SetDisplay(void);
extern void Colon(char Cnum,char ON_OFF) ;
extern void Freescale(char ON_OFF);
extern void WriteDirection(unsigned char direction);
unsigned char *string;
unsigned int length, i;
/////////////////////////////////////////////////////////////////////////////////////////
// Peripheral Init
// --------------------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////////////////////////////
void Sys_Peripheral_Init(void){
SOPT1 = 0x63; //disable cop. enable stop.
SPMSC1 = 0x00; //LVD disabled
// For Stop2
// SPMSC2_PPDC = 1;
IRQSC_IRQPE =1;
////////////////////////////////////////////////////////////////////////////
// Init ADC
////////////////////////////////////////////////////////////////////////
ADCCFG = 0x60;
APCTL1_ADPC6 = 1; //Rz Light sensor
APCTL1_ADPC0 = 1; //Pot
APCTL1_ADPC1 = 1; //X
APCTL1_ADPC2 = 1; //Y
APCTL1_ADPC3 = 1; //Z
////////////////////////////////////////////////////////////////////////////
// Init TOD
////////////////////////////////////////////////////////////////////////
TODC_TODCLKS = 0;
TODC_TODPS = 1;
TODC_TODEN =1;
////////////////////////////////////////////////////////////////////////////
// Init TPM
////////////////////////////////////////////////////////////////////////
// Timer2 overflow about every 1ms
TPM1MOD = 8000;
// Stops timer2 and select 1 as prescaler divisor
TPM1SC = 0x08;
// Timer2 overflow about every 1ms
TPM2MOD = 8000;
// Stops timer2 and select 1 as prescaler divisor
TPM2SC = 0x00;
////////////////////////////////////////////////////////////////////////////
// Init KBI
////////////////////////////////////////////////////////////////////////
PTAPE = 0x80; //enable pullups for KBI7
KBIPE = 0x80; //Enable KBI7
KBISC_KBIE = 1; //Enable KBI Interrupts
KBISC_KBACK = 1; // Clear Pending interrupt
EnableInterrupts;
////////////////////////////////////////////////////////////////////////////
// Clock Init ICS
////////////////////////////////////////////////////////////////////////
ICSC2 = 0x07;
ICSC1 = 0x00;
////////////////////////////////////////////////////////////////////////////
// Init GPIO
////////////////////////////////////////////////////////////////////////
PTADD = 0x3F;
PTBDD = 0xFF;
PTCDD =0x7f;
PTDDD = 0xFF;
PTEDD =0xff;
PTAD = 0x00;
PTBD = 0x00 ;
PTCD =0x00;
PTDD = 0x00;
PTED =0x00;
////////////////////////////////////////////////////////////////////////////
// Init LCD
////////////////////////////////////////////////////////////////////////
LCDPEN0 = 0xFF;
LCDPEN1 = 0xFF;
LCDPEN2 = 0xFF;
LCDPEN3 = 0xFF;
//Enable all LCD Pins
//Enable Back Plane Pins
LCDBPEN0 = 0xDB;
LCDBPEN1 = 0x03;
//Set Back Planes
LCDWF0 = 0x01;
LCDWF3 = 0x02;
LCDWF6 = 0x04;
LCDWF9 = 0x08;
LCDWF1 = 0x10;
LCDWF7 = 0x20;
LCDWF4 = 0x40;
LCDWF8 = 0x80;
//Front plane data
SetDisplay();
///LCD Control registers
LCDRVC = 0x8b;
LCDSUPPLY = 0x83;
LCDC0 = 0xA7;
//Set up blink mode
LCDBCTL_BRATE =4;
//LCDBCTL_BLINK = 1;
}
////////////////////////////////////////////////////////////////////////////
// Play sound at passed frequency for Quarter Second
////////////////////////////////////////////////////////////////////////
void PlaySound(unsigned int frequency)
{
TPM1C0SC = 0x54;
TPM1C0V=frequency;
TODSC_QSECF =1;
while(TODSC_QSECF ==0);
TODSC_QSECF =1;
while(TODSC_QSECF ==0);
TPM1C0SC = 0x00;
}
////////////////////////////////////////////////////////////////////////////
// Scroll String
////////////////////////////////////////////////////////////////////////
void ScrollString(unsigned char *strng)
{
int j,l;
l = strlen(strng)-8;
j = 0;
while(j!= l)//Scroll Demo Board
{
SetAlpha(1,strng[j]);
SetAlpha(2,strng[j+1]);
SetAlpha(3,strng[j+2]);
SetAlpha(4,strng[j+3]);
SetAlpha(5,strng[j+4]);
SetAlpha(6,strng[j+5]);
SetAlpha(7,strng[j+6]);
SetAlpha(8,strng[j+7]);
SetAlpha(9,strng[j+8]);
j=j+1;
while(TODSC_QSECF == 0);
TODSC_QSECF = 1;
}
}
////////////////////////////////////////////////////////////////////////////
// Display String //must be less than 9 characters
////////////////////////////////////////////////////////////////////////
void DisplayString(unsigned char *dstrng)
{
int j,l;
char n;
l = strlen(dstrng);
j = 0;
n = 1;
while(j!=l){
SetAlpha(n,dstrng[j]);
j = j+1;
n++;
}
}
////////////////////////////////////////////////////////////////////////////
// State 0 display time in stop mode
////////////////////////////////////////////////////////////////////////
void StopClock(void)
{
ClearDisplay();
while(TODSC_SECF == 0);
TODSC_SECIE = 1;
Colon(3,ON);
Colon(2,ON);
Freescale(ON);
/////////////////////STATE 0////////////////////////
//////Simple Clock in stop Mode////////////////////
while(state ==0){
//display current second while in stop mode
AsmStop;
ADCSC1 = 0x20; //Read Pot
while (!(ADCSC1 & 0x80)); // Waits until ADC conversion is completed
Result = ADCRL;
Result = Result/16;
Result |=0x80;
LCDRVC = Result;
// TPM2Cx_PWM(2,25,666,0x04);
SetAlpha(4,thour+48);
SetAlpha(5,hour+48);
SetAlpha(6,tmins+48);
SetAlpha(7,mins+48);
SetAlpha(8,tsec+48);
SetAlpha(9,sec+48);
}
}
////////////////////////////////////////////////////////////////////////////
// State 1 Compare Light sensor to POT and ALARM
// if Light sensor reading is higher than POT
////////////////////////////////////////////////////////////////////////
void LightSense(void) {
int Low, High;
ClearDisplay();
Freescale(ON);
SetAlpha(1,'P');
SetAlpha(2,'T');
SetAlpha(3,'0');
SetAlpha(4,'0');
SetAlpha(5,' ');
SetAlpha(6,'R');
SetAlpha(7,'Z');
SetAlpha(8,'0');
SetAlpha(9,'0');
TODSC_SECIE = 0;
////////////STATE 1///////////////////
////////// ADC Features State//////////
/////Display POT Reading and Rz reading
/////Sound Alarm if RZ is Greater than POT
while(state == 1) {
ADCSC1 = 0x20; //Read Pot
while (!(ADCSC1 & 0x80)); // Waits until ADC conversion is completed
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -