前一阵子,一位小伙伴咨询我一款新ic芯片怎么使用,借此机会我顺便把我日常工作中经常用到的一种调试方法介绍给小伙伴们,希望对对大家有所帮助。准备仓促,文中难免有技术性错误,欢迎大家给予指正,并给出好的建议...
前言:
我们在单片机的项目开发过程中经常会遇到使用新ic芯片的情况,某宝卖家有个时候也提供不了对应开发程序,到网上找资料也找不到;很多初学者面对这样的问题往往束手无策,这里我给大家介绍我经常用的其中一种新ic调试的方法。
因为这个芯片比较简单我这里采用下面步骤进行:
第一步: 先用arduino+面包板快速搭建电路验证芯片功能
第二步: 使用stm32cubeide快速搭建工程验证在stm32上工作是否正常
tips :由于我手头没有 万用表 ,这里我使用arduino的模拟电压采集功能通过串口打印出来作为电位计的电压监控用。
首先,我们先快速浏览芯片数据手册,获取重要信息
ic型号:tlp0501
电压范围:2.7~5.5v
温度范围:-40~125℃
通信方式:spi
阻值:100kω
阻值偏差:±20%
该芯片是德州仪器的一款单通道数字电位计,通信方式是spi总线,单方向的,即只能控制芯片,不能读取输入数据,下面是数据手册的具体介绍。
环境参数:主要包括温度使用范围、电压使用范围、误差、温度漂移以及实物引脚对应关系都在这里
环境参数
功能框图:主要介绍该芯片的内部组成和工作原理
功能框图
引脚定义:每个引脚的功能介绍
引脚定义
spi通信说明:主要介绍芯片的通信方式,这个芯片因为没有设置模式功能,只需要对芯片直接写数据即可,通信方式与我们所使用的74hc595的方式类似
spi通信说明
真值对照表:也就是数字量对应的实际电阻值,我这里只截取了一部分,剩下的大家可以自行去参考详细手册
真值对照表
对于要调通这个芯片这些介绍基本满足我们的需求了
芯片模块的快速制作
在芯片商城上买了两片回来调试,芯片购买的费用知乎小伙伴给付了
准备好芯片+转接板
焊接两块是为了防止在使用过程中意外弄坏另一块可以立马补上,确保调试正常进行而不耽误太多时间
焊接好排针,并在供电端加上0.1uf的滤波电容,降低高频供电干扰
这样我们的模块就制作完成了
arduino快速搭建工程:
电路原理图
电路原理图
引脚对应关系:
arduino uno 引脚4 -> clk
arduino uno 引脚5 -> din
arduino uno 引脚6 -> cs
arduino uno 引脚5v -> vcc、vref
arduino uno 引脚gnd -> gnd、l
arduino uno 引脚a0 -> vres_out
首先用面包板+杜邦线搭好电路
说明:我在这里使用的供电电压和数字电位计参考电压都是使用的5v,相应的输出结果也是在0~5v范围
然后使用arduino自带的库,编写代码,再变动阻值参数,看下输出的实际结果和真值表是否对应的上
数值为0x00时对应的模拟电压输出
数值为0x88时对应的模拟电压输出
数值为0xff时对应的模拟电压输出
经过验证,在arduino上跑没有问题,接下来我们准备在stm32上去运行
arduino代码部分:
/* analogreadserial reads an analog input on pin 0, prints the result to the serial monitor. graphical representation is available using serial plotter (tools > serial plotter menu). attach the center pin of a potentiometer to pin a0, and the outside pins to +5v and ground. this example code is in the public domain. http://www.arduino.cc/en/tutorial/analogreadserial*/int cs_pin = 6;int clk_pin = 4;int din_pin = 5; //这里定义了那三个脚// the setup routine runs once when you press reset:void setup() { pinmode(cs_pin,output); pinmode(clk_pin,output); pinmode(din_pin,output); //让三个脚都是输出状态 // initialize serial communication at 9600 bits per second: serial.begin(9600); digitalwrite(cs_pin,low); shiftout(din_pin,clk_pin,msbfirst,0xff); digitalwrite(cs_pin,high);}// the loop routine runs over and over again forever:void loop() { // read the input on analog pin 0: int sensorvalue = analogread(a0); float voltage= sensorvalue * (5.0 / 1023.0); //换算成电压 // print out the value you read: serial.println(voltage,dec); delay(100); // delay in between reads for stability}stm32搭建工程验证
说明:stm32使用的供电电压和数字电位计参考电压都是3v3,相应的输出结果也是在0~3v3范围
电路原理图
引脚对应关系:
pa4 -> clk
pa5 -> din
pa6 -> cs
3v3 -> vcc、vref
arduino uno 引脚gnd -> gnd、l
arduino uno 引脚a0 -> vres-out
开发板+面包板搭建电路
用stm32cubeide建立一个工程
配置好pa4、pa5、pa6引脚为输出
生成代码,并添加tlp0501的驱动代码
编译看运行的效果,输入值为0x55
更改输入的数值为0x22,验证是否正确
main函数代码部分:
/* user code begin header *//** ****************************************************************************** * @file : main.c * @brief : main program body ****************************************************************************** * @attention * * © copyright (c) 2021 stmicroelectronics. * all rights reserved. * * this software component is licensed by st under bsd 3-clause license, * the license; you may not use this file except in compliance with the * license. you may obtain a copy of the license at: * opensource.org/licenses/bsd-3-clause * ****************************************************************************** *//* user code end header *//* includes ------------------------------------------------------------------*/#include main.h/* private includes ----------------------------------------------------------*//* user code begin includes *//* user code end includes *//* private typedef -----------------------------------------------------------*//* user code begin ptd *//* user code end ptd *//* private define ------------------------------------------------------------*//* user code begin pd */#define tlp0501_din_h() hal_gpio_writepin(gpioa, gpio_pin_5, gpio_pin_set)#define tlp0501_din_l() hal_gpio_writepin(gpioa, gpio_pin_5, gpio_pin_reset)#define tlp0501_cs_h() hal_gpio_writepin(gpioa, gpio_pin_6, gpio_pin_set)#define tlp0501_cs_l() hal_gpio_writepin(gpioa, gpio_pin_6, gpio_pin_reset)#define tlp0501_clk_h() hal_gpio_writepin(gpioa, gpio_pin_4, gpio_pin_set)#define tlp0501_clk_l() hal_gpio_writepin(gpioa, gpio_pin_4, gpio_pin_reset)/* user code end pd *//* private macro -------------------------------------------------------------*//* user code begin pm *//* user code end pm *//* private variables ---------------------------------------------------------*//* user code begin pv *//* user code end pv *//* private function prototypes -----------------------------------------------*/void systemclock_config(void);static void mx_gpio_init(void);/* user code begin pfp *//* user code end pfp *//* private user code ---------------------------------------------------------*//* user code begin 0 */void tlp0501_writebyte( uint8_t data ){ uint8_t j; for ( j=8; j >=1; j--) { tlp0501_clk_l(); __nop(); if(data & 0x80 ){ tlp0501_din_h(); } else { tlp0501_din_l(); } data < <= 1; tlp0501_clk_h(); }}/* user code end 0 *//** * @brief the application entry point. * @retval int */int main(void){ /* user code begin 1 */ /* user code end 1 */ /* mcu configuration--------------------------------------------------------*/ /* reset of all peripherals, initializes the flash interface and the systick. */ hal_init(); /* user code begin init */ /* user code end init */ /* configure the system clock */ systemclock_config(); /* user code begin sysinit */ /* user code end sysinit */ /* initialize all configured peripherals */ mx_gpio_init(); /* user code begin 2 */ tlp0501_cs_l(); tlp0501_writebyte(0x22); tlp0501_cs_h(); /* user code end 2 */ /* infinite loop */ /* user code begin while */ while (1) { /* user code end while */ /* user code begin 3 */ } /* user code end 3 */}/** * @brief system clock configuration * @retval none */void systemclock_config(void){ rcc_oscinittypedef rcc_oscinitstruct = {0}; rcc_clkinittypedef rcc_clkinitstruct = {0}; /** initializes the rcc oscillators according to the specified parameters * in the rcc_oscinittypedef structure. */ rcc_oscinitstruct.oscillatortype = rcc_oscillatortype_hsi; rcc_oscinitstruct.hsistate = rcc_hsi_on; rcc_oscinitstruct.hsicalibrationvalue = rcc_hsicalibration_default; rcc_oscinitstruct.pll.pllstate = rcc_pll_none; if (hal_rcc_oscconfig(&rcc_oscinitstruct) != hal_ok) { error_handler(); } /** initializes the cpu, ahb and apb buses clocks */ rcc_clkinitstruct.clocktype = rcc_clocktype_hclk|rcc_clocktype_sysclk |rcc_clocktype_pclk1|rcc_clocktype_pclk2; rcc_clkinitstruct.sysclksource = rcc_sysclksource_hsi; rcc_clkinitstruct.ahbclkdivider = rcc_sysclk_div1; rcc_clkinitstruct.apb1clkdivider = rcc_hclk_div1; rcc_clkinitstruct.apb2clkdivider = rcc_hclk_div1; if (hal_rcc_clockconfig(&rcc_clkinitstruct, flash_latency_0) != hal_ok) { error_handler(); }}/** * @brief gpio initialization function * @param none * @retval none */static void mx_gpio_init(void){ gpio_inittypedef gpio_initstruct = {0}; /* gpio ports clock enable */ __hal_rcc_gpiod_clk_enable(); __hal_rcc_gpioa_clk_enable(); /*configure gpio pin output level */ hal_gpio_writepin(gpioa, gpio_pin_4|gpio_pin_5|gpio_pin_6, gpio_pin_reset); /*configure gpio pins : pa4 pa5 pa6 */ gpio_initstruct.pin = gpio_pin_4|gpio_pin_5|gpio_pin_6; gpio_initstruct.mode = gpio_mode_output_pp; gpio_initstruct.pull = gpio_nopull; gpio_initstruct.speed = gpio_speed_freq_low; hal_gpio_init(gpioa, &gpio_initstruct);}/* user code begin 4 *//* user code end 4 *//** * @brief this function is executed in case of error occurrence. * @retval none */void error_handler(void){ /* user code begin error_handler_debug */ /* user can add his own implementation to report the hal error return state */ /* user code end error_handler_debug */}#ifdef use_full_assert/** * @brief reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval none */void assert_failed(uint8_t *file, uint32_t line){ /* user code begin 6 */ /* user can add his own implementation to report the file name and line number, tex: printf(wrong parameters value: file %s on line %drn, file, line) */ /* user code end 6 */}#endif /* use_full_assert *//************************ (c) copyright stmicroelectronics *****end of file****/总结:
1、这里介绍了众多新ic芯片调试方式中的一种,后期有机会再陆续介绍其他ic或新模块的调试方法。
2、文中只是简单的对芯片进行功能测试,实际项目中还会有移植、驱动的可靠性、稳定性等测试工作 。
3、我们要善于运用手头的工具、arduino等快速验证开发环境;模块的快速验证,特别是在项目开发过程中,时间就是金钱,对每一种工具的熟练掌握也是单片机开发过程中不可或缺的重要技能。
4、硬件调试与软件调试有很大的区别,很多时候是一次性,不可逆转的,不像软件ctl+z可以撤销;硬件在使用过程中出现意外损坏情况很正常:焊接不当、意外插错,静电防护不到位等等;我们要善于运用一些项目技巧,权衡时间或花费;这里之所以选择焊接两个芯片模块也是为了防止这种意外的发生而对调试造成不必要的时间耽搁。
浅谈模拟和数字布线的区别
巴林政府计划到2025年新增光伏装机255MW
走向“数据融合” MEMS传感器创新可穿戴与医疗应用
ttl电路中输入端悬空代表什么 ttl电路正确接线图图解
iPhone8最新消息:还在问iPhone8什么时候上市?iPhone8终极版设计图抢先看,发布时间待定
单片机项目中使用新IC芯片调试方法
USB Rubber Ducky脚本编码器的制作
ADI推出精密单芯片RMS-to-DC转换器AD8436
农业传感器在温室大棚物联网的实践
煤矿无线监控系统的结构组成及应用设计
拥有私有5G网络的企业的未来
首创的人工智能挑战,旨在测试智能代理的功能并加速AI的研发
工业机器人智能化潮流下 库柏特致力于让机器人具备视觉与触觉感知能力
未来智能家居行业的发展方向将会是怎样的
人都分不好垃圾,机器能分好吗?
自主研发芯片才能摆脱美国控制_阿里已收购5家半导体公司
轻奢“粉”少女心 华为MateBook 13 2020款樱粉金正式上市
ANIA正在保险业进行区块链试验,计划用该技术处理与汽车责任索赔有关的纠纷
浅谈meas压力传感器使用方法
特斯拉主动大幅度降价 意图挽回颓势