伴随着浓浓的中国芯热情下,国产芯片越来越好了,国产单片机也逐渐成为工程师的优选。
那么用国产单片机开发究竟是怎样的体验?
1、利用东软载波开发板进行ADC及PWM调光测试
IO引脚配置如下:
static void adc_pin_init(void)
{
/* Initialize ADC pin */
gpio_init_t x;
/* Initialize PB0 pin */
x.mode = GPIO_MODE_INPUT;
x.odos = GPIO_PUSH_PULL;
x.pupd = GPIO_PUSH_UP;
x.odrv = GPIO_OUT_DRIVE_NORMAL;
x.flt = GPIO_FILTER_DISABLE;
x.type = GPIO_TYPE_TTL;
x.func = GPIO_FUNC_0;
ald_gpio_init(GPIOB, GPIO_PIN_0, &x);
return;
}
ADC0的配置如下:
void adc_init(void)
{
/* clear lpuart_handle_t structure */
memset(&h_adc, 0x0, sizeof(adc_handle_t));
/* clear adc_nch_conf_t structure */
memset(®_config, 0x0, sizeof(adc_nch_conf_t));
adc_pin_init();
/* Initialize adc */
h_adc.perh = ADC0;
h_adc.init.data_align = ADC_DATAALIGN_RIGHT;
h_adc.init.scan_mode = DISABLE;
h_adc.init.cont_mode = DISABLE;
h_adc.init.nch_len = ADC_NCH_LEN_1;
h_adc.init.disc_mode = ADC_ALL_DISABLE;
h_adc.init.disc_nbr = ADC_DISC_NBR_1;
h_adc.init.conv_res = ADC_CONV_RES_10;
h_adc.init.clk_div = ADC_CKDIV_128;
h_adc.init.neg_ref = ADC_NEG_REF_VSS;
h_adc.init.pos_ref = ADC_POS_REF_VDD;
ald_adc_init(&h_adc);
/* Initialize regular convert channel */
reg_config.channel = ADC_CHANNEL_14;
reg_config.rank = ADC_NCH_RANK_1;
reg_config.samp_time = ADC_SAMPLETIME_4;
ald_adc_normal_channel_config(&h_adc, ®_config);
return;
}
static void tim3_pin_init(void)
{
gpio_init_t GPIO_InitStructure;
/* Initialize tim0 ch1 pin */
GPIO_InitStructure.mode = GPIO_MODE_OUTPUT;
GPIO_InitStructure.odos = GPIO_PUSH_PULL;
GPIO_InitStructure.pupd = GPIO_PUSH_UP;
GPIO_InitStructure.odrv = GPIO_OUT_DRIVE_NORMAL;
GPIO_InitStructure.flt = GPIO_FILTER_DISABLE;
GPIO_InitStructure.type = GPIO_TYPE_TTL;
GPIO_InitStructure.func = GPIO_FUNC_3;
ald_gpio_init(GPIOC, GPIO_PIN_6, &GPIO_InitStructure);
}
TIM3初始化程序如下:
void tim3_init(timer_handle_t *tim_h, timer_oc_init_t *tim_ocinit, timer_clock_config_t *tim_clock)
{
/* Initialize pin */
tim3_pin_init();
/* Initialize TIM3 */
tim_h->perh = GP16C2T1;
tim_h->init.prescaler = 0;
tim_h->init.mode = TIMER_CNT_MODE_UP;
tim_h->init.period = (uint32_t)(255 - 1);
tim_h->init.clk_div = TIMER_CLOCK_DIV1;
tim_h->init.re_cnt = 0;
ald_timer_pwm_init(tim_h);
/* Initialize clock source */
tim_clock->source = TIMER_SRC_INTER;
ald_timer_config_clock_source(tim_h, tim_clock);
/* Common configuration for all channels */
tim_ocinit->oc_mode = TIMER_OC_MODE_PWM1;
tim_ocinit->oc_polarity = TIMER_OC_POLARITY_HIGH;
tim_ocinit->oc_fast_en = DISABLE;
tim_ocinit->ocn_polarity = TIMER_OCN_POLARITY_HIGH;
tim_ocinit->ocn_idle = TIMER_OCN_IDLE_RESET;
tim_ocinit->oc_idle = TIMER_OC_IDLE_RESET;
/* Set the pulse value for channel 1 */
tim_ocinit->pulse = (uint32_t)(255 * 20 / 100);
ald_timer_oc_config_channel(tim_h, tim_ocinit, TIMER_CHANNEL_1);
/* Set the pulse value for channel 2 */
tim_ocinit->pulse = (uint32_t)(255 * 50 / 100);
ald_timer_oc_config_channel(tim_h, tim_ocinit, TIMER_CHANNEL_2);
/* Start input pwm from tim2/3 channel 1 */
ald_timer_oc_start(tim_h, TIMER_CHANNEL_1);
}
/* ADC测试 */
value = adc_average();
sprintf((char *)tx_buf, "ADC_Value: %d \n", value);
ald_uart_send(&h_uart, tx_buf, sizeof("ADC_Value: %d \n"), 1000);
/* TIM3 PWM测试 */
duty = value/32.72;
if(duty <= 1)
duty = 1;
ald_timer_pwm_set_duty(&tim3.tim_h, TIMER_CHANNEL_1, (uint16_t)duty);
/**
* @defgroup DHT11_Public_Functions dht11 Public Functions
* [url=home.php?mod=space&uid=247401]@brief[/url] BSP DHT11 Functions
* @verbatim
===============================================================================
##### BSP DHT11 Functions #####
===============================================================================
[..] This section provides functions allowing to:
(+) Initialize bsp dht11.
(+) get dht11 data.
@endverbatim
* @{
*/
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] get dht11 data
* @retval result 1 or 0
*/
uint8_t dht11_read_data(uint8_t *temp, uint8_t *humi)
{
uint8_t buf[5] = {0};
uint8_t i;
dht11_reset();
if (dht11_check() == 0)
{
for (i = 0; i < 5; i++)
buf[i] = dht11_read_byte();
if ((buf[0] + buf[1] + buf[2] + buf[3]) == buf[4])
{
*humi = buf[0];
*temp = buf[2];
}
}
else
return 1;
return 0;
}
/**
* @brief init dht11
* @retval result 1 or 0
*/
uint8_t dht11_init(void)
{
dht11_pin_init();
ald_delay_ms(100);
dht11_reset();
return dht11_check();
}
rtc.c
/**
* @brief rtc module initialize function
* @retval None
*/
void rtc_init(void *rtc_param)
{
rtc_param_t *p_rtc = (rtc_param_t *)rtc_param;
/* Initialize RTC */
p_rtc->init.asynch_pre_div = 0;
p_rtc->init.synch_pre_div = 32767;
p_rtc->init.hour_format = RTC_HOUR_FORMAT_24;
p_rtc->init.output = RTC_OUTPUT_DISABLE;
p_rtc->init.output_polarity = RTC_OUTPUT_POLARITY_HIGH;
ald_rtc_init(&p_rtc->init);
/* Set current time and date */
p_rtc->time.hour = 18;
p_rtc->time.minute = 31;
p_rtc->time.second = 43;
p_rtc->time.sub_sec = 123;
p_rtc->date.day = 15;
p_rtc->date.month = 12;
p_rtc->date.year = 19;
p_rtc->date.week = 7;
ald_rtc_set_time(&p_rtc->time, RTC_FORMAT_DEC);
ald_rtc_set_date(&p_rtc->date, RTC_FORMAT_DEC);
/* Enable second interrupt */
ald_rtc_interrupt_config(RTC_IT_SEC, ENABLE);
}
【二】OLED显示屏模块:
这里使用了OLED12864显示液晶,采用模拟IIC进行驱动,便于程序的移植。
int main()
{
/* Initialize ALD */
ald_cmu_init();
/* Configure system clock */
ald_cmu_pll1_config(CMU_PLL1_INPUT_HOSC_3, CMU_PLL1_OUTPUT_48M);
ald_cmu_clock_config(CMU_CLOCK_PLL1, 48000000);
/* Initialize tx_buf */
memset(tx_buf, 0x55, sizeof(tx_buf));
/* Initialize uart_usb */
uart_usb_init();
/* Initialize led */
led_pin_init();
/* Initialize led */
dht11_init();
/* Initialize oled */
OLED_Init();
/* Initialize rtc */
rtc_init(&rtc_h);
while (1) {
/* Send a message */
//sprintf((char *)tx_buf, "es32 uart test!\n");
//ald_uart_send(&h_uart, tx_buf, sizeof("es32 uart test!\n"), 1000);
/* LED灯测试 */
cnt++;
if(cnt == 5)
{
ald_gpio_write_pin(LED1_PORT, LED1_PIN, 0);
ald_gpio_write_pin(LED2_PORT, LED2_PIN, 0);
sprintf((char *)tx_buf, "LED ON ");
OLED_P8x16Str(0u,4u,(uint8_t *)tx_buf);
//ald_delay_ms(1000);
}else if(cnt == 10)
{
ald_gpio_write_pin(LED1_PORT, LED1_PIN, 1);
ald_gpio_write_pin(LED2_PORT, LED2_PIN, 1);
sprintf((char *)tx_buf, "LED OFF");
OLED_P8x16Str(0u,4u,(uint8_t *)tx_buf);
//ald_delay_ms(1000);
cnt = 0;
}
/* DHT11温湿度传感器测试 */
dht11_read_data(&temp, &humi);
sprintf((char *)tx_buf, "Temp: %d Humi: %d \n", temp, humi);
ald_uart_send(&h_uart, tx_buf, sizeof("Temp: %d Humi: %d \n"), 1000);
/* RTC测试 */
ald_rtc_get_time(&rtc_v, RTC_FORMAT_DEC);
sprintf((char *)tx_buf, "Time:%02d-%02d-%02d", rtc_v.hour, rtc_v.minute, rtc_v.second);
OLED_P8x16Str(0u,6u,(uint8_t *)tx_buf);
/* OLED显示屏测试 */
OLED_P8x16Str(0u,0u,(uint8_t *)"ES32 PDS BOARD");
sprintf((char *)tx_buf, "Temp:%d Humi:%d", temp, humi);
OLED_P8x16Str(0u,2u,(uint8_t *)tx_buf);
//OLED_P8x16Str(0u,6u,(uint8_t *)"Date:2019-12-15");
}
}
除了免费用开发板
对于测评优秀的玩家
我们还奉上丰盛好礼