📄 main.c
字号:
Result = ADCRL;
Low = Result&0x0F;
High = (Result&0xf0)/16;
if(Low<=0x09){
SetAlpha(4,Low+48);
} else {
SetAlpha(4,Low+55);}
if(High<=0x09){
SetAlpha(3,High+48);
} else {
SetAlpha(3,High+55);}
ADCSC1 = 0x26; //Read Rz
while (!(ADCSC1 & 0x80)); // ADC conversion completed?
if(ADCRL>=Result){
PlaySound(2350);}
Result = ADCRL;
Low = Result&0x0F;
High = (Result&0xf0)/16;
if(Low<=0x09){
SetAlpha(9,Low+48);
} else {
SetAlpha(9,Low+55);}
if(High<=0x09){
SetAlpha(8,High+48);
} else {
SetAlpha(8,High+55);}
}
}
////////////////////////////////////////////////////////////////////////////
// State 2 Accelerometer Demo
// Show Right/Left UP/ Down
////////////////////////////////////////////////////////////////////////
void AccelDemo(void) {
unsigned char x,y,z;
TODSC_SECIE = 0;
ClearDisplay();
Freescale(ON);
string = " ACCEL DEMO TILT BOARD UP DOWN LEFT RIGHT ";
ScrollString(string);
/////////////////////////////////
while(state==2){
//Get X Y Z
ADCSC1 = 0x21; //Read Pot
while (!(ADCSC1 & 0x80)); // Waits until ADC conversion is completed
x = ADCRL;
ADCSC1 = 0x22; //Read Pot
while (!(ADCSC1 & 0x80)); // Waits until ADC conversion is completed
y = ADCRL;
ADCSC1 = 0x23; //Read Pot
while (!(ADCSC1 & 0x80)); // Waits until ADC conversion is completed
z = ADCRL;
//Check Right Left UP Down
if(x>=(C_x + 25)){
//Set Right
string = "RIGHT ";
DisplayString(string);
WriteDirection(RIGHT);
PlaySound(2350);
} else if(x<=(C_x -25)){
//Set Left
string = "LEFT ";
WriteDirection(LEFT);
DisplayString(string);
PlaySound(2350);
} else if(y<=(C_y - 25)){
//Set UP
string = "UP ";
DisplayString(string);
WriteDirection(UP);
PlaySound(2350);
} else if(y>=C_y +25){
//Set Down
string = "DOWN ";
DisplayString(string);
WriteDirection(DOWN);
PlaySound(2350);
}else {
string = "MOVE ";
DisplayString(string);
}
}
}
// Initialize the TPM2 for PWM outputs (Channel 2 for PTC1 LED)
void TPM2Cx_PWM(byte CHAN, byte DUTY_CYCLE, word MOD, byte CLK_PS) {
int val;
if (MOD==0) {
TPM2SC=0x00; // disable PWM.
return;
}
if ((TPM2MODH==(MOD>>8)) && (TPM2MODL==(MOD-1))) {
} else {
TPM2MODH = (byte) (MOD>>8); // set period
TPM2MODL = (byte) (MOD-1);
}
switch (CHAN) {
case 0:
TPM2C0SC = 0x24; // Center-aligned low-true pulses
if (MOD>100){
val = DUTY_CYCLE*(MOD/100);
}
else {
val = DUTY_CYCLE*MOD/100;
}
TPM2C0VH = (byte) (val>>8); // set duty cycle for Channel 0
TPM2C0VL = (byte) (val);
break;
case 1:
TPM2C1SC = 0x24; // Center-aligned low-true pulses
if (MOD>100){
val = DUTY_CYCLE*(MOD/100);
}
else {
val = DUTY_CYCLE*MOD/100;
}
TPM2C1VH = (byte) (val>>8); // set duty cycle for Channel 1
TPM2C1VL = (byte) (val);
break;
break;
default:
break;
}
TPM2SC = (byte) (0x28|(CLK_PS&0x07)); // All center-aligned, bus frequency/Prescale, start timer
} //end TPM2Cx_PWM
////////////////////////////////////////////////////////////////////////////
// Main Routine
////////////////////////////////////////////////////////////////////////
void main(void)
{
Sys_Peripheral_Init();
//Calibrate Accell
ADCSC1 = 0x21; //Read Pot
while (!(ADCSC1 & 0x80)); // Waits until ADC conversion is completed
C_x = ADCRL;
ADCSC1 = 0x22; //Read Pot
while (!(ADCSC1 & 0x80)); // Waits until ADC conversion is completed
C_y = ADCRL;
ADCSC1 = 0x23; //Read Pot
while (!(ADCSC1 & 0x80)); // Waits until ADC conversion is completed
C_z = ADCRL;
string = " 9S08LL16 DEMO BOARD ";
ScrollString(string);
state = 3;
string = "LOGICANLY";
DisplayString(string);
for(;;) //Main Loop
{
ADCSC1 = 0x20; //Read Pot
while (!(ADCSC1 & 0x80)); // Waits until ADC conversion is completed
Result = ADCRL;
TPM2Cx_PWM(0,(byte)((Result&0x00FF)>>1),666,0x07);
TPM2Cx_PWM(1,(byte)((Result&0x00FF)>>1),666,0x07);
if(state == 0)
{ StopClock(); //clock demo
}else if(state ==1){
LightSense(); //light sense demo
}else if(state ==2){
AccelDemo(); //Accelerometer Demo
}
}
}
/* loop forever */
/////////////////////////////////////////////////////////////////////////////////////////
// KBI_ISR
// --------------------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////////////////////////////
interrupt VectorNumber_Vkeyboard void KBI_ISR(void){
if (PTAD_PTAD7 == 0)
{
while(PTAD_PTAD7 ==0);
while(PTAD_PTAD7 ==0);
if(PTAD_PTAD7 ==1){
if(state >=2){
state =0 ;
}else {
state = state +1 ;
}
}
}
//clear KBF
KBISC_KBACK = 1;
}
/////////////////////////////////////////////////////////////////////////////////////////
// TOD_ISR
// --------------------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////////////////////////////
interrupt VectorNumber_Vtod void tod_ISR(void){
TODSC_SECF = 1;
sec = sec +1;
if(sec ==10) {
tsec = tsec +1;
sec =0;
}
if(tsec ==6){
mins = mins +1;
tsec = 0;
}
if (mins == 10){
tmins = tmins +1;
mins =0;
}
if(tmins == 6){
hour = hour +1;
tmins = 0;
}
if(hour == 10){
thour = thour +1;
hour = 0;
}
if ((thour == 2)&&(hour ==4)){
thour = 0;
hour = 0 ;
}
}
/////////////////////////////////////////////////////////////////////////////////////////
// TPM_ISR
// --------------------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////////////////////////////
interrupt VectorNumber_Vtpm1ch0 void timer1_CH0(void)
{
// Clear CH0F flag
TPM1C0SC &=0x7F;
//Clear the 16-bit timer1 counter
TPM1CNTH = 0x00;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -