[PIC®/AVR®/dsPIC®产品] PIC33F64MC506芯片,IC模块的使用

[复制链接]
 楼主| galjewf 发表于 2020-7-7 17:21 | 显示全部楼层 |阅读模式
刚接触这款芯片,靠着生啃英文手册写出的IC输出捕捉的程序,看了很久也不知道哪里出了问题,请各位指导




  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include<p33FJ64MC506.h>


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

  6. //配置文件
  7. _FOSCSEL(FNOSC_FRC)                 //初始振荡器源选择:000=FRC振荡器
  8. _FOSC(FCKSM_CSECMD & OSCIOFNC_ON)   //时钟切换模式:01=使能时钟切换,禁止故障保护时钟监视器
  9.                                     //OSC2引脚功能位(XT和HS模式除外):0=OSC2为通用数字I/O引脚
  10. _FWDT(FWDTEN_OFF)                   //看门狗定时器使能:0=通过用户软件使能/ 禁止看门狗定时器
  11. _FPOR(FPWRT_PWR128)                 //上电复位定时器值选择位:111=PWRT=128ms
  12. _FICD(ICS_PGD1 & JTAGEN_OFF)        //ICD通信通道选择使能位:11=在PGEC1和PGED1上进行通信
  13.                                     //JTAG使能位:0=禁止JTAG
  14. void delay_us(int n)
  15. {
  16.         int j;
  17.         while(n--)
  18.         for(j=0;j<10;j++);
  19. }
  20. void delay_ms(int n)
  21. {
  22.         while(n--)
  23.         delay_us(1000);
  24. }
  25. void init_PWM(void);
  26. void pwm1_init();
  27. int Input_caputre(void)
  28. {
  29.         IC1CONbits.ICM = 0b00;
  30.         IC1CONbits.ICTMR = 1;        //Select Timer2 as the IC1 Time base
  31.         IC1CONbits.ICI = 0b01;                // Interrupt on every fourth capture event
  32.         IC1CONbits.ICM = 0b011;                //Capture mode, every 4th rising edge
  33.        
  34.         IPC0bits.IC1IP = 3; // Setup IC1 interrupt priority level
  35.         IFS0bits.IC1IF = 0; // Clear IC1 Interrupt Status Flag
  36.         IEC0bits.IC1IE = 1; // Enable IC1 interrupt
  37.        
  38.         return 1;
  39. }
  40. int timer2_init()
  41. {
  42.     T2CONbits.TON = 0;              // 确保T2关闭,再进行初始化
  43.         T2CONbits.TCKPS = 0;
  44.         T2CONbits.T32 = 0;
  45.     T2CONbits.TCS = 0;
  46.         T2CONbits.TON = 1;
  47.         return 1;
  48. }
  49. /**************************************************************************************
  50. * FunctionName   : main()
  51. * Description    : 主函数
  52. * EntryParameter : NO
  53. * ReturnValue    : N0
  54. **************************************************************************************/
  55. int main()
  56. {

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

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

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

  70.         AD1PCFGL=0xFFFF;

  71.     //PWM模块IO初始
  72.     PORTD = LATD | 0x2038;
  73.     PORTD = LATD & 0xFF7F;
  74.     TRISD &= 0xdf47;       //IGBTEN初始为低,配置为输出引脚pwm6H、6L、7H、7L配置为输出引脚,初始高电平
  75.    
  76.     //pwm1_init();
  77.          timer2_init();
  78.         Input_caputre();
  79.         TRISB = 0x0000;
  80.     pwm1_init();
  81.     while(1)                                             /* Infinite Loop */
  82.         {
  83.                        
  84.                         //delay_ms(100);
  85.         }
  86. }



  87. /**************************************************************************************
  88. * FunctionName   : init_PWM()
  89. * Description    : 初始化PWM设置
  90. * EntryParameter : NO
  91. * ReturnValue    : N0
  92. **************************************************************************************/
  93. void init_PWM()
  94. {

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

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


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

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

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

  107. void pwm1_init()
  108. {
  109.         PTPER=47188;

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

  122. void __attribute__((__interrupt__,no_auto_psv))_IC1Interrupt(void)
  123. {
  124.         IFS0bits.IC1IF=0;
  125.         LATB=(PORTB ^ 0x01);
  126. }
tfqi 发表于 2020-8-4 14:28 | 显示全部楼层
iic对时序的要求很高的
qcliu 发表于 2020-8-4 14:28 | 显示全部楼层
对时间延时做一下微小的调整
nawu 发表于 2020-8-4 14:29 | 显示全部楼层
用示波器看看哪里不对
kxsi 发表于 2020-8-4 14:29 | 显示全部楼层
楼主找到问题出在哪里了吗
您需要登录后才可以回帖 登录 | 注册

本版积分规则

9

主题

41

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部

9

主题

41

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部