先生,我不知道爲什麼我的顏色傳感器的C代碼無法正常工作。我使用的是ATMEGA16微控制器,我使用的傳感器是連接到微控制器的TO引腳PB0(PORTB0)的TCS230傳感器。請幫助我,我在這裏附上我的C代碼 - 請記住,我使用20%的縮放比例,並且已經將S0和S1引腳連接到1(VCC)和0(Gnd)。顏色傳感器(TCS230)接口不適用於ATMEGA16?
#define S2 PA0
#define S3 PA1
#define F_CPU 11.0592
#include <util/delay.h>
#include<avr/io.h>
#include<avr/interrupt.h>
//Variable declarations
unsigned char state;
unsigned int counter_r,counter_g,counter_b,counter_no;
unsigned char i=0; //to store value of counter
unsigned char flag=0;
void chkcolour();
int main()
{
DDRB=0x00; //PB0 and T0(counter pin) Input
DDRA=0xFF; //PA2(R),PA3(G) & PA4(B) for RGB LED ,PA0(S2) & PA1(S3) for RGB selection ,Output Pins
TCNT0=0x00;
TCCR0=0x07;
state=0; //start from 0 then 1,2,then again same
sei();
while(1)
{ flag=0;
switch(state)
{
case 0:
PORTA=0b00000000; //For Red
_delay_ms(1000);
counter_r=TCNT0;
TCNT0=0x00;
state=1;
case 1:
PORTA=0b00000010; //For blue
_delay_ms(1000);
counter_b=TCNT0;
TCNT0=0x00;
state=2;
case 2:
PORTA=0b00000011; //For Green
_delay_ms(1000);
counter_g=TCNT0;
TCNT0=0x00;
state=3;
case 3:
PORTA=0b00000001; //No Filter
_delay_ms(1000);
counter_no=TCNT0;
TCNT0=0x00;
state=0;
break;
}
chkcolour();
}
return 0;
}
void chkcolour()
{
if((counter_r > counter_b) && (counter_r > counter_g))
{
PORTA=0b00000100; //Glow RED LED,Off Green LED,Off Blue LED
flag=1;
}
else if((counter_g > counter_r) && (counter_g > counter_b))
{
PORTA=0b00001000; //Glow GEREEN LED,Off RED LED,Off Blue LED
flag=1;
}
else if((counter_b > counter_r) && (counter_b > counter_g) )
{
PORTA=0b00010000; //Glow BLUE LED,Off RED LED,Off GREEN LED
flag=1;
}
else
{
PORTA=0b00000000; //0ff GEREEN LED,Off RED LED,Off Blue LED
flag=1;
}
}
請編輯您的問題,以解釋您的意思是「不工作」。 – Blackwood