查看: 28740|回复: 101
|
[教学]入门microchip c18教程(PIC18F4620)
[复制链接]
|
|
本帖最后由 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 编辑 ] |
|
|
|
|
|
|
|
楼主 |
发表于 28-6-2009 01:15 PM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 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..呵呵- #pragma config OSC = HS //hs osc
- #pragma config BOREN = OFF //no brownout reset
- #pragma config WDT = OFF //no watchdog
- #pragma config MCLRE = OFF //no mclr
复制代码 把portb.1 set成output.每250ms toggle portb.1.
就是2hz.的led blinking.- while (1){
- LATBbits.LATB1 = !LATBbits.LATB1;
- delay_ms(250);
- }
复制代码 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.- void delay_ms (unsigned int milisec)
- {
- while (milisec--
- Delay1KTCYx(1);
- }
复制代码 完整的program- #include <p18cxxx.h>
- #include <delays.h>
- #pragma config OSC = HS //hs osc
- #pragma config BOREN = OFF //no brownout reset
- #pragma config WDT = OFF //no watchdog
- #pragma config MCLRE = OFF //no mclr
- void delay_ms (unsigned int milisec);
- int main (void)
- {
- TRISBbits.TRISB1 = 0;
- while (1){
- LATBbits.LATB1 = !LATBbits.LATB1;
- delay_ms(250);
- }
- return 1;
- }
- void delay_ms (unsigned int milisec)
- {
- while (milisec--)
- Delay1KTCYx(1);
- }
复制代码 这个program 的 outcome将会是RB1的led会以2hz的 frequency来闪烁。
[ 本帖最后由 fritlizt 于 7-7-2009 10:59 PM 编辑 ] |
|
|
|
|
|
|
|
发表于 29-6-2009 10:16 AM
|
显示全部楼层
Create project 后,必须把 linker script 也加进去吧? |
|
|
|
|
|
|
|
楼主 |
发表于 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
|
显示全部楼层
|
|
|
|
|
|
|
发表于 1-7-2009 05:39 PM
|
显示全部楼层
我compile 了有error
unable to locate 'p18cxxx.h'
unable to locate 'delays.h'
是什么事情? |
|
|
|
|
|
|
|
楼主 |
发表于 2-7-2009 10:20 AM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 3-7-2009 10:54 PM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 4-7-2009 12:59 AM
|
显示全部楼层
|
|
|
|
|
|
|
楼主 |
发表于 6-7-2009 10:11 AM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 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
|
显示全部楼层
|
|
|
|
|
|
|
楼主 |
发表于 7-7-2009 11:04 PM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 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
|
显示全部楼层
|
|
|
|
|
|
|
发表于 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 的呱 |
评分
-
查看全部评分
|
|
|
|
|
|
|
发表于 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 |
|
|
|
|
|
|
| |
本周最热论坛帖子
|