fengyunyu 发表于 2014-11-21 12:15:19

求一个使用内部振荡器的PIC24F工程

第一次用PIC,网上找的例程都是使用外部晶振。哪位坛友给一个使用内部振荡器的PIC24F工程?需要开一个定时器,并定时改变某个IO口电平即可。

fengyunyu 发表于 2014-11-21 13:07:42

在一网站看到的配置代码,不知道有无问题。

PIC24 clock setup

config word FOSCSEL selects which oscillator.
each oscillator option can be with or without PLL (which multiplies by 4)
The builtin RC oscillator is 8mhz, so
FNOSC_FRC is 8mhz, and FNOSC_FRCPLL is 32mhz.

THEN, there's a postscaler. CLKDIVbits.RCDIV sets the divide-by. 0 means
don't.

NOTE: The default setting of the postscaler is DIVIDE BY TWO. If you don't
want it to halve the clock freq, you have to set CLKDIVbits.RCDIV=0 early
in the program.

THEN, whatever the output of all the above, is divided by two.

For example
_FOSCSEL(FNOSC_FRC)
CLKDIVbits.RCDIV = 0;
gives the builtin 8mhz oscillator, not divided by a postscaler, and
finally divided by two = 4MHz.

_FOSCSEL(FNOSC_FRCPLL)
gives builtin 8mhz, times 4 (pll) = 32MHz, divided by 2 = 16 MHz.



UART BAUD RATE SETTINGS.

Builtin RC oscillator @ 8MHZ, PLL enabled, CLKDIV=0 = system clock at 16MHz

_FOSCSEL(FNOSC_FRCPLL)
CLKDIVbits.RCDIV = 0;
U1MODEbits.BRGH = 1;

9600bps: U1BRG=416
19200bps: U1BRG=207
38400bps: U1BRG=103
57600bps: U1BRG=70
115200bps: U1BRG=34

fengyunyu 发表于 2014-11-21 13:17:04

找了一个

24F32KA301 Example LED Flash using Microchip C

Example program for the Microchip PIC 24F32KA301 using Microchip C compiler

Using Timer1 and interrupts, flashes LED on A0 once per second

       
/*
* Microchip C Compiler Demo Program for PIC24F32KA301
* PIC24F32KA301 flash LED on A0 using Timer1 and interrupts
* Copyright (C)2011 HobbyTronics.co.uk
* Freely distributable.
*/

//include basic header definition
#include <p24f32ka301.h>

//config
_FOSCSEL(FNOSC_FRCPLL & IESO_OFF);    // FRC Oscillator with PLL (32MHz)
_FOSC (FCKSM_CSDCMD & SOSCSEL1_SOSCLP & POSCFREQ_MS
       & OSCIOFNC_OFF & POSCMOD_NONE);

/*Clock switching and Fail-Safe Clock monitor DISABLED
Secondary Oscillator Select
Primary Oscillator Frequency Range
OSCO Pin Configuration OFF
Oscillator Selection
*/

//prototypes
void timer1_init(void);

//main loop
int main(void)
{
   //Set up Clock
   OSCCON = 0x11C0;    // select INTERNAL FRC (32MHz), Post Scale & PLL
   CLKDIV = 0x0000;    // No clock Divide
   
   //Set Ports to Digital
   ANSELA=0x0000;
   ANSELB=0x0000;
   ANSELC=0x0000;
   TRISA=0x0000;
   LATAbits.LATA0 = 0;

   timer1_init();
   SRbits.IPL = 0 ;    // turn on all interrupt priorities
   
   //Loop forever
   while(1)
   {
   }
}

//Set up Timer, target 2Hz interrupts
void timer1_init(void)
{
   //Setup Timer1
   PR1 = 0x7A12;          // set for 2Hz (Clock is 32KHz/2 = 16MHz)
   IPC0bits.T1IP = 5;   //set interrupt priority
   T1CON = 0b1000000000110000;    //turn on the timer
   IFS0bits.T1IF = 0;   //reset interrupt flag
   IEC0bits.T1IE = 1;   //turn on the timer1 interrupt
}

//_T1Interrupt() is the T1 interrupt service routine (ISR).
void __attribute__((__interrupt__, auto_psv)) _T1Interrupt(void)
{
   LATAbits.LATA0 = ~LATAbits.LATA0;    //Toggle output to LED
   IFS0bits.T1IF = 0;
}

fengyunyu 发表于 2014-11-21 13:28:54

//Set up Timer, target 2Hz interrupts
void timer1_init(void)
{
   //Setup Timer1
   PR1 = 0x7A12;          // set for 2Hz (Clock is 32KHz/2 = 16MHz)
   IPC0bits.T1IP = 5;   //set interrupt priority
   T1CON = 0b1000000000110000;    //turn on the timer
   IFS0bits.T1IF = 0;   //reset interrupt flag
   IEC0bits.T1IE = 1;   //turn on the timer1 interrupt
}

哪位坛友给解说下,2Hz是如何算出来的?

stanley.zhao 发表于 2014-11-21 12:15:20

fengyunyu 发表于 2014-11-21 13:28
//Set up Timer, target 2Hz interrupts
void timer1_init(void)
{


资料都找到了,放到板子上实验下就好了,反正烧不坏芯片的。

void timer1_init(void)
{
   //Setup Timer1
   PR1 = 0x7A12;          // set for 2Hz (Clock is 32MHz/2 = 16MHz)
   IPC0bits.T1IP = 5;   //set interrupt priority
   T1CON = 0b1000000000110000;    //turn on the timer,bit5:bit4=11,时钟分频1:256
   IFS0bits.T1IF = 0;   //reset interrupt flag
   IEC0bits.T1IE = 1;   //turn on the timer1 interrupt
}

2Hz ,周期为0.5s=5000000us
0x7A12 = 31250
1/16*256*31250=5000000

LZ认真看DS,都是最基本的
看我这么仔细回答的份上,100分给我吧{:lol:}

fengyunyu 发表于 2014-11-21 13:58:12

stanley.zhao 发表于 2014-11-21 13:50
资料都找到了,放到板子上实验下就好了,反正烧不坏芯片的。

void timer1_init(void)


谢谢解答。再请教下,MCLR有外接复位芯片,看网上有说不能直接用ICD或PICKIT烧写或仿真,是这样么?

stanley.zhao 发表于 2014-11-21 14:03:13

fengyunyu 发表于 2014-11-21 13:58
谢谢解答。再请教下,MCLR有外接复位芯片,看网上有说不能直接用ICD或PICKIT烧写或仿真,是这样么? ...

这个真心不清楚了{:sad:} ,还是老话实验出真知!{:lol:}

nos002 发表于 2014-12-2 18:39:25

#ifndef __p24FConfig_H
#define __p24FConfig_H

#include<p24Fxxxx.h>


_FBS
(
    BWRP_OFF &      // Boot Segment Write Protect (Disabled)
    BSS_OFF         // Boot segment Protect (No boot flash segment)
)

_FGS
(
    GWRP_OFF &      // General Segment Flash Write Protect (General segment may be written)
    GSS0_OFF      // General Segment Code Protect (No Protection)
)

_FOSCSEL
(
    FNOSC_FRCPLL &// Oscillator Select
    SOSCSRC_DIG &   // SOSC Source Type (Analog Mode for use with crystal)
    LPRCSEL_HP &    // LPRC Power and Accuracy (High Power/High Accuracy)
    IESO_ON         // Internal External Switch Over bit (Internal External Switchover mode enabled (Two-speed Start-up enabled))
)

_FOSC
(
    POSCMD_NONE &   // Primary Oscillator Mode (Primary oscillator disabled)
    OSCIOFNC_OFF &   // CLKO Enable Configuration bit (CLKO output signal enabled)
    POSCFREQ_MS &    // Primary Oscillator Frequency Range Configuration bits (Primary oscillator/external clock frequency between 100kHz to 8MHz)
    SOSCSEL_SOSCLP & // SOSC Power Selection Configuration bits (Secondary Oscillator configured for high-power operation)
    FCKSM_CSDCMD   // Clock Switching and Monitor Selection (Clock Switching and Fail-safe Clock Monitor Enabled)
)

_FWDT
(
    WDTPS_PS2 & // Watchdog Timer Postscale Select bits (1:32768)
    FWPSA_PR32 &   // WDT Prescaler bit (WDT prescaler ratio of 1:128)
    FWDTEN_OFF &    // Watchdog Timer Enable bits (WDT disabled in hardware; SWDTEN bit disabled)
    WINDIS_OFF      // Windowed Watchdog Timer Disable bit (Standard WDT selected (windowed WDT disabled))
)


// Warning:
// Always enable MCLRE_ON config bit setting so that the MCLR pin function will
// work for low-voltage In-Circuit Serial Programming (ICSP). The Microstick
// programming circuitry only supports low-voltage ICSP. If you disable MCLR pin
// functionality, a high-voltage ICSP tool will be required to re-program the
// part in the future.
_FPOR
(
    BOREN_BOR3 &    // Brown-out Reset Enable bits (Enabled in hardware; SBOREN bit disabled)
    PWRTEN_ON &   // Power-up Timer Enable (PWRT enabled)
    I2C1SEL_PRI &   // Alternate I2C1 Pin Mapping bit (Default SCL1/SDA1 Pins for I2C1)
    BORV_V18 &      // Brown-out Reset Voltage bits (Brown-out Reset at 1.8V)
    MCLRE_ON      // MCLR Pin Enable bit (RA5 input disabled; MCLR enabled)
)

_FICD
(
    ICS_PGx2      // ICD Pin Placement Select (EMUC/EMUD share PGC3/PGD3)
)

#endif

nos002 发表于 2014-12-2 18:44:00

本帖最后由 nos002 于 2014-12-2 18:48 编辑

/****************************************************************************************
*名 称:SYSYTEMInit
*参 数:
*返 回:
*说 明:设定各种初始用户参数
*****************************************************************************************/
void SYSTEMInit(void)
{
    OSCCON = 0;
    OSCCONbits.CLKLOCK = 0;
    OSCCONbits.LOCK = 1; //PLL模块锁定
    CLKDIVbits.RCDIV = 0b000; //内部时钟8M,1:1分频
    OSCCONbits.NOSC = 1;
    OSCCONbits.COSC = 1; //当前振荡器选择001,带后分频和4倍PLL模块的8M FRC振荡器
    U1MODEbits.UARTEN = 0;
    RCONbits.SWDTEN = 1;

}


/**************************************************************
* 名 称:T4Initial
* 参 数:
* 返 回:
* 说 明:定时5ms,Fosc=32M,指令周期等于Fosc/2,一次计时等于一个指令周期,
*      等于62.5ns,0.0625x78x16x16=1248us.
***************************************************************/
void T4Initial(void)
{
    //CORCON=0;
    //INTCON1=0x8000;
    //INTCON2=0x0000;
    TMR4 = 0;
    PR4 = 78;
    T4CON = 0x007e; //前分频1:16,后分频1:16
    IPC6bits.T4IP = 3;
    IFS1bits.T4IF = 0;
    IEC1bits.T4IE = 1;
    T4CONbits.TON = 1;
}

/**************************/
void IOInit(void)
{
    //AD1PCFG = 0xFFFF;
    ANSA = 0x0000; // Enable PORTA Digital Input Buffers
    ANSB = 0x0000; // Enable PORTB Digital Input Buffers

    ODCBbits.ODB8 = 1;
    ODCBbits.ODB9 = 1;

    LATA = 0xffa3; //RA0低电平驱动buzzer,设为高电平;RA2:3为晶振脚,晶振未接,设为低电平
    LATB = 0x0300; //RB8:9 为键盘扫描口,设高电平,其他设为低电平
    TRISA = 0xffa2;//RA1为ADC输入
    TRISB = 0x00f2;//RB1为 U2RX、RB4:7为键盘输入口,设为输入
}
/********************************************************************************************
* 名 称:main
* 参 数:
* 返 回:
* 说 明:
*********************************************************************************************/
int main(void)
{
    SYSTEMInit();
    IOInit();
    T4Initial();
    while(1);
}
/*************************************************************************************
* 名 称:T4中断
* 参 数:
* 返 回:
* 说 明:1.25ms中断,产生1.25ms和5ms嘀嗒信号,1.25ms作为数码动态显示时基,5ms作为键盘等时基
*************************************************************************************/
void __attribute__((interrupt, shadow, auto_psv))_T4Interrupt()
{
    static unsigned char i;
    IFS1bits.T4IF = 0;
    GLOBALbits.Tick1ms = 1;
    if (3 < i++)//4*1.25=5ms
    {
      i = 0;
      GLOBALbits.Tick5ms = 1;
    }
    //return ReceiveDataServer(Uart2RBuf);
}
页: [1]
查看完整版本: 求一个使用内部振荡器的PIC24F工程