|
我做的程序如下:
/*************主机程序****************
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 uint unsigned int
static volatile uchar Num;
uchar comd[8]; //命令控制字
//void WaitComd(uchar *command);
void WaitComd(void);
//void ProcessComd(void);
void ComdSlave(void);
//interrupt[USART_RXC] 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[USART_RXC] void RecieveData(void)
{
uchar i;
volatile uchar Num1;
/*
EN485=1;
SendByte(0xff);
SendByte(Num);
SendByte(0xff);*/
// if (Num1>7)
// Num1=0;
#asm("cli")
comd[Num1]=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 [TIM0_OVF] 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不能累加呢! |
阿莫论坛20周年了!感谢大家的支持与爱护!!
一只鸟敢站在脆弱的枝条上歇脚,它依仗的不是枝条不会断,而是自己有翅膀,会飞。
|