×
单片机 > 单片机程序设计 > 详情

PIC16F887 实战编程 单片机编程 基础实验教程

发布时间:2024-05-09 发布时间:
|

基础操作需要看一下以下的博客,跟着走一遍。
安装MPLAB V4.05+XC8 V1.41,老师不会管你用什么版本,别的版本bug多,老版本反而好用:
/zixunimg/eeworldimg/blog.csdn.net/x1131230123/article/details/100907773
安装proteus 8.9:
/zixunimg/eeworldimg/blog.csdn.net/x1131230123/article/details/106951776
做一个基础实验(想改写到MPLAB V5+XC8 V2也看这里的博客6.3 移植操作指南):
/zixunimg/eeworldimg/blog.csdn.net/x1131230123/article/details/108757436


实物连接指南(持续更新):
/zixunimg/eeworldimg/blog.csdn.net/x1131230123/article/details/108780168

2 PIC工程建立与仿真

这一步主要教大家怎么做一个完整的工程,整个过程熟悉后,就可以做自己的实际项目了。
注意:下面你将阅读到的内容并不涉及到MPLAB和PROTEUS联合调试,先忘记你之前所学的然后跟着下面博客的去理解这个思路。
1、
首先去桌面创建2个文件夹,如下面这图。一个必须是英文的,一个可以中文;一个用于存放PIC工程的,一个存放proteus仿真文件。题外话:电脑用户名是中文的建议重装电脑系统。

2、
打开IDE

打开后:

3、
新建一个工程:
file->new project

嵌入式标准工程:

芯片型号

仿真器

npasm其实就是MPLAB自带的编译器,需要写汇编语言的。
下面的XC8是我们自己装的编译器产品,我们选XC8。注意!V1.41可以更好的编译,新的V2版本有的地方有bug,我不清楚指令的详细区别,所以直接建议就安装MPLAB V4.05 +XC8 V1.41 。

工程名字也必须英文,不然有bug:

选工程路径,去选择桌面上新建的那个文件夹,这样整个程序工程都会存在那个文件夹里。

编码选择UTF-8。什么是编码?
汉字编码有哪些?UTF-8才是最常用的编码,GB2312只是汉字编码太局限了。

完成后:

4、
为新建的工程添加一个main.c:右键点source files --> 左键点new --> 左键点main.c…

不要中文

这样就完成了!

5、
这个时候就可以编写单片机C程序了,写个简单的(下面这程序就是控制端口B的低四位输出0101电平):

#include


// #pragma config statements should precede project file includes.

// Use project enums instead of #define for ON and OFF.

// CONFIG1

#pragma config FOSC = XT // Oscillator Selection bits (XT oscillator: Crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)

#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)

#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)

#pragma config MCLRE =ON // RE3/MCLR pin function select bit (RE3/MCLR pin function is digital input, MCLR internally tied to VDD)

#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)

#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)

#pragma config BOREN = OFF // Brown Out Reset Selection bits (BOR disabled)

#pragma config IESO = OFF // Internal External Switchover bit (Internal/External Switchover mode is disabled)

#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)

#pragma config LVP = OFF // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)


// CONFIG2

#pragma config BOR4V = BOR40V // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)

#pragma config WRT = OFF // Flash Program Memory Self Write Enable bits (Write protection off)


void main(void)

{


TRISB=0b11110000;//高四位输入 低四位输出

PORTB=0b00000101;//高四位的设置无效 低四位输出0101

while(1){

}

}

在所有的预处理指令中,#pragma

指令可能是最复杂的了,它的作用是设定编译器的状态或者是指示编译器完成一些特定的动作。#pragma指令对每个编译器给出了一个方法,在保持与C和C++语言完全兼容的情况下,给出主机或操作系统专有的特征。依据定义,编译指示是机器或操作系统专有的,且对于每个编译器都是不同的


不用管前面那些#pragma,那些全是对xc8编译器进行一些设置的,告诉编译器怎么处理怎么处理。

6、
写好程序后,怎么把程序弄到单片机里?单片机需要的是hex文件。
我们是C语言是人看的,我们需要需要借助MPLAB和XC8将C语言编译成hex文件。
点击下图里的图标就可以将C语言编译好。

注意:这里是编译,英文单词是build,编译的目的是将c语言转换成hex机器文件,单片机只需要这个文件就能够正确执行我们的程序。
你如果需要联调、调试、运行这几个关键词,英文单词是debug或者run等字眼,这类操作只在看程序运行到哪里,运行成了什么情况,是嵌入式程序员在开发过程中所需要的一种模式(要是经验足够也不需要这种模式)。这种事情要么需要proteus联调设置,要么需要实物单片机连接仿真器后插入了你的电脑,不要去点这些,除非你在自己进行联调操作或者调试实物操作。

会在底下看到编译提示:

make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf

make[1]: Entering directory 'C:/Users/xd_du/Desktop/PICproject/helloworld.X'

make -f nbproject/Makefile-default.mk dist/default/production/helloworld.X.production.hex

make[2]: Entering directory 'C:/Users/xd_du/Desktop/PICproject/helloworld.X'

"D:Program Files (x86)Microchipxc8v1.41binxc8.exe" --pass1 --chip=16F887 -Q -G --double=24 --float=24 --opt=+asm,+asmfile,-speed,+space,-debug,-local --addrqual=ignore --mode=free -P -N255 --warn=-3 --asmlist -DXPRJ_default=default --summary=default,-psect,-class,+mem,-hex,-file --output=default,-inhx032 --runtime=default,+clear,+init,-keep,-no_startup,-osccal,-resetbits,-download,-stackcall,+clib --output=-mcof,+elf:multilocs --stack=compiled:auto:auto "--errformat=%f:%l: error: (%n) %s" "--warnformat=%f:%l: warning: (%n) %s" "--msgformat=%f:%l: advisory: (%n) %s" -obuild/default/production/main.p1 main.c

"D:Program Files (x86)Microchipxc8v1.41binxc8.exe" --chip=16F887 -G -mdist/default/production/helloworld.X.production.map --double=24 --float=24 --opt=+asm,+asmfile,-speed,+space,-debug,-local --addrqual=ignore --mode=free -P -N255 --warn=-3 --asmlist -DXPRJ_default=default --summary=default,-psect,-class,+mem,-hex,-file --output=default,-inhx032 --runtime=default,+clear,+init,-keep,-no_startup,-osccal,-resetbits,-download,-stackcall,+clib --output=-mcof,+elf:multilocs --stack=compiled:auto:auto "--errformat=%f:%l: error: (%n) %s" "--warnformat=%f:%l: warning: (%n) %s" "--msgformat=%f:%l: advisory: (%n) %s" --memorysummary dist/default/production/memoryfile.xml -odist/default/production/helloworld.X.production.elf build/default/production/main.p1

Microchip MPLAB XC8 C Compiler (Free Mode) V1.41

Build date: Jan 24 2017

Part Support Version: 1.41

Copyright (C) 2017 Microchip Technology Inc.

License type: Node Configuration


:: warning: (1273) Omniscient Code Generation not available in Free mode


Memory Summary:

Program space used Dh ( 13) of 2000h words ( 0.2%)

Data space used 2h ( 2) of 170h bytes ( 0.5%)

EEPROM space used 0h ( 0) of 100h bytes ( 0.0%)

Data stack space used 0h ( 0) of 60h bytes ( 0.0%)

Configuration bits used 2h ( 2) of 2h words (100.0%)

ID Location space used 0h ( 0) of 4h bytes ( 0.0%)



You have compiled in FREE mode.

Using Omniscient Code Generation that is available in PRO mode,

you could have produced up to 60% smaller and 400% faster code.

See /zixunimg/eeworldimg/www.microchip.com/MPLABXCcompilers for more information.


make[2]: Leaving directory 'C:/Users/xd_du/Desktop/PICproject/helloworld.X'

make[1]: Leaving directory 'C:/Users/xd_du/Desktop/PICproject/helloworld.X'


BUILD SUCCESSFUL (total time: 1s)

Loading code from C:/Users/xd_du/Desktop/PICproject/helloworld.X/dist/default/production/helloworld.X.production.hex...

Loading completed


我们的hex文件在哪里?编译完成后,我们就可以在桌面那个工程文件夹里去找下图里这个hex文件,这个文件就是机器文件。proteus加载这个文件就可以执行我们写的程序。!!!!找到这个文件我们就已经成功了!

题外话:编译的时候提示了一句话:

You have compiled in FREE mode.



题外话:所以如何才能不处于免费模式?右键工程–>properties–>按图里的点击。
题外话:作用:此选项选择编译器的基本操作模式。可用类型有pro、std和free。在PRO模式下运行的编译器使用完全优化并生成最小的代码大小。标准模式使用有限的优化,而自由模式只使用最小的优化级别,将生成相对较大的代码。
提示:选择PRO在很多时候都不是一个明智的选择,PRO模式在编译器会“智能地”改变一些C语言的汇编实现方法,这有时候会有益于代码的最优化,但有时候会显得有些“智障”,会让能你认为能好好工作的C代码工作起来不正常。这个PRO主要是提供给有经验的嵌入式开发工程师用于优化代码的。



『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
STM32单片机的复用端口初始化的步骤及方法