//Good code but output is somewhat random, need to remove the MIN and MAX
sbit ADSK at RD2_bit;
sbit ADDO at RD3_bit;
sbit ADSK_Direction at TRISD.B2;
sbit ADDO_Direction at TRISD.B3;
sbit LCD_RS at LATA5_bit;
sbit LCD_EN at LATA4_bit;
sbit LCD_D4 at LATA0_bit;
sbit LCD_D5 at LATA1_bit;
sbit LCD_D6 at LATA2_bit;
sbit LCD_D7 at LATA3_bit;
sbit LCD_RS_Direction at TRISA5_bit;
sbit LCD_EN_Direction at TRISA4_bit;
sbit LCD_D4_Direction at TRISA0_bit;
sbit LCD_D5_Direction at TRISA1_bit;
sbit LCD_D6_Direction at TRISA2_bit;
sbit LCD_D7_Direction at TRISA3_bit;
char *txt;
char *txt2;
int i, j;
long int data_average;
long int count;
long int array[25] ;
long int temp;
void bubble_sort() {
/*for(i = 0; i<25; i++) {
LCD_Init();
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Cmd(_LCD_CLEAR);
LongToStr(array[i], txt);
IntToStr(i, txt2);
lcd_out(1, 1, "step 1");
Lcd_Out(1,5,txt);
lcd_out(2, 3, txt2);
Delay_ms(300);
}*/
for(i = 0; i<25; i++) {
for(j = i+1; j<25; j++)
{
if(array[j] < array[i]) {
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
/*for(i = 0; i<25; i++) {
LCD_Init();
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Cmd(_LCD_CLEAR);
LongToStr(array[i], txt);
IntToStr(i, txt2);
lcd_out(1, 1, "Step 2");
lcd_out(1, 3, txt2);
Lcd_Out(2,1,txt);
Delay_ms(300);
}*/
} // end bubble sort
void main(void) {
C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;
ADCON1 |= 0x0F;
ADCON1 = 0b00000110; // all ADC ports digital*/
// SLRCON = 0; // Configure all PORTS at the standard Slew Rate
////////////////////
VREFCON1=0; // vref off
VREFCON2=0; // vref off
PORTD = 0b00000000;
// INTCON = 0b00000000; // disable all interrupts ...INT0IE: RB0/INT External Interrupt Enable bit
ANSELD = 0b11000000; // Configure all PORTS at the standard Slew Rate
LCD_Init();
LCD_Cmd(_LCD_CURSOR_OFF);
LCD_Cmd(_LCD_CLEAR);
while(1) {
data_average = 0;
for(j = 0; j < 25; j++) {
TRISD = 0b10110010; //0 = Output,
ADDO = 1;
ADSK = 0;
TRISD = 0b10111010; //0 = Output, 1 = Input RD3
while(ADDO ) ;
//wait until ADDO is zero
count = 0;
for(i = 0; i < 24; i++) {
Delay_us(9);
ADSK = 1;
Delay_us(9);
ADSK = 0;
if(ADDO=1) count = count +1 ;
if(ADDO=0) count = count ; // dont really need this line
count = count << 1 ;
} // end for loop
ADSK = 1;
count = count ^ 0x800000;
ADSK = 0;
array[j] = count;
if (count > 0xFFFFFF)
{
LCD_Cmd(_LCD_CURSOR_OFF);
LCD_Cmd(_LCD_CLEAR);
lcd_out(1, 1, "Out of range");
LCD_Out(2,1,txt);
Delay_ms(200);
}
data_average = count - 8453990;
// data_average = count /3;
// data_average = data_average / 10;
/*LCD_Cmd(_LCD_CURSOR_OFF);
LCD_Cmd(_LCD_CLEAR);
IntToStr(j, txt);
LongToStr(array[j], txt);
lcd_out(1, 1, "init array");
lcd_out(2, 1, txt);
Delay_ms(300);*/
} // end for loop j
// Call array_sort function to sort the 25 values. Keep the middle 15 values only
/*LCD_Cmd(_LCD_CURSOR_OFF);
LCD_Cmd(_LCD_CLEAR);
lcd_out(1, 1, "bubble_sort");
Delay_ms(500);*/
bubble_sort(); // Sorting from ascending order take middle 13 samples
temp =0;
for(j = 9; j < 17; j++) {
temp = temp + array[j];
} // end for loop for sampling average
/* 8453990 - no weight
8454048 - 20g diff
8454138 - 50g
8388662 - 70g
*/
temp = temp /8 ;
LCD_Cmd(_LCD_CURSOR_OFF);
LCD_Cmd(_LCD_CLEAR);
LongToStr(temp, txt);
lcd_out(1, 1, "Final value");
lcd_out(2, 1, txt);
Delay_ms(2120);
} // end infinit while loop
} // end main