📄 accelerometer.c
字号:
BUZZER_INIT_MACRO
BUZZER = BUZZER_ON;
#endif
LED1 = LED_ON; /* Turn LEDs ON */
LED2 = LED_ON; /* Turn LEDs ON */
LED3 = LED_ON; /* Turn LEDs ON */
LED4 = LED_ON; /* Turn LEDs ON */
PB0PU = 1; /* Pushbutton directions and pull-ups */
PB0DIR = 0;
PB1PU = 1;
PB1DIR = 0;
PB2PU = 1;
PB2DIR = 0;
PB3PU = 1;
PB3DIR = 0;
//KBI1PE = 0x0C; /* Enable Pins 2-3 */
KBI1SC = 0x02; /* Turn On Interrupts */
/* Initialize the packet. */
gsTxPacket.u8DataLength = 0; /* Initialize the gsTxPacket global */
gsTxPacket.pu8Data = &gau8TxDataBuffer[0]; /* Set the pointer to point to the tx_buffer */
gsRxPacket.u8MaxDataLength = 0;
gsRxPacket.pu8Data = &gau8RxDataBuffer[0];
gsRxPacket.u8MaxDataLength = 16; /* Arbitrary, bigger than xXyYzZ format. */
gsRxPacket.u8Status = 0; /* initialize status to 0. */
sCalValues.NxOff = sCalDefaultValues->NxOff;
sCalValues.NxMax = sCalDefaultValues->NxMax;
sCalValues.NyOff = sCalDefaultValues->NyOff;
sCalValues.NyMax = sCalDefaultValues->NyMax;
sCalValues.NzOff = sCalDefaultValues->NzOff;
sCalValues.NzMax = sCalDefaultValues->NzMax;
#if BUZZER_ENABLED==TRUE
BUZZER = BUZZER_OFF;
#endif
/* OTAP entry */
#if SMAC_FEATURE_OTAP == TRUE
if (PB0 == 0){
KBI1SC = 0x00; /* Turn On Interrupts */
OTAPActivate();
}
#endif // SMAC_FEATURE_OTAP == TRUE
LED1 = LED_OFF; /* Turn LEDs OFF */
LED2 = LED_OFF; /* Turn LEDs OFF */
LED3 = LED_OFF; /* Turn LEDs OFF */
LED4 = LED_OFF; /* Turn LEDs OFF */
/* Setup the S08 modes */
#if defined (HCS08Q)
RTCSC=RTCSC&~0x08; /* Disable wake up timer. */
SPMSC2=SPMSC2&~0x03; /* Enable deep sleep mode stop3. */
#else
SRTISC=SRTISC&~0x07; /* Disable wake up timer. */
SPMSC2=SPMSC2&~0x03; /* Enable deep sleep mode stop3. */
#endif
/* Setup Timer 1 */
TPM1MOD = 625; /* Set the timer for a 26ms timer rate (1/8E6) * 128 * 625 = 10msec */
/* Timer rate = (1/BUSCLOCK) * TIMER_PRESCALER * TIMER MOD */
TPM1SC = 0x4F; /* Timer divide by 128. (16uS timebase for 8MHz bus clock). interrupt on*/
/* Blink the Leds twice and sound buzzer twice */
#if BUZZER_ENABLED==TRUE
BUZZER = BUZZER_ON;
#endif
setLed(0xF, 20);
#if BUZZER_ENABLED==TRUE
BUZZER = BUZZER_OFF;
#endif
setLed(0x0, 20);
#if BUZZER_ENABLED==TRUE
BUZZER = BUZZER_ON;
#endif
setLed(0xF, 20);
#if BUZZER_ENABLED==TRUE
BUZZER = BUZZER_OFF;
#endif
setLed(0x0, 20);
}
/**************************************************************
* Function: delay
* Parameters: 16-bit delay value
* Return: none
*
* Simple delay loop.
**************************************************************/
void delay(INT16 count)
{
UINT16 u16Match;
u16Match = gu16msTimer + count;
while (gu16msTimer != u16Match);
}
UINT8 compareADCReading(UINT8 u8LastReading, UINT8 u8CurrentReading, UINT8 threshold) {
UINT8 u8MaxReading = 255 - threshold;
UINT8 u8MinReading = threshold;
UINT8 u8Upper, u8Lower;
/* check for the boundary conditions of 0 and 255 */
if (u8CurrentReading < u8MinReading)
return FALSE;
if (u8CurrentReading > u8MaxReading)
return FALSE;
u8Upper = u8LastReading + threshold;
u8Lower = u8LastReading - threshold;
if (u8CurrentReading > u8Upper )
return TRUE;
if (u8CurrentReading < u8Lower )
return TRUE;
return FALSE;
}
UINT8 gu8timerEvent = 0;
#if defined (HCS08Q)
#pragma CODE_SEG __NEAR_SEG NON_BANKED
__interrupt void IRQTimer1(void) {
gu16msTimer++;
if (gu16timerEvent[0] != 0) {
if(gu16timerEvent[0] == gu16msTimer) {
gu16Events |= TIMER_EVENT0;
gu16timerEvent[0] = 0; /* Turn off the timer0 */
}
}
if (gu16timerEvent[1] != 0) {
if(gu16timerEvent[1] == gu16msTimer) {
gu16Events |= TIMER_EVENT1;
gu16timerEvent[1] = 0; /* Turn off the timer1 */
}
}
TPM1SC &= ~0x80; /* clear the interrupt */
}
#pragma CODE_SEG DEFAULT
#else
interrupt void IRQTimer1(void) {
gu16msTimer++;
if (gu16timerEvent[0] != 0) {
if(gu16timerEvent[0] == gu16msTimer) {
gu16Events |= TIMER_EVENT0;
gu16timerEvent[0] = 0; /* Turn off the timer0 */
}
}
if (gu16timerEvent[1] != 0) {
if(gu16timerEvent[1] == gu16msTimer) {
gu16Events |= TIMER_EVENT1;
gu16timerEvent[1] = 0; /* Turn off the timer1 */
}
}
TPM1SC &= ~0x80; /* clear the interrupt */
}
#endif
/**************************************************************
* Function: doAccelMeasurement
* Parameters: UINT8 axis
* Return: none
*
* Performs x,y,z measurements compensated
**************************************************************/
#pragma MESSAGE DISABLE C5703
void doAccelMeasurement(INT8 axis)
{
#if ACCEL_ENABLED==TRUE
INT16 i16i;
if (axis & X_AXIS) {
ACCEL_START_X
while((ATD1SC & 0x80) != 0x80);
gau8TxDataBuffer[1] = ATD1RH;
i16i = gau8TxDataBuffer[1] + (128 - sCalValues.NxOff);
if (i16i > 220) i16i = 220;
if (i16i < 33) i16i = 33;
gau8TxDataBuffer[1] = (UINT8) i16i;
return;
}
if (axis & Y_AXIS) {
ACCEL_START_Y
while((ATD1SC & 0x80) != 0x80);
gau8TxDataBuffer[1] = ATD1RH;
i16i = gau8TxDataBuffer[1] + (128 - sCalValues.NyOff);
if (i16i > 220) i16i = 220;
if (i16i < 33) i16i = 33;
gau8TxDataBuffer[1] = (UINT8) i16i;
return;
}
if (axis & Z_AXIS) {
ACCEL_START_Z
while((ATD1SC & 0x80) != 0x80);
gau8TxDataBuffer[1] = ATD1RH;
i16i = gau8TxDataBuffer[1] + (128 - sCalValues.NzOff);
if (i16i > 220) i16i = 220;
if (i16i < 33) i16i = 33;
gau8TxDataBuffer[1] = (UINT8) i16i;
return;
}
#endif
}
/**************************************************************
* Function: doAccelMeasurementNoCompensation
* Parameters: UINT8 axis
* Return: none
*
* Performs x,y,z measurements with no compensation
* (returns the actual ATD reading)
**************************************************************/
void doAccelMeasurementNoCompensation(INT8 axis)
{
#if ACCEL_ENABLED==TRUE
if (axis & X_AXIS) {
ACCEL_START_X
while((ATD1SC & 0x80) != 0x80);
gau8TxDataBuffer[1] = ATD1RH;
return;
}
if (axis & Y_AXIS) {
ACCEL_START_Y
while((ATD1SC & 0x80) != 0x80);
gau8TxDataBuffer[1] = ATD1RH;
return;
}
if (axis & Z_AXIS) {
ACCEL_START_Z
while((ATD1SC & 0x80) != 0x80);
gau8TxDataBuffer[1] = ATD1RH;
return;
}
#endif
}
/**************************************************************
* Function: doAccelMeasurement
* Parameters: UINT8 axis
* Return: none
*
* Performs x,y,z measurements
**************************************************************/
void calibration_app(void) {
INT16 X=0, Y=0, Z=0;
UINT8 i;
UINT8 temp;
#pragma MESSAGE DISABLE C1805
/* Get the data */
for (i = 0; i < 8; i++){
doAccelMeasurementNoCompensation(X_AXIS);
X += (UINT16) gau8TxDataBuffer[1];
doAccelMeasurementNoCompensation(Y_AXIS);
Y += (UINT16) gau8TxDataBuffer[1];
doAccelMeasurementNoCompensation(Z_AXIS);
Z += (UINT16) gau8TxDataBuffer[1];
delay(10);
}
X >>= 3;
Y >>= 3;
Z >>= 3;
/* Calculate the calibration values */
sCalValues.NxMax = (UINT8) Z;
sCalValues.NyMax = (UINT8) Z;
sCalValues.NzMax = (UINT8) Z;
sCalValues.NxOff = (UINT8) X;
sCalValues.NyOff = (UINT8) Y;
sCalValues.NzOff = (UINT8)((INT16)(Z - 189) + 128);
asm nop;
DisableInterrupts;
FCDIV = 39;
FSTAT = 0x80; //Put FCBEF at 1.
Page_Erase(0xC200);
if(temp==0xFF){
asm nop;
}
temp = Program_Byte(0xC202,sCalValues.NxOff);
if(temp==0xFF){
asm nop;
}
temp = Program_Byte(0xC203,sCalValues.NxMax);
if(temp==0xFF){
asm nop;
}
temp = Program_Byte(0xC204,sCalValues.NyOff);
if(temp==0xFF){
asm nop;
}
temp = Program_Byte(0xC205,sCalValues.NyMax);
if(temp==0xFF){
asm nop;
}
temp = Program_Byte(0xC206,sCalValues.NzOff);
if(temp==0xFF){
asm nop;
}
temp = Program_Byte(0xC207,sCalValues.NzMax);
if(temp==0xFF){
asm nop;
}
asm nop;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -