⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 final.cpp

📁 读取avago的optical sensor的数据
💻 CPP
字号:
#include "WProgram.h"char spi_transfer(volatile char data);char read_register(char register_name);void write_register(char register_name, byte data);void setup_spi_com();void setup_spi_acc();#include "math.h"#define SSNT  10  // chip selsect(slave select), active low#define MOSI  11  // SDA(master out, slave in)#define MISO  12  // SDO(master in, slave out)#define SCLK  13  // SPI clock/* ------------------------------------------------------------------------------- * Function name: char spi_transfer(volatile char) * Function description:  * Note: The ATMega168 has a register called SPDR that, when written to, begins   * an SPI transaction. Once you start a transaction, you have a choice of two  * ways to handle it. One is to simply wait around until the transaction is over,  * indicated by the SPIF bit being set. The other is to set up an interrupt  * vector for this bit, which will result in a designated function being called  * when the interrupt occurs.Performing the actual transaction over the SPI bus  * is handled largely in hardware. * ------------------------------------------------------------------------------- */char spi_transfer(volatile char data) {   SPDR = data;   // Loop right here until the transaction is complete. the SPIF bit is the SPI   // Interrupt Flag. When interrupts are enabled, and the SPIE bit is set   // enabling SPI interrupts, this bit will set when the transaction is finished.   while (!(SPSR & (1<<SPIF))){}                                                                   // received data appears in the SPDR register   return SPDR; }/* ------------------------------------------------------------------------------- * Function name: char read_register(char)  * Function description: reads a register * ------------------------------------------------------------------------------- */char read_register(char register_name) {   char in_byte;     // need to reset bit 7 to indicate a read   register_name &= 127;     // SS is active low   digitalWrite(SSNT, LOW);     // send the address of the register we want to read first   spi_transfer(register_name);     // send nothing, but here's when the device sends back the register's value as an 8 bit byte   in_byte = spi_transfer(0);     // deselect the device   digitalWrite(SSNT, HIGH);     return in_byte; } /* ------------------------------------------------------------------------------- * Function name: void write_register(char, byte) * Function description: writes a register * ------------------------------------------------------------------------------- */void write_register(char register_name, byte data) {     // set bit 7 to indicate we're doing a write   register_name |= 128;     // SS is active low   digitalWrite(SSNT, LOW);     // send the address of the register we want to write   spi_transfer(register_name);     // send the data we're writing   spi_transfer(data);     digitalWrite(SSNT, HIGH); }  /* -------------------------------------------------------------------------------- * Fun name: void setup_spi_com () * Fun desc: configures common SPI pins * -------------------------------------------------------------------------------- */ int MOTION = 9;   void setup_spi_com(){  pinMode(MOSI,OUTPUT);  pinMode(MISO,INPUT);  pinMode(SCLK,OUTPUT);  pinMode(MOTION,INPUT);}/* -------------------------------------------------------------------------------- * Fun name: void setup_spi_acc () * Fun desc: start up the device, this essentially activates the device, powers  *           it on, enables all axes, and turn off the self test * -------------------------------------------------------------------------------- */ void setup_spi_acc(){  pinMode(SSNT,OUTPUT);   digitalWrite(SSNT,HIGH); // disable device   pinMode(SSNT, OUTPUT);    // Set the SPCR register to 01010000; interrupt disabled, spi enabled, msb 1st,   // master, clk low when idle, sample on leading edge of clk, system clock/4 rate   SPCR = (1<<SPE)|(1<<MSTR)|(0<<CPOL)|(0<<CPHA);   // Write 0x5a to register 0x3a   write_register(0x3a, 0x5A);   delay(250);}/* -------------------------------------------------------------------------------- * Fun name: self_test * Fun desc: * -------------------------------------------------------------------------------- */  void self_test() {   byte crc0, crc1, crc2, crc3;      write_register(0x10, 0x01);      crc0 = read_register(0x0c);   crc1 = read_register(0x0d);   crc2 = read_register(0x0e);   crc3 = read_register(0x0f);      Serial.print(crc0, DEC);   Serial.print(44, BYTE);   Serial.print(crc1, DEC);   Serial.print(44, BYTE);   Serial.print(crc2, DEC);   Serial.print(44, BYTE);   Serial.print(crc3, DEC);   Serial.print('\n', BYTE);      delay(300); } /* -------------------------------------------------------------------------------- * Fun name:  * Fun desc: * -------------------------------------------------------------------------------- */     int x_val, y_val, motion, x_sum=0, y_sum=0, product_id, inverse_product_id, motionpin; void setup() {   Serial.begin(9600);     setup_spi_com();  setup_spi_acc(); } void loop() {   product_id = read_register(0x00);  inverse_product_id = read_register(0x3f);  inverse_product_id = ~inverse_product_id;  if(product_id==inverse_product_id)  {  motionpin = digitalRead(MOTION);        if(motionpin==0){  motion = read_register(0x02);  y_val = read_register(0x03);   x_val = read_register(0x04);  x_sum = x_sum + x_val;  y_sum = y_sum + y_val;  Serial.print(x_sum, DEC);  Serial.print(44, BYTE);  Serial.print(y_sum, DEC);  Serial.print(44, BYTE);  Serial.print('\n', BYTE);  }  }  write_register(0x00, 0x0D);  write_register(0x3f, 0xF2);} int main(void){	init();	setup();    	for (;;)		loop();        	return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -