第一范文网 - 专业文章范例文档资料分享平台

北京理工大学-PIC单片机在电子系统设计中的应用-实验三-定时器中断 - 图文

来源:用户分享 时间:2025/5/24 12:00:41 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

7、将程序烧写到单片机,观察实验现象

五、实验结果:

观察电路显示:

六、实验心得:

这次实验比较容易,在熟悉示例代码后,根据函数库以及变量说明,发现了需要完善的地方。设置中断,设置预分频器后,最终成功运行。这次试验熟悉了中断的使用以及分频器的设定。

经过这次的实验,我个人得到了不少的收获,一方面加深了自己对理论知识的认识,另一方面也提高了自己的实验逻辑思考能力及实验操作能力。

七、源代码如下:

#include #include #include

#pragma config WDT = OFF #pragma config OSC = HS #pragma config LVP = OFF

void isr_high(void);

volatile unsigned char Long_Count ; volatile unsigned char Direct_LED ; volatile unsigned char Dir_Count ;

void main(void) { TRISD=0x00;

PORTD=0b00000001;

//******************************************** // Exercise

// Enable Timer2 interrupt with high Priority //

//********************************************

// Set output port for LED driver // Set b0 of LED is On

//(Refer 18fxx2_cn(39564b) of Page 75-84)

RCONbits.IPEN=1; // Enable Interrupt Priority bit

IPR1bits.TMR2IP=1; // Set Timer2 for High Priority INTCONbits. GIEH=1;

// Enable High Priority Interrupt

/***********************************/ /* Interrupt Time

*/

/*(4/16Mhz) [16*10*(199+1)] = 8mS */

/***********************************/

//(Refer MPLAB C18函数库 of Page 50)

OpenTimer2 (TIMER_INT_ON &T2_PS_1_16 & T2_POST_1_10) ; PR2=199;

Long_Count=0; Direct_LED=0;

Dir_Count=0;

while(1);

}

//************************************************ //* #pragma Interrupt Declarations * //*

Function: isr_high_direct * //* Direct execution to the actual * //* high-priority interrupt code. * //************************************************ #pragma code isrhighcode = 0x0008

void isr_high_direct(void) {

_asm

// Turn On the Timer2 with Interrupt //(4/16Mhz) [16*10*(199+1)] = 8mS

// Loop Here!

//begin in-line assembly

goto isr_high _endasm

//go to isr_high function

//end in-line assembly

}

#pragma code

//************************************************ //* Function: isr_high(void) * //* High priority interrupt will * //* Save the received data to buffer Rec_Data * //************************************************ #pragma interrupt isr_high

void isr_high(void) { PIR1bits.TMR2IF=0;

if (Long_Count <= 6) Long_Count++; else {

Long_Count=0;

if (Direct_LED==0x00)

{ PORTD<<=1;

Dir_Count++; if (Dir_Count==7)

{ Dir_Count=0;

Direct_LED=0x1;

}

}

// Clear Timer2 interrupt Flag

// 8mS * 6 = 48mS // Time is 48mS, do the function

// Right or Left shift

// LED left shift

// End of LED position?

// Yes, set flag of right shift else { }

}

}

PORTD>>=1;

// LED right shift

Dir_Count++; if (Dir_Count==7) { Dir_Count=0; Direct_LED=0x00;

}

北京理工大学-PIC单片机在电子系统设计中的应用-实验三-定时器中断 - 图文.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c33uqe0xnhv3j4le875ru_2.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top