BLE基础外设使用
<h2>创建工程</h2><div>选择 New project wizard</div>
<div></div>
<div>点击next</div>
<div></div>
<div>双击empty c project。</div>
<div></div>
<div>选择copy contents</div>
<div>点finish</div>
<div>进入software component,添加外设组件。</div>
<h2>外设</h2>
<h3>VCOM</h3>
<div>install</div>
<div>Platform->driver->swo</div>
<div>Platform->peripheral->usart</div>
<div>Services->peripheral-> Automatic Device Initialization</div>
<div>Services: IO Stream: USARTInstall</div>
<div>创建一个vcom实例</div>
<div></div>
<div>Services :IO Stream: Retarget STDIOInstall</div>
<div>导入以下头文件到app.c</div>
<div><strong>#include</strong> <stdio.h></div>
<div><strong>#include</strong> <string.h></div>
<div><strong>#include</strong> "em_chip.h"</div>
<div><strong>#include</strong> "sl_iostream.h"</div>
<div><strong>#include</strong> "sl_iostream_init_instances.h"</div>
<div><strong>#include</strong> "sl_iostream_handles.h"</div>
<div><strong>const</strong> <strong>char</strong> str1[] = "IOstream USART example\r\n\r\n";</div>
<div>sl_iostream_write(sl_iostream_vcom_handle, str1, <strong>strlen</strong>(str1));</div>
<div></div>
<div>这样就可以用l_iostream_write写入数据到串口</div>
<h4>Printf 重定向</h4>
<div>在software component搜索tiny printf</div>
<div>在third party ->unitl->tiny printf</div>
<div></div>
<div></div>
<h3>GPIO</h3>
<h4>Led</h4>
<div>Platform:driver:led:Generic LED API</div>
<div></div>
<div>点击install</div>
<div>点击done创建一个实例</div>
<div></div>
<div><strong>#include</strong> "sl_simple_led.h"</div>
<div>里是led的功能函数</div>
<div>sl_simple_led_init(&simple_led0_context);初始化</div>
<div><strong>sl_simple_led_turn_on</strong><strong>点灯</strong></div>
<div><strong>sl_simple_led_turn_off</strong><strong>关灯</strong></div>
<div><strong>sl_simple_led_toggle</strong><strong>反转</strong></div>
<div><strong>sl_simple_led_get_state</strong><strong>获取灯的状态</strong></div>
<div><strong>typedef</strong> <strong>struct</strong> {</div>
<div>GPIO_Port_TypeDef port; ///< LED port</div>
<div>uint8_t pin; ///< LED pin</div>
<div>sl_led_polarity_t polarity; ///< Initial state of LED</div>
<div>} sl_simple_led_context_t;//LED初始化结构体</div>
<div>底层头文件</div>
<div><strong>#include</strong> "em_gpio.h</div>
<div><strong>GPIO_PinOutClear</strong><strong>()</strong>Set bits in DOUT register for a port to 0. <strong>GPIO_PortOutSet</strong> Set bits GPIO data out register to 1.</div>
<div><strong>GPIO_PortOutToggle</strong>Toggle pins in GPIO port data out register.</div>
<div><strong>GPIO_PinOutGet</strong>Get current setting for a pin in a GPIO port data out regisl_simple_led_context_t *led = context;ster.</div>
<div>初始化过程</div>
<div>sl_simple_led_context_t simple_led0_context = {</div>
<div>.port = SL_SIMPLE_LED_LED0_PORT,</div>
<div>.pin = SL_SIMPLE_LED_LED0_PIN,</div>
<div>.polarity = SL_SIMPLE_LED_LED0_POLARITY,</div>
<div>};初始化结构体</div>
<div>sl_simple_led_context_t *led = context;</div>
<div>CMU_ClockEnable(<em>cmuClock_GPIO</em>, true);//时钟使能</div>
<div>GPIO_PinModeSet(led->port,</div>
<div>led->pin,</div>
<div><em>gpioModePushPull</em>,</div>
<div>!led->polarity);</div>
<div>配置引脚</div>
<h4>Button</h4>
<div>Platform:driver:led:Generic LED button API</div>
<div>创建一个button实例</div>
<div></div>
<div><strong>void</strong> <strong>button_init</strong>()</div>
<div>{</div>
<div>CMU_ClockEnable(<em>cmuClock_GPIO</em>, true);</div>
<div>GPIO_PinModeSet(<em>gpioPortB</em>,</div>
<div>2,</div>
<div><em>gpioModeInput</em>,</div>
<div>0);</div>
<div>//初始化输入模式</div>
<div>//浮空输入<em>gpioModeInput</em>,</div>
<div><em>gpioModeInputPull</em><em>,上来下拉由第四个参数决定</em> <em>1</em><em>上拉</em><em>/</em><em>0</em><em>下拉</em></div>
<div>}</div>
<div>GPIO_PinInGet(<em>gpioPortB</em>,2)获取b2的输入电平</div>
<h3>time</h3>
<div>sleeptimer</div>
<div>service:time:sleeptimer</div>
<div>Sleepimer驱动程序使用低频实时时钟外设提供软件定时器、延迟、计时和日期功能。睡眠定时器使用一个硬件定时器并创建多个软件定时器实例。睡眠定时器可用于创建与电源管理紧密集成的定时器。电源管理器需要精确的计时,以使所有时钟按时准备就绪,因此唤醒发生得更早一点,为系统在正确的时间做好准备。</div>
<div></div>
<div></div>
<div>sl_sleeptimer_init();//初始化睡眠定时器</div>
<div>sl_sleeptimer_timer_handle_t handle;</div>
<div>sl_sleeptimer_start_periodic_timer(&handle,1000,timecallback,NULL,0,true);开启定时周期回调</div>
<div><strong>void</strong> <strong>timecallback</strong>()//回调函数</div>
<div>{</div>
<div>printf("1");</div>
<div>printf("sl_simple_button_get_state %d\r\n",GPIO_PinInGet(<em>gpioPortB</em>,2));</div>
<div>}</div>
<div>Silicon lab的定时器有用了app_timer封装了一层sleeptime使用</div>
<div>用法一样只不过少了初始化步骤</div>
<div>static app_timer_t app_periodic_timer;</div>
<div> sc = app_timer_start(&app_periodic_timer,</div>
<div> SL_BT_HT_MEASUREMENT_INTERVAL_SEC * 1000,</div>
<div> app_periodic_timer_cb,</div>
<div> NULL,</div>
<div> true);//开启定时器</div>
<div> app_periodic_timer_cb(&app_periodic_timer, NULL);//重写写定时回调函数 执行定时任务</div>
<h3>temp</h3>
<div>platform:driver:tempdev</div>
<div>温度驱动器使用一些EFR32和EFM32设备上的EMU内部温度传感器测量温度。特定于应用程序的回调函数可以注册,并将在给定的温度阈值上调用。EMU内部温度传感器在EM0-EM4H中运行,能够在温度变化时唤醒核心。EMU温度传感器连续运行,每250毫秒进行一次测量。对于适用勘误表EMU_E201的设备,温度驱动器执行一种变通方法,根据测量的温度调整EMU设置。有关此勘误表解决方法的更多信息,请参阅应用说明AN1027。</div>
<div></div>
<div>导入 <strong>#include</strong> "tempdrv.h"</div>
<div>TEMPDRV_Init();</div>
<div>printf("TEMPDRV_GetTemp %d\r\n",TEMPDRV_GetTemp());</div>
<p>感谢分享,不过希望楼主可以使用编辑器的高级模式,该模式有专门的代码块编辑,会使代码看起来更方便简洁,像下面这样</p>
<pre>
<code class="language-python">import os
def hello():
print('Hello World!')
hello()</code></pre>
<p> </p>
极限零 发表于 2024-1-19 11:41
感谢分享,不过希望楼主可以使用编辑器的高级模式,该模式有专门的代码块编辑,会使代码看起来更方便简洁, ...
<p>有高级模式的截图吗</p>
<p> </p>
zsbyg 发表于 2024-1-19 19:44
有高级模式的截图吗
<p>发贴时可以直接用,点完了直接粘代码就行,操作很简单</p>
<p> 回贴时有高级模式,进入之后和上面的是一样的</p>
<p> </p>
极限零 发表于 2024-1-20 13:03
发贴时可以直接用,点完了直接粘代码就行,操作很简单
回贴时有高级模式,进入之后和上面的是 ...
<p>好的</p>
<p>不用高级模式也可以贴代码的,选择代码,要不然这样看起来太累了,有需要的人cp也不方便</p>
<p>感谢</p>
页:
[1]