MPLAB® C30 C 编译器用户指南

MPLAB® C30 C 编译器用户指南 MPLAB® C30 C 编译器用户指南

ie.ksu.edu.tw
from ie.ksu.edu.tw More from this publisher
10.07.2015 Views

MPLAB ® C30 用 户 指 南例 6-1:实 时 时 钟 示 例 代 码/*** Sample Real Time Clock for dsPIC**** Uses Timer1, TCY clock timer mode** and interrupt on period match*/#include /* Timer1 period for 1 ms with FOSC = 20 MHz */#define TMR1_PERIOD 0x1388struct clockType{unsigned int timer; /* countdown timer, milliseconds */unsigned int ticks; /* absolute time, milliseconds */unsigned int seconds; /* absolute time, seconds */} volatile RTclock;void reset_clock(void){RTclock.timer = 0; /* clear software registers */RTclock.ticks = 0;RTclock.seconds = 0;TMR1 = 0; /* clear timer1 register */PR1 = TMR1_PERIOD; /* set period1 register */T1CONbits.TCS = 0; /* set internal clock source */IPC0bits.T1IP = 4; /* set priority level */IFS0bits.T1IF = 0; /* clear interrupt flag */IEC0bits.T1IE = 1; /* enable interrupts */SRbits.IPL = 3; /* enable CPU priority levels 4-7*/T1CONbits.TON = 1; /* start the timer*/}void __attribute__((__interrupt__)) _T1Interrupt(void){ static int sticks=0;}if (RTclock.timer > 0) /* if countdown timer is active */RTclock.timer -= 1; /* decrement it */RTclock.ticks++; /* increment ticks counter */if (sticks++ > 1000){ /* if time to rollover */sticks = 0; /* clear seconds ticks */RTclock.seconds++; /* and increment seconds */}IFS0bits.T1IF = 0; /* clear interrupt flag */return;DS51284C_CN 第 82 页© 2005 Microchip Technology Inc.

器 件 支 持 文 件6.6 使 用 宏处 理 器 头 文 件 除 定 义 了 特 殊 功 能 寄 存 器 外 , 还 为 dsPIC30F 系 列 数 字 信 号 控 制 器 定 义了 有 用 的 宏 。6.6.1 配 置 位 设 置 宏提 供 了 可 用 来 设 置 配 置 位 的 宏 。 例 如 , 为 使 用 宏 设 置 FOSC 位 , 可 在 C 源 代 码 开 头 前插 入 下 面 的 代 码 :_FOSC(CSW_FSCM_ON & EC_PLL16);这 将 使 能 外 部 时 钟 , PLL 设 置 为 16x。 同 时 使 能 时 钟 切 换 和 时 钟 失 效 保 护 监 测 。同 样 , 设 置 FBORPOR 位 :_FBORPOR(PBOR_ON & BORV_27 & PWRT_ON_64 & MCLR_DIS);这 将 使 能 2.7V 的 欠 压 复 位 , 将 上 电 延 时 定 时 器 初 始 化 为 64 ms, 并 将 MCLR 引 脚 配 置为 普 通 I/O 口 。每 个 配 置 位 的 设 置 列 表 , 参 见 处 理 器 头 文 件 。6.6.2 行 内 汇 编 使 用 的 宏下 面 列 出 了 用 于 在 C 中 定 义 汇 编 代 码 的 宏 :#define Nop() {__asm__ volatile ("nop");}#define ClrWdt() {__asm__ volatile ("clrwdt");}#define Sleep() {__asm__ volatile ("pwrsav #0");}#define Idle() {__asm__ volatile ("pwrsav #1");}6.6.3 数 据 存 储 器 分 配 宏本 节 讲 述 可 用 于 分 配 数 据 存 储 空 间 的 宏 。 有 两 种 类 型 : 需 要 参 数 的 宏 和 不 需 要 参 数 的宏 。下 面 的 宏 需 要 一 个 参 数 N 来 指 定 对 齐 。 N 必 须 是 2 的 次 幂 , 最 小 值 为 2。#define _XBSS(N) __attribute__((space(xmemory), aligned(N)))#define _XDATA(N) __attribute__((space(xmemory), aligned(N)))#define _YBSS(N) __attribute__((space(ymemory), aligned(N)))#define _YDATA(N) __attribute__((space(ymemory), aligned(N)))#define _EEDATA(N) __attribute__((space(eedata), aligned(N)))例 如 , 声 明 一 个 未 初 始 化 数 组 位 于 X 存 储 区 中 , 对 齐 到 32 字 节 地 址 :int _XBSS(32) xbuf[16];声 明 一 个 未 初 始 化 数 组 位 于 数 据 EEPROM 中 , 没 有 特 殊 对 齐 方 式 :int _EEDATA(2) table1[] = {0, 1, 1, 2, 3, 5, 8, 13, 21};下 面 的 宏 不 需 要 参 数 。 这 些 宏 可 用 于 将 变 量 分 配 到 持 久 数 据 存 储 区 或 near 数 据 存 储 区中 。#define _PERSISTENT __attribute__((persistent))#define _NEAR __attribute__((near))例 如 , 声 明 器 件 复 位 后 能 保 留 其 值 的 两 个 变 量 :int _PERSISTENT var1,var2;© 2005 Microchip Technology Inc. DS51284C_CN 第 83 页

器 件 支 持 文 件6.6 使 用 宏处 理 器 头 文 件 除 定 义 了 特 殊 功 能 寄 存 器 外 , 还 为 dsPI<strong>C30</strong>F 系 列 数 字 信 号 控 制 器 定 义了 有 用 的 宏 。6.6.1 配 置 位 设 置 宏提 供 了 可 用 来 设 置 配 置 位 的 宏 。 例 如 , 为 使 用 宏 设 置 FOSC 位 , 可 在 C 源 代 码 开 头 前插 入 下 面 的 代 码 :_FOSC(CSW_FSCM_ON & EC_PLL16);这 将 使 能 外 部 时 钟 , PLL 设 置 为 16x。 同 时 使 能 时 钟 切 换 和 时 钟 失 效 保 护 监 测 。同 样 , 设 置 FBORPOR 位 :_FBORPOR(PBOR_ON & BORV_27 & PWRT_ON_64 & MCLR_DIS);这 将 使 能 2.7V 的 欠 压 复 位 , 将 上 电 延 时 定 时 器 初 始 化 为 64 ms, 并 将 MCLR 引 脚 配 置为 普 通 I/O 口 。每 个 配 置 位 的 设 置 列 表 , 参 见 处 理 器 头 文 件 。6.6.2 行 内 汇 编 使 用 的 宏下 面 列 出 了 用 于 在 C 中 定 义 汇 编 代 码 的 宏 :#define Nop() {__asm__ volatile ("nop");}#define ClrWdt() {__asm__ volatile ("clrwdt");}#define Sleep() {__asm__ volatile ("pwrsav #0");}#define Idle() {__asm__ volatile ("pwrsav #1");}6.6.3 数 据 存 储 器 分 配 宏本 节 讲 述 可 用 于 分 配 数 据 存 储 空 间 的 宏 。 有 两 种 类 型 : 需 要 参 数 的 宏 和 不 需 要 参 数 的宏 。下 面 的 宏 需 要 一 个 参 数 N 来 指 定 对 齐 。 N 必 须 是 2 的 次 幂 , 最 小 值 为 2。#define _XBSS(N) __attribute__((space(xmemory), aligned(N)))#define _XDATA(N) __attribute__((space(xmemory), aligned(N)))#define _YBSS(N) __attribute__((space(ymemory), aligned(N)))#define _YDATA(N) __attribute__((space(ymemory), aligned(N)))#define _EEDATA(N) __attribute__((space(eedata), aligned(N)))例 如 , 声 明 一 个 未 初 始 化 数 组 位 于 X 存 储 区 中 , 对 齐 到 32 字 节 地 址 :int _XBSS(32) xbuf[16];声 明 一 个 未 初 始 化 数 组 位 于 数 据 EEPROM 中 , 没 有 特 殊 对 齐 方 式 :int _EEDATA(2) table1[] = {0, 1, 1, 2, 3, 5, 8, 13, 21};下 面 的 宏 不 需 要 参 数 。 这 些 宏 可 用 于 将 变 量 分 配 到 持 久 数 据 存 储 区 或 near 数 据 存 储 区中 。#define _PERSISTENT __attribute__((persistent))#define _NEAR __attribute__((near))例 如 , 声 明 器 件 复 位 后 能 保 留 其 值 的 两 个 变 量 :int _PERSISTENT var1,var2;© 2005 Microchip Technology Inc. DS51284C_CN 第 83 页

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!