PIC32延时程序

[复制链接]
914|35
手机看帖
扫描二维码
随时随地手机跟帖
ousj|  楼主 | 2020-9-3 21:29 | 显示全部楼层 |阅读模式
据说PIC32单片机的延时,可以用内部计数器进行延时。我想延时2us,但是,死活都不能达到2us

使用特权

评论回复
supernan| | 2020-9-3 21:33 | 显示全部楼层
示波器显示多少

使用特权

评论回复
ousj|  楼主 | 2020-9-3 21:36 | 显示全部楼层
示波器显示是40us。示波器用的是虚拟示波器。系统时钟是40Mhz

使用特权

评论回复
ousj|  楼主 | 2020-9-3 21:40 | 显示全部楼层
也就是说,当我用 delay_us(2) 的时候,虚拟示波器显示的时间是 40us。还是说,必须用定时器才能达到1us的延时?

使用特权

评论回复
ousj|  楼主 | 2020-9-3 21:45 | 显示全部楼层
怎么才能如同STM32单片机那样,可以实现1uS啊?

使用特权

评论回复
GYGD| | 2020-9-3 21:49 | 显示全部楼层
自己写,自己调

使用特权

评论回复
hanwe| | 2020-9-3 21:51 | 显示全部楼层

楼主程序可以公开吗?贴程序看下吧,这么说看不出什么原因

使用特权

评论回复
ousj|  楼主 | 2020-9-3 21:59 | 显示全部楼层
#define SYSCLK_FREQUENCY       40000000     //* 200MHz,修改成你自己的系统时钟
#define PBCLK7_FREQUENCY        SYSCLK_FREQUENCY
#define CORE_TIMER_FREQUENCY        (PBCLK7_FREQUENCY / 2)
#define CORE_TIMER_MILLISECONDS     (CORE_TIMER_FREQUENCY / 100)
#define CORE_TIMER_MICROSECONDS     (CORE_TIMER_FREQUENCY / 10000)

unsigned int __attribute__((nomips16)) ReadCoreTimer(void)
{
    unsigned int timer;

    asm volatile("mfc0   %0, $9" : "=r"(timer));

    return timer;
}

void delay_us(unsigned int delayUs)
{
    unsigned int delayStart;

    delayStart = ReadCoreTimer();
    while ((ReadCoreTimer() - delayStart) < (delayUs * CORE_TIMER_MICROSECONDS));
}

void delay_ms(unsigned int delayUs)
{
    unsigned int delayStart;

    delayStart = ReadCoreTimer();
    while ((ReadCoreTimer() - delayStart) < (delayUs * CORE_TIMER_MILLISECONDS));
}

使用特权

评论回复
langgq| | 2020-9-3 22:02 | 显示全部楼层
配置位设置?

使用特权

评论回复
ousj|  楼主 | 2020-9-3 22:06 | 显示全部楼层
/******************* 配置位设置 ******************************/
// DEVCFG3
// USERID = No                                                           //Setting


#pragma config FSRSSEL = PRIORITY_7                 // SRS Select (SRS Priority 7)
#pragma config FMIIEN = OFF                                // Ethernet RMII/MII Enable (RMII Enabled)
#pragma config FETHIO = OFF                               // Ethernet I/O Pin Select (Alternate Ethernet I/O)
#pragma config FCANIO = OFF                               // CAN I/O Pin Select (Alternate CAN I/O)
#pragma config FUSBIDIO = ON                             // USB USID Selection (Controlled by the USB Module)
#pragma config FVBUSONIO = ON                         // USB VBUS ON Selection (Controlled by USB Module)

// DEVCFG2
#pragma config FPLLIDIV = DIV_2                       // PLL Input Divider (2x Divider)
#pragma config FPLLMUL = MUL_20                  // PLL Multiplier (20x Multiplier)
#pragma config UPLLIDIV = DIV_1                     // USB PLL Input Divider (12x Divider)
#pragma config UPLLEN = OFF                          // USB PLL Enable (Disabled and Bypassed)
#pragma config FPLLODIV = DIV_2                    // System PLL Output Clock Divider (PLL Divide by 1)
                                                                           // SYSCLK = (8MHz Crystal/ FPLLIDIV * FPLLMUL / FPLLODIV)=40M
// DEVCFG1
#pragma config FNOSC = PRIPLL                      // Oscillator Selection Bits (Fast RC Osc with PLL)
#pragma config FSOSCEN = ON                       // Secondary Oscillator Enable (Disabled)
#pragma config IESO = ON                              // Internal/External Switch Over (Disabled)
#pragma config POSCMOD = HS                      // Primary Oscillator Configuration (HS osc mode)
#pragma config OSCIOFNC = OFF                   // CLKO Output Signal Active on the OSCO Pin (Disabled)
#pragma config FPBDIV = DIV_1                    // Peripheral Clock Divisor (Pb_Clk is Sys_Clk/8)=5M
#pragma config FCKSM = CSDCMD                // Clock Switching and Monitor Selection (Clock Switch Disable, FSCM Disabled)
#pragma config WDTPS = PS1                       // Watchdog Timer Postscaler (1:1)
#pragma config FWDTEN = OFF                    // Watchdog Timer Enable (WDT Disabled (SWDTEN Bit Controls))

// DEVCFG0
#pragma config DEBUG = ON                         // Background Debugger Enable (Debugger is enabled)
#pragma config ICESEL = ICS_PGx1             // ICE/ICD Comm Channel Select (ICE EMUC1/EMUD1 pins shared with PGC1/PGD1)
#pragma config PWP = PWP512K                 // Program Flash Write Protect (First 512K)
#pragma config BWP = ON                           // Boot Flash Write Protect bit (Protection Enabled)
#pragma config CP = ON                             // Code Protect (Protection Enabled)

//*************** 宏定义 *******************************************************

使用特权

评论回复
happy_10| | 2020-9-3 22:11 | 显示全部楼层

2us为何延时不了?
你心理没数啊?

使用特权

评论回复
bqyj| | 2020-9-3 22:15 | 显示全部楼层
你操作配置单时间都超过2us
这种直接用软延时就行了。

使用特权

评论回复
ousj|  楼主 | 2020-9-3 22:18 | 显示全部楼层

大神,软延时怎么弄?求指教
能发个例程吗?

使用特权

评论回复
songqian17| | 2020-9-3 22:20 | 显示全部楼层
问题贴里带有芯片完整型号、系统配置(尤其时钟),最好有完整工程,这样应该能更好的帮助你。

使用特权

评论回复
llljh| | 2020-9-3 22:23 | 显示全部楼层
从你的信息看,40MHz系统频率,按Core timer为系统时钟除以2,那么1us就,20个core timer周期(40个指令周期),2us也就是80个指令周期;函数进出需要的时间需要剔除才能得到精准时间,所以应该输入参数需要微调才能准确。

使用特权

评论回复
houcs| | 2020-9-3 22:29 | 显示全部楼层
不过你的测试相差太大,更多应该是时钟配置可能不对导致。

使用特权

评论回复
ousj|  楼主 | 2020-9-3 22:33 | 显示全部楼层
调一个库函数都超过这个时间了

使用特权

评论回复
ousj|  楼主 | 2020-9-3 22:41 | 显示全部楼层
芯片型号是PIC32MX795F512L ,晶振是8M。

使用特权

评论回复
ousj|  楼主 | 2020-9-3 22:46 | 显示全部楼层
/******************* 配置位设置 ******************************/
// DEVCFG3
// USERID = No //Setting


#pragma config FSRSSEL = PRIORITY_7     // SRS Select (SRS Priority 7)
#pragma config FMIIEN = OFF             // Ethernet RMII/MII Enable (RMII Enabled)
#pragma config FETHIO = OFF             // Ethernet I/O Pin Select (Alternate Ethernet I/O)
#pragma config FCANIO = OFF             // CAN I/O Pin Select (Alternate CAN I/O)
#pragma config FUSBIDIO = ON            // USB USID Selection (Controlled by the USB Module)
#pragma config FVBUSONIO = ON           // USB VBUS ON Selection (Controlled by USB Module)

// DEVCFG2
#pragma config FPLLIDIV = DIV_2         // PLL Input Divider (2x Divider)
#pragma config FPLLMUL = MUL_20         // PLL Multiplier (20x Multiplier)
#pragma config UPLLIDIV = DIV_1         // USB PLL Input Divider (12x Divider)
#pragma config UPLLEN = OFF             // USB PLL Enable (Disabled and Bypassed)
#pragma config FPLLODIV = DIV_2         // System PLL Output Clock Divider (PLL Divide by 1)
                                        // SYSCLK = (8MHz Crystal/ FPLLIDIV * FPLLMUL / FPLLODIV)=40M
// DEVCFG1
#pragma config FNOSC = PRIPLL             // Oscillator Selection Bits (Fast RC Osc with PLL)
#pragma config FSOSCEN = ON            // Secondary Oscillator Enable (Disabled)
#pragma config IESO = ON                // Internal/External Switch Over (Disabled)
#pragma config POSCMOD = HS             // Primary Oscillator Configuration (HS osc mode)
#pragma config OSCIOFNC = OFF           // CLKO Output Signal Active on the OSCO Pin (Disabled)
#pragma config FPBDIV = DIV_1           // Peripheral Clock Divisor (Pb_Clk is Sys_Clk/8)=5M
#pragma config FCKSM = CSDCMD           // Clock Switching and Monitor Selection (Clock Switch Disable, FSCM Disabled)
#pragma config WDTPS = PS1              // Watchdog Timer Postscaler (1:1)
#pragma config FWDTEN = OFF             // Watchdog Timer Enable (WDT Disabled (SWDTEN Bit Controls))

// DEVCFG0
#pragma config DEBUG = ON               // Background Debugger Enable (Debugger is enabled)
#pragma config ICESEL = ICS_PGx1        // ICE/ICD Comm Channel Select (ICE EMUC1/EMUD1 pins shared with PGC1/PGD1)
#pragma config PWP = PWP512K            // Program Flash Write Protect (First 512K)
#pragma config BWP = ON                 // Boot Flash Write Protect bit (Protection Enabled)
#pragma config CP = ON                  // Code Protect (Protection Enabled)

//*************** 宏定义 *******************************************************

使用特权

评论回复
wangpe| | 2020-9-3 22:49 | 显示全部楼层
有没有其他方式能延时us级别的?

使用特权

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

本版积分规则

712

主题

7557

帖子

1

粉丝