完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
嗨,第一篇文章,我是一个退休的OLDIMER,我一直在使用模拟电子产品,(虽然我在很久以前做了一些编程),但是我现在正在尝试PIC处理器的运气,并且正在慢慢地尝试它是如何工作的。我现在要做的是让PIC运行2。不同的进程取决于是否按下一个按钮,这一次我用一个PUBB改变中断来管理,但我的问题是,我想要一个这样的功能:在PIC上,PIC应该执行进程1,只要不按下按钮,就继续这样做。按钮2秒或更长的时间,转到过程2,只要按钮在PISSEDIF按钮上停留5秒以上,PIC就应该离开流程2,回到流程1I,几乎可以做到这一点,除了简单的部分,我想有不同的时间进入PR。Oxess 1比退出进程1。附加的简单程序将切换到2个连接在PORTC位0和1上的LED,这取决于连接到RB4的按钮是否连接到VSS或VDD,(它已经拉到VDD),但是在转换到LED 0之前和在BAC之前的时间需要2秒。K到LED 1。我的意图是,当按下按钮后,它应该立即改变,然后在2秒后返回。它为什么不能按预期工作的原因可能很简单,但我想不出来。当RB4从高到低变为D时,应该设置为1,从而开启LLE。即时RC0为什么不呢?非常感谢任何Helpab73.CPIC使用的是16F690HANS。
以上来自于百度翻译 以下为原文 Hi, First post here, I am an retired oldtimer that has been working with analog electronics all my life, (although I did some programming long time ago), I am now trying my luck with PIC processors and are slowly trying to understand how it works. What I am trying to do now is to make a PIC running 2 different processes depending on if a push button is pushed or not, this I have alreday managed using a PORTB change interrupt but my problem is that I want a function like this:
I have almost managed to do this except for the simple part that I want to have different times for going into process 1 than going out of process 1. The attached simple program will switch on 2 LEDs connected at PortC bit 0 and 1 depending on if the pushbutton connected to RB4 is connected to VSS or VDD, (it has pullup to VDD) but it takes 2 second before changing to LED 0 and the same time before it goes back to LED 1. My intention was that it should change instantly when the push button is pressed and then go back after 2 seconds. The reason why it doesn't work as intended is probably very simple but I can't figure it out. When RB4 is going from high to low variable D should be set to 1 and thereby switching on LED RC0 instantly why doesn't it? Many thanks for any helpLab73.C PIC used is 16F690 Hans Attachment(s) Lab73.C (5.14 KB) - downloaded 54 times |
|
相关推荐
2个回答
|
|
只是一些评论。不包括Stdit.h,你没有使用任何东西。不包括HTC。H。这是旧HiTeC C编译器的残余(XC8是基于),并且被XC.HAWAL用单字母变量名覆盖。这使得代码很难让其他人理解,并且意味着你不能轻易地在代码中搜索它们。任何写在ISR内部并在ISR外部读取的变量都必须用“易失性”限定符来声明。hifting LEDsThis告诉编译器,除了C指令的正常流程之外,还可以改变这个变量。这可能是当前代码中的主要问题。请确保你的注释反映了你的代码。现在是大人物,这是你构造程序的全部方式。在ISR中放置延迟是非常糟糕的做法,而“改变”中断实际上不是读取按钮的好方法。它使得运行常规定时器中断更容易,并轮询按钮内的按钮。每个毫秒都会很好。如果你需要一个5ms的延迟,在做某事之前只需数到5个中断。再加上一点工作,这项技术会使你所有的时间延迟更加精确和有用,因为你可以同时做其他事情而不影响时间延迟。
以上来自于百度翻译 以下为原文 Just a couple of comments. Don't include stdint.h, you're not using anything from it. Don't include htc.h. That's a remnant of the old HiTech C compiler (that XC8 is based on), and is covered by xc.h Avoid using single letter variable names. That really makes the code hard for others to understand, and means you can't easily search for them in the code. Any variable which is written to inside an ISR, and read outside the ISR, must be declared with the "volatile" qualifier. e.g. volatile unsigned char D; //Variable to select direction of shifting LEDs This tells the compiler something other than the normal flow of C instructions can change this variable. The lack of this is probably the main problem in your current code. Make sure your comments reflect your code. e.g. //Read PORTA to latch the current RA2 voltage level PORTB = PORTB; Now the biggie, which is the whole way you have structured your program. It's very bad practice to put delays inside an ISR, and "change" interrupts are in fact NOT a good way to read pushbuttons. It makes life much easier to run a regular timer interrupt, and poll the pushbutton inside that interrupt. One every millisecond would be good here. If you need a 5ms delay, just count 5 interrupts before doing something. With a little more work, this technique makes all your time delays more accurate and useful too, as you can be doing other things at the same time without affecting the time delay. |
|
|
|
非常感谢您的宝贵帮助!我知道在中断过程中延迟是不好的,我之所以这样做,是因为我从一个微芯片教程“PicDENM实验室开发板用户指南”,41369A.PDF中复制了一些例子。也许微电脑用这种方式教我们不好?关于声明变量作为易失性,我不知道,在上面的指南中没有提到,现在我读了之后,我明白了为什么你需要这个易失性语句,但遗憾的是它并不是在初学者文本中关于中断的使用而写的(但是我发现了非常清楚的信息)。XC8用户指南中的RMIN。现在我有一些作业要做,重写这个代码和我正在做的其他工作,而不是使用定时器中断和轮询,而不是我一直在做的事情。再次感谢你们!汉斯
以上来自于百度翻译 以下为原文 Thank you very much for your valuable help! I somehow knew it was not good to put delays inside an interrupt process, the reason I did it was that I copied some example from a Microchip tutorial "PICDEM™ Lab Development Board User’s Guide", 41369A.pdf. maybe it is not good that Microchip teach us this way? Regarding declaring variables as volatile I didn't know about this, it is not mentioned in the above guide, now after reading about it I understand why you need the volatile statement but a pity that it is not written in a beginners text about the use of interrupts, (however I found very clear information in the XC8 user guide). Now I have some homework to do, to rewrite this code and some other I am working on and instead using timer interrupts and polling instead of what I have been doing so far. Once again thank you! Hans |
|
|
|
只有小组成员才能发言,加入小组>>
4778 浏览 9 评论
1814 浏览 8 评论
1733 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
2941 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2043 浏览 5 评论
419浏览 1评论
1100浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
343浏览 0评论
253浏览 0评论
LAN9252使用SQI通信,进行数字复位后读BYTE_TEST异常
1751浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-3-28 23:35 , Processed in 1.084720 second(s), Total 76, Slave 60 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 深圳华秋电子有限公司
电子发烧友 (电路图) 粤公网安备 44030402000349 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号