简体   繁体   English

C18将char数组传递给函数

[英]C18 Passing a char array to function

I am new to C programming and microcontrollers. 我是C编程和微控制器的新手。 I am using a PIC18F24K20 microcontroller with C18. 我正在将PIC18F24K20微控制器与C18配合使用。 I have it set up to receive information from a computer input using USART transmit and receive functions. 我已将其设置为使用USART发送和接收功能从计算机输入接收信息。 My goal is to compare the received word against known words, and transmit something back to the computer based on what word was received. 我的目标是将接收到的单词与已知单词进行比较,然后根据接收到的单词将某些内容发送回计算机。 Below is the relevant code. 以下是相关代码。

#include "p18f24k20.h"
#include "delays.h"
#include "string.h"
#include "stdlib.h"


void CommTransmit ( rom char * );

void main (void)
{
    char buf[11], data, T;
    int i;

    i = 0;
    memset(buf, 0, sizeof buf);

    while(1)
    {
        if (PIR1bits.RCIF)
        {
            data = USART_receive();
            if (data != 47)             // 47 is /, indicates end of string
            {
                buf[i] = data;
                i++;
            }
            else
            {
                // T = strcmppgm2ram(buf,(const far rom char*)"test");
                CommTransmit(buf);
                USART_transmit('t');
                buf[0] = 0'
            }
        }
    }
}


void CommTransmit ( rom char *CommVariable )
{
    char test;

    test = strcmppgm2ram(CommVariable, (const far rom char*)"test");
    if (test == 0)
    {
        USART_transmit('g');
    }
}

The code is currently set up to test to try to determine what is wrong. 目前已设置该代码以进行测试,以尝试确定出什么问题。 If I run it as is, the computer will receive a 't', as if the microcontroller ran through the CommTransmit function. 如果我按原样运行它,则计算机将收到一个“ t”,就好像微控制器运行了CommTransmit函数一样。 However, it never transmits the 'g'. 但是,它从不传输“ g”。 Even if I put a USART_transmit('g') call in the CommTransmit function, outside of and after the if statement, it never gets called (like it gets stuck in the strcmppgm2ram function?) but yet it still transmits the 't'. 即使我在ifTrans语句之外和之后在CommTransmit函数中放置了USART_transmit('g')调用,也始终不会调用它(就像它卡在strcmppgm2ram函数中一样?),但它仍会传输't'。

It is also strange because if I put a break at the CommTransmit function and run through line by line, it seems to work properly. 这也很奇怪,因为如果我在CommTransmit函数上稍作休息并逐行运行,它似乎可以正常工作。 However, if I watch the CommVariable inside MPLAB IDE, it is never what it supposed to be (though the 'buf' variable prior to being called into the function is correct). 但是,如果我在MPLAB IDE中观看CommVariable,那绝对不是它应该的样子(尽管在调用函数之前的'buf'变量是正确的)。 From what I can tell, the value of CommVariable when I watch it depends on the size of the array. 据我所知,当我观看CommVariable时,它的值取决于数组的大小。

From reading, I think it may caused by how the microcontroller stores the variable (program vs data memory?) but I'm not sure. 从阅读的角度来看,我认为这可能是由微控制器如何存储变量(程序与数据存储器?)引起的,但我不确定。 Any help is greatly appreciated! 任何帮助是极大的赞赏!

edit: I should also add that if I uncomment the T = strcmppgm2ram line in the else statement before the CommTransmit line, it works properly (T = 0 when the two strings are the same). 编辑:我还应该补充一点,如果我在CommTransmit行之前的else语句中取消注释T = strcmppgm2ram行,它将正常工作(当两个字符串相同时,T = 0)。 I believe the array changes when I pass it through the function, which causes the strcmppgm2ram function to not work properly. 我相信通过函数传递数组时会发生变化,这会导致strcmppgm2ram函数无法正常工作。

Looking at signature for strcmppgm2ram 寻找strcmppgm2ram的签名

signed char strcmppgm2ram(const char * str1, const rom char * str2 );

I don't understand why do you have rom char * for CommVariable . 我不明白您为什么要为CommVariable使用rom char * From chapter 2.4.3 ram/rom Qualifiers of MPLAB® C18 C Compiler User's Guide 从《 MPLAB®C18 C编译器用户指南》的2.4.3节ram / rom限定词开始

Because the PICmicro microcontrollers use separate program memory and data memory address busses in their design, MPLAB C18 requires extensions to distinguish between data located in program memory and data located in data memory. 由于PICmicro单片机在设计中使用单独的程序存储器和数据存储器地址总线,因此MPLAB C18需要扩展以区分程序存储器中的数据和数据存储器中的数据。 /---/ Pointers can point to either data memory (ram pointers) or program memory (rom pointers). / --- /指针可以指向数据存储器(RAM指针)或程序存储器(ROM指针)。 Pointers are assumed to be ram pointers unless declared as rom. 除非声明为rom,否则将其视为ram指针。

And in 2.7.3 String Constants : 2.7.3字符串常量中

An important consequence of the separate address spaces for MPLAB C18 is that pointers to data in program memory and pointers to data in data memory are not compatible. MPLAB C18的单独地址空间的一个重要结果是,程序存储器中的数据指针与数据存储器中的数据指针不兼容。 /---/ because they refer to different address spaces. / --- /,因为它们引用不同的地址空间。 /---/ MPLAB C18 automatically places all string constants in program memory. // --- / MPLAB C18自动将所有字符串常量放入程序存储器。 This type of a string constant is “array of char located in program memory”, (const rom char []) . 字符串常量的这种类型是“位于程序存储器中的char数组”(const rom char [])

And also it's not clear the purpose of type casting to const far rom char* for the second argument. 同样,对于第二个参数,类型转换为const far rom char *的目的也不清楚。 That may cause stack corruption because far pointer has bigger size (24 bits). 这可能会导致堆栈损坏,因为远指针的大小更大(24位)。 So, it looks like it should be rewritten as: 因此,看起来应该将其重写为:

void CommTransmit (const char *CommVariable )
{
    if (!strcmppgm2ram(CommVariable, "test")) {
         USART_transmit('g');
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM