Hi guys, i have recently programmed my PIC 16f877a, im using TCS230 as my color sensor, so i have used interrupt function to read the output of TCS230 and send it to PC via rs232, below is the program i did,
, so if you look at the code, i have type 'S' from my pc to get the output from PIC, but what im getting is only (0,0,0) whereby when i measure the output of sensor directly to oscilloscope and there it output and kip changing when the filter changed by the PIC according to the software above, but when i measure the amplitude of the output, its 2.5 to 2.6V in output line of the sensor, is it the reason that PIC couldnt read it? do i need to amplify the signal for PIC? Here i attach the datasheet of TCS230. if i need to amplify, what it the best way? Op-Amp or Transistor?
Rich (BB code):
#include <stdio.h>
#include <htc.h>
#include "usart.h"
#include "delay.h"
__CONFIG(0x3f71);
#define NUMBER_OF_PERIODS 6
unsigned int average;
unsigned char isAverageReady;
int R, G, B;
void delay ( void )
{
DelayMs ( 100 );
}
static interrupt void isr ( void )
{
static unsigned int time [ NUMBER_OF_PERIODS + 1 ];
static unsigned char timeIndex = 0;
if ( INTF )
{
time [ timeIndex++ ] = ( TMR1H << 8 ) + TMR1L;
if ( timeIndex == NUMBER_OF_PERIODS + 1 )// wait until period reach 5 then 5+1 and proceed
{
INTE = 0;
timeIndex = 0;
average = ( time [ NUMBER_OF_PERIODS ] - time [ 0 ] ) / NUMBER_OF_PERIODS; //time is a memory location, time [0] is start and number of period is end, end -start /end
isAverageReady = 1;
}
INTF = 0;
}
}
void getRGB ( void )
{
// Get R Value
RB2 = 0;
RB4 = 0;
delay();
isAverageReady = 0;
INTE = 1;
while ( !isAverageReady )
continue;
R = average;
// Get G Value
RB2 = 1;
RB4 = 1;
delay();
isAverageReady = 0;
INTE = 1;
while ( !isAverageReady )
continue;
G = average;
// Get B Value
RB2 = 0;
RB4 = 1;
delay();
isAverageReady = 0;
INTE = 1;
while ( !isAverageReady )
continue;
B = average;
}
void main ( void )
{
unsigned char input;
// Initializations
init_comms();
TRISB = 0x01;
INTEDG = 0;
INTE = 1;
TMR1CS = 0;
GIE = 1;
while ( 1 )
{
// Body
input = getch();
switch ( input )
{
case 'S':
getRGB();
printf ( "%d,%d,%d\r\n", R, G, B );
break;
}
}
}
Attachments
-
115.8 KB Views: 22