因此,基本上,我正在编写代码来通过微控制器控制LCD。(atmega 32)我的main方法中包含以下内容:
unsigned char str1[9] = "It Works!";
sendString(str1);下面是我的sendString方法:
// Converts each char to hex and sends to LCD
void sendString(unsigned char *string){
sendCommand(0x01); // Clear screen 0x01 = 00000001
_delay_ms(2);
sendCommand(0x38); // Put in 8-bit mode
_delay_us(50);
sendCommand(0b0001110); // LCD on and set cursor off
_delay_us(50);
//For each char in string, write to the LCD
for(int i = 0; i < sizeof(string); i++){
convertASCIIToHex(string[i]);
}
}然后,sendString方法需要转换每个字符。这是我到目前为止所知道的:
unsigned int convertASCIIToHex(unsigned char *ch)
{
int hexEquivilent[sizeof(ch)] = {0};
for(int i = 0; i < sizeof(ch); i++){
// TODO - HOW DO I CONVERT FROM CHAR TO HEX????
}
return hexEquivilent;
}那么我该如何进行转换呢?我对C语言完全陌生,正在慢慢地学习。我感觉这一切都错了,因为我在某处读到,char实际上是以8位整数的形式存储的。如何让我的方法返回每个输入字符的十六进制值?
发布于 2015-01-27 02:43:38
在C中,char是一个8位带符号的整数,你可以用十六进制来表示它。在下面的代码行中,a、b和c具有相同的值,即一个8位整数。
char a = 0x30; //Hexadecimal representation
char b = 48; //Decimal representation
char c = '0'; //ASCII representation我认为你需要的只是发送字符串的字符,而不是任何十六进制的转换。一个问题是您不能使用sizeof()来获取字符串的长度。在C中,字符串以NULL结尾,所以您可以迭代它,直到找到它。试试这个:
// Converts each char to hex and sends to LCD
void sendString(unsigned char *string){
sendCommand(0x01); // Clear screen 0x01 = 00000001
_delay_ms(2);
sendCommand(0x38); // Put in 8-bit mode
_delay_us(50);
sendCommand(0b0001110); // LCD on and set cursor off
_delay_us(50);
//For each char in string, write to the LCD
for(int i = 0; string[i]; i++){
sendCommand(string[i]);
}
}https://stackoverflow.com/questions/28156188
复制相似问题