Write CODE 1 0x00 2 0x27 3 0x81 4 0x1F 5 0x77
#include "a1333.h" const uint16_t WRITE = 0x40; const uint16_t READ = 0x00; const uint16_t COMMAND_MASK = 0xC0; const uint16_t ADDRESS_MASK = 0x3F; #define CS_H() GPIO_SetBits(GPIOB,GPIO_Pin_12) #define CS_L() GPIO_ResetBits(GPIOB,GPIO_Pin_12) static uint16_t spi2_rw(uint16_t cmd,uint16_t *value) { u8 retry=0; while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET) { retry++; if(retry>200)return 0; } SPI_I2S_SendData(SPI2, cmd); retry=0; while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET) { retry++; if(retry>200)return 0; } if(value){ *value = SPI_I2S_ReceiveData(SPI2); }else{ SPI_I2S_ReceiveData(SPI2); } return 1; } static uint16_t PrimaryWrite(uint16_t address, uint16_t value) { u8 s=0; uint16_t command = ((address & ADDRESS_MASK) | WRITE) << 8; CS_L(); Delay_1us(1); s = spi2_rw(command | ((value >> 8) & 0x0FF),0); CS_H(); if(s == 0){ return 0; } command = (((address + 1) & ADDRESS_MASK) | WRITE) << 8; CS_L(); Delay_1us(1); s = spi2_rw(command | (value & 0x0FF),0); CS_H(); if(s == 0){ return 0; } return 1; } static uint16_t PrimaryRead(uint16_t address, uint16_t *value) { u8 s=1; uint16_t command = ((address & ADDRESS_MASK) | READ) << 8; CS_L(); Delay_1us(1); s = spi2_rw(command,0); CS_H(); if(s) { CS_L(); s = spi2_rw(command,value); CS_H(); if(s) { return 1; } return 0; }else{ return 0; } } void a1333_init(void) { GPIO_InitTypeDef GPIO_InitStructure; SPI_InitTypeDef SPI_InitStructure; RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE ); RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE ); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_SetBits(GPIOB,GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15); SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_Init(SPI2, &SPI_InitStructure); SPI_Cmd(SPI2, ENABLE); SPI_I2S_ClearFlag(SPI2, SPI_I2S_FLAG_RXNE); { u16 t = 0,flags; PrimaryRead(0x00,&t); // Unlock the device PrimaryWrite(0x3C, 0x2700); PrimaryWrite(0x3C, 0x8100); PrimaryWrite(0x3C, 0x1F00); PrimaryWrite(0x3C, 0x7700); // Make sure the device is unlocked t = 100; PrimaryRead(0x3C, &flags); while ((flags & 0x0001) != 0x0001) { Delay_1us(10); t--; if (t==0) { LedSet(LED1); LedReset(LED2); while(1){ LedReverse(LED1 | LED2); Delay_1ms(200); } } PrimaryRead(0x3C, &flags); } } } uint16_t a1333_read_angle15(void){ uint16_t read_angle = 0; PrimaryRead(0x32, &read_angle); read_angle = read_angle & 0x7fff; return read_angle; } uint16_t a1333_read_angle15_smooth(uint8_t sm){ uint8_t i; uint32_t total = 0; for(i=0;i<sm;i++){ total += a1333_read_angle15(); } return total / sm; }
#ifndef __A1333_H__ #define __A1333_H__ #include "stm32f10x.h" void a1333_init(void); uint16_t a1333_read_angle15(void); uint16_t a1333_read_angle15_smooth(uint8_t sm); #endif