首页 > 解决方案 > “错误:'_asm':未定义的标识符”问题

问题描述

我已经编写了代码以允许 8051 微控制器能够与 LCD 连接并在 LCD 上显示读取为 8 位二进制值的温度。该代码可以在 LCD 上显示文本并且能够显示文本,但是,当引入代码以允许读取温度时,程序会中断。该错误似乎集中在从 ADC 读取温度的函数周围,并且似乎与我以前从未处理过的“_asm”有关。下面列出了错误:

compiling main.c...
main.c(72): error C231: 'get_temp': redefinition
main.c(74): error C141: syntax error near 'int', expected '__asm'
main.c(74): error C202: 'readvalue': undefined identifier
main.c(75): error C141: syntax error near 'int', expected '__asm'
main.c(75): error C202: 'temp': undefined identifier
main.c(76): error C141: syntax error near 'int', expected '__asm'
main.c(76): error C202: 'binaryvalue': undefined identifier
main.c(76): error C141: syntax error near '{', expected 'sizeof'
main.c(76): error C141: syntax error near '}', expected ';'
main.c(77): error C141: syntax error near 'int', expected '__asm'
main.c(77): error C141: syntax error near '{', expected 'sizeof'
main.c(82): error C141: syntax error near '}', expected ';'
main.c(83): error C141: syntax error near '{', expected 'sizeof'
main.c(83): error C141: syntax error near ')', expected ';'
main.c(85): error C202: 'temp': undefined identifier
main.c(88): error C202: 'temp': undefined identifier
main.c(89): error C231: 'get_temp': redefinition
main.c(118): error C141: syntax error near 'int', expected '__asm'
main.c(118): error C202: 'atemp': undefined identifier
main.c(119): error C202: 'atemp': undefined identifier
main.c(120): error C202: 'atemp': undefined identifier

该代码已在 Keil uVision 中开发,如下所示。函数原型被注释掉,因为它们会导致程序出现进一步的问题,并且在注释掉时,一些错误得到了解决:

#include<reg51.h>
#include<stdio.h>

/********************************************* LCD control signals declaration ***************************************************/
 
sbit RS = P0^0;     //
sbit RW = P0^1;  // Read/write line
sbit Enable = P0^2; // Enable line
#define LCD_PORT P2 // define port
 
/********************************************* LCD function prototypes ************************************************/
//void send_cmd(unsigned char);
//void send_char(unsigned char);
//void send_int()
//void LCD_init(void);
//void delayms(unsigned int);
//int get_temp();
////int get_humidity();

/******************************************* delayms Function declaration***********************************************/
void delayms(unsigned int val)
{
 unsigned int i,j;
   
 for(i=0;i<=val;i++)
 {
  for(j=0;j<=2;j++)  ;
  //_nop_();   // no operation produce 1us time delay
 }
  
}
/*********************************************LCD Command Sending Function declaration********************************/
 
void send_cmd(unsigned char Command)
{
 LCD_PORT = Command;
 RS = 0;      // Select Command Register
 RW = 0;    // write operation
 Enable = 1;      // High to Low pulse provided on the enable pin with nearly 1ms(>450ns)
 delayms(1);   // 1 millisec delay
 Enable = 0;
}
 

 

/********************************************* LCD Initialization Function declaration ********************************/
 
void LCD_init()
{
 send_cmd(0x38);      // configuring LCD as 2 line 5x7 matrix
 send_cmd(0x0E);      // Display on, Cursor blinking
 send_cmd(0x01);      // Clear Display Screen
 send_cmd(0x06);      // Increment Cursor (Right side)
}
 
void send_char(unsigned char character)
{
 LCD_PORT = character;
 RS = 1;    // Select Data Register
 RW = 0;    // write operation
 Enable = 1;      // High to Low pulse provided on the enable pin with nearly 1ms(>450ns)
 delayms(1);   // 1 millisec delay
 Enable = 0;
 delayms(10);   // 100 millisec delay
}
 
 


/****************************************** Temperature Function Declaration ********************************************/
void get_temp(){
    P1 = 0xFF;
    int readvalue = P1;
    int temp;
    int binaryvalue[70] = {01000001,00100001,01100001,00010001,10010001,11010001,10110001,11110001,00001001,01001001,00101001,01101001,11101001,10011001,01011001,00111001,01111001,11111001,10000101,01000101,00100101,10100101,01100101,00010101,10010101,11010101,00110101,01110101,01110101,00001101,10001101,01001101,11001101,00101101,01101101,01101101,11101101,00011101,01011101,01011101,11011101,00111101,10111101,01111101,11111101,11111101,00000011,10000011,10000011,01000011,11000011,00100011,00100011,10100011,10100011,01100011,11100011,11100011,11100011,00010011,00010011,10010011,10010011,01010011,01010011,11010011,11010011,11010011,00110011,00110011,10110011};
    int temperature[70] = {-20,-19,-18,-17,-16,-15,-14,-13,
                                            -12,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,
                                            0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,
                                            15,16,17,18,19,20,21,22,23,24,25,26,
                                            27,28,29,30,31,32,33,34,35,36,37,38,
                                            39,40,41,42,43,44,45,46,47,48,49,50};
    for(i=0;i<sizeof{binaryvalue};i++){
        if(readvalue == binaryvalue[i]){
            temp = temperature[i];
        }
    }
    return temp;
}

/********************************************* Main Funciton declaration ***********************************************/
void main()
{

    LCD_PORT = 0x00; // Make the port as output port
   
    LCD_init();            // LCD initialization
  send_cmd(0x80);     // Force cursor to beginning of 1st line, if the number is 0x83 then force the cursor to 53rd position
  delayms(100);    // Delay of 100millisec
  send_char('T');
    send_char('H');
    send_char('E');
    send_char('R');
    send_char('M');  // Send data
    send_char('I');
    send_char('S');
    send_char('T');
    send_char('O');
    send_char('R');  // Send data   
  send_cmd(0xC0);     // Force cursor to beginning of 2nd line
  delayms(100);    // Delay of 100millisec
  send_char('V');  // Send data 
  send_char('A');  // Send data 
  send_char('L');  // Send data 
    send_char('U');  // Send data 
  send_char('E');  // Send data 
  send_char(':');  // Send data
    int atemp = get_temp();
    while (atemp > 0){
        int k = atemp % 10;
        char send_x;
        itoa(k,send_x,10);
        send_char(send_x);
        temp /= 10;
    }
 //}
}
 

任何帮助将不胜感激,因为我在 C 方面的知识有限。提前致谢。

标签: cmicrocontroller

解决方案


_asm标记内联汇编,正确的语法取决于您使用的机器('asm'、'__asm' 和 '__asm__' 之间有什么区别?)。我不知道您从哪里获得带有 LCD 驱动程序代码的库,但 Keil 中的内联汇编在https://www.keil.com/support/man/docs/armcc/armcc_chr1359124246903.htmhttps://www中有描述.keil.com/support/man/docs/c51/c51_ap_ctoasm.htm(适用于 8051)

错误在行中

void get_temp(){
    P1 = 0xFF;
    _asm int readvalue = P1;
    _asm int temp;

我认为您想读取或修改 P1 特殊功能寄存器(SFR),因为它与您的温度传感器有连接,为此请参阅https://www.includehelp.com/embedded-system/accessing-ports-and-pins- in-8051-microcontroller.aspx

此外,您尝试将 8 位值转换int为至少 16 位的值...

此外,您尝试写入/读取 P1 的锁存器,而不是读取其值(https://developer.arm.com/documentation/ka004317/latest


推荐阅读