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

📄 an1069.c

📁 实现dsPIC33FJ256MC710与AVAGO的OPTICAL SENSOR之间的SPI通讯
💻 C
字号:
/*;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;	Filename:			    AN1069.c            
;	Date:				    April 2008          
;	File Version:		    1.0                             
;	Assembled using:	    MPLAB IDE 7.50.00.0               
;   Author:		  	        Li Kang              
;	Company:			    RRC@NTU
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

#include <p33fxxxx.h>
#include "AN1069.h"
#include <stdio.h>

#define CS _RG9
#define SCK _RG6
#define MISO _RG7
#define MOSI _RG8
#define MOTION _RA0

//config word

//********************************************************************
// Global Variable declarations
//********************************************************************

int main(void)
{
  unsigned char LowAdd;                     //Low Order Address Byte
  unsigned char Data;                       //Data Byte
 
  unsigned int n;

  unsigned char x_val, y_val, x_sum, y_sum, motion, motionpin, product_id, inverse_product_id;

  InitPIC();
  CS = 1;                                 // Deselect device at beginning of code

  while(1){

    LDByteWriteSPI(0x00, 0x0D);
    LDByteWriteSPI(0x3f, 0xF2);

    product_id = LDByteReadSPI(0x00);
    inverse_product_id = LDByteReadSPI(0x3f);
    inverse_product_id = ~inverse_product_id;

    if(product_id == inverse_product_id){
      motionpin = PORTAbits.RA0;

      if(motionpin == 0){
        motion = LDByteReadSPI(0x02);
        y_val = LDByteReadSPI(0x03);
        x_val = LDByteReadSPI(0x04);
        x_sum = x_sum + x_val;
        y_sum = y_sum + y_val;

        printf("%c;%c;\n", x_sum, y_sum);
        }
     }
  };                             // Loop here forever

}


void InitPIC(void)
//here is the SPI setup for 10 MHz operation, Mode 0,0.
{
  TRISF = 0x0080;             
  PLLFBD = 38;                
  CLKDIV = 0x0040;            
  SPI1CON1 = 0x013B;          
  SPI1CON2 = 0x0000;
  SPI1STAT = 0x8000;        
  LDByteWriteSPI(0x3a, 0x5A);
}

⌨️ 快捷键说明

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