stm32模拟iic从机程序

个人记录,保存在此处,逻辑能用,但是通讯速率只能小于5K,但是可以优化(改为用寄存器操作)

 

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
{
  /* Configure the SysTick to have interrupt in 100us time basis*/
  if (HAL_SYSTICK_Config(SystemCoreClock / (10000U / uwTickFreq)) > 0U)
  {
    return HAL_ERROR;
  }

  /* Configure the SysTick IRQ priority */
  if (TickPriority < (1UL << __NVIC_PRIO_BITS))
  {
    HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority, 0U);
    uwTickPrio = TickPriority;
  }
  else
  {
    return HAL_ERROR;
  }

  /* Return function status */
  return HAL_OK;
}

void HAL_Delay(uint32_t Delay)
{
  uint32_t tickstart = HAL_GetTick();
  uint32_t wait = Delay;

  if (wait == 0)
  {
    wait = 1;
  }

  while((HAL_GetTick() - tickstart) < wait)
  {
  }
}

void HAL_Delay_100us(uint32_t Delay)
{
     HAL_Delay(Delay);
}

void HAL_Delay_100us_d(int j){
    int i=27;
    int k=27;
    int m=27;
    int n=27;

    while(i--);
    while(k--);
    while(m--);
    while(n--);
}


/*********************** iic begin *****************************/


/*********************** iic end *******************************/

uint16_t get_adc(uint32_t channel)
{
    ADC_ChannelConfTypeDef sConfig = {0};
    
    sConfig.Channel = ADC_CHANNEL_MASK;  //disabled all channel
    sConfig.Rank = ADC_RANK_NONE;
    HAL_ADC_ConfigChannel(&hadc, &sConfig);

    sConfig.Channel = channel;
    sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
    HAL_ADC_ConfigChannel(&hadc, &sConfig);

    HAL_ADC_Start(&hadc);

    if(HAL_ADC_PollForConversion(&hadc, 10) == HAL_OK)
    {
        return HAL_ADC_GetValue(&hadc);
    }

    return 0;
}


 uint16_t get_adc_av(uint32_t channel, uint8_t times)
{
    uint32_t ad_sum=0;
    uint8_t i;

    for(i=0;i<times;i++)
    {
        ad_sum += get_adc(channel);
    }

    return ad_sum / times;
}


void get_map_x_y(float* x,float* y)
{
    uint16_t x_adc;
    uint16_t y_adc;
    
    GPIO_InitTypeDef GPIO_InitStruct = {0};

    
#if 0
    HAL_NVIC_DisableIRQ(EXTI4_15_IRQn);
    GPIO_InitStruct.Pin = TWI_SCK_Pin|TWI_SDA_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

while(1){

    HAL_GPIO_WritePin(GPIOA, TWI_SCK_Pin, GPIO_PIN_SET);

    HAL_Delay_100us(3000);

    HAL_GPIO_WritePin(GPIOA, TWI_SDA_Pin, GPIO_PIN_SET);

    HAL_Delay_100us(3000);

    HAL_GPIO_WritePin(GPIOA, TWI_SCK_Pin, GPIO_PIN_RESET);

    HAL_Delay_100us(3000);

    HAL_GPIO_WritePin(GPIOA, TWI_SDA_Pin, GPIO_PIN_RESET);
    
    HAL_Delay_100us(3000);
}

#endif

    //X+ PP mode and set high, X- PP mode and set low     
    GPIO_InitStruct.Pin = XL_Pin|XR_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    HAL_GPIO_WritePin(GPIOA, XL_Pin, GPIO_PIN_SET);   //set X+ high
    HAL_GPIO_WritePin(GPIOA, XR_Pin, GPIO_PIN_RESET); //set X- low

    //Y+ adc mode,Y-float mode
    GPIO_InitStruct.Pin = YD_Pin;  //Y+
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  
    GPIO_InitStruct.Pin = YU_Pin;  //Y-
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    HAL_Delay_100us(5);

    y_adc = get_adc_av(ADC_CHANNEL_4, 10);

    *y = 3.3 * y_adc / 4096;

    GPIO_InitStruct.Pin = XL_Pin|XR_Pin|YD_Pin|YU_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    /* 将四个脚都拉低,目的是把四个脚连接的电容的电放掉 */
    HAL_GPIO_WritePin(GPIOA, XL_Pin, GPIO_PIN_RESET); //set X+ low
    HAL_GPIO_WritePin(GPIOA, XR_Pin, GPIO_PIN_RESET); //set X- low
    HAL_GPIO_WritePin(GPIOA, YD_Pin, GPIO_PIN_RESET); //set Y+ low
    HAL_GPIO_WritePin(GPIOA, YU_Pin, GPIO_PIN_RESET); //set Y- low

    //adc 采样后进行转换计算, 计算方法 点击四个角(现实坐标 )然后几下对应的adc输出值,算出直线方程adc_out=kx+b,就可以化出x=(adc_out-b)/k,y同理
    //*x=(x_orignal_data[1]-505)/240.4;

    //Y+ PP mode and set high, Y- PP mode and set low     
    GPIO_InitStruct.Pin = YD_Pin|YU_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    HAL_GPIO_WritePin(GPIOA, YD_Pin, GPIO_PIN_SET);   //set Y+ high
    HAL_GPIO_WritePin(GPIOA, YU_Pin, GPIO_PIN_RESET); //set Y- low

    //X+ adc mode,X-float mode
    GPIO_InitStruct.Pin = XL_Pin;  //X+
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  
    GPIO_InitStruct.Pin = XR_Pin;  //X-
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    HAL_Delay_100us(5);

    x_adc = get_adc_av(ADC_CHANNEL_0, 10);

    *x = 3.3 * x_adc / 4096;

    GPIO_InitStruct.Pin = XL_Pin|XR_Pin|YD_Pin|YU_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    /* 将四个脚都拉低,目的是把四个脚连接的电容的电放掉 */
    HAL_GPIO_WritePin(GPIOA, XL_Pin, GPIO_PIN_RESET); //set X+ low
    HAL_GPIO_WritePin(GPIOA, XR_Pin, GPIO_PIN_RESET); //set X- low
    HAL_GPIO_WritePin(GPIOA, YD_Pin, GPIO_PIN_RESET); //set Y+ low
    HAL_GPIO_WritePin(GPIOA, YU_Pin, GPIO_PIN_RESET); //set Y- low
}

#define RTP_IIC_ADDR 0x88

#define SCK_READ() HAL_GPIO_ReadPin(GPIOA, TWI_SCK_Pin)
#define SDA_READ() HAL_GPIO_ReadPin(GPIOA, TWI_SDA_Pin)

typedef enum{
    STEP_IDLE = 0,
    STEP_START,
    STEP_DETECT_ADDR,

    STEP_READ_REG_NUM,
    
    STEP_SEND_DATA,
    STEP_READ_ACK,
    
    STEP_STOP,
    STEP_RESTART,
    STEP_READ_DATA,
    STEP_SEND_ACK,
    STEP_SEND_ACK_FINISHED,
} iic_step_e;

typedef enum{
    SDA_INT,
    SDA_OUT,
    SDA_IN,
} sda_mode;

typedef struct{
    uint8_t bit_cnt;
    uint8_t send_temp;
    uint8_t recv_temp;
    uint8_t reg_index;
    uint8_t iic_addr;

    uint8_t send_buffer[4];
    uint8_t recv_buffer[4];
    uint8_t buf0_chged;
    uint8_t buf1_chged;
    uint8_t buf2_chged;
    uint8_t buf3_chged;
    
    iic_step_e iic_send_or_read;
    iic_step_e iic_next_step;
    iic_step_e iic_step;
} iic_s;


iic_s iic = {0, 0, 0, STEP_IDLE};

void set_sda_mode(sda_mode mode)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};

    if(mode == SDA_INT){
        GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;
    }else if(mode == SDA_OUT){
        GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    }else if(mode == SDA_IN){
        GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    }
    
    GPIO_InitStruct.Pin = TWI_SDA_Pin;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}

void iic_send_ack(void)
{
    set_sda_mode(SDA_OUT);
    HAL_GPIO_WritePin(GPIOA, TWI_SDA_Pin, GPIO_PIN_RESET);
}

void iic_wait_ack(void)
{
    set_sda_mode(SDA_INT);
    HAL_GPIO_WritePin(GPIOA, TWI_SDA_Pin, GPIO_PIN_SET); //release sda
}

void iic_set_sda_high(void)
{
    HAL_GPIO_WritePin(GPIOA, TWI_SDA_Pin, GPIO_PIN_SET);
}

void iic_set_sda_low(void)
{
    HAL_GPIO_WritePin(GPIOA, TWI_SDA_Pin, GPIO_PIN_RESET);
}

void iic_set_sck_high(void)
{
    HAL_GPIO_WritePin(GPIOA, TWI_SCK_Pin, GPIO_PIN_SET);
}

void iic_set_sck_low(void)
{
    HAL_GPIO_WritePin(GPIOA, TWI_SCK_Pin, GPIO_PIN_RESET);
}

void iic_set_pb1_high(void)
{
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_SET);
}

void iic_set_pb1_low(void)
{
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_RESET);
}



void iic_init()
{
    iic.iic_step = STEP_IDLE;
    iic.iic_send_or_read = STEP_IDLE;
    iic.send_temp = 0;
    iic.recv_temp = 0;
    iic.bit_cnt = 0;
    iic.iic_addr = 0;
    iic.reg_index = 0;
    
    set_sda_mode(SDA_INT);
}

void iic_sda_process(void)
{
    if(SCK_READ() == 0)
    {
        return;
    }

    if(SDA_READ() == 0)  //falling edge
    {
        iic_init();
        iic.iic_step = STEP_START;
    }
    else  //rising edge
    {
        iic.iic_step = STEP_STOP;
        set_sda_mode(SDA_INT);
    }
}

void iic_scl_falling_process(void)
{
    switch(iic.iic_step)
    {
        case STEP_START:
            iic.iic_step = STEP_DETECT_ADDR;
            break;
            
        case STEP_SEND_ACK:
            set_sda_mode(SDA_OUT);
            HAL_GPIO_WritePin(GPIOA, TWI_SDA_Pin, GPIO_PIN_RESET);
            iic.iic_step = STEP_SEND_ACK;                             //next execute rising STEP_SEND_ACK
    
            break;

        case STEP_SEND_DATA:
            if(iic.bit_cnt >= 8){
                iic.bit_cnt = 0;
                iic.iic_step = STEP_READ_ACK;
                set_sda_mode(SDA_INT);
                break;
            }
            
            set_sda_mode(SDA_OUT);
            
            iic.send_temp = iic.send_buffer[iic.reg_index];

            if(iic.send_temp&(0x80>>iic.bit_cnt)){
                iic_set_sda_high();
            }
            else{
                iic_set_sda_low();
            }

            iic.bit_cnt++;
            
            break;

        case STEP_READ_REG_NUM:
            set_sda_mode(SDA_INT);
            break;

        case STEP_IDLE:
            iic_init();
            break;
            
    }
}

void iic_scl_rising_process(void)
{
    switch(iic.iic_step){
        
        case STEP_DETECT_ADDR:
            
            iic_set_pb1_low();
    
            iic.iic_addr <<= 1;
            iic.iic_addr += SDA_READ();
        
            if(++iic.bit_cnt >= 8)
            {
                iic.bit_cnt = 0;
                
                if((iic.iic_addr & 0xfe) == (RTP_IIC_ADDR & 0xfe)){
                    iic.iic_step = STEP_SEND_ACK;
                    iic.iic_next_step = STEP_READ_REG_NUM;
                
                    if(iic.iic_addr & 0x01){                                        
                        iic.iic_send_or_read = STEP_SEND_DATA;             //master read,slave write
                    }
                    else{                                                  
                        iic.iic_send_or_read = STEP_READ_DATA;             //master write,slave read
                    }
                }
                else{
                    iic_init();
                }
            }
            
            iic_set_pb1_high();
            
            break;

        case STEP_READ_REG_NUM:
            iic.reg_index <<= 1;
            iic.reg_index += SDA_READ();
            if(++iic.bit_cnt >= 8)
            {
                iic.bit_cnt = 0;
                iic.iic_step = STEP_SEND_ACK;
                iic.iic_next_step = iic.iic_send_or_read;
            }
            break;

        case STEP_READ_DATA:
            set_sda_mode(SDA_INT);
            iic.recv_temp <<= 1;
            iic.recv_temp += SDA_READ();
            if(++iic.bit_cnt >= 8)
            {
                iic.recv_buffer[iic.reg_index] = iic.recv_temp;
                
                switch(iic.reg_index){
                    case 0:iic.buf0_chged=1;break;
                    case 1:iic.buf1_chged=1;break;
                    case 2:iic.buf2_chged=1;break;
                    case 3:iic.buf3_chged=1;break;
                }
                
                iic.bit_cnt = 0;
                iic.iic_step = STEP_SEND_ACK;
                iic.iic_next_step = STEP_IDLE;
            }
            break;

        case STEP_SEND_ACK:
            iic.iic_step = iic.iic_next_step;
            set_sda_mode(SDA_INT);
            break;

        case STEP_READ_ACK:
            if(!SDA_READ()){
                iic_init();
            }else{
                iic_init();
            }
            break;

        case STEP_IDLE:
            iic_init();
            break;
    }
}

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
    if(GPIO_Pin == TWI_SCK_Pin)
    {
        if(SCK_READ()){
            iic_scl_rising_process();
        }else{
            iic_scl_falling_process();
        }
    }
    else if(GPIO_Pin == TWI_SDA_Pin)
    {
        iic_sda_process();
    }
}


/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
float x_v;
float y_v;


#if 1

void IIC_Start(void)
{
    set_sda_mode(SDA_OUT);
    iic_set_sda_high();            
    iic_set_sck_high();
    HAL_Delay_100us(1);
     iic_set_sda_low();
    HAL_Delay_100us(1);
    iic_set_sck_low();
}      

void IIC_Stop(void)
{
    set_sda_mode(SDA_OUT);
    iic_set_sck_low();
    iic_set_sda_low();
     HAL_Delay_100us(1);
    iic_set_sck_high(); 
    iic_set_sda_high();
    HAL_Delay_100us(1);
    set_sda_mode(SDA_IN);  //jia
}

uint8_t IIC_Wait_Ack(void)
{
    uint8_t ucErrTime=0;
        
    iic_set_sck_high();
    HAL_Delay_100us(1);
    
    while(SDA_READ())
    {
        ucErrTime++;
        if(ucErrTime>250)
        {
            IIC_Stop();
            return 1;
        }
    }
    iic_set_sck_low();       
    return 0;  
} 
//产生ACK应答
void IIC_Ack(void)
{
    set_sda_mode(SDA_OUT);
    iic_set_sck_low();
    iic_set_sda_low();
    HAL_Delay_100us(1);
    iic_set_sck_high(); 
    HAL_Delay_100us(1);
    iic_set_sck_low();
    set_sda_mode(SDA_IN);  //jia
}
//不产生ACK应答            
void IIC_NAck(void)
{
    set_sda_mode(SDA_OUT);
    iic_set_sck_low();
    iic_set_sda_high();
    HAL_Delay_100us(1);
    iic_set_sck_high(); 
    HAL_Delay_100us(1);
    iic_set_sck_low();
    set_sda_mode(SDA_IN);  //jia
}                                          

void IIC_Send_Byte(uint8_t txd)
{                        
    uint8_t t;   
    set_sda_mode(SDA_OUT);         
    
    for(t=0;t<8;t++)
    {    
        iic_set_sck_low();
        if((txd&0x80)>>7)
            iic_set_sda_high();
        else
            iic_set_sda_low();
        txd<<=1; 

        HAL_Delay_100us(1);
        iic_set_sck_high();
        HAL_Delay_100us(1);
    }
    
    iic_set_sck_low();
    set_sda_mode(SDA_IN);
    HAL_Delay_100us(1);
}         

//读1个字节,ack=1时,发送ACK,ack=0,发送nACK   
uint8_t IIC_Read_Byte(unsigned char ack)
{
    unsigned char i,receive=0;
    set_sda_mode(SDA_IN);     
    for(i=0;i<8;i++ )
    {
        iic_set_sck_low();     
        HAL_Delay_100us(1);
        iic_set_sck_high();
        receive<<=1;
        if(SDA_READ())receive++;   
        HAL_Delay_100us(1);
    }                     
    if (!ack)
        IIC_NAck();//发送nACK
    else
        IIC_Ack(); //发送ACK   
    return receive;
}

void iic_init_h()
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};
#if 1
    GPIO_InitStruct.Pin = TWI_SCK_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
#endif

#if 1
    GPIO_InitStruct.Pin = TWI_SDA_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
#endif

    /* EXTI interrupt init*/
    HAL_NVIC_DisableIRQ(EXTI4_15_IRQn);
}

#endif

void delay_2p5us(){
    int i=7;

    while(i--);
}

void delay_5us(){
    int i=27;

    while(i--);
}

int main(void)
{
    uint8_t rec_buf[5] = {0};
    
    HAL_Init();
    SystemClock_Config();
    MX_GPIO_Init();
    MX_ADC_Init();

    iic.send_buffer[0] = 0xaa;
    iic.send_buffer[1] = 0xfe;
    iic.send_buffer[2] = 0x0e;
    iic.send_buffer[3] = 0x77;

#if 0
    iic_init_h();

    set_sda_mode(SDA_OUT);

    while(1){
        iic_set_sda_low();     
        HAL_Delay_100us(1);
        iic_set_sda_high();
        HAL_Delay_100us(1);
    }
#endif

#if 0

    iic_init_h();

    while(1){
        iic_set_sck_low();
        delay_5us();
        iic_set_sck_high();
        delay_5us();
    }

#if 0

    while(1){
        IIC_Start();
        IIC_Send_Byte(0x89);
        IIC_Wait_Ack();
        IIC_Send_Byte(1);
        IIC_Wait_Ack();
        rec_buf[0] = IIC_Read_Byte(1);
        IIC_Stop();
#if 0
        IIC_Start();
        IIC_Send_Byte(0x89);
        IIC_Wait_Ack();
        IIC_Send_Byte(1);
        IIC_Wait_Ack();
        rec_buf[1] = IIC_Read_Byte(1);
        IIC_Stop();

        IIC_Start();
        IIC_Send_Byte(0x89);
        IIC_Wait_Ack();
        IIC_Send_Byte(2);
        IIC_Wait_Ack();
        rec_buf[2] = IIC_Read_Byte(1);
        IIC_Stop();

        IIC_Start();
        IIC_Send_Byte(0x89);
        IIC_Wait_Ack();
        IIC_Send_Byte(3);
        IIC_Wait_Ack();
        rec_buf[3] = IIC_Read_Byte(1);
        IIC_Stop();
#endif
        
        //HAL_Delay_100us_d(2);
/**********************************/
#if 0
        IIC_Start();
        IIC_Send_Byte(0x88);
        IIC_Wait_Ack();
        IIC_Send_Byte(1);
        IIC_Wait_Ack();
        IIC_Send_Byte(0xaa);
        IIC_Wait_Ack();
        IIC_Stop();

        IIC_Start();
        IIC_Send_Byte(0x88);
        IIC_Wait_Ack();
        IIC_Send_Byte(1);
        IIC_Wait_Ack();
        IIC_Send_Byte(0xf0);
        IIC_Wait_Ack();
        IIC_Stop();

        IIC_Start();
        IIC_Send_Byte(0x88);
        IIC_Wait_Ack();
        IIC_Send_Byte(2);
        IIC_Wait_Ack();
        IIC_Send_Byte(0xee);
        IIC_Wait_Ack();
        IIC_Stop();

        IIC_Start();
        IIC_Send_Byte(0x88);
        IIC_Wait_Ack();
        IIC_Send_Byte(3);
        IIC_Wait_Ack();
        IIC_Send_Byte(0xbb);
        IIC_Wait_Ack();
        IIC_Stop();
#endif
        
        rec_buf[4] = 0;

        HAL_Delay_100us_d(500);
    }
#endif
#endif

#if 1
    while (1)
    {
        get_map_x_y(&x_v, &y_v);
    }
#endif
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_4;
  RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

 

posted @ 2021-06-25 11:56  olivertian  阅读(1024)  评论(0编辑  收藏  举报