找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 732|回复: 1
打印 上一主题 下一主题
收起左侧

MCU进入串口中断后,得到一个时间,但是这个值别的地方用不起来,是作用域的问题吗?

[复制链接]
跳转到指定楼层
楼主
ID:980872 发表于 2022-3-18 13:44 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
各位大神,我用的是C8051F330的单片机,串口发送和接收都没有问题,我在进入串口中断后,读串口,把串口读到的值赋给全局变量time,如果读到的是F1,延时函数就delay()就是5s,但是好像不起作用,不知道是不是time的作用域有问题?
#include <c8051f330.h>                 // SFR declarations
#include <stdio.h>

//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------

#define SYSTEMCLOCK      24500000           // SYSCLK frequency in Hz
#define BAUDRATE             9600           // Baud rate of UART in bps


//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------

void SYSCLK_Init (void);
void UART0_Init (void);
void PORT_Init (void);
void Timer2_Init (int);

//-----------------------------------------------------------------------------
// Global Variables
//-----------------------------------------------------------------------------


char Byte,flag;

unsigned char temp1;

unsigned int time;

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void delay()
{
unsigned int i,j;
        for(i=0;i<time;i++)//100和30000约1s
                {
                        for(j=0;j<30000;j++)//30000是0.008s
                        {
                        
                        }
                }
}


void main (void)
{
                P1=0x00;
        temp1=0x00;
        time=1500;
        PCA0MD &= ~0x40;                    // WDTE = 0 (clear watchdog timer
                                       // enable)
   PORT_Init();                        // Initialize Port I/O
   SYSCLK_Init ();                     // Initialize Oscillator
   UART0_Init();

   EA = 1;
   while(1)
   {
               
                if(temp1<101)//<100
                                {
                                        P1=temp1;
                                                                                                

                        SBUF0=temp1;
                        
                        while(!TI0);
                                TI0=0;        
                                        delay();
                                        temp1++;
                                }
                                else        if(temp1==101)
                                        {        
                                                
                                                temp1=0x80;
                                                
                                                for(temp1=0x80;temp1<0xe5;temp1++)
                                                {
                                                        P1=temp1;
                                                                delay();
                                                        SBUF0=temp1;
                        while(!TI0);
                                                                                TI0=0;
                                                }

                                                        temp1=0x00;
                                        }                                
   }
}


void PORT_Init (void)
{
   P0MDOUT |= 0x10;                    // Enable UTX as push-pull output
        P1MDOUT |= 0xff;
   XBR0    = 0x01;                     // Enable UART on P0.4(TX) and P0.5(RX)
   XBR1    = 0x40;                     // Enable crossbar and weak pull-ups
}

void SYSCLK_Init (void)
{
   OSCICN |= 0x03;                     // Configure internal oscillator for
                                       // its maximum frequency
   RSTSRC  = 0x04;                     // Enable missing clock detector
}


void UART0_Init (void)
{
   SCON0 = 0x10;                       // SCON0: 8-bit variable bit rate
                                       //        level of STOP bit is ignored
                                       //        RX enabled
                                       //        ninth bits are zeros
                                       //        clear RI0 and TI0 bits
   if (SYSTEMCLOCK/BAUDRATE/2/256 < 1) {
      TH1 = -(SYSTEMCLOCK/BAUDRATE/2);
      CKCON &= ~0x0B;                  // T1M = 1; SCA1:0 = xx
      CKCON |=  0x08;
   } else if (SYSTEMCLOCK/BAUDRATE/2/256 < 4) {
      TH1 = -(SYSTEMCLOCK/BAUDRATE/2/4);
      CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 01
      CKCON |=  0x01;
   } else if (SYSTEMCLOCK/BAUDRATE/2/256 < 12) {
      TH1 = -(SYSTEMCLOCK/BAUDRATE/2/12);
      CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 00
   } else {
      TH1 = -(SYSTEMCLOCK/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;
   TR1 = 1;                            // START Timer1   IP |= 0x10;                         // Make UART high priority
   ES0 = 1;                            // Enable UART0 interrupts

}

void UART0_Interrupt (void) interrupt 4
{

unsigned int ab;

   if(RI0==1)
                {
                 RI0 = 0;                           // Clear interrupt flag

      Byte = SBUF0;                      // Read a character from UART
                 
                        P1=Byte;
                        
                        
                        SBUF0=Byte;
                        while(!TI0);
                        TI0=0;
                        if(Byte==0xF1)
                                {
                                time=500;
                                }
                                        else if(Byte==0xF2)
                                                {
                                                        time=1000;
                                                }
                                                                else if(Byte==0xF3)
                                                                        {
                                                                                time=1500;
                                                                        }
                                                                                else if(Byte==0xF4)
                                                                                {
                                                                                        time=2000;
                                                                                }
                                                                                        else
                                                                                                {
                                                                                                         P1=Byte;
                                                                                                }
                        flag=1;
                }
   }


        



分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:980872 发表于 2022-3-18 15:22 | 只看该作者
是Byte的类型不对,改成unsigned char就行了

评分

参与人数 1黑币 +20 收起 理由
admin + 20 回帖助人的奖励!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表