对于单片机系统,中断是非常重要的一个特性,也是很值得去研究的。
中断的定义:出现需要时,CPU暂时停止当前程序的执行转而执行处理新情况的程序和执行过程。
MSP430F5438A的很多部分都可以产生中断,比如IO口(只有P1,P2口),ADC,Timer_A 等,但是当多种中断在同时等待时,它们之间内部的优先级决定了哪个中断应该先被处理,哪个中断应该后处理。中断处理优先级和中断向量BASEADDRESS如下所示:
暂时只检测只有一个中断的情况,即不用考虑中断优先级的情况。以ADC12_A为中断源.
ADC12_A的中断标志位寄存器可以记为ADC12IFGx,ADC12IFG0是ADC12IFGx的最低位,当ADC12MEM0装载了转换结果后,这个位置1,而当ADC12MEM0存取后,这个位被重置,它也可以由软件重置。
在IAR_workbench 里面写入以下代码:
#include "msp430x54x.h"
volatile unsigned int result; // Needs to be global in this example,Otherwise, the compiler removes it
// because it is not used for anything.
void InitADC12_A()
{
ADC12CTL0 = ADC12ON+ADC12MSC+ADC12SHT0_15;// Turn on ADC12, set sampling time
ADC12CTL1 = ADC12SHP+ADC12CONSEQ_0; // Use sampling timer, single
ADC12MCTL0 = ADC12SREF_2 + ADC12INCH_3; // ref+=Veref=2.5V, channel = A3
ADC12IE = ADC12IE0; // Enable ADC12IFG.0
ADC12CTL0 |= ADC12ENC; // Enable conversions
}
#pragma vector=ADC12_VECTOR
__interrupt void ADC12ISR (void)
{
switch(__even_in_range(ADC12IV,34))
{
case 0: break; // Vector 0: No interrupt
case 2: break; // Vector 2: ADC overflow
case 4: break; // Vector 4: ADC timing overflow
case 6: // Vector 6: ADC12IFG0
result = ADC12MEM0; // Move results
__bic_SR_register_on_exit(LPM4_bits); // Exit active CPU, SET BREAKPOINT HERE
case 8: break; // Vector 8: ADC12IFG1
case 10: break; // Vector 10: ADC12IFG2
case 12: break; // Vector 12: ADC12IFG3
case 14: break; // Vector 14: ADC12IFG4
case 16: break; // Vector 16: ADC12IFG5
case 18: break; // Vector 18: ADC12IFG6
case 20: break; // Vector 20: ADC12IFG7
case 22: break; // Vector 22: ADC12IFG8
case 24: break; // Vector 24: ADC12IFG9
case 26: break; // Vector 26: ADC12IFG10
case 28: break; // Vector 28: ADC12IFG11
case 30: break; // Vector 30: ADC12IFG12
case 32: break; // Vector 32: ADC12IFG13
case 34: break; // Vector 34: ADC12IFG14
default: break;
}
}
void main(void)
{
InitADC12_A();
WDTCTL = WDTPW+WDTHOLD; // Stop watchdog timer
ADC12CTL0 |= ADC12SC; // Start convn - software trigger
__bis_SR_register(LPM4_bits + GIE); // Enter LPM4, Enable interrupts
__no_operation(); // For debugger
}
在A3口接入电压为1.407V,然后debug,在debug页面发现:
所以成功转换,而且通过中断将结果写入了result里面。
中断的作用说到底只是用来实现突发事件的检测和运行,例如我们也可以实现,运用ADC12的中断实现P11.0上的红色LED灯实现点亮的操作。
#include "msp430x54x.h"
volatile unsigned int result; // Needs to be global in this example,Otherwise, the compiler removes it
// because it is not used for anything.
void InitADC12_A()
{
ADC12CTL0 = ADC12ON+ADC12MSC+ADC12SHT0_15;// Turn on ADC12, set sampling time
ADC12CTL1 = ADC12SHP+ADC12CONSEQ_0; // Use sampling timer, single
ADC12MCTL0 = ADC12SREF_2 + ADC12INCH_3; // ref+=Veref=2.5V, channel = A3
ADC12IE = ADC12IE0; // Enable ADC12IFG.0
ADC12CTL0 |= ADC12ENC; // Enable conversions
}
#pragma vector=ADC12_VECTOR
__interrupt void ADC12ISR (void)
{
switch(__even_in_range(ADC12IV,34))
{
case 0: break; // Vector 0: No interrupt
case 2: break; // Vector 2: ADC overflow
case 4: break; // Vector 4: ADC timing overflow
case 6: // Vector 6: ADC12IFG0
result = ADC12MEM0; // Move results
P11OUT &= ~BIT0; //light red LED
__bic_SR_register_on_exit(LPM4_bits);
case 8: break; // Vector 8: ADC12IFG1
case 10: break; // Vector 10: ADC12IFG2
case 12: break; // Vector 12: ADC12IFG3
case 14: break; // Vector 14: ADC12IFG4
case 16: break; // Vector 16: ADC12IFG5
case 18: break; // Vector 18: ADC12IFG6
case 20: break; // Vector 20: ADC12IFG7
case 22: break; // Vector 22: ADC12IFG8
case 24: break; // Vector 24: ADC12IFG9
case 26: break; // Vector 26: ADC12IFG10
case 28: break; // Vector 28: ADC12IFG11
case 30: break; // Vector 30: ADC12IFG12
case 32: break; // Vector 32: ADC12IFG13
case 34: break; // Vector 34: ADC12IFG14
default: break;
}
}
void main(void)
{
InitADC12_A();
WDTCTL = WDTPW+WDTHOLD; // Stop watchdog timer
ADC12CTL0 |= ADC12SC; // Start convn - software trigger
P11DIR |= BIT0; //设置P11.0为输出
P11OUT |= BIT0;
__bis_SR_register(LPM4_bits + GIE); // Enter LPM4, Enable interrupts
__no_operation(); // For debugger
}
结果成功实现了A4通道的转换,将结果写到了result里面,并且点亮了红色LED灯。
上一篇:MSP430F5438学习笔记 TA1溢出中断加比较匹配中断
下一篇:MSP430学习笔记2-跑马灯程序,熟悉定时器寄存器的配置