bobooliu 发表于 2007-7-13 12:08:25

为什么我定义的变量不能累加呢?急

我做的程序如下:

/*************主机程序****************

MCU Type:Atmega8515

Creat Date:07/06/14

Creat By:boboo

Modify Date:07/07/3

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

#include <mega8515.h>

#include <stdio.h>

#include <delay.h>   



#define EN485    PORTC.0    //发送、接收使能。EN485=0;单片机接收

#define slave    0x01



#define uchar unsigned char

#define uintunsigned int

                     

static volatile ucharNum;   

uchar comd;            //命令控制字

//void WaitComd(uchar *command);

voidWaitComd(void);

//void ProcessComd(void);

void ComdSlave(void);

//interrupt void RecieveData(void);

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

Fuction:发送1个字节

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

void SendByte(uchar dat)   

{

   while (!(UCSRA&0x20));



   UDR = dat;

}                                                                        

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

Fuction:接收1个字节

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

uchar RecvByte(void)

{               

   uchar dat;

               

   while (!(UCSRA&0x80));

    dat=UDR;



    return dat;

}   

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

void TimerInit(void)

{

   TCCR0=0x00;//预分频1/256

   TCNT0=0x00;

   TIMSK=0x02;//T1中断使能

   ACSR=0x80;

   TCCR1A=0x00;

   TCCR1B=0x00;

}               

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

Fuction:串口初始化

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

void InitUrat(void)

{            

UCSRA = 0x00;//单倍速,多机模式

UCSRB = 0xD8;//发送,接收使能,8位数据格式

UCSRC = 0x86;//奇效验,设置数据格式

UBRRH = 0x00;

UBRRL = 0xbf;//baud=4800;

}

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

Function:串口接收中断程序

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

interrupt void RecieveData(void)

{         

uchar i;

volatile uchar Num1;

            /*

EN485=1;      

SendByte(0xff);

SendByte(Num);

SendByte(0xff);*/

//if (Num1>7)

    //    Num1=0;

    #asm("cli")

    comd=UDR;

    Num1++;                              

if (UDR==0xba)

    {PORTB=0x00;

   for (i=0;i<0xff;i++)

      {

      EN485=1;

      //SendByte(i);

      }

      //SendByte(UDR);                     

      SendByte(Num1);

      delay_ms(800);

   }

    PORTB=0xff;   

    EN485=0;   

    #asm("sei")

}

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

interrupt void Timer0(void)

{            

static uchar k=0;



PORTB=0;

TCNT0+=0x06;

k++;

if (k>125)

    {

   k=0;

   Num++;

   EN485=1;

   PORTB=0x00;

   SendByte(Num);

    }

}

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

void main(void)

{

uchar i;

   

InitUrat();

TimerInit();

DDRB = 0xff;

PORTB = 0xff;

DDRC=0xff;      

EN485=0;                        //从机处于接收状态

#asm("sei")                     //插入汇编语句,打开全局中断



while (1)

{      

}      

}





Num不能累加呢!

doujiao 发表于 2007-7-17 21:08:38

那可能是你没有进中断

dellp4_51 发表于 2007-8-2 01:00:56

timer0 的溢出中断使能打开了没有?
页: [1]
查看完整版本: 为什么我定义的变量不能累加呢?急