MA730 MT6835 MT6825 编码器角度读取和寄存器操作

MA730 MT6835 MT6825 编码器角度读取和寄存器操作

MA730

外设配置:

image.png

代码


static inline uint16_t MA730_read_raw(void){
	
	uint16_t txData = 0;
	uint16_t rxData;

	NCS_RESET();
	HAL_SPI_TransmitReceive(&hspi2, (uint8_t*)&txData, (uint8_t*)&rxData, 1, 100);
	NCS_SET();

	return (rxData>>2);	
	
}


uint16_t write_reg(uint16_t addr, uint16_t val){
	
	uint16_t tx_value = 0, rx_value = 0;
	
	tx_value = val;
	tx_value |= addr << 8;
	tx_value |= 1 << 15;
	
	NCS_RESET();
	HAL_SPI_TransmitReceive(&hspi2, (uint8_t*)&tx_value, (uint8_t*)&rx_value, 1, 10);
	while( hspi1.State == HAL_SPI_STATE_BUSY );	// wait for transmission complete
	NCS_SET();
	HAL_Delay(20);
	
	tx_value = 0;
	rx_value = 0;
	
	NCS_RESET();
	HAL_SPI_TransmitReceive(&hspi2, (uint8_t*)&tx_value, (uint8_t*)&rx_value, 1, 10);
	while( hspi1.State == HAL_SPI_STATE_BUSY );	// wait for transmission complete
	NCS_SET();
	
	return (rx_value>>8);
}

uint16_t read_reg(uint16_t addr){
	
	uint16_t tx_value = 0, rx_value = 0;
	tx_value |= addr << 8;
	tx_value |= 1 << 14;
	
	NCS_RESET();
	HAL_SPI_TransmitReceive(&hspi2, (uint8_t*)&tx_value, (uint8_t*)&rx_value, 1, 100);
	while( hspi1.State == HAL_SPI_STATE_BUSY );	// wait for transmission complete
	NCS_SET();
	
	tx_value = 0;
	rx_value = 0;
	
	HAL_Delay(1);
	NCS_RESET();
	HAL_SPI_TransmitReceive(&hspi2, (uint8_t*)&tx_value, (uint8_t*)&rx_value, 1, 100);
	while( hspi1.State == HAL_SPI_STATE_BUSY );	// wait for transmission complete
	NCS_SET();

	return rx_value>>8;
}

MT6835

外设配置:

image.png

寄存器读取代码

#define  CMD_RD  	3  /**< user read register. */
#define  CMD_WR  	6  /**< user write register. */
#define  CMD_EEPROM 12	/**< user erase and program EEPROM. */
#define  CMD_ZERO  	5  /**< AUTO setting zero. */
#define  CMD_BURST 	10  /**< burst mode. */


void write_enc_reg(uint8_t addr, uint8_t val){
	
	uint8_t tx_buff[3] = {0};
	uint16_t tx = 0;
	
	tx = CMD_WR<<12 | addr;
	tx_buff[0] = tx>>8;
	tx_buff[1] = tx&0x00FF;
	tx_buff[2] = val;

	HAL_GPIO_WritePin(ENC_CS, GPIO_PIN_RESET); 	// CS low
	HAL_SPI_Transmit(&ENC_SPI, (uint8_t*)&tx_buff, 3, 100);
	while( ENC_SPI.State == HAL_SPI_STATE_BUSY );  					// wait for transmission complete
	HAL_GPIO_WritePin(ENC_CS, GPIO_PIN_SET ); 	// CS high	
}

int read_enc_reg(uint8_t addr){
	
	uint8_t tx_buff[2]={0};
	uint16_t tx = CMD_RD<<12 | addr;
	
	tx_buff[0] = tx>>8;
	tx_buff[1] = tx&0x00FF;
	
	uint8_t rx = 0;
	
	HAL_GPIO_WritePin(ENC_CS, GPIO_PIN_RESET); 	// CS low
	HAL_SPI_Transmit(&ENC_SPI,tx_buff,2,100);
	while( ENC_SPI.State == HAL_SPI_STATE_BUSY );  					// wait for transmission complete
	HAL_SPI_Receive(&ENC_SPI,&rx,1,10);
	while( ENC_SPI.State == HAL_SPI_STATE_BUSY ); 
	HAL_GPIO_WritePin(ENC_CS, GPIO_PIN_SET ); 	// CS high	
	
	return rx;
}
//这里说明一下,因为我要使用快速读取,寄存器设置完成后spi改成了16位模式。
//这里改成了16位 注意!!! hspi3.Init.DataSize = SPI_DATASIZE_16BIT;
//下面角度读取也是用的16位
void ps_sample(void){
	uint16_t TxData;
	uint16_t RxData[3];
	TxData = RED_ANGE|0x0003;
	HAL_GPIO_WritePin(ENC_CS, GPIO_PIN_RESET); 	// CS low
	HAL_SPI_TransmitReceive(&ENC_SPI, (uint8_t*)&TxData, (uint8_t *)&RxData, 3, 100);
	while( ENC_SPI.State == HAL_SPI_STATE_BUSY );  					// wait for transmission complete
	HAL_GPIO_WritePin(ENC_CS, GPIO_PIN_SET ); 	// CS high	
	int raw = ((RxData[1]<<5)|(RxData[2]>>11));
	}

MT6835更新 配置同上


enum {
  CMD_RD = (0b0011),  /**< user read register. */
  CMD_WR = (0b0110),  /**< user write register. */
  CMD_EEPROM=(0b1100),/**< user erase and program EEPROM. */
  CMD_ZERO = (0b0101),  /**< AUTO setting zero. */
  CMD_BURST= (0b1010),  /**< burst mode. */
};

enum {
  REG_ID=(0x001),
  REG_ANGLE3 = (0x003),
  REG_ANGLE2 = (0x004),
  REG_ANGLE1 = (0x005),
  REG_CRC = (0x006),
  REG_ABZ_RES2 = (0x007),
  REG_ABZ_RES1 = (0x008),
  REG_ZERO2 = (0x009),
  REG_ZERO1 = (0x00A),
  REG_UVW=(0x00B),
  REG_PWM=(0x00C),
  REG_HYST=(0x00D),
  REG_AUTOCAL=(0x00E),
};
uint32_t MT_rdAngle(){

	uint8_t Angle[3];
	Angle[0]=MT_rdReg(REG_ANGLE1);
	Angle[1]=MT_rdReg(REG_ANGLE2);
	Angle[2]=MT_rdReg(REG_ANGLE3);

	return Angle[2]<<12|Angle[1]<<4|Angle[0]>>4;
}

uint8_t MT_rdID(){

	return MT_rdReg(REG_ID);
}

void MT_wrID(uint8_t data){

	MT_wrReg(REG_ID,data);
}

void MT_wrABZRES(uint16_t res){

	uint8_t resHi,resLow;
	uint8_t temp;
	resHi=res>>6;


	temp=MT_rdReg(REG_ABZ_RES1)&0x3;

	resLow=(res& 0x003F)<<2|temp;

	MT_wrReg(REG_ABZ_RES2,resHi);
	MT_wrReg(REG_ABZ_RES1,resLow);

}

uint16_t MT_rdABZRES(){

	uint8_t resHi,resLow;

	resHi=MT_rdReg(REG_ABZ_RES2);
	resLow=MT_rdReg(REG_ABZ_RES1);

	return resHi<<6|resLow>>2;
}

uint8_t MT_rdUVW(){

	return MT_rdReg(REG_UVW)&0xF;
}

void MT_wrUVW(uint8_t UVW){

	uint8_t temp;

	temp=MT_rdReg(REG_UVW)&0xF0;
	MT_wrReg(REG_UVW,temp|(UVW & 0x0F));

}


void MT_wrReg(uint8_t reg, uint8_t data){

	uint8_t txbuf[3];
	uint16_t tx;
	HAL_StatusTypeDef status;

	tx=CMD_WR<<12|reg;

	txbuf[0]=tx>>8;
	txbuf[1]=tx & 0x00FF;
	txbuf[2]=data;

	HAL_GPIO_WritePin(GPIOB,GPIO_PIN_6,GPIO_PIN_RESET);

	status=HAL_SPI_Transmit(&hspi1,txbuf,3,10);

	HAL_GPIO_WritePin(GPIOB,GPIO_PIN_6,GPIO_PIN_SET);

}

uint8_t MT_wrEEPROM(){

	uint8_t txbuf[2],rxbuf[1];
	HAL_StatusTypeDef status;

	txbuf[0]=CMD_EEPROM<<4;
	txbuf[1]=0;

	HAL_GPIO_WritePin(GPIOB,GPIO_PIN_6,GPIO_PIN_RESET);
	status=HAL_SPI_Transmit(&hspi1,txbuf,2,10);
	HAL_Delay(1);
	status=HAL_SPI_Receive(&hspi1,rxbuf,1,10);

	return rxbuf[0];

}




uint8_t MT_rdReg(uint8_t reg){

	uint8_t txbuf[2];
	uint16_t tx;
	HAL_StatusTypeDef status;
	uint8_t rx;

	tx=CMD_RD<<12|reg;

	txbuf[0]=tx>>8;
	txbuf[1]=tx & 0x00FF;

	HAL_GPIO_WritePin(GPIOB,GPIO_PIN_6,GPIO_PIN_RESET);
	status=HAL_SPI_Transmit(&hspi1,txbuf,2,10);
	HAL_Delay(1);
	status=HAL_SPI_Receive(&hspi1,&rx,1,10);

	HAL_GPIO_WritePin(GPIOB,GPIO_PIN_6,GPIO_PIN_SET);

	return rx;


}

MT6825快速读取

配置

image.png

代码

void ps_sample(){
	uint16_t read_data[3];
	encoder->spi_tx_word = 0x8300;
	HAL_GPIO_WritePin(ENC_CS, GPIO_PIN_RESET); 	// CS low
	HAL_SPI_TransmitReceive(&ENC_SPI, (uint8_t*)encoder->spi_tx_buff, (uint8_t *)read_data, 3, 100);
	while( ENC_SPI.State == HAL_SPI_STATE_BUSY );  					// wait for transmission complete
	HAL_GPIO_WritePin(ENC_CS, GPIO_PIN_SET ); 	// CS high
	int raw = ENC_CPR - (((read_data[0]&0xFF)<<10) +((read_data[1]&0xFC00)>>6)+((read_data[2]&0x00F0)>>4));
	}

最后吐槽一下 麦歌恩的手册真给人感觉是文科生写的!!!

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值