完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
PIC16F87A;4x4矩阵键盘;2×16 LCD。现在,一切都很好,它工作!我在网上查过,没有资料来源,希望有用。
以上来自于百度翻译 以下为原文 /* KEYPAD INTERFACING WITH PIC16F877A */ char kpi; kpi1; kpi2; total1; total; counter; remainder; char lookup_table[] = " 741 8520963=/*-+" ; int i ; b; // Keypad module connections char keypadPort at PORTD; // End Keypad module connections // LCD Module connections ***it LCD_RS at RB2_bit; ***it LCD_EN at RB3_bit; ***it LCD_D7 at RB7_bit; ***it LCD_D6 at RB6_bit; ***it LCD_D5 at RB5_bit; ***it LCD_D4 at RB4_bit; // End LCD module connections // LCD Pin direction ***it LCD_RS_Direction at TRISB2_bit; ***it LCD_EN_Direction at TRISB3_bit; ***it LCD_D7_Direction at TRISB7_bit; ***it LCD_D6_Direction at TRISB6_bit; ***it LCD_D5_Direction at TRISB5_bit; ***it LCD_D4_Direction at TRISB4_bit; // End of LCD Pin direction void main() { Lcd_Init(); // Initialize LCD Lcd_Cmd(_LCD_CLEAR); // Clear Display Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor Off Lcd_Out(1,1,"BINARYCONVERSION"); // Write "KEYPAD INTERFACE" in the first row delay_ms(500); // Delay of 0.5s Lcd_Cmd(_LCD_CLEAR); // Clear Display delay_ms(500); // Delay of 0.5s Keypad_Init(); // Initialize Keypad Lcd_Out(1,1,"PRESS A NUMBER"); // Write "PRESS A KEY" in the first row delay_ms(500); // Delay of 0.5s Lcd_Cmd(_LCD_CLEAR); // Clear Display do { kpi = 0; // Reset key code variable // Wait for key to be pressed and released do kpi = Keypad_Key_Click(); // Store key code in kpi variable while (!kpi); if (kpi <= 16) { kpi = lookup_table[kpi]; } Lcd_Chr(1, 2, kpi); // Print key ASCII value on Lcd kpi1= 0; // Reset key code variable // Wait for key to be pressed and released do kpi1 = Keypad_Key_Click(); // Store key code in kpi variable while (!kpi1); if (kpi1<= 16) { kpi1= lookup_table[kpi1]; } Lcd_Chr(1, 3, kpi1); // Print key ASCII value on Lcd kpi2 = 0; do kpi2 = Keypad_Key_Click(); while(!kpi2); if(kpi2<= 16){ kpi2 = lookup_table[kpi2]; } Lcd_Chr(1, 4, kpi2); kpi &= 0x0F; kpi1 &= 0x0F; kpi2 &= 0x0F; total = 0; total = (kpi*100) + (kpi1*10) + kpi2 ; for (b = 8; b != 0; b--) //loop 8 times { { remainder = total%2; total = total/2; if (remainder==1) { Lcd_Chr(2,b,'1'); } else Lcd_Chr( 2,b,'0'); } } delay_ms(3000); Lcd_Cmd(_LCD_CLEAR); // Clear Display } while(1); } Pic16f877a; 4x4 Matrix Keypad; 2*16 LCD. Now , everything is fine and it works ! I checked on internet and there is no source for this , i hope it will be useful. Regards. |
|
相关推荐
12个回答
|
|
键的值是否正确地在第一行上正确地输出?(例如,当按下4时,7行第一行输出:47)?也有可能调试和查看当执行if语句时KPI和KPI1的值是什么?这些可能有助于弄清楚为什么陈述没有评估你所期望的方式。
以上来自于百度翻译 以下为原文 Do the values of the keys pressed output properly on the first row? (e.g. when you press 4 then 7 does the first row output: 47)? Also is it possible to debug and see what the values of kpi and kpi1 are when the if statement is executed? These may help figure out why the statement is not evaluating the way you expect. |
|
|
|
重新阅读你的代码,我想我发现了这个问题。在您的第一套开关语句和写KPI到LCD之后,您重置KPI。我相信您打算在这里重置kpi1,因为这个语句在命中if语句时使kpi总是等于零,所以它总是被评估为false。可能的意图是:
以上来自于百度翻译 以下为原文 Re-reading your code, I think I found the issue. After your first set of switch statements and writing kpi to LCD, you reset kpi. ... Lcd_Chr(1, 2, kpi) ; //Print key ASCII value on LCD kpi = 0; //Reset key code variable //wait for key code to be pressed and released ... I believe you intend to be resetting kpi1 here, as this statement will make kpi always equal to zero when you hit your if statement, so it will always evaluate as false. What was likely intended was: ... Lcd_Chr(1, 2, kpi) ; //Print key ASCII value on LCD kpi1 = 0; //Reset key code variable //wait for key code to be pressed and released ... |
|
|
|
有一个很简单的原因,你的测试总是失败。这条线:所以你在测试之前正在清除KPI!对编码风格也有一点建议。为什么要使用ASCII字符的十进制值,强迫您添加注释来说明它们是什么?这是更多的工作,而且容易出错。直接使用它们。例如
以上来自于百度翻译 以下为原文 There's a very simple reason your test always fails. This line: So you are clearing kpi before you get to the test! Also a little advice on coding style. Why use decimal values for ASCII characters, forcing you to add comments to say what they are? That is more work, and error prone. Just use them directly. e.g. switch (kpi) { case 1: kpi = '7'; break; case 2: kpi = '4'; break; case 3: kpi = '1'; break; case 4: kpi = ' '; break; // Space case 5: kpi = '8'; break; case 6: kpi = '5'; break; case 7: kpi = '2'; break; case 8: kpi = '0'; break; case 9: kpi = '9'; break; case 10: kpi = '6'; break; case 11: kpi = '3'; break; case 12: kpi = '='; break; case 13: kpi = '/'; break; case 14: kpi = 'x'; break; case 15: kpi = '-'; break; case 16: kpi = '+'; break; } |
|
|
|
OMG THX,现在我检查了代码,我看到我忘了把1的KPI重置KPI1,谢谢NJT。
以上来自于百度翻译 以下为原文 omg thx , now i checked the code and i see that i forgot putting 1 to kpi for resetting kpi1 , thanks NJT. |
|
|
|
或者在文件开始时,put:而不是switch语句,执行以下操作:(由您决定是否需要测试以确保永远不会读出数组的末尾。)p.s.您没有提到您正在运行这个PIC。如果您缺少RAM,通过将声明修改为:p.s.2不要在8位MCU上使用“in t”变量,在“char”或“unsigned char”的作用下。
以上来自于百度翻译 以下为原文 Or this code will be smaller and faster At the start of your file, put: char lookup_table[] = " 741 8520963=/*-+"; and instead of the switch statement, do this: if (kpi <= 16) kpi = lookup_table[kpi]; (It's up to you to decide if the test is required to ensure you never read beyond the end of the array.) p.s. You didn't mention what PIC you are running this on. If you're short on RAM, you can force the lookup table to stay in ROM by modifying the declaration to: const char lookup_table[] = " 741 8520963=/*-+"; p.s.2 Don't use "int" variables on an 8 bit MCU where a "char" or "unsigned char" will do. |
|
|
|
首先,您需要将两个小数转换为单个值。假设您首先为每个数字创建vraiables,以保存原始值,而不是ASCCI值。然后您只需要将它们组合如下:0和1位每次进入ASCII“0”和“1”。
以上来自于百度翻译 以下为原文 First you need to convert your two decimals to a single value. Assuming you first create vraiables for each digit, to hold the raw value, not the ASCCI value. char tens; //raw value of first digit 0-9 char ones; //raw value of second digit 0-9 char total; //total value 0-99 then you just need to combine them as follows: total = (tens * 10) + ones; Then you need to extract the individual bits from the "total" variable one at a time, converting the zero and one bits into ASCII "0" and "1" each time. Something like: char counter; //scratrch variable to count 8 bits for (counter = 8, counter != 0; counter--) //loop 8 times { if (total & 0b10000000) lcd_putc('1'); //the bit was 1 else lcd_putc('0'); //the bit was 0 total <<= 1; //shift the value in "total" left one bit } //end of loop |
|
|
|
此外,您在代码中有一个错误。我用我的方式说你需要乘法步骤的“原始”值。你使用的是ASCII值。我特别说过不要这样做。如果您知道您只有数字(“0”-“9”),那么可以通过使用0x0Fe.g.kpi&=0x0F简化ANDing来将ASCII转换为.;但是您有其他值,如“+”、“-”、“=”等,这将使所有这些复杂化。
以上来自于百度翻译 以下为原文 Also, you have a mistake in that code. I went out of my way to say you need "raw" values for the multiplication step. You are using the ASCII values. I specifically said to not do that. If you know that you only have digits ("0" - "9"), then ASCII can be converted to raw by simplay ANDing with 0x0F e.g. kpi &= 0x0F; but you have other values like "+", "-", "=" etc. which will complicate all this. |
|
|
|
是的。就像我说的,如果你知道它只是从“0”到“9”的数字,那么它就可以正常工作。而且,我们只使用8位作为结果,所以不要尝试大于255的数字!
以上来自于百度翻译 以下为原文 Yes. As I said, if you know it's just digits from "0" to "9", then that will work fine. Also, we're only using 8 bits for the result, so don't try a number bigger than 255 ! |
|
|
|
怎么了?我看不到LCD第二行的任何值。
以上来自于百度翻译 以下为原文 /* KEYPAD INTERFACING WITH PIC16F877A */ char kpi; kpi1; kpi2; total1 ; int i; remainder; total; binary; binary1; char lookup_table[] = " 741 8520963=/*-+" ; // Keypad module connections char keypadPort at PORTD; // End Keypad module connections // LCD Module connections ***it LCD_RS at RB2_bit; ***it LCD_EN at RB3_bit; ***it LCD_D7 at RB7_bit; ***it LCD_D6 at RB6_bit; ***it LCD_D5 at RB5_bit; ***it LCD_D4 at RB4_bit; // End LCD module connections // LCD Pin direction ***it LCD_RS_Direction at TRISB2_bit; ***it LCD_EN_Direction at TRISB3_bit; ***it LCD_D7_Direction at TRISB7_bit; ***it LCD_D6_Direction at TRISB6_bit; ***it LCD_D5_Direction at TRISB5_bit; ***it LCD_D4_Direction at TRISB4_bit; // End of LCD Pin direction void main() { Lcd_Init(); // Initialize LCD Lcd_Cmd(_LCD_CLEAR); // Clear Display Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor Off Lcd_Out(1,1,"BINARYCONVERSION"); // Write "KEYPAD INTERFACE" in the first row delay_ms(500); // Delay of 0.5s Lcd_Cmd(_LCD_CLEAR); // Clear Display delay_ms(500); // Delay of 0.5s Keypad_Init(); // Initialize Keypad Lcd_Out(1,1,"PRESS A NUMBER"); // Write "PRESS A KEY" in the first row delay_ms(500); // Delay of 0.5s Lcd_Cmd(_LCD_CLEAR); // Clear Display do { kpi = 0; // Reset key code variable // Wait for key to be pressed and released do kpi = Keypad_Key_Click(); // Store key code in kpi variable while (!kpi); if (kpi <= 16) { kpi = lookup_table[kpi]; } Lcd_Chr(1, 2, kpi); // Print key ASCII value on Lcd kpi1= 0; // Reset key code variable // Wait for key to be pressed and released do kpi1 = Keypad_Key_Click(); // Store key code in kpi variable while (!kpi1); if (kpi1<= 16) { kpi1= lookup_table[kpi1]; } Lcd_Chr(1, 3, kpi1); // Print key ASCII value on Lcd kpi2 = 0; do kpi2 = Keypad_Key_Click(); while(!kpi2); if(kpi2<= 16){ kpi2 = lookup_table[kpi2]; } Lcd_Chr(1, 4, kpi2); kpi &= 0x0F; kpi1 &= 0x0F; kpi2 &= 0x0F; total = 0; total = (kpi*100) + (kpi1*10) + kpi2; binary = 0; i = 1; if ( total <=255 );{ do { remainder = (total % 2); total = (total/2); binary = binary + (remainder*i) ; i = (i*10); } while(total != 0); } ByteToStr(binary, total1); Lcd_Out(2, 1, total1); delay_ms(3000); Lcd_Cmd(_LCD_CLEAR); // Clear Display } while(1); } What is wrong ? I can not see any value on LCD'S second row. |
|
|
|
BytToStTo()是做什么的?为什么要丢掉我为您编写的二进制输出代码,这样做可以正常工作,然后用thisWhites替换它?
以上来自于百度翻译 以下为原文 What does ByteToStr() do? Why did you throw away the binary output code I wrote for you, that would work, and replace it with this do { remainder = (total % 2); total = (total/2); binary = binary + (remainder*i) ; i = (i*10); } while(total != 0); } Which is rubbish. Maybe if you added some comments to your code, explaining what it is trying to do, you will see it won't do anything useful. |
|
|
|
|
|
|
|
感谢您的帮助QHB!如果你生我的气,我很抱歉,我知道有时我不明白,因为我已经醒了20小时了,对不起。如果你不说kpi&=0X0F,我就不能。我编辑了第一篇文章,希望有人能利用这一点。
以上来自于百度翻译 以下为原文 Thanks for your helps qhb! if you mad on me i am sorry , i know sometimes i did not understand because i have been awake for 20hours , sorry . if you did not say kpi&=0X0F i could not do. I editted first post , i hope someone can utilize from this. |
|
|
|
只有小组成员才能发言,加入小组>>
5212 浏览 9 评论
2019 浏览 8 评论
1944 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3192 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2246 浏览 5 评论
760浏览 1评论
647浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
567浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
659浏览 0评论
557浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-11 22:33 , Processed in 1.777797 second(s), Total 103, Slave 85 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号