//********************************************************************** /*   <XYZ加速度(ガル)表示器)> */ //********************************************************************** unsigned long measurement(unsigned short channel) { static unsigned int cnt; static unsigned long ad; // ad = 0; for (cnt = 0; cnt < 10; cnt++) { ad += Adc_Read(channel); } return (ad / 10); } //********************************************************************** const char character1[] = {16,16,16,16,16,16,16,16}; const char character2[] = {24,24,24,24,24,24,24,24}; const char character3[] = {28,28,28,28,28,28,28,28}; const char character4[] = {30,30,30,30,30,30,30,30}; const char character5[] = {31,31,31,31,31,31,31,31}; void RegistCustomChar() { char i; // LCD_Cmd(64); for (i = 0; i<=7; i++) { LCD_Chr_Cp(character1[i]); } for (i = 0; i<=7; i++) { LCD_Chr_Cp(character2[i]); } for (i = 0; i<=7; i++) { LCD_Chr_Cp(character3[i]); } for (i = 0; i<=7; i++) { LCD_Chr_Cp(character4[i]); } for (i = 0; i<=7; i++) { LCD_Chr_Cp(character5[i]); } LCD_Cmd(LCD_RETURN_HOME); } //********************************************************************** void BarDisp(char index, unsigned int dat) { short i, j, k, cnt; // i = (dat * 10) / 128; j = i / 5; k = i - (j * 5); // if (index == 1) Lcd_Cmd(LCD_FIRST_ROW); else Lcd_Cmd(LCD_SECOND_ROW); // for (cnt = 1; cnt <= j; cnt++) { Lcd_Chr_Cp(4); } Lcd_Chr_Cp(k); for (cnt++; cnt <= 16; cnt++) { Lcd_Chr_Cp(' '); } } //********************************************************************** double offset_x, offset_y, offset_z, offset_1g; void initKXM52() { PORTB.F0 = 0; Delay_ms(100); // offset_z = (double)measurement(2) * 4.8828125; // offset_y = (double)measurement(3) * 4.8828125; // offset_x = (double)measurement(4) * 4.8828125; // PORTB.F0 = 1; Delay_ms(100); // offset_1g = measurement(4) * 4.8828125; offset_1g = offset_1g - offset_x; // PORTB.F0 = 0; Delay_ms(100); } //********************************************************************** void main() { static unsigned short cnt, buf[16]; static unsigned long tmp_x, tmp_y, tmp_z, tmp; static double ad; // OSCCON = 0b01110000; // クロックを8Mhzに設定する。 TRISA = 0b11111111; TRISB = 0b11111110; // ANSEL = 0b00011100; // Lcd_Config(&PORTB,3,1,2,7,6,5,4); RegistCustomChar(); Lcd_Cmd(LCD_CURSOR_OFF); Lcd_Cmd(LCD_CLEAR); for (cnt = 0; cnt < 16; cnt++) { Lcd_Chr(1, cnt + 1, 0xFF); Delay_ms(50); } for (cnt = 0; cnt < 16; cnt++) { Lcd_Chr(2, cnt + 1, 0xFF); Delay_ms(50); } Delay_ms(1000); Lcd_Cmd(LCD_CLEAR); // initKXM52(); // while (1) { // Z,Y,X tmp_z = measurement(2); tmp_y = measurement(3); tmp_x = measurement(4); // Z ad = (double)tmp_z * 4.8828125; ad = ad - offset_z; ad = (((double)labs(ad)) * 1000) / offset_1g; tmp = ad / 10; if ((ad - (tmp * 10)) >= 5) tmp++; WordToStr(tmp * 10, buf); Lcd_Out(1, 1, &buf[1]); // Y ad = (double)tmp_y * 4.8828125; ad = ad - offset_y; ad = (((double)labs(ad)) * 1000) / offset_1g; tmp = ad / 10; if ((ad - (tmp * 10)) >= 5) tmp++; WordToStr(tmp * 10, buf); Lcd_Out(1, 6, &buf[1]); // X ad = (double)tmp_x * 4.8828125; ad = ad - offset_x; ad = (((double)labs(ad)) * 1000) / offset_1g; tmp = ad / 10; if ((ad - (tmp * 10)) >= 5) tmp++; WordToStr(tmp * 10, buf); Lcd_Out(1, 11, &buf[1]); // switch (PORTA & 0x03) { case 0: Lcd_Chr(1, 16, 'X'); BarDisp(2, tmp_x); break; case 1: Lcd_Chr(1, 16, 'Y'); BarDisp(2, tmp_y); break; case 2: Lcd_Chr(1, 16, 'Z'); BarDisp(2, tmp_z); break; case 3: Lcd_Chr(1, 16, ' '); BarDisp(2, 0); break; } // if (PORTA.F5 == 0) initKXM52(); } } //**********************************************************************