先生,我不知道为什么我的颜色传感器的C代码不能工作。我使用的是ATMEGA16单片机,使用的传感器是TCS230传感器,它与单片机的引脚PB0(PORTB0)相连。请帮帮我,我在这里附上我的c代码--请记住,我正在使用20%的缩放,并且已经用1(VCC)和0(Gnd)连接了S0和S1引脚。
#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;
}
} 发布于 2015-07-29 07:03:01
很多问题。
1)不要忘记在每个case块之后将break添加到开关中
2)对端口A的赋值覆盖此端口中的先前值。也就是说,在这个结构中:
PORTA=(0<<PORTA2); //Off RED LED
PORTA=(1<<PORTA3); //Glow Green LED
PORTA=(0<<PORTA4); //off Blue LED PORTA的所有输出上都会有一个零(最后一行)。
3)甚至,如果一些东西将在chkcolour()中发送到PORTA,它将在接下来的几微秒内被覆盖,因为在下一次迭代中它将被赋值:
PORTA=(0<<S3) | (0<<S2) ; //For Red要设置或清除位,请使用如下结构:
PORTA |= (1 << bitnum); // to set a bit
PORTA &= ~(1 << bitnum); // to clear (note bitwise negation ~ symbol)https://stackoverflow.com/questions/31674467
复制相似问题