GD32F207 相关驱动

[复制链接]
1378|25
手机看帖
扫描二维码
随时随地手机跟帖
chenqiang10|  楼主 | 2018-10-30 20:36 | 显示全部楼层 |阅读模式


初始化用户Led设备

void EvbLedConfig(void)
{
    GPIO_InitPara GPIO_InitStructure;
    RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_GPIOD,ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_PIN_11 | GPIO_PIN_12|GPIO_PIN_13;
    GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
    GPIO_Init(GPIOD,&GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_PIN_11 | GPIO_PIN_12 |GPIO_PIN_13;
    GPIO_Init(GPIOD,&GPIO_InitStructure);
}

chenqiang10|  楼主 | 2018-10-30 20:37 | 显示全部楼层
控制Led的点亮和熄灭
void EvbLedControl(int index, int cmd)
{
    switch (index)
    {
        case LED1:
        {
            if (cmd == LED_ON)
            {
                GPIO_SetBits(GPIOD,GPIO_PIN_11);  /*点亮Led1灯*/
            }
            else
            {
                GPIO_ResetBits(GPIOD,GPIO_PIN_11);  /*熄灭Led1灯*/
            }
            break;
        }
        case LED2:
        {
            if (cmd == LED_ON)
            {
                GPIO_SetBits(GPIOD,GPIO_PIN_12);  /*点亮Led2灯*/
            }
            else
            {
                GPIO_ResetBits(GPIOD,GPIO_PIN_12);  /*熄灭Led2灯*/
            }
            break;
        }
        case LED3:
        {
            if (cmd == LED_ON)
            {
                GPIO_SetBits(GPIOD,GPIO_PIN_13);  /*点亮Led3灯*/
            }
            else
            {
                GPIO_ResetBits(GPIOD,GPIO_PIN_13);  /*熄灭Led3灯*/
            }
            break;
        }
        default:
        {
            if (cmd == LED_ON)
            {
                GPIO_SetBits(GPIOD,GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13); /*点亮所有的灯*/
            }
            else
            {
                GPIO_ResetBits(GPIOD,GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13); /*熄灭所有的灯*/
            }
            break;
        }
    }
}

使用特权

评论回复
chenqiang10|  楼主 | 2018-10-30 20:38 | 显示全部楼层
事件触发LED翻转
void EvbLedToggle(int index)
{
    BitState value;
    switch (index)
    {
        case LED1:
        {
            value =  (BitState)(1-GPIO_ReadOutputBit(GPIOD, GPIO_PIN_11));
            GPIO_WriteBit(GPIOD, GPIO_PIN_11, value);
            break;
        }
        case LED2:
        {
            value =  (BitState)(1-GPIO_ReadOutputBit(GPIOD, GPIO_PIN_12));
            GPIO_WriteBit(GPIOD, GPIO_PIN_12, value);
            break;
        }
        case LED3:
        {
            value =  (BitState)(1-GPIO_ReadOutputBit(GPIOD, GPIO_PIN_13));
            GPIO_WriteBit(GPIOD, GPIO_PIN_13, value);
            break;
        }
        default:
        {
            value =  (BitState)(1-GPIO_ReadOutputBit(GPIOD, GPIO_PIN_11));
            GPIO_WriteBit(GPIOD, GPIO_PIN_11, value);
            value =  (BitState)(1-GPIO_ReadOutputBit(GPIOD, GPIO_PIN_12));
            GPIO_WriteBit(GPIOD, GPIO_PIN_12, value);
            value =  (BitState)(1-GPIO_ReadOutputBit(GPIOD, GPIO_PIN_13));
            GPIO_WriteBit(GPIOD, GPIO_PIN_13, value);
            break;
        }
    }
}

使用特权

评论回复
chenqiang10|  楼主 | 2018-10-30 20:39 | 显示全部楼层
蜂鸣器初始化
void EVB_BuzzerConfig(void)
{
    GPIO_InitPara GPIO_InitStructure;
   
    /* 使能GPIOB时钟 */
    RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_GPIOD,ENABLE);

    /* PD1用于控制蜂鸣器 */
    GPIO_InitStructure.GPIO_Pin = GPIO_PIN_1;
    GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
    GPIO_Init(GPIOD,&GPIO_InitStructure);
}

使用特权

评论回复
chenqiang10|  楼主 | 2018-10-30 20:40 | 显示全部楼层
蜂鸣器的开关操作
void EVB_BuzzerControl(int value)
{
    if (value)
    {
        GPIO_SetBits(GPIOD, GPIO_PIN_1);
    }
    else
    {
        GPIO_ResetBits(GPIOD, GPIO_PIN_1);
    }
}

使用特权

评论回复
chenqiang10|  楼主 | 2018-10-30 20:41 | 显示全部楼层
// 中配配置
static void NVIC_Configuration(void)
{
    NVIC_InitPara NVIC_InitStructure;

    /* Enable the USARTx Interrupt */
    NVIC_InitStructure.NVIC_IRQ = USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQPreemptPriority = 0;
    NVIC_InitStructure.NVIC_IRQSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQEnable = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}

使用特权

评论回复
chenqiang10|  楼主 | 2018-10-30 20:41 | 显示全部楼层
串口初始化

oid EvbUart1Config(void)
{
    /* Configure the GPIO ports */
    GPIO_InitPara  GPIO_InitStructure;
    USART_InitPara  USART_InitStructure;
    //NVIC_InitPara NVIC_InitStructure;

    /* Enable GPIOA clock */
    RCC_APB2PeriphClock_Enable( RCC_APB2PERIPH_GPIOB, ENABLE );
    /* Enable USART1 APB clock */
    RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_USART1, ENABLE );
    RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_AF, ENABLE);
    GPIO_InitStructure.GPIO_Pin     = GPIO_PIN_6 ;
    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_7;
    GPIO_InitStructure.GPIO_Mode    = GPIO_MODE_IN_FLOATING;;
    GPIO_Init( GPIOB , &GPIO_InitStructure);

    GPIO_PinRemapConfig(GPIO_REMAP_USART1, ENABLE);
   
    //USART_DeInit(USART1 );
    USART_InitStructure.USART_BRR                 = 115200;
    USART_InitStructure.USART_WL                  = USART_WL_8B;
    USART_InitStructure.USART_STBits              = USART_STBITS_1;
    USART_InitStructure.USART_Parity              = USART_PARITY_RESET;
    USART_InitStructure.USART_HardwareFlowControl = USART_HARDWAREFLOWCONTROL_NONE;
    USART_InitStructure.USART_RxorTx              = USART_RXORTX_RX | USART_RXORTX_TX;
    USART_Init(USART1, &USART_InitStructure);

    /* USART enable */
    USART_Enable(USART1, ENABLE);            
#if 0
    /* Enable the USARTx Interrupt */
    NVIC_InitStructure.NVIC_IRQ = USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQPreemptPriority = 0;
    NVIC_InitStructure.NVIC_IRQSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQEnable = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    /* Enable the USART1 Receive interrupt */
    USART_INT_Set(USART1, USART_INT_RBNE, ENABLE );
        USART_INT_Set(USART1, USART_INT_IDLEF, ENABLE );
#endif
}

使用特权

评论回复
chenqiang10|  楼主 | 2018-10-30 20:42 | 显示全部楼层
向串口1发送一个字符
void EvbUart1WriteByte(char c)
{
    USART_DataSend(USART1, c);
    while (USART_GetBitState(USART1, USART_FLAG_TBE) == RESET)
        ;
}

使用特权

评论回复
chenqiang10|  楼主 | 2018-10-30 20:43 | 显示全部楼层
void EvbUart1WriteByte2(char c)
{
    USART_DataSend(USART1, c);
}

使用特权

评论回复
chenqiang10|  楼主 | 2018-10-30 20:43 | 显示全部楼层
向串口1发送一个字符串

void EvbUart1WriteStr(const char* str)
{

    while (*str)
    {
        USART_DataSend(USART1, * str++);
        while (USART_GetBitState(USART1, USART_FLAG_TBE) == RESET)
            ;
    }
}

使用特权

评论回复
chenqiang10|  楼主 | 2018-10-30 20:44 | 显示全部楼层
从串口1接收一个字符

void EvbUart1ReadByte(char* c)
{
    while (USART_GetBitState(USART1, USART_FLAG_RBNE) == RESET)
        ;
    *c = (USART_DataReceive(USART1));
}

使用特权

评论回复
chenqiang10|  楼主 | 2018-10-30 20:44 | 显示全部楼层
static char _buffer[256];
void EvbUart1Printf(char* fmt, ...)
{
    int i;
    va_list ap;
    va_start(ap, fmt);
    vsprintf(_buffer, fmt, ap);
    va_end(ap);

    for (i = 0; _buffer[i] != '\0'; i++)
    {
        EvbUart1WriteByte(_buffer[i]);
    }
}

使用特权

评论回复
chenqiang10|  楼主 | 2018-10-30 20:44 | 显示全部楼层
void EvbUart1EnableRxInt(void)
{
    /* Enable the USART1 Receive interrupt */
    USART_INT_Set( USART1, USART_INT_RBNE, ENABLE );

}

void EvbUart1EnableTxInt(void)
{
    /* Enable the USART1 Transmoit interrupt */
    USART_INT_Set( USART1, USART_INT_TBE, ENABLE );
}

使用特权

评论回复
chenqiang10|  楼主 | 2018-10-30 20:47 | 显示全部楼层
ADC
void GPIO_Configuration(void)
{
    GPIO_InitPara GPIO_InitStructure;

    GPIO_InitStructure.GPIO_Pin = GPIO_PIN_9;
    GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
    GPIO_Init( GPIOA , &GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin  = GPIO_PIN_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_MODE_IN_FLOATING;
    GPIO_Init( GPIOA , &GPIO_InitStructure);
}

使用特权

评论回复
chenqiang10|  楼主 | 2018-10-30 20:48 | 显示全部楼层
Configure ADC Configuration.
void ADC_Configuration(void)
{
    /* Config ADC1 --------------------------------------------------------------*/
    ADC_InitStructure.ADC_Mode_Scan = DISABLE;
    ADC_InitStructure.ADC_Mode_Continuous = DISABLE;
    ADC_InitStructure.ADC_Trig_External = ADC_EXTERNAL_TRIGGER_MODE_NONE;
    ADC_InitStructure.ADC_Data_Align = ADC_DATAALIGN_RIGHT;
    ADC_InitStructure.ADC_Mode = ADC_MODE_INDEPENDENT;
    ADC_InitStructure.ADC_Channel_Number = 1;
    ADC_Init(ADC1,&ADC_InitStructure);

    /* ADC1 regular channels configuration */
    ADC_RegularChannel_Config(ADC1,ADC_CHANNEL_16, 1, ADC_SAMPLETIME_239POINT5);

    ADC_ExternalTrigConv_Enable(ADC1,ENABLE);
    ADC_TempSensorVrefint_Enable(ENABLE);

    /* Enable ADC1 */
    ADC_Enable(ADC1,ENABLE);
    ADC_Calibration(ADC1);

    /* Start ADC1 Software Conversion */
    ADC_SoftwareStartConv_Enable(ADC1,ENABLE);
}

使用特权

评论回复
chenqiang10|  楼主 | 2018-10-30 20:48 | 显示全部楼层
Configure system clocks.
void RCC_Configuration(void)
{
    /* ADCCLK = PCLK2/6 */
    RCC_ADCCLKConfig(RCC_ADCCLK_APB2_DIV6);

    /* Enable DMA1 and GPIOC clock */
    RCC_APB2PeriphClock_Enable( RCC_APB2PERIPH_GPIOA|RCC_APB2PERIPH_AF , ENABLE);
    /* Enable USART1 APB clock */
    RCC_APB2PeriphClock_Enable( RCC_APB2PERIPH_USART1 , ENABLE );
    /* Enable ADC1 clock */
    RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_ADC1, ENABLE);
}

使用特权

评论回复
chenqiang10|  楼主 | 2018-10-30 20:49 | 显示全部楼层
void CAN_Config(void)
{  
    /* CAN register init */
    CAN_DeInit(CANX);
    CAN_StructInit(&CAN_InitStructure);

#if (1 == USE_CAN_NUM)
    RCC_APB1PeriphClock_Enable(RCC_APB1PERIPH_CAN1, ENABLE);
#else /*__CAN2_USED__*/
    RCC_APB1PeriphClock_Enable(RCC_APB1PERIPH_CAN1, ENABLE);
    RCC_APB1PeriphClock_Enable(RCC_APB1PERIPH_CAN2, ENABLE);
#endif /* __CAN1_USED__ */
   
    /* CAN cell init */
    CAN_InitStructure.CAN_TTC = DISABLE;
    CAN_InitStructure.CAN_ABOR = DISABLE;
    CAN_InitStructure.CAN_AWK = DISABLE;
    CAN_InitStructure.CAN_ARD = DISABLE;
    CAN_InitStructure.CAN_RFOD = DISABLE;
    CAN_InitStructure.CAN_TFO = DISABLE;
    CAN_InitStructure.CAN_Mode = CAN_MODE_NORMAL;
  
    /* CAN Baudrate = 1MBps*/
    CAN_InitStructure.CAN_SJW = CAN_SJW_1TQ;
    CAN_InitStructure.CAN_BS1 = CAN_BS1_2TQ;
    CAN_InitStructure.CAN_BS2 = CAN_BS2_1TQ;
    CAN_InitStructure.CAN_Prescaler = 15;
    CAN_Init(CANX, &CAN_InitStructure);

    /* CAN filter init */
#if (1 == USE_CAN_NUM)
    CAN_FilterInitStructure.CAN_FilterNumber = 0;
#else /* USE_CAN2 */
    CAN_FilterInitStructure.CAN_FilterNumber = 14;
#endif /* USE_CAN_NUM */
    CAN_FilterInitStructure.CAN_FilterMode = CAN_FILTERMODE_MASK;
    CAN_FilterInitStructure.CAN_FilterScale = CAN_FILTERSCALE_32BIT;
    CAN_FilterInitStructure.CAN_FilterListHigh = 0x0000;
    CAN_FilterInitStructure.CAN_FilterListLow = 0x0000;
    CAN_FilterInitStructure.CAN_FilterMaskListHigh = 0x0000;
    CAN_FilterInitStructure.CAN_FilterMaskListLow = 0x0000;
    CAN_FilterInitStructure.CAN_FilterFIFOAssociation = CAN_FIFO0;
    CAN_FilterInitStructure.CAN_FilterWork = ENABLE;
    CAN_FilterInit(&CAN_FilterInitStructure);  
}

使用特权

评论回复
chenqiang10|  楼主 | 2018-10-30 20:50 | 显示全部楼层
void NVIC_Config(void)
{
    NVIC_InitPara NVIC_InitStructure;

    NVIC_PRIGroup_Enable(NVIC_PRIGROUP_0);
  
#if (1 == USE_CAN_NUM)  
    NVIC_InitStructure.NVIC_IRQ = CAN1_RX0_IRQn;
#else  /*__CAN2_USED__*/
    NVIC_InitStructure.NVIC_IRQ = CAN2_RX0_IRQn;
#endif /*__CAN1_USED__*/

    NVIC_InitStructure.NVIC_IRQPreemptPriority = 0x0;
    NVIC_InitStructure.NVIC_IRQSubPriority = 0x0;
    NVIC_InitStructure.NVIC_IRQEnable = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}

使用特权

评论回复
chenqiang10|  楼主 | 2018-10-30 20:50 | 显示全部楼层
void Init_RxMes(CanRxMessage *RxMessage)
{
    uint8_t i = 0;

    RxMessage->StdId = 0x00;
    RxMessage->ExtId = 0x00;
    RxMessage->FF = CAN_FF_STANDARD;
    RxMessage->DLC = 0;
    RxMessage->FI = 0;
    for (i = 0;i < 8;i++)
    {
        RxMessage->Data[i] = 0x00;
    }
}

使用特权

评论回复
chenqiang10|  楼主 | 2018-10-30 20:50 | 显示全部楼层
void GPIO_Config(void)
{
    GPIO_InitPara  GPIO_InitStructure;

    /* GPIO clock enable */
    RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_AF |
                               RCC_APB2PERIPH_GPIOA|
                               RCC_APB2PERIPH_GPIOB|
                               RCC_APB2PERIPH_GPIOC, ENABLE);
    GPIO_PinRemapConfig(GPIO_REMAPPING_CAN , ENABLE);
    /* Configure USART1 TX pin */
    GPIO_InitStructure.GPIO_Pin = GPIO_PIN_9;
    GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
    GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    /* Configure USART1 RX pin */
    GPIO_InitStructure.GPIO_Pin = GPIO_PIN_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_MODE_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
   
    /* Configure User Key Button pin */
    GPIO_InitStructure.GPIO_Pin = KEY_GPIO_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_MODE_IN_FLOATING;
    GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
    GPIO_Init(KEY_GPIO_PORT, &GPIO_InitStructure);

    /* Configure CAN pin: RX */
    GPIO_InitStructure.GPIO_Pin = GPIO_PIN_CAN_RX;
    GPIO_InitStructure.GPIO_Mode = GPIO_MODE_IPU;
    GPIO_Init(GPIO_CAN, &GPIO_InitStructure);
  
    /* Configure CAN pin: TX */
    GPIO_InitStructure.GPIO_Pin = GPIO_PIN_CAN_TX;
    GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
    GPIO_Init(GPIO_CAN, &GPIO_InitStructure);
}

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

39

主题

940

帖子

1

粉丝