在C中分割半字节时卡住 [英] stuck when splitting nibbles in C
问题描述
嘿,我想知道是否有人可以发现我的错误...
我要做的就是在C中的4位数据线上发送一个字节(也带有使能线).它相当繁琐,因为如果我发送上半字节或下半字节(具有更长的延迟和一些LED),则两个半字节的数据都会按计划输出.当我尝试在switch语句中连续发送它们时,事情似乎有点像梨形...这里是代码...
Hey guys, I was wondering if somebody could spot my mistake(s)...
All i''m trying to do is send a byte over a 4 bit data line in C(also with an enable line). Its quite fustrating because If I send EITHER the upper or lower nibble (with a longer delay and some LEDs) the data of both nibbles comes out as planned. Its when I try to send them consecutively in a switch statement things seem to go a bit pear shaped...and heres the code...
case 0x02: //case 0x02 from firmware
IDENT_PORT = 0X00;
j = 0x16; // bit mask ie, & with 1111 0000 to clear bottom nibble
upper_nibble = j & 0x0f0; // 0001 0110
PORTB = upper_nibble; // present upper nibble on port b
RB1 = 1; // pulse enable line
DelayUs(100);
RB1 = 0;
DelayUs(100);
k = (j << 4) | (j >> 4); // swap upper and lower nibbles of 0x16
lower_nibble = k & 0x0f0; // again bit mask to leave the lower nibble (0110)
PORTB = lower_nibble; // put on port
RB1 = 1;
DelayUs(100); // pulse enable line
RB1 = 0;
DelayUs(100);
PORTB = 0x00; // clear port
break;
如果我可以提供其他任何信息,请告诉我.
任何建议都会被采纳,因为我要开始撞墙了!
谢谢
M.
If I can provide any other info PLEASE let me know.
Any suggestions would be appriciated, as I''m about to start banging my head against a wall!
Thanks
M.
推荐答案
类似您的代码的声音告诉字节发送,但不等待它们,因此如果您不停顿它们会发生冲突.
Sounds like your code tells the bytes to send, but does not wait for them, and so they collide if you don''t pause.
这篇关于在C中分割半字节时卡住的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!