微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在编写代码时摸索并失败工作代码,不适合我

如何解决在编写代码时摸索并失败工作代码,不适合我

我已经安装了 MPLAB X IDE、XC8 和 MPLAB IPE 来编写代码。另外,我购买了 2 个开发板,用于使用 PIC16F88 测试代码一个小芯片,让我的脚湿透。当我查看许多网站时,我看到他们提供了用于测试的 CODE。通常是按下按钮,使 LED 亮起的示例。

#include <stdio.h>
#include <stdlib.h>
#define _XTAL_FREQ 4000000                  // Fosc  frequency for _delay()  library

/*
 * 
 */

#include <xc.h>     /* Hardware device support files. */
#include "16F88_xc8_header.h"


/*
 * 
 */
void main()

{
  OSCCON = 0b01100000;              // Internal frequency 4MHz
  TRISA.F0 = 1;                    //Configure 1st bit of PORTA as input
  TRISA.F1 = 1;                    //Configure 2nd bit of PORTA as input
  TRISA.F2 = 1;
  TRISA.F3 = 1;
  TRISA.F0 = 0;                    //Configure 1st bit of PORTB as output
  TRISB.F1 = 0;                    //Configure 2nd  bit of PORTB as output
  TRISB.F2 = 0;
  TRISB.F3 = 0;
  PORTB = "0x00";                   //All LEDs OFF
  do
  {
    if(PORTA.F0==0)            //If 1st switch is pressed
    {
      __delay_ms(100);    //Switch Debounce
      if(PORTA.F0==0)//If the switch is still pressed
      {
         PORTB.F0 = 1;           //1st LED ON
         __delay_ms(1000);              //1 Second Delay
         PORTB.F0 = 0;           //LED OFF
      }
   }
   if(PORTA.F1 == 0)            //If the 2nd switch is pressed
   {
     PORTB.F1 = 1;           //2nd LED ON
     __delay_ms(1000);              //1 Second Delay
     PORTB.F1 = 0;           //LED OFF
   }
   if(PORTA.F2 == 0)            //If the 3rd switch is pressed
   {
     PORTB.F2 = 1;           //3rd LED ON
     __delay_ms(1000);              //1 Second Delay
     PORTB.F2 = 0;           //LED OFF
   }
   if(PORTA.F3 == 0)            //If 4th switch is pressed
   {
     PORTB.F3 = 1;           //4thLED ON
     __delay_ms(1000);              //1 Second Delay
     PORTB.F3 = 0;           //LED OFF
   }
}

while(1);

}

这是代码来自的网站... https://microcontrollerslab.com/use-input-output-ports-pic18f452/

我调整了一些东西,例如更改 PORT 字母和尝试构建时出现的错误。但是有一些错误我不明白,所以我可以纠正。这是构建错误...

make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf
make[1]: Entering directory 'F:/PIC Stuff/Basic Scanner PIC16F88/B_SCAN_TEST2.X'
make  -f nbproject/Makefile-default.mk dist/default/production/B_SCAN_TEST2.X.production.hex
make[2]: Entering directory 'F:/PIC Stuff/Basic Scanner PIC16F88/B_SCAN_TEST2.X'
"C:\Program Files\microchip\xc8\v2.32\bin\xc8-cc.exe"  -mcpu=16F88 -c   -mdfp="C:/Program Files/microchip/MPLABX/v5.45/packs/microchip/PIC16Fxxx_DFP/1.2.33/xc8"  -fno-short-double -fno-short-float -O0 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -DXPRJ_default=default  -msummary=-psect,-class,+mem,-hex,-file  -ginhx032 -Wl,--data-init -mno-keep-startup -mno-osccal -mno-resetbits -mno-save-resetbits -mno-download -mno-stackcall   -std=c99 -gdwarf-3 -mstack=compiled:auto:auto     -o build/default/production/16F88_TEST2.p1 16F88_TEST2.c 
::: advisory: (2049) C99 compliant libraries are currently not available for baseline or mid-range devices,or for enhanced mid-range devices using a reentrant stack; using C90 libraries
16F88_TEST2.c:28:8: error: member reference base type 'volatile unsigned char' is not a structure or union
  TRISA.F0 = 1;                    //Configure 1st bit of PORTA as input
  ~~~~~^~~
16F88_TEST2.c:30:8: error: member reference base type 'volatile unsigned char' is not a structure or union
  TRISA.F1 = 1;                    //Configure 2nd bit of PORTA as input
  ~~~~~^~~
16F88_TEST2.c:32:8: error: member reference base type 'volatile unsigned char' is not a structure or union
  TRISA.F2 = 1;
  ~~~~~^~~
16F88_TEST2.c:34:8: error: member reference base type 'volatile unsigned char' is not a structure or union
  TRISA.F3 = 1;
  ~~~~~^~~
16F88_TEST2.c:36:8: error: member reference base type 'volatile unsigned char' is not a structure or union
  TRISA.F0 = 0;                    //Configure 1st bit of PORTB as output
  ~~~~~^~~
16F88_TEST2.c:38:8: error: member reference base type 'volatile unsigned char' is not a structure or union
  TRISB.F1 = 0;                    //Configure 2nd  bit of PORTB as output
  ~~~~~^~~
16F88_TEST2.c:40:8: error: member reference base type 'volatile unsigned char' is not a structure or union
  TRISB.F2 = 0;
  ~~~~~^~~
16F88_TEST2.c:42:8: error: member reference base type 'volatile unsigned char' is not a structure or union
  TRISB.F3 = 0;
  ~~~~~^~~
16F88_TEST2.c:44:9: warning: incompatible pointer to integer conversion assigning to 'volatile unsigned char' from 'char [5]' [-Wint-conversion]
  PORTB = "0x00";                   //All LEDs OFF
        ^ ~~~~~~
16F88_TEST2.c:50:9: error: member reference base type 'volatile unsigned char' is not a structure or union
if(PORTA.F0==0)            //If 1st switch is pressed
   ~~~~~^~~
16F88_TEST2.c:53:16: error: member reference base type 'volatile unsigned char' is not a structure or union
       if(PORTA.F0==0)//If the switch is still pressed
          ~~~~~^~~
16F88_TEST2.c:55:15: error: member reference base type 'volatile unsigned char' is not a structure or union
         PORTB.F0 = 1;           //1st LED ON
         ~~~~~^~~
16F88_TEST2.c:57:15: error: member reference base type 'volatile unsigned char' is not a structure or union
         PORTB.F0 = 0;           //LED OFF
         ~~~~~^~~
16F88_TEST2.c:63:9: error: member reference base type 'volatile unsigned char' is not a structure or union
if(PORTA.F1 == 0)            //If the 2nd switch is pressed
   ~~~~~^~~
16F88_TEST2.c:67:15: error: member reference base type 'volatile unsigned char' is not a structure or union
         PORTB.F1 = 1;           //2nd LED ON
         ~~~~~^~~
16F88_TEST2.c:71:15: error: member reference base type 'volatile unsigned char' is not a structure or union
         PORTB.F1 = 0;           //LED OFF
         ~~~~~^~~
16F88_TEST2.c:78:9: error: member reference base type 'volatile unsigned char' is not a structure or union
if(PORTA.F2 == 0)            //If the 3rd switch is pressed
   ~~~~~^~~
16F88_TEST2.c:82:15: error: member reference base type 'volatile unsigned char' is not a structure or union
         PORTB.F2 = 1;           //3rd LED ON
         ~~~~~^~~
16F88_TEST2.c:86:15: error: member reference base type 'volatile unsigned char' is not a structure or union
         PORTB.F2 = 0;           //LED OFF
         ~~~~~^~~
16F88_TEST2.c:90:9: error: member reference base type 'volatile unsigned char' is not a structure or union
if(PORTA.F3 == 0)            //If 4th switch is pressed
   ~~~~~^~~
Fatal error: too many errors emitted,stopping Now [-ferror-limit=]
1 warning and 20 errors generated.
(908) exit status = 1
nbproject/Makefile-default.mk:107: recipe for target 'build/default/production/16F88_TEST2.p1' Failed
make[2]: Leaving directory 'F:/PIC Stuff/Basic Scanner PIC16F88/B_SCAN_TEST2.X'
nbproject/Makefile-default.mk:91: recipe for target '.build-conf' Failed
make[1]: Leaving directory 'F:/PIC Stuff/Basic Scanner PIC16F88/B_SCAN_TEST2.X'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' Failed
make[2]: *** [build/default/production/16F88_TEST2.p1] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2

BUILD Failed (exit value 2,total time: 781ms)

解决方法

所有位声明都是错误的。查看控制器标题。 例如这个

TRISA.F0 = 1;

应该

TRISAbits.TRISA0 = 1;  //Bit 0 of PORTA is an input

正如@Tagli 提到的,还有更多错误,例如:

 PORTB = "0x00"; 

应该是:

PORTB = 0x00;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?