佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

楼主: fritlizt

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

  [复制链接]
 楼主| 发表于 10-9-2010 11:55 AM | 显示全部楼层
回复 80# qweeer


   
终于有人回复我了。。。
我做这个教学做到很显。。。不懂是没人看还是什么。。。
的空我再update update下。
回复

使用道具 举报


ADVERTISEMENT

发表于 16-1-2011 11:34 PM | 显示全部楼层
近来会用到在final project
但有问题==现在新的是MPLAB_IDE_v8_63吗?
回复

使用道具 举报

发表于 3-3-2011 02:20 PM | 显示全部楼层
5楼那里用的
Delay1KTCYx

这个FUNCTION是build in 的还是自己define的?_?

有什么地方可以让我查查build in function 吗?
回复

使用道具 举报

发表于 6-3-2011 08:40 PM | 显示全部楼层
很详细啊,虽然大部分我都懂了,不过这个贴真的能帮很多刚上手的学生,感谢lz默默的耕耘与分享~
回复

使用道具 举报

 楼主| 发表于 6-3-2011 09:27 PM | 显示全部楼层
近来会用到在final project
但有问题==现在新的是MPLAB_IDE_v8_63吗?
lycheekhang 发表于 16-1-2011 11:34 PM



一样的。ide只是一个工具。帮你开发的工具。
还有一般上compiler(c18,不是 mplab) version 之间不会有太大的变动。c syntax会是一样的,library可能会更新之类。
回复

使用道具 举报

 楼主| 发表于 6-3-2011 09:29 PM | 显示全部楼层
回复 83# kjying


   
可以。你去c18 的installation folder,找doc文件夹。
里面都是c18的说明和built in library的说明
回复

使用道具 举报

Follow Us
发表于 7-3-2011 04:31 PM | 显示全部楼层
回复  kjying


   
可以。你去c18 的installation folder,找doc文件夹。
里面都是c18的说明和built ...
fritlizt 发表于 6-3-2011 09:29 PM



    找到了谢谢~~

  1. #if        XTAL_FREQ >= 12MHZ
  2.         #define        DelayUs(x)        { unsigned char _dcnt; \
  3.                                   _dcnt = (x)*((XTAL_FREQ)/(12MHZ)); \
  4.                                   while(--_dcnt != 0) \
  5.                                           continue; }
  6. #else
  7.         #define        DelayUs(x)        { unsigned char _dcnt; \
  8.                                   _dcnt = (x)/((12MHZ)/(XTAL_FREQ))|1; \
  9.                                   while(--_dcnt != 0) \
  10.                                           continue; }
  11. #endif
复制代码

这有个地方看不明白...
#define DelayUS(x)

define 后面不是应该像这样的东西吗?
#define    LED1    LATBbits.LATB6

上面的代表着什么?



  1. void DelayMs(unsigned char cnt)        // delay function (mili second)
  2. {
  3.         #if        XTAL_FREQ <= 2MHZ
  4.                 do {
  5.                         DelayUs(996);
  6.                 } while(--cnt);
  7.         #endif
  8.        
  9.         #if    XTAL_FREQ > 2MHZ       
  10.                 unsigned char        i;
  11.                 do {
  12.                         i = 4;
  13.                         do {
  14.                                 DelayUs(250);
  15.                         } while(--i);
  16.                 } while(--cnt);
  17.         #endif
  18. }
复制代码


里面的
while(--cnt)
while(--i)

是什么..@_@"
回复

使用道具 举报

 楼主| 发表于 9-3-2011 01:36 PM | 显示全部楼层
回复 87# kjying


   
#define是c preprocessor.
你除了可以define constant, name,你也可以define macros.
这个看起来像function,用起来也像function,不过本质上不一样。不懂要怎样解释。
比如说,
#define MULT(x, y) x * y

int z = MULT(3 + 2, 4 + 2);
这个相等于:
int z = 3 + 2 * 4 + 2;    // 2 * 4 will be evaluated first!

详细情况, 看
http://www.cprogramming.com/tutorial/cpreprocessor.html
不懂再问。 我会跟你讲如何用mplab来了解和verify。

while (--i), while(--cnt),
这个是基本的c do-while looping.如果你想用c programming来做你的project,你需要多加练习。


  1. i = 4;
  2. do {
  3.     DelayUs(250);
  4. } while(--i);
复制代码


这个写的是:
1。delayus(250)
2。--i 是0吗?是就结束这个loop跳出去。不是就回去1。

--i是decrement. 现在i = 4, --i,过后 i = 3.

如果你要用delay,这些是不用管的。 这些都是microchip define 的 macro,给你调用。
你需要知道的是如何看manual, help file.要include什么header/c file,来调用这些已经有了的函数来做你要的东西。
回复

使用道具 举报


ADVERTISEMENT

发表于 9-3-2011 04:47 PM | 显示全部楼层
回复  kjying


   
#define是c preprocessor.
你除了可以define constant, name,你也可以define ma ...
fritlizt 发表于 9-3-2011 01:36 PM

  ^^

你在21楼用的SOFTWARE是什么名?

可以STIMULATE RESULT的?
回复

使用道具 举报

 楼主| 发表于 9-3-2011 07:41 PM | 显示全部楼层
回复 89# kjying


    这个贴只有4面,不难爬。
回复

使用道具 举报

发表于 9-3-2011 10:50 PM | 显示全部楼层
回复  kjying


    这个贴只有4面,不难爬。
fritlizt 发表于 9-3-2011 07:41 PM


看到有人问可是没看到答案@_@"
回复

使用道具 举报

 楼主| 发表于 10-3-2011 04:42 PM | 显示全部楼层
回复 91# kjying


   
【讨论】Proteus VSM
http://cforum.cari.com.my/viewthread.php?tid=1032953&highlight=vsm
回复

使用道具 举报

发表于 16-3-2011 11:07 AM | 显示全部楼层
  1.    1. #include <p18cxxx.h>
  2.    2. #include <timers.h>
  3.    3.

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

  9.    9. void waitTimeout (void);
  10.   10.

  11.   11. void main (void)
  12.   12. {
  13.   13.         TRISB = 0xFF;                //portb as input
  14.   14.         TRISC = 0x00;                //portc as output
  15.   15.         TRISD = 0x00;
  16.   16.         OpenTimer1(T1_16BIT_RW & T1_SOURCE_INT & T1_PS_1_2);
  17.   17.         CloseTimer1();
  18.   18.

  19.   19.         while (1){
  20.   20.                 LATDbits.LATD0 =~ LATDbits.LATD0;
  21.   21.                 waitTimeout ();
  22.   22.                 LATC = LATB;
  23.   23.         }
  24.   24.         while(1);
  25.   25. }
  26.   26.

  27.   27. void waitTimeout (void)
  28.   28. {
  29.   29.         WriteTimer1(15536);
  30.   30.         T1CONbits.TMR1ON = 1;            //start timer
  31.   31.         while (!PIR1bits.TMR1IF);        //wait for timer overflow
  32.   32.         T1CONbits.TMR1ON = 0;            //stop timer
  33.   33.         PIR1bits.TMR1IF = 0;            //clear overflow flag
  34.   34. }
复制代码


上面TIMER的...
第20行...

LATDbits.LATD0 =~ LATDbits.LATD0;

有什么用?
如果我用

  1. while (1){
  2. LATC = LATB;
  3.   waitTimeout ();
  4. }
复制代码


应该是一样的吧..
回复

使用道具 举报

 楼主| 发表于 17-3-2011 02:17 AM | 显示全部楼层
回复 93# kjying


   
不一样。 google查查看~=是什么。
回复

使用道具 举报

发表于 3-7-2011 09:46 AM | 显示全部楼层
我也是在默默的支持LZ大大的哦
回复

使用道具 举报

发表于 12-10-2011 04:45 PM | 显示全部楼层
谁可以放个SERIAL PORT 传送DATA的教程...
我ONLINE找到的都不成功>_<
回复

使用道具 举报


ADVERTISEMENT

发表于 12-10-2011 11:57 PM | 显示全部楼层
请问一下必备的工具
本人可以购买这样东西为基本出发吗 ?
http://www.cytron.com.my/viewPro ... p%20Kit%20Combo%202
如果问错地方清多多原谅...
回复

使用道具 举报

发表于 13-10-2011 08:30 AM | 显示全部楼层
回复 97# angels1026

SK40-C2 + UIC00B 才可以。

UIC00B是programmer tools来的,SK40-C2没有包括这个工具在内。
回复

使用道具 举报

发表于 16-10-2011 11:40 PM | 显示全部楼层
请问一下..例子
//==========================================================================
//
Author
: xxxxxxxxxxxxxxxxxxxxxxxxxxx
//
Project
: Analog Sensomple code for PIC16F887
//
Project description
: Analog Sensor: Range using Ultrasonic Range Finder
//
  3 mode : (1: Analog)
//
   (2: PWM   )
//
   (3: UART  )
//==========================================================================

//
include
//==========================================================================
#include <htc.h>
#include "lcd.h"
#include "adc.h"
#include "system.h"
#include "uart.h"

//   Configuration
//==========================================================================
__CONFIG(HS &
// External Crystal at High Speed

WDTDIS &
// Disable Watchdog Timer.

PWRTEN &
// Enable Power Up Timer.

BORDIS &
// Disable Brown Out Reset.

MCLREN &
// MCLR function is enabled

LVPDIS);
// Disable Low Voltage Programming.

//
global variable
//===========================================================================
unsigned int To=0,T=0,TH=0;
unsigned char data[6] = {0};

//  Interrrupt Subroutine
//============================================================================
static void interrupt isr(void)
{

if (T0IF)
// TMR0 is overflow

{

T0IF = 0;
// clear flag bit

To +=0x100;
// count number of TMR0 overflow ( make it to 16bit TMR)

}



if(RBIF)
// there is change bit on RB4-RB7

{

RBIF = 0;
//
                                 
____

if (PWM_IN)
// PWM_IN(RB2) is 1 mean is rising form 0  __|         

{

TMR0 = 0;
// clear all counter involved, start new count for period of RB4 high

To = 0;

}

//
___

else TH = TMR0 + To;
// PWM_IN(RB2) is 0 mean is falling form 1
   |_____  // save TH, RB4 high period

}
}

//
main function
//==========================================================================
void main(void)
{
unsigned char mode = 1;

PORTA = 0;
// Clear Port

PORTB = 0;

PORTC = 0;

PORTD = 0;




TRISA = 0b11111111;
// set PORTA as INPUT

TRISB = 0b00000111;
// set PORTB<7:3> as OUTPUT , PORTB<2:0> as INPUT

TRISC = 0b10000000;

TRISD = 0b00000000;
// set PORTD as output




ANSELH = 0;
// SET PORTB as DIGITAL I/O for PIC16F887

WPUB = 1;
// PORTB Weak Pull Up enable


// Interupt on change

IOCB0 = 0;


IOCB1 = 0;

IOCB2 = 1;
// when PORTB.2 detect change will interrupt

IOCB3 = 0;

IOCB4 = 0;

IOCB5 = 0;

IOCB6 = 0;

IOCB7 = 0;


RBIE = 1;
// PORTB Change Interrupt Enable bit




// TMR 0 configuation

T0CS = 0;


PSA = 0;


PS2 = 1;
// prescale 1:256

PS1 = 1;


PS0 = 1;


T0IE = 1;
// TMR0 Interrupt

TMR0 = 0;



GIE = 1;
// Global Intterupt enable

PEIE = 1;
// Peripheral Interrupt enable


lcd_initialize();
// Initialise LCD


adc_initialize();
// Initialise ADC


uart_initialize();
// Initialise UART


TX == 1;
// ON Transmit pin (refer Maxbotix EZ1 Ultrasonic datasheet)


lcd_home();

lcd_putstr("Dist:");

lcd_goto(0x0B);

lcd_putstr("inch");

while (1)

{

switch(mode)

{

case 1:
lcd_2ndline();

lcd_putstr("Analog");

while (SW1 == 1)

{

unsigned int adc_value = 0;

unsigned int range_an = 0;  

unsigned char j;

adc_on();

for(j = 0 ; j < 10 ; j++)
// take analog result for 20 times

{

adc_value = adc_value + ui_adc_read();


}



adc_value = adc_value/10;
// adc_value devide by 10 to get average result

range_an = adc_value/2;
// max vslue adc_value = 2.55/5 *1024 - 1 =  522

lcd_goto(0x05);
// max is 254 inch... 522/254 = ~2


lcd_bcd(3,range_an);
// (1 to 6 inch) is readed as 6 inch (refer to ultrasonic datasheet)

}


while (SW1 == 0);

break;


case 2:
lcd_2ndline();

lcd_putstr("PWM   ");

unsigned int range_pwm;

while(SW1 == 1)

{

range_pwm = TH;
// read TH from interrupt subroutine

lcd_goto(0x05);
// each value = 256*4/20mhz = 51.2us, 1 inch = 147us  

range_pwm = (range_pwm*100)/288;
// 147us/51.2us = 2.87

lcd_bcd(3,range_pwm);


}

while (SW1 == 0);

break;


case 3:

lcd_2ndline();

lcd_putstr("UART   ");

unsigned int value,k,l = 0;

while(SW1 == 1)

{



for (l=0 ; l<6 ; l++)

{

uc_uart_receive();
//take value from UART subroutine

if(RCREG == 'R') data[k=0] = RCREG;
// check if start byte 'R' is met  

if(data[0] == 'R') data[k++] = RCREG;
// save the data in data array

if (k>4) k = 4;
// if the data array reached max, set the index to 4

}

lcd_goto(0x05);

send_lcd_data(1,data[1]);

send_lcd_data(1,data[2]);

send_lcd_data(1,data[3]);

}

}


while (SW1 == 0);

if (++mode > 3)
// if SW1 is press, increase the mode number until it is max and loop back

{

mode = 1;

}




}

}


这些东西是用 keyboard 写出来的吗。。。还是另有来源例如用software的只是set function 然后变出这些来 ? ... 谢谢。。>.> 我知道我很笨...
回复

使用道具 举报

 楼主| 发表于 24-10-2011 08:22 AM | 显示全部楼层
回复 99# angels1026


    你可以用“代码”这个功能来paste你的code.这样看很难看。
答案:不是,自己写的。不过有sample/library可以参考。
回复

使用道具 举报

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

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


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

GMT+8, 29-3-2024 06:05 AM , Processed in 0.080356 second(s), 21 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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