完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
嘿,伙计们,我对PIC单片机编程很陌生,但我对编程有很好的理解。我在0到45之间有一个随机数。因为我在C编程,所以我尝试使用RAND功能,但是好像我没有足够的内存在芯片上。只需要一些帮助就可以走了。贝娄:在我不使用RAND函数的时候,你会发现我的代码,我只是在重复一个数字。贝娄强调了[0]中的随机数([lt;字体)],也强调了A34,这应该是一个随机数在5-45π之间,包括:ldit;sdio.h & gt;= OX-TraceMaqICOMIGMCLRE=O*定义xTalthFRIQ 400 000空隙Lead OLED(int随机数);空隙时间延迟(int数);空隙主(空){选项=0B90000;//用这个GPIO 2将不运行RunTrISGPIO=0BMIN;/ /将所有的PIN设置为输出(GPIO3仅输入)INT=0;应该是随机数(1){Listar LED([lt;Frand)数);数+=1;如果(数字和gt;4){数=0;}时间延迟(1011);}返回;}空隙时间延迟(int数){(数){α-DelayyMs(1000);数字};}空隙光LED(int随机数){if(随机数=0){if(GPIOBIT.GP0==0){GPIOBItiT.GP0=1;{if(GPIOBITS.GP1==0){GPIOBITIT.GP1=0;} }否则(随机数=2){if(GPIOBIT.GP2==0){GPIOBITIT.GP2=1;}否则{GPIOBIT.GP2=0;}否则(随机数=3){if(GPIOBIT.GP4==0){GPIOBITIT.GP4= 1;} { } {GPIOBIT.gp0=0;}否则,如果(随机数=1)GPIOBIT.GP4=0;}否则(随机数=4){if(GPIOBIT.GP5==0){GPIOBIT.GP5=1;}否则{GPIOBIT.GP5=0;}}
以上来自于百度翻译 以下为原文 hey guys I'm very new to programming pic micro controllers but i do have a good understanding of programming in general. I'm having issues making a random number between 0-45. Since I'm programming in C I tried to use the rand function but it seems that i don't have enough memory on the chip. Just need some help on what way to go. Bellow you will find my code at the moment I'm not using the rand function I'm just iterating over a number. bellow i highlighted the [ #include #include #include #pragma config OSC = IntRC #pragma config WDT = OFF #pragma config CP = OFF #pragma config MCLRE = ON #define _XTAL_FREQ 4000000 void light_led(int randomNumber); void time_delay(int number); void main(void) { OPTION = 0b00000000; //with out this gpio 2 will not run TRISGPIO = 0b000000; //setting all of my pins to outputs (gpio3 is input only) int number = 0; //this number should be a random number while(1){ light_led([ number += 1; if(number > 4){ number = 0; } time_delay(1011); } return; } void time_delay(int number){ while(number){ __delay_ms(1000); number--; } } void light_led(int randomNumber){ if(randomNumber == 0){ if(GPIObits.GP0 == 0){ GPIObits.GP0 = 1; }else { GPIObits.GP0 = 0; } }else if(randomNumber == 1){ if(GPIObits.GP1 == 0){ GPIObits.GP1 = 1; }else { GPIObits.GP1 = 0; } }else if(randomNumber == 2){ if(GPIObits.GP2 == 0){ GPIObits.GP2 = 1; }else { GPIObits.GP2 = 0; } }else if(randomNumber == 3){ if(GPIObits.GP4 == 0){ GPIObits.GP4 = 1; }else { GPIObits.GP4 = 0; } }else if(randomNumber == 4){ if(GPIObits.GP5 == 0){ GPIObits.GP5 = 1; }else { GPIObits.GP5 = 0; } } } |
|
相关推荐
11个回答
|
|
ADC是获得一些随机输入的好方法,但是PIC没有。使用RAM和ROM如此少的PIC确实使您自己的生活变得很困难。您能添加一些外部组件来向输入引脚中输入一些噪声吗?你不需要你的来源。大多数嵌入式程序根本不需要它们。
以上来自于百度翻译 以下为原文 An ADC is a good way to get some random input, but your PIC doesn't have one. You're really making life hard for yourself using a PIC with so little RAM and ROM. Can you add some external components to feed some noise into an input pin? n.b. You don't need #include #include in your source. Most embedded programs don't need them at all. |
|
|
|
至于增加噪音,我不能肯定是否在我的价格范围内,它的可能性,但我也试图保持尽可能低的预算,因为我想复制一个项目的x1000。经过更多的研究,我能够找到这个.http://www..hip.com/for./m138719.aspxanoth。在最后一篇博文中,我有一个类似的问题,我完全不理解。我也想出了一个主意,但是因为我没有真正的方法来运行代码某种类型的控制台,以查看我的输出是什么,所以我不能真正实现它。但是由你来运行它。所以_u._ms(1000)在某个时钟上运行,我猜它与_XTAL_FREQ有关,因为如果没有它,我不能运行我的延迟。所以,如果我用任何时钟来延迟我们,最后两个数字,会给我一个0-99的数字,然后我可以把它模数给我一个随机数,我想。对不起新手问题,提前谢谢你。
以上来自于百度翻译 以下为原文 as far as adding noise i'm not to sure if its within my price range and its a possibility but i'm also trying to keep this as low budget as possible because i want to duplicate it x1000 for a project. after doing more research i was able to find this. http://www.microchip.com/forums/m138719.aspx another person having a similar issues the very last post there i'm not understanding at all. i also came up with an idea but since i have no real way to run the code some type of console to see what my outputs are i cant really implement it. but ill run it by you. so __delay_ms(1000) runs on some clock i'm guessing it has something to do with _XTAL_FREQ because with out that i cant run my delay. so if i take whatever clock its using for the delay us the last two number that will give me a number from 0-99 and then i can modulo it to give me a random number i desire. sorry for the newbi questions and thank you in advance. |
|
|
|
|
|
|
|
内置的_u._ms()是一个宏,它通过计算指令周期(基本上)来等待指定的毫秒数(大约),因此不会产生“随机数”(没有要读取的时钟计数器)。要获得随机数,您必须使用伪随机算法,或者引入不依赖于系统时钟的东西(外部源通常最简单)。如果与产品存在人工交互,那么通常可以测量该交互时间(按下按钮多长时间等),并将其作为随机数/种子使用,可能产生良好的效果。
以上来自于百度翻译 以下为原文 The built-in __delay_ms() is a macro that just waits the specified number of milliseconds (approximately) by counting instructions cycles (basically) so this will not produce a "random number" (there is no clock counter to read). To get a random number you must either use a pseudo random algorithm or introduce something that is not dependent on the system clock (external source is usually easiest). If there is human interaction with the product then you usually can measure that interaction time (how long a button is pressed, etc) and use that as a random number/seed with possibly good results. |
|
|
|
这是使用RAND()函数,你说你没有足够的ROM空间来运行。它使用主CPU时钟,它运行你的指令,所以它不会是随机的,它每次都会产生相同的数字。这就是为什么我说,你需要一些与你无关的外部事件。R内部执行,因为每次运行时都会做同样的事情。
以上来自于百度翻译 以下为原文 That's using the rand() function, which you say you don't have enough ROM space to run. It's using the main cpu clock, which what is running your instructions, so it won't be random, it will generate the same numbers every time. That's why I said, you need some external event which is NOT tied to your internal execution, because that will do the same thing every time you run it. |
|
|
|
HiI的确(Google)并且以thishttp://www..hip.com/for./m936108.aspx963173;I结尾。
以上来自于百度翻译 以下为原文 Hi I did (Google) and ended up with this http://www.microchip.com/forums/m936108.aspx#963173 Its quite easy to understand and translate or can simply be inlined in "C". HIH Best regards Jorga |
|
|
|
我不明白你给我的这个链接的代码我不熟悉汇编。“C”中的内联是什么意思?
以上来自于百度翻译 以下为原文 i do not understand the code in this link you gave i am not familiar with assembly. what do you mean by inlined in "C"? |
|
|
|
尝试一个较新的8针PIC16S(或在较旧),它会更便宜,有更多的内存。
以上来自于百度翻译 以下为原文 Try one of the newer 8 pin PIC16s ( or on that is less old) It will be cheaper and have more memory. |
|
|
|
对于c中的LFSR实现,请尝试这个链接:https://gist.github.com/rcc/380978它可能优化代码和/或内联它。链接上的代码是非常通用的,并具有许多不同大小的结果(以位为单位)的功能。可能有一些用途…
以上来自于百度翻译 以下为原文 For an LFSR implementation in c try this link: https://gist.github.com/rcc/380978 It may be possible to optimize the code and/or inline it. The code at the link is very general use and has functions for many different size results (in bits). May be of some use..... |
|
|
|
PIC12F508:1000关闭量:0.43美元-0.54速度:4MHZROM:512 WordsRAM:25字节:1x8位PWM:非硬件堆栈:2级SPIC12F15711000关量:0.41美元-0.5亿速度:32兆赫兹:1024字节:4x10位:2x8位,4x16位PWM:3x16双硬件堆栈:16级
以上来自于百度翻译 以下为原文 PIC12F508: 1,000 off quantities: $0.43 - $0.54 Speed: 4MHz ROM: 512 words RAM: 25 bytes ADC: None Timers: 1x8-bit PWM: None Hardware stack: 2 levels PIC12F1571 1,000 off quantities: $0.41 - $0.59 Speed: 32MHz ROM: 1024 words RAM: 128 bytes ADC: 4x10bit Timers: 2x8-bit, 4x16-bit PWM: 3x16-bit Hardware stack: 16 levels |
|
|
|
在“C”中嵌入ASM意味着在“C”代码的中间放置ASM语句。例如,相同的代码,但是位于“C”过程内。注意:此代码不适用于XC8编译器。但是如果您不熟悉ASM,那么也许您不应该尝试这种方法。其他人已经指出了一种更简单的解决方案。还有,找一个更好的PIC和一个“C”中的现成的LFSR。总之,在像12F508这样的PIC中,你不能用“C”编译器填很多东西。我用了很多,上次在不久以前,我甚至没有考虑过“C”的使用。
以上来自于百度翻译 以下为原文 Hi Inlining ASM in 'C' means to place ASM statements in the middle of the 'C' code. Like this for example, the same code but inside a "C" procedure. NOTE: This code is not for the XC8 compiler. /* ; ------------------------------------------------------------------------------- ; Subroutine to extract the next pseudo random value ; Call each time a new random number is needed ; Upon return use the value at RandomL for an 8bit random ; or the pair RandomH-RandomL for a 16 bit one. */ //; Pseudo Random Generator void GaloisLFSR_Next(void) { // Galois LFSR generator (Mark Jeronimus of Digital Mosular http://www.piclist.com/techref/microchip/rand8bit.htm) asm{ bcf STATUS, C ; Clear carry flag rrf _RandomH, F rrf RandomL, F btfss STATUS, C goto GLFSR_1 movlw 0xa1 ; Galois LFSR polynom is 0xa1a1 xorwf RandomH, F xorwf RandomL, F GLFSR_1: } // asm } But if you are not familiar with ASM, maybe you should not try this way. Others have pointed to an easier solution, get a better PIC and a ready made LFSR in 'C'. Anyhow, in a PIC like the 12F508 you can't stuff much using a 'C' compiler. I've used plenty of those, the last time it wasn't long ago, but never even considered using 'C'. HIH Best regards Jorge |
|
|
|
只有小组成员才能发言,加入小组>>
5166 浏览 9 评论
2000 浏览 8 评论
1928 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3174 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2226 浏览 5 评论
734浏览 1评论
615浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
506浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
631浏览 0评论
528浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-24 17:31 , Processed in 1.551107 second(s), Total 98, Slave 82 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号