搜索
bottom↓
回复: 1

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

[复制链接]

出0入0汤圆

发表于 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[2];
unsigned char dis_buff[4];//显示缓冲区,存放要显示的6个字符的段码值
unsigned char led_7[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
flash unsigned char position[4]={0xef,0xdf,0xbf,0x7f};
unsigned char SPI_rx_buffer[10]; //接收缓冲区
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[rx_wr_index]= 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[rx_rd_index];
  //PORTA=data;
  //PORTC=position[k];
// while(!(SPSR&1<<SPIF));
  //PORTB|=(1<<PB0);
  //SPSR = 0x00; //11010101
  //temp = SPSR;//清空接收
  //PORTA=SPDR;
  //PORTB&=~(1<<PB0);
//PORTC=position[k];
//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[j++]=time%10;
  dis_buff[j++]=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[k];
delay(2);
PORTC=0xff; //关闭所有的选通位
}
  
}
}
2)主机程序:

  #include <iom128v.h>
#include <macros.h>
#define SIZE 10
unsigned char led_7[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
flash unsigned char position[4]={0xef,0xdf,0xbf,0x7f};
unsigned char SPI_tx_buff[SIZE];
unsigned char tx_wr_index,tx_rd_index,tx_counter;
unsigned char time[2];
unsigned char dis_buff[4];//显示缓冲区,存放要显示的6个字符的段码值
unsigned char time_counter=0;//1s计数器
unsigned char  temp;
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[tx_rd_index];
   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[j++]=time%10;
  dis_buff[j++]=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[dis_buff[k]];
  while (tx_counter == SIZE);//发送缓冲区满,等待
  CLI();
  if (tx_counter || ((SPSR & 0x80) == 0))//发送缓冲区已中有待发数据
    { //或SPI正在发送数据时
      SPI_tx_buff[tx_wr_index] = c; //将数据放入发送缓冲区排队
          if(tx_counter)
    {
   --tx_counter;
   SPDR=SPI_tx_buff[tx_rd_index];
   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[1]=56;time[0]=23;//时间初值为58:55
time_to_disbuffer();
while(1)
{
putSPIchar(i);
i++;
if(++time_counter>=40)//显示扫描,执行时间为12ms
  {
  time_counter=0;
if(++time[0]>=60)
  {
  time[0]=0;
  if(++time[1]>=60)
  {  time[1]=0;
  }
}
time_to_disbuffer();
  }
delay(17);//延时13ms,可进行其他处理
}
}

阿莫论坛20周年了!感谢大家的支持与爱护!!

知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)

出0入0汤圆

发表于 2008-5-13 22:27:03 | 显示全部楼层
需要定义一个通信包.比如最简单这个包为2个字节,前一个是数值,后一个是位值.SPI必须每次送一个包.

用户通信协议的指定,具体可参考我编写书中RS232通信的部分.
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-7-23 09:23

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

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