lizhiguo 发表于 2008-5-13 22:10:37

恳请马老师看看我的通信时钟程序!

敬爱的马老师:
    您好!我用AVR的SPI接口实现两片MEGA128通信的实验,在从机的LED数码管显示出来.我是想做一 个通信时钟,也就是在主机中编写时钟程序,通过SPI接口,在从机的6位数码管显示一个时钟.但SPI接口只有8位(一个字节),只能传输一个字节,双机通信我已经实现了一个LED位的传输.我的那个数码管是循环显示的,有4位,那么我该怎样让这四位显示分秒呢?在自发自收中我是实现出来了,但在双机通信中,却实现不了.因为在双机中,不可能让A口和C口的高四位(控制LED的)循环发送啊?
    恳请马老师看看我的程序,并指点一下,谢谢马老师!!
1)主机程序:


#include <iom128v.h>
#include <macros.h>
#define SIZE 4
unsigned char time;
unsigned char dis_buff;//显示缓冲区,存放要显示的6个字符的段码值
unsigned char led_7={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
flash unsigned char position={0xef,0xdf,0xbf,0x7f};
unsigned char SPI_rx_buffer; //接收缓冲区
unsigned char time_counter=0;//1s计数器
unsigned char rx_wr_index,rx_rd_index,rx_counter,rx_buffer_overflow;
unsigned char k;
void port_init(void)
{
PORTA = 0x00;
DDRA= 0x00;
PORTB = 0x00;
DDRB= 0x00;
PORTC=0x00;
DDRC=0x00;
PORTD = 0x00;
DDRD= 0x00;
PORTE = 0x00;
DDRE= 0x00;
PORTF = 0x00;
DDRF= 0x00;
PORTG = 0x00;
DDRG= 0x00;

}
void delay_1ms(void)
{unsigned int i;
for(i=1;i<4*143-2;i++)
;
}
void delay(unsigned int n)
{
unsigned int i=0;
for(i=0;i<n;i++)
delay_1ms();
}
// clock rate: 57599hz
void spi_init(void)
{
char temp;
SPCR = 0xE3; //setup SPI,11100011
SPSR = 0x00; //setup SPI
temp=SPSR;
temp=SPDR;
}

//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
spi_init();

MCUCR = 0x00;
EICRA= 0x00;
EICRB=0x00;
EIMSK=0x00;
EIFR=0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
#pragma interrupt_handler spi_stc_isr:18
void spi_stc_isr(void)
{
//byte in SPDR has been sent/received

SPI_rx_buffer= SPDR;
if (++rx_wr_index == SIZE) rx_wr_index = 0; //放入接收缓冲区,并调整队列指针
if (++rx_counter == SIZE)
    {
      rx_counter = 0;
      rx_buffer_overflow = 1;
    }
       

}
unsigned char getSPIchar(data)
{
//unsigned char temp;
while(rx_counter==0)
//for(k=0;k<=3;k++)
data=SPI_rx_buffer;
//PORTA=data;
//PORTC=position;
// while(!(SPSR&1<<SPIF));
//PORTB|=(1<<PB0);
//SPSR = 0x00; //11010101
//temp = SPSR;//清空接收
//PORTA=SPDR;
//PORTB&=~(1<<PB0);
//PORTC=position;
//delay(2);
//PORTC=0xff;}
if(++rx_rd_index==SIZE)rx_rd_index=0;
CLI();
--rx_counter;
SEI();
return data;

}

void time_to_disbuffer(void)//时间值送显示缓冲区函数
{
unsigned char i,j=0;
for(i=0;i<=1;i++)
{
dis_buff=time%10;
dis_buff=time/10;
}
}
//call this routine to initialize all peripherals

void main(void)

{//PORTA = 0x00;
//DDRA= 0xff;
// PORTC=0x0f;
//DDRC=0xf0;
unsigned char j,temp;
CLI();
//spi_init();

   //初始化SPI接口
init_devices();
PORTA=0Xff;
DDRA=0XFF;
PORTC=0Xff;
DDRC=0Xf0;
   SEI();
DDRB|=(1<<PB3); //MIso 设置为输出
   while(1)
{
for(k=0;k<=3;k++)
{getSPIchar(j);//等待spi中断{};
   while(!(SPSR&1<<SPIF));
PORTB|=(1<<PB0);
SPSR = 0x00; //11010101
temp = SPSR;//清空接收
PORTA=j;
PORTB&=~(1<<PB0);
PORTC=position;
delay(2);
PORTC=0xff; //关闭所有的选通位
}

}
}
2)主机程序:

#include <iom128v.h>
#include <macros.h>
#define SIZE 10
unsigned char led_7={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
flash unsigned char position={0xef,0xdf,0xbf,0x7f};
unsigned char SPI_tx_buff;
unsigned char tx_wr_index,tx_rd_index,tx_counter;
unsigned char time;
unsigned char dis_buff;//显示缓冲区,存放要显示的6个字符的段码值
unsigned char time_counter=0;//1s计数器
unsigned chartemp;
void port_init(void)
{
PORTA = 0x00;
DDRA= 0x00;
PORTB = 0x00;
DDRB= 0x00;
PORTC = 0x00;
DDRC= 0x00;
PORTD = 0xFF;
DDRD= 0xFF;
PORTE = 0x00;
DDRE= 0x00;
PORTF = 0x00;
DDRF= 0x00;
PORTG = 0x00;
DDRG= 0x00;
}

//SPI initialize
//clock rate: 6mhz
void spi_init(void)

{
PORTB |= (1<<PB0) | (1<<PB1) | (1<<PB2)|(1<<PB3) ;
DDRB |= (1<<DDB0) | (1<<DDB1) | (1<<DDB2);      //Set MOSI, SCK AND SS as outputs
SPCR = 0x73; //setup SPI, 11110011
SPSR = 0x00; //setup SPI,fosc/128
}

//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
spi_init();

MCUCR = 0x00;
EICRA= 0x00;
EICRB=0x00;
EIMSK=0x00;
EIFR=0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}

   #pragma interrupt_handler spi_stc_isr:18
void spi_stc_isr(void)
{if(tx_counter)
{
   --tx_counter;
   SPDR=SPI_tx_buff;
   if(++tx_rd_index==SIZE)tx_rd_index=0;
   }
   }
void time_to_disbuffer(void)//时间值送显示缓冲区函数
{
unsigned char i,j=0;
for(i=0;i<=1;i++)
{
dis_buff=time%10;
dis_buff=time/10;
}
}
void putSPIchar(char c)
{ unsigned char k;
PORTB &=~ (1<<PB0); //拉底ss,强制接收方进入从模式
SPCR |= (1<<MSTR); // MSTR有时会被清零,这里强制进入主机模式
//启动数据传输
   for(k=0;k<=3;k++)
   {c=led_7];
while (tx_counter == SIZE);//发送缓冲区满,等待
CLI();
if (tx_counter || ((SPSR & 0x80) == 0))//发送缓冲区已中有待发数据
    { //或SPI正在发送数据时
      SPI_tx_buff = c; //将数据放入发送缓冲区排队
          if(tx_counter)
    {
   --tx_counter;
   SPDR=SPI_tx_buff;
   if(++tx_rd_index==SIZE)tx_rd_index=0;
   }
      if (++tx_wr_index == SIZE) tx_wr_index = 0; //调整指针
      ++tx_counter;
    }
else
    SPDR = c; //发送缓冲区中空且SPI口空闲,直接放入SPDR由SIP口发送
SEI();
//unsigned char k,cData;
}
}




void delay_1ms(void)
{unsigned int i;
for(i=1;i<4*143-2;i++)
;
}
void delay(unsigned int n)
{
unsigned int i=0;
for(i=0;i<n;i++)
delay_1ms();
}

void main(void)

{
unsigned char i;
init_devices();
time=56;time=23;//时间初值为58:55
time_to_disbuffer();
while(1)
{
putSPIchar(i);
i++;
if(++time_counter>=40)//显示扫描,执行时间为12ms
{
time_counter=0;
if(++time>=60)
{
time=0;
if(++time>=60)
{time=0;
}
}
time_to_disbuffer();
}
delay(17);//延时13ms,可进行其他处理
}
}

machao 发表于 2008-5-13 22:27:03

需要定义一个通信包.比如最简单这个包为2个字节,前一个是数值,后一个是位值.SPI必须每次送一个包.

用户通信协议的指定,具体可参考我编写书中RS232通信的部分.
页: [1]
查看完整版本: 恳请马老师看看我的通信时钟程序!