感谢帮助!
初步接触单片机,编的程序比较简单。手里的板子是MSP430FR4133母版。现在遇到两个问题:
1. 第一次运行程序的时候LCD正常显示,再次开机要运行程序的时候LCD不再工作,并且单片机刚查进电脑时也没有上一次的工作记忆
2. 一共用了外部中断,ADC中断和定时器三个中断,通过用断点来检测,并没有进入外部中断,ADC和定时器中断都正常工作。找来找去没有发现外部中断的设置错在哪。
附上主程序
#include <msp430.h>
#include <driverlib.h>
#include "hal_LCD.h"
#include <driverlib.h>
#include "hal_LCD.h"
int number=0;
int time=1;
int time=1;
// External Interrupt Port1
#pragma vector = PORT1_VECTOR
__interrupt void P1_ISR(void)
{
switch(__even_in_range(P1IV,P1IV_P1IFG7)) {
case P1IV_P1IFG7: //It is SW1,trigger interrupt
showChar('N', pos1);
GPIO_clearInterrupt(GPIO_PORT_P1, GPIO_PIN2);
break;
}
}
switch(__even_in_range(P1IV,P1IV_P1IFG7)) {
case P1IV_P1IFG7: //It is SW1,trigger interrupt
showChar('N', pos1);
GPIO_clearInterrupt(GPIO_PORT_P1, GPIO_PIN2);
break;
}
}
//External Interrupt Port2
#pragma vector = PORT2_VECTOR
__interrupt void P2_ISR(void)
#pragma vector = PORT2_VECTOR
__interrupt void P2_ISR(void)
{
switch(__even_in_range(P2IV,P2IV_P2IFG7))
{
case P2IV_P2IFG7: //It is SW2,change the time period of timer
time++; // pressing times
if(time==4) time=1;
GPIO_clearInterrupt(GPIO_PORT_P2,GPIO_PIN6);
break;
switch(__even_in_range(P2IV,P2IV_P2IFG7))
{
case P2IV_P2IFG7: //It is SW2,change the time period of timer
time++; // pressing times
if(time==4) time=1;
GPIO_clearInterrupt(GPIO_PORT_P2,GPIO_PIN6);
break;
}}
//ADC Interrupt
#pragma vector=ADC_VECTOR // these two lines are used by IAR and CCC
__interrupt void ADC_ISR(void)
{
switch(__even_in_range(ADCIV,ADCIV_ADCIFG))
{
case ADCIV_ADCIFG: // conversion complete
number = ADCMEM0;
break;
}
ADC_clearInterrupt(ADC_BASE,ADC_COMPLETED_INTERRUPT);
}
#pragma vector=ADC_VECTOR // these two lines are used by IAR and CCC
__interrupt void ADC_ISR(void)
{
switch(__even_in_range(ADCIV,ADCIV_ADCIFG))
{
case ADCIV_ADCIFG: // conversion complete
number = ADCMEM0;
break;
}
ADC_clearInterrupt(ADC_BASE,ADC_COMPLETED_INTERRUPT);
}
//Timer Interrupt
#pragma vector = TIMER0_A0_VECTOR
__interrupt void TIMERA0_ISR0(void) //Flag cleared automatically
{ int a,b,c,d;
showChar('N', pos1);
a=number/1000; // Kilobit
b=number/100%10; // Hundreds
c=number/10%10; // Decade
d=number%10; // the unit
showChar(a+48,pos3); // the position of kilobit
showChar(b+48,pos4); // the position of hundreds
showChar(c+48,pos5); // the position of decade
showChar(d+48,pos6); // the position of the unit
TA0CCR0 = time*20000; //the compare value, confirm it can trigger the interrupt }
#pragma vector = TIMER0_A0_VECTOR
__interrupt void TIMERA0_ISR0(void) //Flag cleared automatically
{ int a,b,c,d;
showChar('N', pos1);
a=number/1000; // Kilobit
b=number/100%10; // Hundreds
c=number/10%10; // Decade
d=number%10; // the unit
showChar(a+48,pos3); // the position of kilobit
showChar(b+48,pos4); // the position of hundreds
showChar(c+48,pos5); // the position of decade
showChar(d+48,pos6); // the position of the unit
TA0CCR0 = time*20000; //the compare value, confirm it can trigger the interrupt }
//Timer general interrupt
#pragma vector = TIMER0_A1_VECTOR
__interrupt void TIMERA0_ISR1(void){
switch(__even_in_range(TA0IV,10)) //Clears the flag {
case 2:
ADCCTL0|=ADCENC|ADCSC; //ADC enabled and start sample-and-conversion
TA0CCR1 = 0x800; // the compare value,ensure that TA0CCR1 is less than TA0CCR0
}
Timer_A_clearTimerInterrupt(TIMER_A0_BASE);
}
#pragma vector = TIMER0_A1_VECTOR
__interrupt void TIMERA0_ISR1(void){
switch(__even_in_range(TA0IV,10)) //Clears the flag {
case 2:
ADCCTL0|=ADCENC|ADCSC; //ADC enabled and start sample-and-conversion
TA0CCR1 = 0x800; // the compare value,ensure that TA0CCR1 is less than TA0CCR0
}
Timer_A_clearTimerInterrupt(TIMER_A0_BASE);
}
// main function
int main(void)
{
//Default MCLK = 1MHz
int main(void)
{
//Default MCLK = 1MHz
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
// Disable the GPIO power-on default high-impedance mode
// to activate previously configured port settings
GPIO_selectInterruptEdge(GPIO_PORT_P1, GPIO_PIN2, GPIO_LOW_TO_HIGH_TRANSITION);
GPIO_setAsInputPinWithPullUpResistor(GPIO_PORT_P1, GPIO_PIN2);
GPIO_setAsInputPinWithPullUpResistor(GPIO_PORT_P1, GPIO_PIN2);
GPIO_clearInterrupt(GPIO_PORT_P1, GPIO_PIN2);
GPIO_enableInterrupt(GPIO_PORT_P1, GPIO_PIN2);
GPIO_selectInterruptEdge(GPIO_PORT_P2, GPIO_PIN6, GPIO_LOW_TO_HIGH_TRANSITION);
GPIO_setAsInputPinWithPullUpResistor(GPIO_PORT_P2, GPIO_PIN6);
GPIO_setAsInputPinWithPullUpResistor(GPIO_PORT_P2, GPIO_PIN6);
GPIO_clearInterrupt(GPIO_PORT_P2, GPIO_PIN6);
GPIO_enableInterrupt(GPIO_PORT_P2, GPIO_PIN6);
// ADC
GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P8, GPIO_PIN1, GPIO_PRIMARY_MODULE_FUNCTION); //output port P8.1
SYSCFG2|=ADCPCTL9; //ADC input A9 enabled
ADCCTL0|=ADCSHT_1|ADCON; //set samping time (0001b=8 ADCCLK cycles), turn on ADC
ADCCTL1|=ADCSHP; //using sampling time, signal is sourced from the sampling timer
ADCCTL2|=ADCRES; //change to 10bits, 12 clock cycle conversion time
ADCMCTL0|=ADCINCH_9; // Input channel select A9
ADC_clearInterrupt(ADC_BASE,ADC_COMPLETED_INTERRUPT); //configure the Interrupt
ADC_enableInterrupt(ADC_BASE,ADC_COMPLETED_INTERRUPT); //open the interrupt
// Timer
TA0CTL |= TASSEL_1; //32k ACLK clock
TA0CTL |= ID_1; //Divide by 2
TA0CTL |= MC_1; //Count up
TA0CTL |= TACLR; //Clear the timer
TA0CTL |=TAIE; //Interrupt enabled
TA0CCTL0 |=CCIE; //Interrupt enabled
TA0CCR0 = 0x800; //2048 – 1 second, the compare value
TA0CCTL1 |=CCIE; //Interrupt enabled
TA0CCR1 = 0x666; //1638 – 1 second, the compare value
ADCCTL0|=ADCENC|ADCSC; //ADC enabled and start sample-and-conversion
GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P8, GPIO_PIN1, GPIO_PRIMARY_MODULE_FUNCTION); //output port P8.1
SYSCFG2|=ADCPCTL9; //ADC input A9 enabled
ADCCTL0|=ADCSHT_1|ADCON; //set samping time (0001b=8 ADCCLK cycles), turn on ADC
ADCCTL1|=ADCSHP; //using sampling time, signal is sourced from the sampling timer
ADCCTL2|=ADCRES; //change to 10bits, 12 clock cycle conversion time
ADCMCTL0|=ADCINCH_9; // Input channel select A9
ADC_clearInterrupt(ADC_BASE,ADC_COMPLETED_INTERRUPT); //configure the Interrupt
ADC_enableInterrupt(ADC_BASE,ADC_COMPLETED_INTERRUPT); //open the interrupt
// Timer
TA0CTL |= TASSEL_1; //32k ACLK clock
TA0CTL |= ID_1; //Divide by 2
TA0CTL |= MC_1; //Count up
TA0CTL |= TACLR; //Clear the timer
TA0CTL |=TAIE; //Interrupt enabled
TA0CCTL0 |=CCIE; //Interrupt enabled
TA0CCR0 = 0x800; //2048 – 1 second, the compare value
TA0CCTL1 |=CCIE; //Interrupt enabled
TA0CCR1 = 0x666; //1638 – 1 second, the compare value
ADCCTL0|=ADCENC|ADCSC; //ADC enabled and start sample-and-conversion
__enable_interrupt(); //open the interrupt
PMM_unlockLPM5();
while(1)
{
PMM_unlockLPM5();
while(1)
{
}
}
}
感谢帮助!!
Tian Tian5:
刚才自己找出一个错误 case P1IV_P1IFG7 IFG后面应该写PIN口。改过来以后,外部中断只能进去设置time的那个,但是触发计时器的P1.2的中断还是进不了
灰小子:
回复 Tian Tian5:
程序比较长的时候,建议先分模块调试。没问题再组合在一起。
或者调试的时候,先把问题定位到一定范围,然后查找具体原因。