Beispiel #1
0
/**
	*	@brief		MPU9250_WriteReg
	*	@note		MPU9250 write one byte
	*	@param		WriteAddr= reg to write
					WriteData= data to write
	*	@retval		None
	*/
static void MPU9250_WriteReg( uint8_t WriteAddr, uint8_t WriteData )
{
	SPIx_CSM_PIN_L();
	SPI_WriteByte(SPIx, WriteAddr);
	SPI_WriteByte(SPIx, WriteData);
	SPIx_CSM_PIN_H();
}
/*=====================================================================================================*/
static void NRF_WriteReg( u8 WriteAddr, u8 WriteData )
{
  NRF_CSN = 0;
  SPI_WriteByte(NRF_SPI, WriteAddr);
  SPI_WriteByte(NRF_SPI, WriteData);
  NRF_CSN = 1;
}
Beispiel #3
0
uint8_t W5100_MemReadWord(uint16_t addr, uint16_t *val) {
  uint8_t low, high;
  
  SPI_RequestSPIBus();

  W5100_CS_ENABLE();
  SPI_WriteByte(W5100_CMD_READ);
  SPI_WriteByte(addr>>8); /* high address */
  SPI_WriteByte(addr&0xff); /* low address */
  high = SPI_ReadByte(); /* data */
  W5100_CS_DISABLE();
  
  addr++;
  W5100_CS_ENABLE();
  SPI_WriteByte(W5100_CMD_READ);
  SPI_WriteByte(addr>>8); /* high address */
  SPI_WriteByte(addr&0xff); /* low address */
  low = SPI_ReadByte(); /* data */
  W5100_CS_DISABLE();
  
  SPI_ReleaseSPIBus();
  
  *val = (high<<8)|low;
  return ERR_OK;
}
void MPU_WriteReg( u8 WriteAddr, u8 WriteData )
{
    MPU_CS = 0;
    SPI_WriteByte(MPU_SPI, WriteAddr);
    SPI_WriteByte(MPU_SPI, WriteData);
    MPU_CS = 1;
}
Beispiel #5
0
static void MPU9250_WriteReg( u8 WriteAddr, u8 WriteData )
{
  IMU_CSM = 0;
  SPI_WriteByte(SPIx, WriteAddr);
  SPI_WriteByte(SPIx, WriteData);
  IMU_CSM = 1;
}
/*=====================================================================================================*/
void nRF_WriteReg( u8 WriteAddr, u8 WriteData )
{
  NRF_CE = 0;
  NRF_CSN = 0;
  SPI_WriteByte(nRF_SPI, WriteAddr);
  SPI_WriteByte(nRF_SPI, WriteData);
  NRF_CSN = 1;
}
status_t LSM303C::begin(InterfaceMode_t im, MAG_DO_t modr, MAG_FS_t mfs,
    MAG_BDU_t mbu, MAG_OMXY_t mxyodr, MAG_OMZ_t mzodr, MAG_MD_t mm,
    ACC_FS_t afs, ACC_BDU_t abu, uint8_t aea, ACC_ODR_t aodr)
{
  uint8_t successes = 0;
  // Select I2C or SPI
  interfaceMode = im;

  if (interfaceMode == MODE_SPI)
  {
    debug_println("Setting up SPI");
    // Setup pins for SPI
    // CS & CLK must be outputs DDRxn = 1
    bitSet(DIR_REG, CSBIT_MAG);
    bitSet(DIR_REG, CSBIT_XL);
    bitSet(DIR_REG, CLKBIT);
    // Deselect SPI chips
    bitSet(CSPORT_MAG, CSBIT_MAG);
    bitSet(CSPORT_XL, CSBIT_XL);
    // Clock polarity (CPOL) = 1
    bitSet(CLKPORT, CLKBIT);
    // SPI Serial Interface Mode (SIM) bits must be set
    SPI_WriteByte(ACC, ACC_CTRL4, 0b111);
    SPI_WriteByte(MAG, MAG_CTRL_REG3, _BV(2));
  }
  else
  {
    Wire.begin();
    Wire.setClock(400000L);
  }
  ////////// Initialize Magnetometer //////////
  // Initialize magnetometer output data rate
  successes += MAG_SetODR(modr);
  // Initialize magnetic field full scale
  successes += MAG_SetFullScale(mfs);
  // Enabling block data updating
  successes += MAG_BlockDataUpdate(mbu);
  // Initialize magnetometer X/Y axes ouput data rate
  successes += MAG_XY_AxOperativeMode(mxyodr);
  // Initialize magnetometer Z axis performance mode
  successes += MAG_Z_AxOperativeMode(mzodr);
  // Initialize magnetometer run mode.
  successes += MAG_SetMode(mm);

  ////////// Initialize Accelerometer //////////
  // Initialize acceleration full scale
  successes += ACC_SetFullScale(afs);
  // Enable block data updating
  successes += ACC_BlockDataUpdate(abu);
  // Enable X, Y, and Z accelerometer axes
  successes += ACC_EnableAxis(aea);
  // Initialize accelerometer output data rate
  successes += ACC_SetODR(aodr);

  return (successes == IMU_SUCCESS) ? IMU_SUCCESS : IMU_HW_ERROR;
}
/*=====================================================================================================*/
static void NRF_WriteBuf( u8 WriteAddr, u8 *WriteBuf, u8 Bytes )
{
  u8 i;

  NRF_CSN = 0;
  SPI_WriteByte(NRF_SPI, WriteAddr);
  for(i=0; i<Bytes; i++)
    SPI_WriteByte(NRF_SPI, WriteBuf[i]);
  NRF_CSN = 1;
}
Beispiel #9
0
uint8_t W5100_MemReadByte(uint16_t addr, uint8_t *val) {
  SPI_RequestSPIBus();
  W5100_CS_ENABLE();
  SPI_WriteByte(W5100_CMD_READ);
  SPI_WriteByte(addr>>8); /* high address */
  SPI_WriteByte(addr&0xff); /* low address */
  *val = SPI_ReadByte(); /* data */
  W5100_CS_DISABLE();
  SPI_ReleaseSPIBus();
  return ERR_OK;
}
void MPU_WriteBuf( u8 WriteAddr, u8 *WriteBuf, u8 Bytes )
{
    u8 i;

    MPU_CS = 0;

    SPI_WriteByte(MPU_SPI, WriteAddr);

    for(i=0; i<Bytes; i++)
        SPI_WriteByte(MPU_SPI, WriteBuf[i]);

    MPU_CS = 1;
}
Beispiel #11
0
//读取触摸屏Y返回值
u16 TouchReadY(void)
{
	u16 y=0;
	TP_CS();
	SpiDelay(10);
	SPI_WriteByte(0xD0);
	SpiDelay(10);
	y=SPI_WriteByte(0x00);
	y<<=8;
	y+=SPI_WriteByte(0x00);
	SpiDelay(10);
	TP_DCS();
	y = y>>3;
	return (y);
}
Beispiel #12
0
//读取触摸屏X返回值
u16 TouchReadX(void)
{
   u16 x=0;
   TP_CS();
   SpiDelay(10);
   SPI_WriteByte(0x90);
   SpiDelay(10);
   x=SPI_WriteByte(0x00);
   x<<=8;
   x+=SPI_WriteByte(0x00);
   SpiDelay(10);
   TP_DCS();
   x = x>>3;
   return (x);
}
Beispiel #13
0
//从芯片中addr地址读一个BYTE的数据
unsigned char GT_Readbyte(unsigned long addr)  
	{
	 unsigned char DATA=0;
	 union {unsigned char DB8[4]; unsigned long DB32;} ADD;
	 ADD.DB32=addr;
	 GT20_CS0;
	 SPI_WriteByte(0X03);
	 SPI_WriteByte(ADD.DB8[2]);
	 SPI_WriteByte(ADD.DB8[1]);
	 SPI_WriteByte(ADD.DB8[0]);
	 DATA=SPI_WriteByte(0XFF);
	 GT20_CS1;
	
	 return DATA;
	}
Beispiel #14
0
/**
	*	@brief		MPU9250_ReadReg
	*	@note		MPU9250 read one byte
	*	@param		ReadAddr=reg address
					ReadData=save the data of reading
	*	@retval		None
	*/
static void MPU9250_ReadReg( uint8_t ReadAddr, uint8_t *ReadData )
{
	SPIx_CSM_PIN_L();
	SPI_WriteByte(SPIx, 0x80 | ReadAddr);
	*ReadData = SPI_ReadByte(SPIx);
	SPIx_CSM_PIN_H();
}
Beispiel #15
0
void SCA_Init( void )
{
	  SPI_Reset();
    SPI_Start();
    SPI_WriteByte( MEAS );
    SPI_Stop();
}
Beispiel #16
0
/*************************************************
函数名:Lcd_Clear
功能:全屏清屏函数
入口参数:填充颜色COLOR
返回值:无
*************************************************/
void LCD_Fill(u16 sx,u16 sy,u16 ex,u16 ey,u16 color)            
{	
   unsigned int i,m;
   Lcd_SetRegion(sx,sy,ex,ey);
   //LCD_CS_CLR;
   //LCD_RS_SET;
   LCD_RS_SET;
   //SPIv_WriteByte(Data);

   
   for(i=0;i<=ey-sy;i++)
   {
    for(m=0;m<=ex-sx;m++)
      {	 
	  	//SPIv_WriteByte(Color>>8);  
		//SPIv_WriteByte(Color);
		SPI_WriteByte(SPI2,color>>8);
		SPI_WriteByte(SPI2,color);
	  	//Lcd_WriteData16Bit(Color>>8,Color);
		//Lcd_WriteData(Color>>8);
		//Lcd_WriteData(Color);
      }   
	}
	 // LCD_CS_SET;  
}
Beispiel #17
0
/*
**函數 : MPU9250_ReadReg
**功能 : 讀暫存器
**輸入 : ReadAddr
**輸出 : ReadData
**使用 : MPU9250_ReadReg(ReadAddr, &DeviceID);
*/
static void MPU9250_ReadReg( u8 ReadAddr, u8 *ReadData )
{
  IMU_CSM = 0;
  SPI_WriteByte(SPIx, 0x80 | ReadAddr);
  *ReadData = SPI_ReadByte(SPIx);
  IMU_CSM = 1;
}
Beispiel #18
0
void Lcd_Clear(u16 Color)               
{	
   unsigned int i,m;
   Lcd_SetRegion(0,0,X_MAX_PIXEL-1,Y_MAX_PIXEL-1);
   //LCD_CS_CLR;
   //LCD_RS_SET;
   LCD_RS_SET;
   //SPIv_WriteByte(Data);

   
   for(i=0;i<Y_MAX_PIXEL;i++)
   {
    for(m=0;m<X_MAX_PIXEL;m++)
      {	 
	  	//SPIv_WriteByte(Color>>8);  
		//SPIv_WriteByte(Color);
		SPI_WriteByte(SPI2,Color>>8);
		SPI_WriteByte(SPI2,Color);
	  	//Lcd_WriteData16Bit(Color>>8,Color);
		//Lcd_WriteData(Color>>8);
		//Lcd_WriteData(Color);
      }   
	}
	 // LCD_CS_SET;  
}
Beispiel #19
0
//从芯片addr地址连续读取n个BYTE的数据到指定的数组
void GT_Read_nByte(unsigned char *BUFF,unsigned long addr,unsigned int n)
{
  unsigned int i;
  union {unsigned char DB8[4]; unsigned long DB32;} ADD;

	 ADD.DB32=addr;
	 GT20_CS0;
	 SPI_WriteByte(0X03);
	 SPI_WriteByte(ADD.DB8[2]);
	 SPI_WriteByte(ADD.DB8[1]);
	 SPI_WriteByte(ADD.DB8[0]);
    for(i=0;i<n;i++)
	 {
	  BUFF[i] = SPI_WriteByte(0XFF);
	 }
    GT20_CS1;
}
Beispiel #20
0
/****************************************************************************
* 名    称:void ili9220B_WriteData(u16 dat)
* 功    能:写 ili9220B 寄存器数据
* 入口参数:dat     寄存器数据
* 出口参数:无
* 说    明:向控制器指定地址写入数据,调用前需先写寄存器地址,内部函数
****************************************************************************/
void Lcd_WriteData(u8 Data)
{
   //LCD_CS_CLR;
   LCD_RS_SET;
   //SPIv_WriteByte(Data);
   SPI_WriteByte(SPI2,Data);

   //LCD_CS_SET; 
}
Beispiel #21
0
/**
 * @brief Funcion para obtener el registro STATUS.
 * @return  Valor del registro STATUS.
 */
uint8_t NRF_GetStatus(void){
    SPI_ClearRxBuffer();
    Control_Write(0);
    SPI_WriteByte(NRF_NOP);
    while(!(SPI_ReadTxStatus() & SPI_STS_SPI_IDLE));
    Control_Write(1);
    
	return (uint8_t)SPI_ReadRxData();  
}
Beispiel #22
0
static void MPU9250_ReadRegs( u8 ReadAddr, u8 *ReadBuf, u8 Bytes )
{
  u8 i = 0;

  IMU_CSM = 0;
  SPI_WriteByte(SPIx, 0x80 | ReadAddr);
  for(i=0; i<Bytes; i++)
    ReadBuf[i] = SPI_ReadByte(SPIx);
  IMU_CSM = 1;
}
Beispiel #23
0
uint16 SPI_GetY( void )
{
    uint16 result;
    SPI_Start();
    SPI_WriteByte( RDAY );
    result = SPI_ReadBits(11);
    SPI_Stop();
    return result;

}
Beispiel #24
0
uint16 SPI_GetReg(uint8 Cmd,uint8 Length)
{
    uint16 result;
    SPI_Start();
    SPI_WriteByte( Cmd );
    result = SPI_ReadBits(Length);
    SPI_Stop();
    return result;

}
Beispiel #25
0
/****************************************************************************
* 名    称:void ili9220B_WriteIndex(u16 idx)
* 功    能:写 ili9220B 控制器寄存器地址
* 入口参数:idx   寄存器地址
* 出口参数:无
* 说    明:调用前需先选中控制器,内部函数
****************************************************************************/
void Lcd_WriteIndex(u8 Index)
{
   //SPI 写命令时序开始
   //LCD_CS_CLR;
   LCD_RS_CLR;
   //SPIv_WriteByte(Index);
   SPI_WriteByte(SPI2,Index);
   
   //LCD_CS_SET;
}
/*=====================================================================================================*/
static void NRF_ReadBuf( u8 ReadAddr, u8 *ReadBuf, u8 Bytes )
{
  u8 i = 0;

  NRF_CSN = 0;
  SPI_WriteByte(NRF_SPI, ReadAddr);
  for(i=0; i<Bytes; i++)
    ReadBuf[i] = SPI_ReadByte(NRF_SPI);
  NRF_CSN = 1;
}
Beispiel #27
0
/****************************************************************************
* 名    称:Lcd_WriteIndex(u8 Index)
* 功    能:向液晶屏写一个8位指令
* 入口参数:Index   寄存器地址
* 出口参数:无
* 说    明:调用前需先选中控制器,内部函数
****************************************************************************/
void Lcd_WriteIndex(u8 Index)
{
   LCD_CS_CLR;
   LCD_RS_CLR;
#if USE_HARDWARE_SPI   
   SPI_WriteByte(SPI2,Index);
#else
   SPIv_WriteData(Index);
#endif 
   LCD_CS_SET;
}
Beispiel #28
0
/****************************************************************************
* 名    称:Lcd_WriteData(u8 Data)
* 功    能:向液晶屏写一个8位数据
* 入口参数:dat     寄存器数据
* 出口参数:无
* 说    明:向控制器指定地址写入数据,内部函数
****************************************************************************/
void Lcd_WriteData(u8 Data)
{
   LCD_CS_CLR;
   LCD_RS_SET;
#if USE_HARDWARE_SPI   
   SPI_WriteByte(SPI2,Data);
#else
   SPIv_WriteData(Data);
#endif 
   LCD_CS_SET;
}
Beispiel #29
0
//******************************************************************
//函数名:  LCD_WR_REG
//作者:    xiao冯@全动电子
//日期:    2013-02-22
//功能:    向液晶屏总线写入写16位指令
//输入参数:Reg:待写入的指令值
//返回值:  无
//修改记录:无
//******************************************************************
void LCD_WR_REG(u16 data)
{ 
   LCD_CS_CLR;
   LCD_RS_CLR;
#if USE_HARDWARE_SPI   
   SPI_WriteByte(SPI1,data);
#else
   SPIv_WriteData(data);
#endif 
   LCD_CS_SET;
}
/*=====================================================================================================*/
static u8 NRF_ReadReg( u8 ReadAddr )
{
  u8 ReadData;

  NRF_CSN = 0;
  SPI_WriteByte(NRF_SPI, ReadAddr);
  ReadData = SPI_ReadByte(NRF_SPI);
  NRF_CSN = 1;

  return ReadData;
}