赤菟开发板(CH32V307VCT6)实现流水灯

Delay_Ms延时函数,替换debug.c里面的延时函数

void Delay_Init(void)
{
    p_us=SystemCoreClock/8000000;
    p_ms=(uint16_t)p_us*1000;
}

/*******************************************************************************
* Function Name  : Delay_Us
* Description    : Microsecond Delay Time.
* Input          : n:Microsecond number.
* Return         : None
*******************************************************************************/
void Delay_Us(uint32_t n)
{
    uint32_t i;

    SysTick->CTLR = (1<<4);
    i = (uint32_t)n*p_us;

    SysTick->CMP = i;
    SysTick->CTLR |= (1<<5)|(1<<0);

    while((SysTick->SR & (1<<0)) != (1<<0));
    SysTick->SR &= ~(1<<0);
}

/*******************************************************************************
* Function Name  : Delay_Ms
* Description    : Millisecond Delay Time.
* Input          : n:Millisecond number.
* Return         : None
*******************************************************************************/
void Delay_Ms(uint32_t n)
{
    uint32_t i;

    SysTick->CTLR = (1<<4);
    i = (uint32_t)n*p_ms;

    SysTick->CMP = i;
    SysTick->CTLR |= (1<<5)|(1<<0);

    while((SysTick->SR & (1<<0)) != (1<<0));
    SysTick->SR &= ~(1<<0);
}

/******************************************************************************/

只需要替换下面的两个函数即可实现流水灯LED1和LED2

 main.c主函数

/* Includes ------------------------------------------------------- */
#include "debug.h"//包含ch32v307的头文件,c标准单元库和delay()函数

void GPIO11_INIT(void)//PE11管脚初始化
{
    GPIO_InitTypeDef  GPIO_InitStructure={0};
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);//使能为1
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;PE11端口
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_Init(GPIOE, &GPIO_InitStructure);//初始化IO口
    GPIO_SetBits(GPIOE, GPIO_Pin_11);//PE11输出高
    //AITA_LED1_CLR;
}

void GPIO12_INIT(void)//PE12管脚初始化
{
    GPIO_InitTypeDef  GPIO_InitStructure={0};
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_Init(GPIOE, &GPIO_InitStructure);
    GPIO_SetBits(GPIOE, GPIO_Pin_12);
}

void LED(void)
{
   Delay_Init();//延时初始化
  while(1){
      Delay_Ms(1000);
      GPIO_WriteBit(GPIOE, GPIO_Pin_11, 0);//配置 PE11 即 LED1 状态, 0点亮,1灭灯
      GPIO_WriteBit(GPIOE, GPIO_Pin_12, 1);//配置 PE12 即 LED2 状态
      Delay_Ms(2000);
      GPIO_WriteBit(GPIOE, GPIO_Pin_11, 1);
      GPIO_WriteBit(GPIOE, GPIO_Pin_12, 0);
      Delay_Ms(2000);
      GPIO_WriteBit(GPIOE, GPIO_Pin_11, 1);
      GPIO_WriteBit(GPIOE, GPIO_Pin_12, 1);
      Delay_Ms(1000);
      GPIO_WriteBit(GPIOE, GPIO_Pin_11, 0);
      GPIO_WriteBit(GPIOE, GPIO_Pin_12, 0);
      Delay_Ms(1000);
      GPIO_WriteBit(GPIOE, GPIO_Pin_11, 1);
      GPIO_WriteBit(GPIOE, GPIO_Pin_12, 1);
      Delay_Ms(1000);
  }
}


int main(void)
{
    GPIO11_INIT();
    GPIO12_INIT();
    LED();
    while(1);//死循环
}

debug.c函数

/********************************** (C) COPYRIGHT  *******************************
* File Name          : debug.c
* Author             : WCH
* Version            : V1.0.0
* Date               : 2021/06/06
* Description        : This file contains all the functions prototypes for UART
*                      Printf , Delay functions.
*******************************************************************************/
#include "debug.h"

static uint8_t  p_us=0;
static uint16_t p_ms=0;

/*******************************************************************************
* Function Name  : Delay_Init
* Description    : Initializes Delay Funcation.
* Input          : None
* Return         : None
*******************************************************************************/
void Delay_Init(void)
{
    p_us=SystemCoreClock/8000000;
    p_ms=(uint16_t)p_us*1000;
}

/*******************************************************************************
* Function Name  : Delay_Us
* Description    : Microsecond Delay Time.
* Input          : n:Microsecond number.
* Return         : None
*******************************************************************************/
void Delay_Us(uint32_t n)
{
    uint32_t i;

    SysTick->CTLR = (1<<4);
    i = (uint32_t)n*p_us;

    SysTick->CMP = i;
    SysTick->CTLR |= (1<<5)|(1<<0);

    while((SysTick->SR & (1<<0)) != (1<<0));
    SysTick->SR &= ~(1<<0);
}

/*******************************************************************************
* Function Name  : Delay_Ms
* Description    : Millisecond Delay Time.
* Input          : n:Millisecond number.
* Return         : None
*******************************************************************************/
void Delay_Ms(uint32_t n)
{
    uint32_t i;

    SysTick->CTLR = (1<<4);
    i = (uint32_t)n*p_ms;

    SysTick->CMP = i;
    SysTick->CTLR |= (1<<5)|(1<<0);

    while((SysTick->SR & (1<<0)) != (1<<0));
    SysTick->SR &= ~(1<<0);
}

/*******************************************************************************
* Function Name  : USART_Printf_Init
* Description    : Initializes the USARTx peripheral.
* Input          : baudrate: USART communication baud rate.
* Return         : None
*******************************************************************************/
void USART_Printf_Init(uint32_t baudrate)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  USART_InitTypeDef USART_InitStructure;

#if (DEBUG == DEBUG_UART1)
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);

  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);

#elif (DEBUG == DEBUG_UART2)
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

#elif (DEBUG == DEBUG_UART3)
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

#endif

  USART_InitStructure.USART_BaudRate = baudrate;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Tx;

#if (DEBUG == DEBUG_UART1)
  USART_Init(USART1, &USART_InitStructure);
  USART_Cmd(USART1, ENABLE);

#elif (DEBUG == DEBUG_UART2)
  USART_Init(USART2, &USART_InitStructure);
  USART_Cmd(USART2, ENABLE);

#elif (DEBUG == DEBUG_UART3)
  USART_Init(USART3, &USART_InitStructure);
  USART_Cmd(USART3, ENABLE);

#endif
}

/*******************************************************************************
* Function Name  : _write
* Description    : Support Printf Function
* Input          : *buf: UART send Data.
*                  size: Data length
* Return         : size: Data length
*******************************************************************************/
int _write(int fd, char *buf, int size)
{
  int i;

  for(i=0; i<size; i++)
  {
#if (DEBUG == DEBUG_UART1)
    while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
    USART_SendData(USART1, *buf++);
#elif (DEBUG == DEBUG_UART2)
    while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
    USART_SendData(USART2, *buf++);
#elif (DEBUG == DEBUG_UART3)
    while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
    USART_SendData(USART3, *buf++);
#endif
  }

  return size;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值