三軸角度檢測(傾角傳感器MMA7455(加速度傳感器)) 作者: 時間:2016-11-29 來源:網(wǎng)絡 加入技術交流群 掃碼加入和技術大咖面對面交流海量資料庫查詢 收藏 void iic_write_byte(uchar wdata)//函數(shù)功能:向I2C從機寫入一個字節(jié){uchar i,temp,temp1;temp1=wdata;for(i=0;i<8;i++){scl = 0;iic_delay();temp=temp1;temp=temp&0x80;if(temp==0x80)sda=1; elsesda=0;iic_delay();scl=1;iic_delay();scl=0;iic_delay();temp1=temp1<<1;}}char iic_read_byte(void) //函數(shù)功能:從I2C從機中讀出一個字節(jié){uchar x;char data_data;for (x=0;x<8;x++){ data_data=data_data<<1;sda=1;iic_delay();scl=0;iic_delay();scl=1;iic_delay();if(sda==1)data_data|=0x01; //else // data_data&=0xfe;}return data_data;}void iic_write(uchar byte_add,uchar wdata)//函數(shù)功能:按地址寫入一字節(jié)數(shù)據(jù){uchar t;t=(IIC_WRITE<<1);iic_start();iic_write_byte(t);iic_ack();iic_write_byte(byte_add);iic_ack();iic_write_byte(wdata);iic_ack();iic_stop();}char iic_read(uchar byte_add) //函數(shù)功能:按地址讀出一字節(jié)數(shù)據(jù){uchar t;char x;t=(IIC_WRITE<<1);iic_start();iic_write_byte(t);iic_ack();iic_write_byte(byte_add);iic_ack();t=((IIC_READ<<1)|0x01);iic_start();iic_write_byte(t);iic_ack();x=iic_read_byte();iic_ack();iic_stop();return x;}void main(){delay_1ms(10); //延時lcd_init();delay_1ms(5);iic_write(0x16,0x5);iic_write(0x10,0x32);//校正X值iic_write(0x12,0x62);//校正Y值delay_50ms(20);while(1){x=iic_read(0x06);y=iic_read(0x07);z=iic_read(0x08);num[0]=x/100;num[1]=x/10%10;num[2]=x%10;num[3]=y/100;num[4]=y/10%10;num[5]=y%10;num[6]=z/100;num[7]=z/10%10;num[8]=z%10;lcd_pos(0,0);write_dat(num[0]+48);delay_1ms(5); write_dat(num[1]+48);delay_1ms(5);write_dat(num[2]+48);delay_1ms(5);lcd_pos(1,0);write_dat(num[3]+48);delay_1ms(5); write_dat(num[4]+48);delay_1ms(5);write_dat(num[5]+48);delay_1ms(5);lcd_pos(2,0);write_dat(num[6]+48);delay_1ms(5); write_dat(num[7]+48);delay_1ms(5);write_dat(num[8]+48);delay_1ms(5);}} 上一頁 1 2 下一頁
評論