打印
[PIC®/AVR®/dsPIC®产品]

PIC33F64MC506芯片,IC模块的使用

[复制链接]
503|4
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
galjewf|  楼主 | 2020-7-7 17:21 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
刚接触这款芯片,靠着生啃英文手册写出的IC输出捕捉的程序,看了很久也不知道哪里出了问题,请各位指导




#include <stdio.h>
#include <stdlib.h>
#include<p33FJ64MC506.h>


#define BIT7    0x0080
#define IGBTEN_ON PORTD=LATD&(~BIT7)

//配置文件
_FOSCSEL(FNOSC_FRC)                 //初始振荡器源选择:000=FRC振荡器
_FOSC(FCKSM_CSECMD & OSCIOFNC_ON)   //时钟切换模式:01=使能时钟切换,禁止故障保护时钟监视器
                                    //OSC2引脚功能位(XT和HS模式除外):0=OSC2为通用数字I/O引脚
_FWDT(FWDTEN_OFF)                   //看门狗定时器使能:0=通过用户软件使能/ 禁止看门狗定时器
_FPOR(FPWRT_PWR128)                 //上电复位定时器值选择位:111=PWRT=128ms
_FICD(ICS_PGD1 & JTAGEN_OFF)        //ICD通信通道选择使能位:11=在PGEC1和PGED1上进行通信
                                    //JTAG使能位:0=禁止JTAG
void delay_us(int n)
{
        int j;
        while(n--)
        for(j=0;j<10;j++);
}
void delay_ms(int n)
{
        while(n--)
        delay_us(1000);
}
void init_PWM(void);
void pwm1_init();
int Input_caputre(void)
{
        IC1CONbits.ICM = 0b00;
        IC1CONbits.ICTMR = 1;        //Select Timer2 as the IC1 Time base
        IC1CONbits.ICI = 0b01;                // Interrupt on every fourth capture event
        IC1CONbits.ICM = 0b011;                //Capture mode, every 4th rising edge
       
        IPC0bits.IC1IP = 3; // Setup IC1 interrupt priority level
        IFS0bits.IC1IF = 0; // Clear IC1 Interrupt Status Flag
        IEC0bits.IC1IE = 1; // Enable IC1 interrupt
       
        return 1;
}
int timer2_init()
{
    T2CONbits.TON = 0;              // 确保T2关闭,再进行初始化
        T2CONbits.TCKPS = 0;
        T2CONbits.T32 = 0;
    T2CONbits.TCS = 0;
        T2CONbits.TON = 1;
        return 1;
}
/**************************************************************************************
* FunctionName   : main()
* Description    : 主函数
* EntryParameter : NO
* ReturnValue    : N0
**************************************************************************************/
int main()
{

  /******** Configure Oscillator to operate the device at 39.6288Mhz(MIPS)
                         Fosc= Fin*M/(N1*N2), Fcy=Fosc/2
   //Fosc= 7.3728*(43)/(2*2)=79.2576Mhz for Fosc, Fcy = 40Mhz *********/

    /* Configure PLL prescaler, PLL postscaler, PLL divisor */
    PLLFBD=41;              /* M=PLLFBD+2=43 */
    CLKDIVbits.PLLPOST=0;   /* N2=PLLPOST+2=2 */
    CLKDIVbits.PLLPRE=0;    /* N1=PLLPRE+2=2 */

    __builtin_write_OSCCONH(0x01);                /* 快速RC振荡器(FRC)→带PLL的快速RC振荡器(FRC) */
    __builtin_write_OSCCONL(0x01);                  /* Enable Switch */
      
    // Wait for Clock switch to occur
    while(OSCCONbits.COSC != 0b001);                /* Wait for new Oscillator to become FRC with PLL */  
    while(OSCCONbits.LOCK != 1);                /* Wait for Pll to Lock */

        AD1PCFGL=0xFFFF;

    //PWM模块IO初始
    PORTD = LATD | 0x2038;
    PORTD = LATD & 0xFF7F;
    TRISD &= 0xdf47;       //IGBTEN初始为低,配置为输出引脚pwm6H、6L、7H、7L配置为输出引脚,初始高电平
   
    //pwm1_init();
         timer2_init();
        Input_caputre();
        TRISB = 0x0000;
    pwm1_init();
    while(1)                                             /* Infinite Loop */
        {
                       
                        //delay_ms(100);
        }
}



/**************************************************************************************
* FunctionName   : init_PWM()
* Description    : 初始化PWM设置
* EntryParameter : NO
* ReturnValue    : N0
**************************************************************************************/
void init_PWM()
{

    PTPER=47188;                       /* PTPER = ((1 / 20kHz) / 1.0596ns) = 47188, where 20kHz is the desired switching frequency and 1.0596ns is PWM resolution. */
    PWM1CON1bits.PEN1H      = 1;                      /* 1 = PWM模块控制PWM1H引脚 */
    PWM1CON1bits.PEN1L      = 0;                      /* 0 = GPIO模块控制PWM1L引脚 */
    PWM1CON1bits.PMOD1      = 1;                      /* PWM I/O引脚对处于真正独立输出模式 */

    PDC1 = 10874;                                        /* Initial Duty cycle 40%(最终输出60%)*/


    /*~~~~~~~~~~~~~~~~~~~~~~~ PWM7 Configuration ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    PWM1CON1bits.PEN2H      = 1;                      /* PWM2H is controlled by PWM module */
    PWM1CON1bits.PEN2L      = 0;                      /* PWM2L is controlled by GPIO module */
    PWM1CON1bits.PMOD2      = 1;                      /* Select Independent Output PWM mode */

    PDC2 = 28313;                                        /* Initial Duty cycle 60(最终输出40%)*/

    PTCONbits.PTEN       = 1;                              /* Enable the PWM Module */
}

void pwm1_init()
{
        PTPER=47188;

        PTCONbits.PTEN = 1;                //PWM time base is on
        PTCONbits.PTMOD = 0;                // operates in a Free-Running mode
        PTCONbits.PTCKPS = 0;        // PWM time base input clock period is TCY (1:1 prescale)
       
        PWMCON1bits.PMOD1 = 1;        //PWM I/O pin pair is in the Independent PWM Output mode
        PWMCON1bits.PEN1H = 1;        //PWMxH pin is enabled for PWM output
        PWMCON1bits.PEN1L = 1;        //PWMxL pin is enabled for PWM output
        PWMCON2bits.SEVOPS = 0;        //PWM Special Event Trigger Output Postscale
        PWMCON2bits.IUE = 1;                // Updates to the active PDC registers are immediate
       
        PDC1 = 20000;
}

void __attribute__((__interrupt__,no_auto_psv))_IC1Interrupt(void)
{
        IFS0bits.IC1IF=0;
        LATB=(PORTB ^ 0x01);
}

使用特权

评论回复
沙发
tfqi| | 2020-8-4 14:28 | 只看该作者
iic对时序的要求很高的

使用特权

评论回复
板凳
qcliu| | 2020-8-4 14:28 | 只看该作者
对时间延时做一下微小的调整

使用特权

评论回复
地板
nawu| | 2020-8-4 14:29 | 只看该作者
用示波器看看哪里不对

使用特权

评论回复
5
kxsi| | 2020-8-4 14:29 | 只看该作者
楼主找到问题出在哪里了吗

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

9

主题

41

帖子

0

粉丝