- #include
- #include
- #include
- #include
- #include
- #include
- #define BAUD 9600
- #define F_CPU 11059200
- #define uchar unsigned char
- #define uint unsigned int
- #define ON 1
- #define OFF 0
- #define PE 2
- #define DOR 3
- #define FE 4
- void PortInit(void);
- void UsatInit(void);
- //宏定义
- #define FRAMING_ERROR (1<
- #define PARITY_ERROR (1<
- #define DATA_OVERRUN (1<
- #define DATA_REGISTER_EMPTY (1<
- #define RX_COMPLETE (1<
- //#define Reset_key (PINH&(1 << 6)) //复位开关
-
- #define Raster PINE.2 //光栅
- #define start_key PINE.3
- #define down_cy_L PORTJ &= ~(1 << 0) //下压汽缸
- #define down_cy_H PORTJ |= (1 << 0)
- #define up_sen (PINH&(1 << 5)) //下压汽缸上面 sensor
- #define down_sen (PINH&(1 << 4)) //下压汽缸下面 sensor
- #define in_top_cy_L PORTJ &= ~(1 << 1) //推进电磁阀 推进
- #define in_top_cy_H PORTJ |= (1 << 1)
- #define in_bott_cy_L PORTJ &= ~(1 << 2) //推进电磁阀 推出
- #define in_bott_cy_H PORTJ |= (1 << 2)
- #define top_sen (PINH&(1 << 3)) //推进汽缸前面 sensor
- #define bott_sen (PINH&(1 << 2)) //推进汽缸后面 sensor
- #define CommSize 200 //指令长度
- /*------------------------TCS3414寄存器地址定义---------------------*/
- #define REG_Control 0X80
- #define REG_Timing 0X81
- #define REG_Interrupt 0x82
- #define REG_IntSource 0x83
- #define REG_ID 0x84
- #define REG_Gain 0x87
- #define REG_Low_Thresh_Low_Byte 0x88
- #define REG_Low_Thresh_High_Byte 0x89
- #define REG_High_Thresh_Low_Byte 0x8A
- #define REG_High_Thresh_High_Byte 0x8B
- #define REG_green_Low 0x90
- #define REG_green_High 0X91
- #define REG_red_Low 0X92
- #define REG_red_High 0X93
- #define REG_blue_Low 0X94
- #define REG_blue_High 0X95
- #define REG_clear_Low 0X96
- #define REG_clear_High 0X97
- #define TCS_address 0x72
- unsigned int TCS_green, TCS_red, TCS_blue, TCS_clear;
- float x1, y1, z1, x, y , z;
- #asm
- .equ __i2c_port=0x02
- .equ __sda_bit=1
- .equ __scl_bit=0
- #endasm
- //函数声明
- void PortInit(void);
- void UsatInit(void);
- unsigned char TCS_read_data(unsigned int TCS_command_add)
- {
- unsigned char TCS_data;
- i2c_start();
- i2c_write(TCS_address);
- i2c_write(TCS_command_add);
- i2c_start();
- i2c_write(TCS_address|1);
- TCS_data=i2c_read(0);
- i2c_stop();
- return TCS_data;
- }
- void TCS_write_data(unsigned int TCS_command_add,unsigned char TCS_data)
- {
- i2c_start();
- i2c_write(TCS_address);
- i2c_write(TCS_command_add);
- i2c_write(TCS_data);
- i2c_stop();
- delay_ms(50);
- }
- void RGB_read()
- {
- unsigned int data_Low,data_High;
- data_Low=TCS_read_data(REG_green_Low);
- data_High=TCS_read_data(REG_green_High);
- TCS_green=((256*data_High)+data_Low);
- data_Low=TCS_read_data(REG_red_Low);
- data_High=TCS_read_data(REG_red_High);
- TCS_red=((256*data_High)+data_Low);
- data_Low=TCS_read_data(REG_blue_Low);
- data_High=TCS_read_data(REG_blue_High);
- TCS_blue=((256*data_High)+data_Low);
- data_Low=TCS_read_data(REG_clear_Low);
- data_High=TCS_read_data(REG_clear_High);
- TCS_clear=((256*data_High)+data_Low);
- printf("green=%drn",TCS_green);
- printf("red=%drn",TCS_red);
- printf("blue=%drn",TCS_blue);
- printf("clear=%drn",TCS_clear);
- x1=(-0.14282)*TCS_red + (1.54924)*TCS_green + (-0.95641)*TCS_blue;
- y1=(-0.32466)*TCS_red + (1.57837)*TCS_green + (-0.73191)*TCS_blue;
- z1=(-0.68202)*TCS_red + (0.77073)*TCS_green + (0.56332)*TCS_blue;
- printf("x1=%frn",x1);
- printf("y1=%frn",y1);
- printf("z1=%frn",z1);
- x=x1/(x1+y1+z1);
- y=y1/(x1+y1+z1);
- printf("x=%frn",x);
- printf("y=%frn",y);
- }
- /*-------------------------主函数----------------------*/
- void main(void)
- {
- unsigned char i,TCS_ID=0;
- PortInit();
- UsatInit();
- i2c_init();
- TCS_ID=TCS_read_data(0x84);
- printf("TCS_ID=%drn",TCS_ID);
- TCS_write_data(0X80,0X03);
- TCS_write_data(0X87,0X00);
- delay_ms(1000);
- printf("Hello!rn@_@");
-
- #asm("sei")
- while(1)
- {
- RGB_read();
- delay_ms(500);
- }
- }
- void PortInit(void)
- {
- PORTA=0Xff; //初始化
- DDRA=0X00; //输出
- PORTB=0XFF;
- DDRB=0XFF;
- PORTC=0X00;
- DDRC=0XFF;
- PORTD=0X00;
- DDRD=0Xff;
- PORTE=0XFF; //设置RXD0和TXD0,初始值FF
- DDRE=0X02; //txd 1 (output),rxd 0 (input)
- PORTF=0XFF;
- DDRF=0X00;
- PORTH=0Xff;
- DDRH=0X00;
- PORTJ=0X00;
- DDRJ=0XFF;
- PORTK=0XFF;
- DDRK=0X00;
- PORTL=0X00;
- DDRL=0Xff;
- PORTG=0XFF;
- DDRG=0X00;
- }
- //
- /*----------------------串口初始化--------------------*/
- void UsatInit(void)
- {
- UCSR0B=0x98; //允许发送和接收,接收中断使能
- UCSR0C=0x86; //8位数据位+1位停止位
- UBRR0L=(F_CPU/BAUD/16-1)%256;
- UBRR0H=(F_CPU/BAUD/16-1)/256;
- }
复制代码
|
|
2016-12-22 22:46:18
评论
举报
|
|
|