今天接到个修改蜂鸣器声音的任务,在调试程序的时候发现程序里是用定时器中断的方式来延时,从而使蜂鸣器发声.这里的蜂鸣器是有源的,只需用电平就可驱动.但是在定时器初始化的程序中我发现以下程序:
//Hardware about
OpenTimer2(T2_ON | T2_SOURCE_INT | T2_PS_1_256, T2_TICK);
mT2SetIntPriority(2);
mT2SetIntSubPriority(2);
mT2IntEnable(1);
void __ISR(_TIMER_2_VECTOR,ipl2)Timer2Handler(void);
mT2ClearIntFlag();
后几句我在<<32位单片机C语言编程---基于PIC32>>一书中可以找到解释:
mT2SetIntPriority(2);//这句话就是设置定时器2中断的优先级为2(优先级从0至7)
mT2ClearIntFlag();//这是一个宏,它也代表了一组宏定义,可以清除所选中断源的中断标志(-IF位)
mT2IntEnable(1);//这个很明显就是使能定时器2中断
mT2SetIntSubpriority(2);//这是设置定时器2中断的子优先级
void __ISR(_TIMER_2_VECTOR,ipl2)Timer2Handler(void);//采用宏__ISR声明中
//断,_TIMER_2_VECTOR是定时器2中断的中断向量号,可以在PIC32MX460F512L的向量表中找到.
//ipl2是代表优先级2(从ipl1至ipl7,优先级高者先被响应),Timer2Handler为函数名.
剩下的 OpenTimer2(T2_ON | T2_SOURCE_INT | T2_PS_1_256, T2_TICK);书上就以一笔概括更本不能理解括号里的内容,于是我就找到了MPLAB中自带的库函数timer.h:
函数: void OpenTimer2(unsigned int config,unsigned int period)
描述: 配置定时器2的参数
输入参数: config: T2_ON/OFF,T2_IDLE_XXX,T2_GATE_XXX,T1_PS_XXX,
T2_32BIT_MODE_ON,T2_SOURCE_XXX它们的值或者位宽.
注: 如果上面有的项在实际中没有写出,就代表它们被设置为0.
period: 一个在0~0xffff中的值
输出: 无
举例: OpenTimer2(T2_ON | T2_IDLE_CON | T2_PS_1_4, 123)
#define OpenTimer2(config, period) (T2CON = ((config)&~(T2_ON)), TMR2=0, PR2= (period), T2CONSET=((config)&(T2_ON)))
注:这里的'&~'符号一般用作清0,所以这里初始化就是使T2CON清0.至于传递的信息汇报立在T2CONSET这个寄存器中.
- /******************************************************************************
- * Available options for config parameter
- *****************************************************************************/
- // On/off control - values are mutually exclusive
- #define T2_ON (1 << _T2CON_ON_POSITION) /* Timer2 ON */
- #define T2_OFF (0)
- // Stop-in-idle control - values are mutually exclusive
- #define T2_IDLE_STOP (1 << _T2CON_SIDL_POSITION) /* stop during idle */
- #define T2_IDLE_CON (0) /* operate during idle */
- // Timer gate control - values are mutually exclusive
- #define T2_GATE_ON (1 << _T2CON_TGATE_POSITION) /* Timer Gate accumulation mode ON */
- #define T2_GATE_OFF (0)
- // Prescale values - values are mutually exclusive
- #define T2_PS_1_256 (7 << _T2CON_TCKPS_POSITION) /* Prescaler 1:256 */
- #define T2_PS_1_64 (6 << _T2CON_TCKPS_POSITION) /* 1:64 */
- #define T2_PS_1_32 (5 << _T2CON_TCKPS_POSITION) /* 1:32 */
- #define T2_PS_1_16 (4 << _T2CON_TCKPS_POSITION) /* 1:16 */
- #define T2_PS_1_8 (3 << _T2CON_TCKPS_POSITION) /* 1:8 */
- #define T2_PS_1_4 (2 << _T2CON_TCKPS_POSITION) /* 1:4 */
- #define T2_PS_1_2 (1 << _T2CON_TCKPS_POSITION) /* 1:2 */
- #define T2_PS_1_1 (0) /* 1:1 */
- // 32-bit or 16-bit - values are mutually exclusive
- #define T2_32BIT_MODE_ON (1 << _T2CON_T32_POSITION) /* Enable 32-bit mode */
- #define T2_32BIT_MODE_OFF (0)
- // Sync external clock option - values are mutually exclusive
- #define T2_SOURCE_EXT (1 << _T2CON_TCS_POSITION) /* External clock source */
- #define T2_SOURCE_INT (0) /* Internal clock source */
- /***********************************
- * End config parameter values
- ************************************/
注:一开始纳闷这里的"<<"符号,后来明白了,就相当于直接把数值移到对应的位置,这样的方法第一次见到以后
可以常使用.
阅读(188) | 评论(0) | 转发(0) |