milan.rajik
Banned
Try this code.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 #include "float2ascii.h" // Lcd module connections sbit LCD_RS at RB4_bit; sbit LCD_EN at RB5_bit; sbit LCD_D4 at RB0_bit; sbit LCD_D5 at RB1_bit; sbit LCD_D6 at RB2_bit; sbit LCD_D7 at RB3_bit; sbit LCD_RS_Direction at TRISB4_bit; sbit LCD_EN_Direction at TRISB5_bit; sbit LCD_D4_Direction at TRISB0_bit; sbit LCD_D5_Direction at TRISB1_bit; sbit LCD_D6_Direction at TRISB2_bit; sbit LCD_D7_Direction at TRISB3_bit; #define method1 //#define method2 char strDisplay[30]; char unit[] = " G "; double zMax = -3.0; double xMax = -3.0; double gVal = 0.0; char i = 0; double ReadAxis(char channel) { // Read Axis values from DE-ACCM3D char i = 0; double analogvalue = 0.0; while(i < 2) { analogvalue += ADC_Read(channel); Delay_us(10); ++i; } //return ((analogValue / 2.0) * 9.091 / 1023.0) - 3.0; return (analogValue / 225.06) - 3.01; } char interruptFlag = 0; void interrupt() { #ifdef method1 if(INTF_bit) { //If button pressed interruptFlag = 1; } #endif #ifdef method2 if((INTF_bit) && (!INTEDG_bit)) { //If button pressed INTEDG_bit = 1; } else if((INTF_bit) && (INTEDG_bit)) { //If button released interruptFlag = 1; INTEDG_bit = 0; } #endif INTF_bit = 0; } double ReadAxis (char channel) { // Read Axis values from DE-ACCM3D char i = 0; double analogvalue = 0.0; while(i < 2) { analogvalue += ADC_Read(channel); Delay_us(10); ++i; } //return ((analogValue / 2.0) * 9.091 / 1023.0) - 3.0; return (analogValue / 225.1) - 3.0; } void main() { OSCCON = 0b01111010; OSCSTAT = 0b00011001; OSCTUNE = 0b00011111; ADCON1 = 0b10000011; DACCON0 = 0; SRCON0 = 0; CM1CON0 = 0; CM2CON0 = 0; TRISA = 0b00000110; TRISB = 0b00000001; ANSELA = 0b00000110; ANSELB = 0b00000000; Lcd_Init(); Lcd_Cmd(_LCD_CURSOR_OFF); Lcd_Cmd(_LCD_CLEAR); OPTION_REG = 0b10000000; INTCON = 0b11010000; while(1) { if(interruptFlag) { zMax = 0.0; xMax = 0.0; oldZanalogvalue = 0.0; oldXanalogvalue = 0.0; interruptFlag = 0; } if(i == 1) { gVal = ReadAxis(1); Float2Ascii(gVal, strDisplay, 2); strcat(strDisplay, "G"); Lcd_Out(1,1,strDisplay); if(gVal > xMax) { xMax = gVal; Float2Ascii(xMax, strDisplay, 2); Lcd_Out_cp(strDisplay); } gVal = 0; } if(i == 2) { gVal = ReadAxis(2); Float2Ascii(gVal, strDisplay, 2); strcat(strDisplay, "G"); Lcd_Out(1,1,strDisplay); if(gVal > zMax) { zMax = gVal; Float2Ascii(zMax, strDisplay, 2); Lcd_Out_cp(strDisplay); } gVal = i = 0; } i++; Delay_ms(100); } }