佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 28733|回复: 101

[教学]入门microchip c18教程(PIC18F4620)

  [复制链接]
发表于 28-6-2009 01:13 PM | 显示全部楼层 |阅读模式
本帖最后由 fritlizt 于 19-6-2010 12:38 AM 编辑

办一些简单教程,多一点人会, 多点人一起谈论, 才会更热闹。。。。

什么是PIC microcontroller unit (MCU)??
类似电脑。 不过是很小型的电脑. 集成了processor和peripheral在一个小小的晶片里面。peripheral 如ram, rom, spi, uart timer.等等。 有些还有usb, ethernet, can等界面。

rom和ram 通常是以byte -> kilobyte来计算。
peripheral方面, 根据厂家, 每个人都不同。 不过普通的peripheral通常都会有。 如rom, ram, uart, timer.
microchip的pic peripheral方面算是丰富的了。 足于应付普通用途。 当然你要更强的也有。。。
可以参考
http://en.wikipedia.org/wiki/Microcontroller

怎样操作?
和电脑一样, 你要他做什么,你必须通过编程,也就是programming,让mcu知道要怎样做。 mcu其实是很笨的, 你跟他讲要做什么, 它就做什么, 没讲到的, 就不做。不要assume他会做。

写prograM后该怎么做?
通常就是把program hex file 烧录进mcu里面。
看mcu 是不是operate as expected.
参考这贴
http://cforum4.cari.com.my/viewthread.php?tid=1634019&extra=page%3D1

这个贴主要是关于pic18f4620 mcu.如何用c18来program 这个mcu.
所以必备的工具是
mplab ide和microchip c18
ide 是integrated development environment.
什么是ide?
http://en.wikipedia.org/wiki/Integrated_development_environment

microchip c18是c compiler for pic18 series.
也就是可以用c programming 写program给pic18f 系列的mcu.
他会compile program, generate assembly,同时间generate hex file。

c compiler for pic当然不只一家。 ccs c, mikroc, hitech picc.不过hitech picc好像给microchip 买了。

mplab ide, c18是免费下载使用。
下载地址
mplab ide.(由于下载地址会更改, 请自行在microchip.com找找下。)
http://www.microchip.com/

c18.
安装的时候, 记得把选项全选。。(由于下载地址会更改, 请自行在microchip.com找找下。)
http://www.microchip.com



教程
create project ->第二楼
led blinking 1 -> 第五楼
led blinking 2 ->第十五楼
led blinking 3 ->第二十一楼
c18 data types.->第二十二楼
USART.->第二十八楼
简单I/O控制->第六十一楼
Analog to digital convert, A/D转换->第七十楼
Timer ->第七十九楼

[ 本帖最后由 fritlizt 于 7-8-2009 11:54 AM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 28-6-2009 01:15 PM | 显示全部楼层

create project

看看program怎么写。。。。。第一个要会的事。。。。-〉create project.....
install mplab ide,和c18过后打开mplab ide.

click Project -> Project wizard

next


选pic 型号


toolsuite.选c18


你project filename和location


暂时不需要。




接下来会看到这个界面。。。有一点懒惰解释鸟。。。哈哈


create 一个.c file, 记得add进去你的source folder.就可以compile 了。


大概是这样.就可以compile了。

*注意-〉第一次compile的时候可能会出现c018i.0 not found的错误讯息。
这是因为compiler locate不到你的c018i.o library folder.
Project ->Build option->roject->Directories
然后show directories for->library search path.
自己add 进去。 default会是c:\mcc18\lib


[ 本帖最后由 fritlizt 于 2-7-2009 10:27 AM 编辑 ]
回复

使用道具 举报

发表于 28-6-2009 02:30 PM | 显示全部楼层
发问~
1)RB0那里不用pull high吗?还是用了build in 的pull high?
2)Hi tech picc 比较好用还是C18呢?小弟最近都只是用picc没有过其他的

[ 本帖最后由 Yapl 于 28-6-2009 02:31 PM 编辑 ]
回复

使用道具 举报

 楼主| 发表于 28-6-2009 02:32 PM | 显示全部楼层
原帖由 Yapl 于 28-6-2009 02:30 PM 发表
发问~
1)RB0那里不用pull high吗?还是用了build in 的pull high?
2)Hi tech picc 比较好用还是C18呢?小弟最近都只是用picc没有过其他的


打算用internal pull high.
Hi tech picc 比较好用还是C18呢->我不懂。 看个人。
回复

使用道具 举报

 楼主| 发表于 28-6-2009 02:34 PM | 显示全部楼层

闪烁发光二极管(一)

闪烁发光二极管(一)
简称led blinking 1.

哈哈。

简单的schematics.
很简单的schematics.vcc, vss, osc, 一个button还有两个led.
vdd, vss我相信不用解释。。。。+_+"
osc方面我用4mhz crystal., 22pf capacitive load.
button和led......应该不是大问题。


写program之前有几个重要的冬冬一定要看看。
<c18 installation folder>\doc 里面的文件很重要。
hlpC18Lib.chm->一些简单的c18 built in library. software uart, spi, delay function, general software library.
hlpPIC18ConfigSet.chm->confuguration bit 的definition.如果不懂pic18f4620里面有什么configuration bit何耀怎样set.看这个就对了。

MPLAB-C18-Getting-Started_51295f.pdf
hlpC18ug.chm
这两个得空可以看看。 也是很重要。

periph-lib这个folder是hardware peripheral library.关于hardwarelibrary 的资料, 运用可以在这里找到。

接下来,看看program.
很简单.不明白再问。。
#include <p18cxxx.h> include device.h file.根据project setting他会自己决定include 什么device的header file.
#include <delays.h> 如果有用到internal delay file.要include.

configuration word setting. 什么是configuration word?看datasheet..呵呵
  1. #pragma config OSC = HS                                //hs osc
  2. #pragma config BOREN = OFF                        //no brownout reset
  3. #pragma config WDT = OFF                        //no watchdog
  4. #pragma config MCLRE = OFF                        //no mclr
复制代码
把portb.1 set成output.
  1. TRISBbits.TRISB1 = 0;
复制代码
每250ms toggle portb.1.
就是2hz.的led blinking.
  1. while (1){
  2.                 LATBbits.LATB1 = !LATBbits.LATB1;
  3.                 delay_ms(250);
  4.         }
复制代码
delay_ms使我自己写的function.
单位为1ms.
Delay1KTCYx(1);是internal delay function.关于delay function.可以参考hlpC18Lib.chm
代表delay 1000 instruction cycles.
在4mhz下, 一个instruction cycle = 1/4mhz * 4 = 1us.
1000 instruction cycle = 1us*1000 = 1ms.
  1. void delay_ms (unsigned int milisec)
  2. {
  3.         while (milisec--
  4.                 Delay1KTCYx(1);
  5. }
复制代码
完整的program
  1. #include <p18cxxx.h>
  2. #include <delays.h>

  3. #pragma config OSC = HS                                //hs osc
  4. #pragma config BOREN = OFF                        //no brownout reset
  5. #pragma config WDT = OFF                        //no watchdog
  6. #pragma config MCLRE = OFF                        //no mclr

  7. void delay_ms (unsigned int milisec);

  8. int main (void)
  9. {
  10.         TRISBbits.TRISB1 = 0;
  11.         while (1){
  12.                 LATBbits.LATB1 = !LATBbits.LATB1;
  13.                 delay_ms(250);
  14.         }
  15.         return 1;
  16. }

  17. void delay_ms (unsigned int milisec)
  18. {
  19.         while (milisec--)
  20.                 Delay1KTCYx(1);
  21. }
复制代码
这个program 的 outcome将会是RB1的led会以2hz的 frequency来闪烁。

[ 本帖最后由 fritlizt 于 7-7-2009 10:59 PM 编辑 ]
回复

使用道具 举报

发表于 29-6-2009 10:16 AM | 显示全部楼层
Create project 后,必须把 linker script 也加进去吧?
回复

使用道具 举报

Follow Us
 楼主| 发表于 29-6-2009 11:06 AM | 显示全部楼层
原帖由 waiweng83 于 29-6-2009 10:16 AM 发表
Create project 后,必须把 linker script 也加进去吧?


新的不用。 直接add .c file就可以了
回复

使用道具 举报

发表于 29-6-2009 11:27 AM | 显示全部楼层
原帖由 fritlizt 于 29-6-2009 11:06 AM 发表

新的不用。 直接add .c file就可以了

哦?又棵酱的树哦?
新的 MPLAB还是新的mcc18?

原帖由 Yapl 于 28-6-2009 02:30 PM 发表
发问~
2)Hi tech picc 比较好用还是C18呢?小弟最近都只是用picc没有过其他的

呵呵,我以前用hi tech picc是因为 microchip没有pic16f的c compiler。
没有用过18f 的hi tech picc ,不过听讲 hi tech的library比较大,很多function都不用自己写的。。。
回复

使用道具 举报


ADVERTISEMENT

发表于 1-7-2009 05:39 PM | 显示全部楼层
我compile 了有error
unable to locate 'p18cxxx.h'
unable to locate 'delays.h'
是什么事情?
回复

使用道具 举报

 楼主| 发表于 2-7-2009 10:20 AM | 显示全部楼层
原帖由 ^Hins86^ 于 1-7-2009 05:39 PM 发表
我compile 了有error
unable to locate 'p18cxxx.h'
unable to locate 'delays.h'
是什么事情?


install的时候,那些option全选就可以了。
uninstall 过。 再install回。
回复

使用道具 举报

发表于 3-7-2009 10:54 PM | 显示全部楼层
不好意思。。。我是初学者。 之前有研究过怎样用MPLAB IDE v8.10,也是用C programming, CCS C compiler。但是,有一点我不是很明白,到底 MPLAB 有什么优点? 我没有用它,直接用PIC C compiler 也可以啊,compile 了就用software downloader, Winpic 800,就可以烧录进去PIC 咯。为什么要用MPLAB 呢?是不是除了compile 和download,还有其他功能? 谢谢哦。。 我想了很久。。




原帖由 fritlizt 于 28-6-2009 01:13 PM 发表
办一些简单教程,多一点人会, 多点人一起谈论, 才会更热闹。。。。

什么是PIC microcontroller unit (MCU)??
类似电脑。 不过是很小型的电脑. 集成了processor和peripheral在一个小小的晶片里面。periph ...
回复

使用道具 举报

发表于 4-7-2009 12:59 AM | 显示全部楼层
原帖由 fritlizt 于 2-7-2009 10:20 AM 发表


install的时候,那些option全选就可以了。
uninstall 过。 再install回。


build好了
之后要做什么呢
用proteus 做simulation 吗?
回复

使用道具 举报

 楼主| 发表于 6-7-2009 10:11 AM | 显示全部楼层
原帖由 ^Hins86^ 于 4-7-2009 12:59 AM 发表


build好了
之后要做什么呢
用proteus 做simulation 吗?


这个就是你第一个program.
接下来我会教用timer 来generate delay.
回复

使用道具 举报

发表于 6-7-2009 11:46 AM | 显示全部楼层
原帖由 cincailo 于 3-7-2009 10:54 PM 发表
不好意思。。。我是初学者。 之前有研究过怎样用MPLAB IDE v8.10,也是用C programming, CCS C compiler。但是,有一点我不是很明白,到底 MPLAB 有什么优点? 我没有用它,直接用PIC C compiler 也可以啊,compile  ...

他可以用MPLAB SIM作software debug(不用接hardware), 和用proteus 做simulation(不用接hardware) 。。。
他也可以用pickit/icd2/ice来直接作debugging 等等。。。
不过讲真的,MPLAB的text editor就是差了点。。。
回复

使用道具 举报

 楼主| 发表于 7-7-2009 10:58 PM | 显示全部楼层

led blinking 2

接下来。 要做的就是。。。。。还是led blinking.不过有点不一样。
schemetics 如下。 也是有一点点不一样。。。。。。就是crystal不一样。 换去10mhz
顺便在这里教如何enable x4 pll. 把pic clock去40mhz.


写program之前, 先看看peripheral library help.
../MCC18/doc/periph-lib/Timer.htm

里面有几个function.
closetimer->停止timer & timer interrupt.
opentimer->configure & start timer
writetimer->write timer register
readtimer->read timer register.

这里有几个function.自己读读看什么作用的。

#include <p18cxxx.h> 一定要咯。。。。
#include <timers.h> 这个没用delay function,不过会用到timer peripheral function,所以要换去#include <timers.h>
  1. #include <p18cxxx.h>

  2. #include <timers.h>
复制代码


看看configuration bit.和之前的有点不一样。
#pragma config OSC = HSPLL
这就是enable pll x4..其实enable pll很简单。。。。
  1. #pragma config OSC = HSPLL            //hspll osc
  2. #pragma config BOREN = OFF            //no brownout reset
  3. #pragma config WDT = OFF            //no watchdog
  4. #pragma config MCLRE = OFF            //no mclr
复制代码



要用timer前, 你要configure timer mode. pic18f4620里面有好几个timer.
这个example我选用timer0.
OpenTimer0(TIMER_INT_OFF & T0_16BIT  & T0_SOURCE_INT & T0_PS_1_1);
这样是代表什么?简单来讲是->
timer 不generate interrupt.
timer 16bits mode
timer source = internal clock
prescaler 1.
当然不只有这几个option.还有其他的。 参考../MCC18/doc/periph-lib/Timer.htm

CloseTimer0();
这只是把timer停止。 因为open timer除了会configure timer之外, 还会顺便 start timer.+_+"避免不必要的问题, 还没用到之前不要start timer.

while loop没变到。 因为还是blink 2hz.只是不一样方法。
  1. void delay_ms (unsigned int milisec);

  2. void main (void)
  3. {
  4.     TRISBbits.TRISB1 = 0;
  5.     OpenTimer0(TIMER_INT_OFF & T0_16BIT  & T0_SOURCE_INT & T0_PS_1_1);    //configure and start timer
  6.     CloseTimer0();                                                        //stop timer0
  7.     while (1){
  8.         LATBbits.LATB1 = !LATBbits.LATB1;
  9.         delay_ms(250);
  10.     }
  11. }
复制代码




这个就是整个program和之前最不一样的地方。 generate delay的方法不一样。。。。
简单解释timer operation.
timer0是count up timer.每个instruction cycle*prescaler 会增加一。 到0xFFFF + 1的时候会导致overflow.
为什么0xFFFF? 因为我configure成16bits timer. 16bits 的register可以hold value 0-65535而已。
65536就不能hold了, 就会overflow.然后timer register value会变成0。 65535 decimel = 0xFFFF hexadecimel
ok.

要如何算timer register value overflow的时候刚刚好1ms?

1。.首先要知道一个instruction cycles用多少时间?
2.   你要知道要算多少个instruction cycle.

1.
一个instruction cycle(Fcy) = 1/(Fosc/4). 意思就是一个instruction需要1/(Fosc/4)的时间来执行。
什么是Fosc? for 这个case, oscillator configuration是HSPLL.代表你的Fosc = Fcrystal * 4 = 10mhz*4 = 40Mhz.
Fcy = 1/(40Mhz/4) = 1/10mhz = 0.1us

2. 1ms你要算几个Fcy?
简单的数学。 1ms/0.1us = 10000.
timer register要用什么value才能刚好算10000Fcy过后才overflow?
overflow的发生在0xFFFF+1 = 65536的时候, 所以要刚刚好1ms的时候overflow,你就需要65536-1ms的Fcy = 65536-10000 = 55536.

看看program.
WriteTimer0(55536);->在还没start timer 之前, 你要给timer知道它几时要overflow. 55536 会在1ms过后overflow.
T0CONbits.TMR0ON = 1;    timer开始算
while (!INTCONbits.TMR0IF);   等timer overflow
T0CONbits.TMR0ON = 0;  一overflow, 立刻stop timer.
INTCONbits.TMR0IF = 0;  overflow过后, 必须要clear overflow flag.否着你分不出到底是刚才的overflow还是现在的overflow.
  1. void delay_ms (unsigned int milisec)
  2. {
  3.     while (milisec--){
  4.         WriteTimer0(55536);
  5.         T0CONbits.TMR0ON = 1;            //start timer
  6.         while (!INTCONbits.TMR0IF);        //wait for timer overflow
  7.         T0CONbits.TMR0ON = 0;            //stop timer
  8.         INTCONbits.TMR0IF = 0;            //clear overflow flag   
  9.     }
  10. }
复制代码


complete program.
  1. #include <p18cxxx.h>
  2. #include <timers.h>

  3. #pragma config OSC = HSPLL            //hspll osc
  4. #pragma config BOREN = OFF            //no brownout reset
  5. #pragma config WDT = OFF            //no watchdog
  6. #pragma config MCLRE = OFF            //no mclr

  7. void delay_ms (unsigned int milisec);

  8. void main (void)
  9. {
  10.     TRISBbits.TRISB1 = 0;
  11.     OpenTimer0(TIMER_INT_OFF & T0_16BIT  & T0_SOURCE_INT & T0_PS_1_1);    //configure and start timer
  12.     CloseTimer0();                                                        //stop timer0
  13.     while (1){
  14.         LATBbits.LATB1 = !LATBbits.LATB1;
  15.         delay_ms(250);
  16.     }
  17. }

  18. void delay_ms (unsigned int milisec)
  19. {
  20.     while (milisec--){
  21.         WriteTimer0(55536);
  22.         T0CONbits.TMR0ON = 1;            //start timer
  23.         while (!INTCONbits.TMR0IF);        //wait for timer overflow
  24.         T0CONbits.TMR0ON = 0;            //stop timer
  25.         INTCONbits.TMR0IF = 0;            //clear overflow flag   
  26.     }
  27. }
复制代码
回复

使用道具 举报

 楼主| 发表于 7-7-2009 11:04 PM | 显示全部楼层
原帖由 cincailo 于 3-7-2009 10:54 PM 发表
不好意思。。。我是初学者。 之前有研究过怎样用MPLAB IDE v8.10,也是用C programming, CCS C compiler。但是,有一点我不是很明白,到底 MPLAB 有什么优点? 我没有用它,直接用PIC C compiler 也可以啊,compile  ...


个人习惯。 开mplab. editor, program, debug etc mplab都可以搞定。
加上如果我用ccs c。。。我也是用mplab ide的。 只是compiler用ccsc而已。
回复

使用道具 举报


ADVERTISEMENT

发表于 7-7-2009 11:19 PM | 显示全部楼层

回复 16# fritlizt 的帖子

其实我也是这样子的。在mplab里install CCS C compiler 作为 third party,然后再用mplab ide 来写。

什么是debug? MPLAB SIM 其实有什么功能呢? 是不是可以做simulation?
回复

使用道具 举报

发表于 11-7-2009 12:03 AM | 显示全部楼层
不好意思,tumpang 这里问一下。有谁知道 MPLAB C Compiler,Evaluation version 和 Academic Version 到底有什么差别呢?找了好久还是找不到
回复

使用道具 举报

发表于 11-7-2009 01:47 PM | 显示全部楼层

回复 18# waiweng83 的帖子

我用的是evaluation version(才装没几久) ,
"MPLAB C18 v3.30 (feature limited)
Copyright 2000-2009 Microchip Technology Inc.
This version of MPLAB C18 does not support the extended mode
and will not perform all optimizations.  To purchase a full
copy of MPLAB C18, please contact your local distributor or
visit buy.microchip.com."

academic/student version应该是有三个月full version 的呱

评分

参与人数 1积分 +10 收起 理由
fritlizt + 10

查看全部评分

回复

使用道具 举报

发表于 11-7-2009 03:51 PM | 显示全部楼层
原帖由 bakayalo 于 11-7-2009 01:47 PM 发表
我用的是evaluation version(才装没几久) ,
"MPLAB C18 v3.30 (feature limited)
Copyright 2000-2009 Microchip Technology Inc.
This version of MPLAB C18 does not support the extended mode
and will not ...


我以为是一样的.....hehe
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 19-4-2024 09:17 PM , Processed in 0.098457 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表