打印

高手请进,请求帮助。C8051F32X的中断问题

[复制链接]
1301|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
wangfa2011|  楼主 | 2012-6-13 19:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
我想实现的功能是首先PC机向单片机发送(中断发送)信息。然后开始ADC采集(中断方式),然后通过串口发送给PC机。
我自己分别编写了串口的收发,和ADC采集,然后合到一块儿,这样就两个中断的问题了。程序一直在串口中断服务程序跳不出来。


跪求帮助啊,好几天也解决不了啊


#include <c8051f320.h>                 // SFR declarations
#include <stdio.h>
#include <INTRINS.H>
sfr16 DP       = 0x82;                 // data pointer
sfr16 TMR2RL   = 0xca;                 // Timer2 reload value
sfr16 TMR2     = 0xcc;                 // Timer2 counter
sfr16 TMR3RL   = 0x92;                 // Timer3 reload value
sfr16 TMR3     = 0x94;                 // Timer3 counter
sfr16 ADC0     = 0xbd;                 // ADC0 data
sfr16 ADC0GT   = 0xc4;                 // ADC0 greater than window
sfr16 ADC0LT   = 0xc6;                 // ADC0 less than window
sfr16 RCAP2    = 0xca;                 // Timer2 capture/reload
sfr16 T2       = 0xcc;                 // Timer2
#define BAUDRATE     9600              // Baud rate of UART in bps
#define SYSCLK       12000000          // SYSCLK frequency in Hz     
#define SAMPLE_RATE  50000             // Sample frequency in Hz
#define INT_DEC      256               // integrate and decimate ratio
void PORT_Init (void);
void UART0_Init (void);
void ADC0_Init (void);
void Timer2_Init (int counts);
void ADC0_ISR (void);
void Uart0_SendChar(unsigned char chr);
void Uart0_SendBlock(unsigned char *ptr,unsigned char size);
long result;
unsigned char UART_Buffer[10]={'0','0','0','0'};
unsigned char UART_Input_First = 0;
static char Byte;
unsigned char int_ok=0;
unsigned char rx_ready=0;
unsigned int a ,b,c,d;
void Uart0_SendChar(unsigned char chr)
{
while(!TI0);               
SBUF0 = chr;            
    TI0 = 0;      
}
void Uart0_SendBlock(unsigned char *ptr,unsigned int size)
{
unsigned int i;                  

for (i = 0; i < size; i++)
  Uart0_SendChar(*ptr++);  // 发送数据
}
void main (void) {
   int temp_int, temp_frac ;            // integer and fractional portions of
   

   unsigned int temperature;
   unsigned char  Us[4];
                                         // Disable Watchdog timer
   PCA0MD &= ~0x40;                      // WDTE = 0 (clear watchdog timer
                                         // enable)
   OSCICN |= 0x03;                       // Set internal oscillator to highest
   PORT_Init ();                         // initialize crossbar and GPIO
   Timer2_Init (SYSCLK/SAMPLE_RATE);     // initialize Timer3 to overflow at
                                         // sample rate
   UART0_Init ();                        // initialize UART0
   
   ADC0_Init ();                         // init ADC
   AD0EN = 1;                            // enable ADC
   
   EA = 1;
      
  // while(result==0);         //等于0,侧等待                     
   while (1)
   {
     if(int_ok==1)
  {
     int_ok=0;
  UART_Input_First =0;
     a=UART_Buffer[0] -'0';
  b=UART_Buffer[1] -'0';
  c=UART_Buffer[2] -'0';
  d=UART_Buffer[3] -'0';
  printf("%2d%2d%2d%2d\nint_OK\n",a,b,c,d);
  break;
     }

  EA = 0;                          // 关中断
     temperature = result;
  temperature &=0x000fff;
     EA = 1;                              //开中断
     temperature = temperature*100l*3/1024;
     //temperature =1234;
     temp_int  = temperature/100;
  temp_frac = temperature - (temp_int * 100);
  Us[0]=temp_int/10+'0';
  Us[1]=temp_int%10+'0';
  Us[2]=temp_frac/10+'0';
  Us[3]=temp_frac%10+'0';
  Uart0_SendBlock(Us,4);
  printf ("\n");
     printf ("V_input is %02d.%02dV\n", temp_int, temp_frac);
  
}
}
//-----------------------------------------------------------------------------
// PORT配置
//-----------------------------------------------------------------------------
void PORT_Init (void)
{   
   P0SKIP    = 0x4E;
   XBR0    = 0x03;                     // Enable UART on P0.4(TX) and P0.5(RX)                     
   XBR1    = 0x40;                     // Enable crossbar and weak pull-ups
   P2MDIN    = 0x7f;                   // P2.7配置为模拟输入
   P0MDOUT |= 0xff;                    // enable TX0 as a push-pull output  
   P1MDOUT |= 0xff;
   P2MDOUT |= 0xF7;
   P3MDOUT |= 0x04;                    // P3.3 push-pull output

}
//-----------------------------------------------------------------------------
// UART0配置
//-----------------------------------------------------------------------------
// Configure the UART0 using Timer1, for <baudrate> and 8-N-1.
void UART0_Init (void)
{
   SCON0 = 0x10;                       // SCON0: 8-bit variable bit rate
                                       //        level of STOP bit is ignored
                                       //        RX enabled
                                       //        clear RI0 and TI0 bits
   if (SYSCLK/BAUDRATE/2/256 < 1) {
      TH1 = -(SYSCLK/BAUDRATE/2);
      CKCON &= ~0x0B;                  // T1M = 1; SCA1:0 = xx
      CKCON |=  0x08;
   } else if (SYSCLK/BAUDRATE/2/256 < 4) {
      TH1 = -(SYSCLK/BAUDRATE/2/4);
      CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 01                  
      CKCON |=  0x01;
   } else if (SYSCLK/BAUDRATE/2/256 < 12) {
      TH1 = -(SYSCLK/BAUDRATE/2/12);
      CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 00
   } else {
      TH1 = -(SYSCLK/BAUDRATE/2/48);
      CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 10
      CKCON |=  0x02;
   }

   TL1 = TH1;                          // init Timer1
   TMOD &= ~0xf0;                      // TMOD: timer 1 in 8-bit autoreload
   TMOD |=  0x20;        
   
   IP |= 0x10;                         // Make UART high priority
   ES0 = 1;                            // Enable UART0 interrupts
   TI0 = 1;
                  
   TR1 = 1;                            // START Timer1
}
//-----------------------------------------------------------------------------
// ADC0配置,T3定时启动ADC
//-----------------------------------------------------------------------------
void ADC0_Init (void)
{
   ADC0CN = 0xc2;                      // ADC0 T3定时采样
   REF0CN = 0x0e;                      // 启用内部基准源
   AMX0P     = 0x0F;
   AMX0N     = 0x1F;                   //选择采样输入源GND作为负输入ADC工作在单端方式
  //AMX0N     = 0x07;
   ADC0CF = 0x38;   
   EIE1 |= 0x08;                       // 启用 ADC 中断
}
//-----------------------------------------------------------------------------
// Timer2配置,T2定时启动ADC
//-----------------------------------------------------------------------------
void Timer2_Init (int counts)
{
   TMR2CN = 0x00;                      // STOP Timer2; Clear TF2H and TF2L;
                                       // disable low-byte interrupt; disable
                                       // split mode; select internal timebase
   CKCON |= 0x10;                      // Timer2 uses SYSCLK as its timebase
   TMR2RL  = -counts;                  // Init reload values
   TMR2    = TMR2RL;                   // Init Timer2 with reload value
   ET2 = 0;                            // disable Timer2 interrupts
   TR2 = 1;                            // start Timer2
}
//-----------------------------------------------------------------------------
// ADC0采样中断
//-----------------------------------------------------------------------------
void ADC0_ISR (void) interrupt 10
{
   static unsigned int_dec=INT_DEC;   
   static long accumulator=0L;                     
    ADC0CN &= ~0x20;        // 清 ADC 中断标志位
    accumulator += ADC0;                // 累加ADC采样数据
    int_dec--;                          // 指针减1
    if (int_dec == 0)
  {                                  // 累加完了吗?
      int_dec = INT_DEC;                // 指针复位
      result = accumulator>>8;
      accumulator = 0L;                  // 累加和变量清0
     }
}

void UART0_Interrupt (void) interrupt 4
{
   if (RI0 == 1)
   {
      RI0 = 0;                           
      Byte = SBUF0;                     

      if(rx_ready==1)
       {
       if(Byte!=0X25)
        {
      UART_Buffer[UART_Input_First] = Byte;
         UART_Input_First++;            
                  
        }
       else if(Byte== 0X25)    //当缓冲区数据为字符'%'时停止接收,并赋值re_ready=0
     {                 
        int_ok=1;        //返回int_ok值,输出数组值
        rx_ready=0;
                 UART_Input_First=0;
     }
   }
   if(Byte==0X24)    //当缓冲区数据为字符'$'时开始接收,并赋值re_ready=1
   {
     rx_ready=1;
   }
   }

}

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

1

帖子

1

粉丝