完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
在我的程序中,我必须控制5个GPIO的行为:GP0-RA4GP1-RB5GP2-RC2GP3-RC1GP4-RC0,例如,在运行时,用户可以将GP3设置为数字输入,GP0设置为数字输出,GP4设置为模拟输入,用户也可以随时将数字输出(onoff)写入GP0或从GP4读取ADC;ld建立一个指向不同IO控制位的指针映射,然后用更快、可读的C代码控制它们是非常有效的。我正在使用XC8,pic16f1459。我已经知道XC8不支持位的指针和数组,所以我在这里发帖子来看看是否有其他方法来实现它。http//www. McCHIP.COM/FUMMS/M948 .ASPX.另一篇文章也展示了我的情况
以上来自于百度翻译 以下为原文 in my programe,i have to control behavior of 5 GPIOs: GP0--RA4 GP1--RB5 GP2--RC2 GP3--RC1 GP4--RC0 for example,at runtime,user may set GP3 as digital input,GP0 as digital output,and GP4 as analog in; user also may write digital output(onoff) to GP0 or read ADC from GP4 at any time; my question is:if we could build a pointer map to point to different IO's control bit, then it will be very efficient to control them with faster and readable C codes. i'm using XC8,pic16f1459. i have known that XC8 does not support bit's pointer and array, so i make a post here to see if there is some other way to do it. http://www.microchip.com/forums/m948198.aspx the other post also show my situations |
|
相关推荐
11个回答
|
|
请不要在同一个问题上开始多个话题。让我们继续你原来的话题。
以上来自于百度翻译 以下为原文 Please don't start multiple topics about the same question. Let's just continue in your original topic. |
|
|
|
的确.@OP。您需要学习PIC18的汇编语言来理解这一点。这是因为BCF和BSF指令采用一个常量的文字参数来指定位位置。您不能从变量中指定位位置。解决方法是使用带掩码的逻辑AND操作来设置位低,或者使用带掩码的OR操作来设置位高。例如,将设置GPIO寄存器的位6,并且值“0x40”可以来自变量。编辑:现在我们有两个par。关于相同问题的答案
以上来自于百度翻译 以下为原文 Indeed. @OP. You need to study the assembly language of the PIC18 to understand this. It's because the BCF and BSF instructions take a constant literal argument to specify the bit position. You can't specify the bit position from a variable. The workaround is to use a logical AND operation with a mask to set a bit low, or logical OR operation with a mask to set a bit high. e.g. GPIO |= 0x40; will set bit 6 of the GPIO register, and the value "0x40" can come from a variable. Edit: And now we have two parallel topics with answers about the same question... |
|
|
|
对于两篇要求几乎相同的文章,我感到很抱歉;也许我是一个完美主义者,总是想把事情做得更好,做得最好;我会放弃尝试制作IO控件的地图,像我一样使用示例代码
以上来自于百度翻译 以下为原文 sorry for two post with almost same requirement; maybe i have been a perfectionist,always want to make things better and best; i will give up trying to make a map of IO controls,just use sample codes as i have done |
|
|
|
如果你从用户那里得到命令,你可能有一个跳转表来容纳用户命令。一个命令=一个跳转表条目。你可以在一个跳转表中有127个。看起来对我来说很重要。所以,我只想让每个动作在跳转表中都有不同的命令。比如说,读GP1上的ADC是一个命令,读GP2上的ADC是一个不同的命令,它将是最适合PIC16的。
以上来自于百度翻译 以下为原文 If you get commands from the user you probably have a jump-table to accommodate user commands. One command = one jump table entry. You can have 127 of them in one jump table. Looks like a lot to me. So, I would simply make every action a different command with its own entry in the jump table. Say, reading ADC on GP1 is one command, reading ADC on GP2 is a different command etc. It'll be the most suitable for PIC16. |
|
|
|
是的,您显示的与我在编码中所做的类似,但是除了IO的控制变量名称不同,如TRISXbits.TRISXX,LATXbits.LATXX之外,似乎有很多函数都具有几乎相同的代码。
以上来自于百度翻译 以下为原文 yes,your shown is similar with what i have done in my coding,but seems to have many functions with almost same codes except only IO's control variables names are different,like TRISXbits.TRISXX, LATXbits.LATXX |
|
|
|
这可以通过位掩码和指向各种端口SFR的指针的组合来完成,但是它通常比使用构建时声明的固定引脚慢两个数量级,因为它需要100个指令的数量级来转换任意引脚号和访问类型i输入所需的指针、掩码和位逻辑运算。你的PIC自然可以切换一个引脚,产生高达2MHz的方波,同时有两条指令可用来根据命令终止循环。我估计所提议的pin抽象层会以几十KHz切换一个pin,如果pin被选择一次,并且您添加代码来缓存指针和位掩码,那么直接使用它们,那么可能是几百KHz。如果不关心Arduino-land级别的性能缺陷,那么就去使用它们!
以上来自于百度翻译 以下为原文 It can be done with a combination of bit-masks and pointers to the various port SFRs, but it will typically be two orders of magnitude slower than using a fixed pin declared at build time because it will need of the order of 100 instructions to translate an arbitrary pin number and type of access into the required pointer, mask and bitwise logic operation. Natively, your PIC can toggle a pin to produce a squarewave at up to 2MHz, with a couple of instruction available to terminate the loop on command. I estimate the proposed pin abstraction layer would toggle a pin at a few tens of KHz, maybe a few hundred KHz if the pin is selected once and you add code to cache the pointer and bitmasks, then use them directly. If you don't care about an Arduino-land level of performance suckitude, go for it ! |
|
|
|
|
|
|
|
也许我们可以试试使用函数指针数组,这是个好主意吗?
以上来自于百度翻译 以下为原文 maybe we could try using function pointer array,is that a good idea? |
|
|
|
是的,如果你有很多命令,那么这是最好的调用方法。这就是跳转表的本质。
以上来自于百度翻译 以下为原文 Yes, if you have many commands, then it's the best way to call them. That's essentially what the jump table is. |
|
|
|
不,访问将和缓存指针和掩码一样快,但是它会使用更多的程序内存,因为每个抽象的引脚需要四个函数,这是15个引脚X 4的函数,但是您节省了一些,因为6个数字只有引脚可以共享同一个哑模拟模式函数,并且您也可以。使用它作为RA3的虚拟输出和方向函数,因此您将节省7,留下53个实际需要的功能。即使将其限制为只有5个用户可访问的PIN,也就是20个功能。还有一个可怕的大交换机,它有25种方法,每个用户PIN和访问类型的组合都是一个,如果你使用定义了的宏来减少实际的重复源代码的数量,这是一个不太坏的方法。只要命令代码是相邻的整数,编译器就应该在PRO模式下生成相当好的代码。
以上来自于百度翻译 以下为原文 No, Access would be about as fast as cached pointers and masks but it would use more program memory as each abstractable pin would need four functions That's 15 pins x 4 functions, but you save a few as the 6 digital only pins can share the same dummy analog mode function, and you can probably also use it for dummy output and direction functions for RA3, so you'll save 7, leaving 53 functions actually needed. Even if you limit it to only the 5 user accessible pins, that's 20 functions. There's also the horrendously large switch with 25 cases approach - one for each combination of user pin and type of access - that's actually not too bad a way to do it, if you use #defined macros to minimise the amount of actual repeated sourcecode. As long as the command codes are adjacent integers, the compiler should generate reasonably good code for that in PRO mode. |
|
|
|
|
|
|
|
只有小组成员才能发言,加入小组>>
5184 浏览 9 评论
2005 浏览 8 评论
1932 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3179 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2230 浏览 5 评论
742浏览 1评论
629浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
512浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
640浏览 0评论
538浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-29 01:24 , Processed in 1.640100 second(s), Total 97, Slave 80 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号