Tuesday, 27 March 2012

Week 10

Title of Activity : Studying the Program that will Used in the Project which are PIC C Compiler using CCS Programming.

Objective : To improve my skill in programming doing Final Year Project

Content/ Procedure :

1) I install the PIC C Compiler in my laptop for easy doing and testing my Final Year Project coding.



2) In my Final Year Project, I used C language which is CCS programming.

3) Then, I testing with my first coding which is display below


#include<16F877A.h>

#fuses HS,NOLVP,NOWDT
#use delay(clock=20000000)
#include<lcd.c>
void wait_for_one_press()
{  while(input(PIN_B0)) ;
   while(!input(PIN_B0)) ; //Wait for one press
}

void main()
{
   long reading;
   float red, green, blue, total;
   lcd_init();
   lcd_gotoxy(1,1);
   delay_ms(500);
   printf (lcd_putc,"COLOR SENSOR");
   while (true){
   wait_for_one_press();
   lcd_init();
   delay_ms(500);
   lcd_gotoxy(1,1);
   printf (lcd_putc,"SCANNING..");
   setup_adc_ports(RA0_ANALOG);                 //select adc port
   setup_adc(ADC_CLOCK_INTERNAL);               //set adc clock
   set_adc_channel(0);                          //select adc pin RA1
   output_high (PIN_B7);
   delay_ms(1000);
   reading = read_adc();
   red = reading;
   delay_ms(500);
   output_low (PIN_B7);
   output_high (PIN_B6);
   delay_ms(1000);
   reading = read_adc();
   green = reading;
   delay_ms(500);
  
   output_low (PIN_B6);
   output_high (PIN_B5);
   delay_ms(1000);
   reading = read_adc();
   blue = reading;
   delay_ms(500);
   output_low (PIN_B5);
   //delay_ms(200);
   total = red + green + blue;
   delay_ms(500);
  
   if (total < 728){
   lcd_init();
   delay_ms(500);
   lcd_gotoxy(1,1);
   printf (lcd_putc,"COLOR WHITE");
   lcd_gotoxy(1,2);
   printf (lcd_putc,"%f",total);
   delay_ms(1000);
   }
  
  
  
   else if(total == 736){
   lcd_init();
   delay_ms(500);
   lcd_gotoxy(1,1);
   printf (lcd_putc,"COLOR BLUE");
   lcd_gotoxy(1,2);
   printf (lcd_putc,"%f",total);
   delay_ms(1000);
   }
   else if(total == 737){
   lcd_init();
   delay_ms(500);
   lcd_gotoxy(1,1);
   printf (lcd_putc,"COLOR BLUE");
   lcd_gotoxy(1,2);
   printf (lcd_putc,"%f",total);
   delay_ms(1000);
   }
  
   else if(total == 735){
   lcd_init();
   delay_ms(500);
   lcd_gotoxy(1,1);
   printf (lcd_putc,"COLOR GREEN");
   lcd_gotoxy(1,2);
   printf (lcd_putc,"%f",total);
   delay_ms(1000);
   }
  
   else if(total == 734){
   lcd_init();
   delay_ms(500);
   lcd_gotoxy(1,1);
   printf (lcd_putc,"COLOR RED");
   lcd_gotoxy(1,2);
   printf (lcd_putc,"%f",total);
   delay_ms(1000);
   }
  
   else if(total == 733){
   lcd_init();
   delay_ms(500);
   lcd_gotoxy(1,1);
   printf (lcd_putc,"COLOR RED");
   lcd_gotoxy(1,2);
   printf (lcd_putc,"%f",total);
   delay_ms(1000);
   }
  
   else if(total >= 738){
   lcd_init();
   delay_ms(500);
   lcd_gotoxy(1,1);
   printf (lcd_putc,"COLOR BLACK");
   lcd_gotoxy(1,2);
   printf (lcd_putc,"%f",total);
   delay_ms(1000);
   }
  
   else{
   lcd_init();
   delay_ms(500);
   lcd_gotoxy(1,1);
   printf (lcd_putc,"ERROR");
   lcd_gotoxy(1,2);
   printf (lcd_putc,"%f",total);
   delay_ms(1000);
   }
}
}
   
After I compiled, there is no error. Then I burned the program into PIC microcontroller to operate my project. Nice first try. I did it. But, the program is not accurate measurement and display the wrong colour. Furthermore, the range between another colours are so close. So, I must widen the range between another colours. Then, I try with another new coding.

#include<16F877A.h>
#device ADC=10
#fuses HS,NOLVP,NOWDT
#use delay(clock=20000000)
#include<lcd.c>

void wait_for_one_press()
{  while(input(PIN_B0)) ;
   while(!input(PIN_B0)) ; //Wait for one press
}

void main()
{
   long reading;
   float red, green, blue, total;
   lcd_init();
   lcd_gotoxy(1,1);
   delay_ms(500);
   printf (lcd_putc,"COLOR SENSOR");
   while (true){
   wait_for_one_press();
   lcd_init();
   delay_ms(500);
   lcd_gotoxy(1,1);
   printf (lcd_putc,"SCANNING..");
   setup_adc_ports(RA0_ANALOG);                 //select adc port
   setup_adc(ADC_CLOCK_INTERNAL);               //set adc clock
   set_adc_channel(0);                          //select adc pin RA1
   output_high (PIN_B7);
   delay_ms(1000);
   reading = read_adc();
   red = reading;
   delay_ms(500);
   output_low (PIN_B7);
   output_high (PIN_B6);
   delay_ms(1000);
   reading = read_adc();
   green = reading;
   delay_ms(500);
  
   output_low (PIN_B6);
   output_high (PIN_B5);
   delay_ms(1000);
   reading = read_adc();
   blue = reading;
   delay_ms(500);
   output_low (PIN_B5);
   //delay_ms(200);
   total = red + green + blue;
   delay_ms(500);
  
   if (total <= 2940){
   lcd_init();
   delay_ms(500);
   lcd_gotoxy(1,1);
   printf (lcd_putc,"COLOR WHITE");
   lcd_gotoxy(1,2);
   printf (lcd_putc,"%f",total);
   delay_ms(1000);
   }
  
  
  
   else if(total<=2970 && total>= 2963){
   lcd_init();
   delay_ms(500);
   lcd_gotoxy(1,1);
   printf (lcd_putc,"COLOR BLUE");
   lcd_gotoxy(1,2);
   printf (lcd_putc,"%f",total);
   delay_ms(1000);
   }

   else if(total<=2949 && total>=2943){
   lcd_init();
   delay_ms(500);
   lcd_gotoxy(1,1);
   printf (lcd_putc,"COLOR GREEN");
   lcd_gotoxy(1,2);
   printf (lcd_putc,"%f",total);
   delay_ms(1000);
   }
   
   else if(total<=2958 && total>=2950){
   lcd_init();
   delay_ms(500);
   lcd_gotoxy(1,1);
   printf (lcd_putc,"COLOR RED");
   lcd_gotoxy(1,2);
   printf (lcd_putc,"%f",total);
   delay_ms(1000);
   }
  
  
   else if(total >= 2971){
   lcd_init();
   delay_ms(500);
   lcd_gotoxy(1,1);
   printf (lcd_putc,"COLOR BLACK");
   lcd_gotoxy(1,2);
   printf (lcd_putc,"%f",total);
   delay_ms(1000);
   }
  
   else{
   lcd_init();
   delay_ms(500);
   lcd_gotoxy(1,1);
   printf (lcd_putc,"ERROR");
   lcd_gotoxy(1,2);
   printf (lcd_putc,"%f",total);
   delay_ms(1000);
   }
}
}
  
Lastly, I did the program and make the project functioned properly. The colour detector can accurately measure the five colours range without any errors. All the colour can display the results on the LCD screen correctly.

Result and analysis :
 
First coding :



Second coding :




Conclusions :

I feel very happy because I successfully can functioned the project although my first try. I copy the coding in the internet from forum chat in the internet that discussing about the coding in the PIC C Compiler. I also learned the programming from my knowledgeable friends and lecturer on how to edit and lastly burned the program in the PIC microcontroller.



No comments:

Post a Comment