DA14585-blink/timerHW





void periph_init(void)
{

    // Initialize UART2
    uart_initialize(UART2, &uart_cfg);

    // Set pad functionality
    set_pad_functions();

    // Enable the pads
    GPIO_set_pad_latch_en(true);
}

static void set_pad_functions(void)
{
    // Configure UART2 pin functionality
    GPIO_ConfigurePin(UART2_TX_PORT, UART2_TX_PIN, OUTPUT, PID_UART2_TX, false);

    // Configure LED pin functionality
    GPIO_ConfigurePin(LED_PORT, LED_PIN, OUTPUT, PID_GPIO, false);
}

/*

可以直接点亮的
    GPIO_ConfigurePin(GPIO_LED_PORT, GPIO_LED_PIN, OUTPUT, PID_GPIO, false);
	  GPIO_SetActive(GPIO_LED_PORT, GPIO_LED_PIN);
}


*/

void blinky_test(void)
{
    volatile int i=0;

    printf_string(UART, "\n\r\n\r");
    printf_string(UART, "***************\n\r");
    printf_string(UART, "* BLINKY DEMO *\n\r");
    printf_string(UART, "***************\n\r");

    while(1)
    {
        i++;
        if (100000 == i)
        {
            GPIO_SetActive(LED_PORT, LED_PIN);
            printf_string(UART, "\n\r *LED ON* ");
        }

        if (200000 == i)
        {
            GPIO_SetInactive(LED_PORT, LED_PIN);
            printf_string(UART, "\n\r *LED OFF* ");
					  i = 0;
        }

    }
}
//        if (GPIO_GetPinStatus(LED_PORT, LED_PIN))
#define SetWord16(a,d) (* ( volatile uint16_t*)(a)=(d) )

GPIO_SetActive(LED_PORT, LED_PIN);
GPIO_SetActive(1, 0);

void GPIO_SetActive(GPIO_PORT port, GPIO_PIN pin)
{

    const int data_reg = GPIO_BASE + (port << 5);
    const int set_data_reg = data_reg + 2;
    SetWord16(set_data_reg, 1 << pin);
}
GPIO_BASE = 0X50003000
data_reg  = 0X50003020
在绝对地址写一个数据!SetWord16



void GPIO_SetInactive(GPIO_PORT port, GPIO_PIN pin)
{
    const int data_reg = GPIO_BASE + (port << 5);
    const int reset_data_reg = data_reg + 4;

    SetWord16(reset_data_reg, 1 << pin);
}

 

 

 

timer

static void timer0_general_user_callback_function(void)
{
    static uint8_t n = 0;

    // when pass  10 * 100ms
    if ( 10 == n )
    {
        n = 0;
        timeout_expiration--;
        if (GPIO_GetPinStatus(LED_PORT, LED_PIN))
        {
            GPIO_SetInactive(LED_PORT, LED_PIN);
        }
        else
        {
            GPIO_SetActive(LED_PORT, LED_PIN);
        }
     }
     n++;
}

void timer0_general_test(uint8_t times_seconds)
{
    printf_string(UART, "\n\r\n\r");
    printf_string(UART, "***********************\n\r");
    printf_string(UART, "* TIMER0 GENERAL TEST *\n\r");
    printf_string(UART, "***********************\n\r");

    // Stop timer for enter settings
    timer0_stop();

    timeout_expiration = times_seconds;

    // register callback function for SWTIM_IRQn irq
    timer0_register_callback(timer0_general_user_callback_function);

    // Enable the Timer0/Timer2 input clock
    timer0_2_clk_enable(); // CLK_PER_REG

    // Set the Timer0/Timer2 input clock division factor to 8, so 16 MHz / 8 = 2 MHz input clock
    timer0_2_clk_div_set(&clk_div_config);

    // clear PWM settings register to not generate PWM
    timer0_set_pwm_high_counter(NO_PWM);
    timer0_set_pwm_low_counter(NO_PWM);

    // Set timer with 2MHz source clock divided by 10 so Fclk = 2MHz/10 = 200kHz
    timer0_init(TIM0_CLK_FAST, PWM_MODE_ONE, TIM0_CLK_DIV_BY_10);

    // reload value for 100ms (T = 1/200kHz * RELOAD_100MS = 0,000005 * 20000 = 100ms)
    timer0_set_pwm_on_counter(RELOAD_100MS);//20000

    // Enable SWTIM_IRQn irq
    timer0_enable_irq();

    printf_string(UART, "\n\rLED will change state every second.\n\r");
    printf_string(UART, "\n\rTest will run for: ");
    printf_byte(UART, timeout_expiration);
    printf_string(UART, " seconds.\n\r");

    // Start Timer0
    timer0_start();
    printf_string(UART, "\n\rTIMER0 started!"); // TIMER0_CTRL_REG

    // Wait for desired number of seconds
    while (timeout_expiration);

    // Disable the Timer0/Timer2 input clock
    timer0_2_clk_disable();// CLK_PER_REG

    printf_string(UART, "\n\rTIMER0 stopped!\n\r");
    printf_string(UART, "\n\rEnd of test\n\r");
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值