⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uart5.c

📁 uart5/atmega8515/led 时钟显示
💻 C
字号:
#include <avr/io.h>
#include <inttypes.h>
#include<avr/interrupt.h>
#include<avr/signal.h>
#define BAUDRATE       9600
#define uchar unsigned char    //数据类型说明
#define uint  unsigned int     //数据类型说明
unsigned char com_in_buf[3];
uchar dis_code[16]={0xfe,0xc8,0xbd,0xed,0xcb,0xe7,0xf7,0xcc,0xff,0xef,0x01,0x83,0xc6,0xa1,0x86,0x8e} ;
uchar dis_buf[16] ;
uchar cnt=0;
uchar timer[8]={0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa};
uchar data[8]={0,0,0,0,0,0,0,0};

void delay_1ms(void)
  {
   unsigned int i;
   for (i=0;i<1140;i++);
  }
void delay_nms(unsigned int n) 
  {
   unsigned int i=0;
   for (i=0;i<n;i++)
   delay_1ms();
  }
void display(uchar *p)
{uchar sel=0x04,i;
for (i=0;i<6;i++)
{PORTB=0X00;
PORTD=0X00;
asm("nop");
PORTB=dis_code[p[i]];
PORTD=sel;
sel=sel<<1;
delay_nms(6);
}
}
void port_init(void)
{
 PORTA = 0xFF;
 DDRA  = 0xFF;
 DDRC  = 0xff;
 PORTC = 0xff;
 DDRB  = 0xff;
 PORTB = 0xff;
 DDRD  = 0xff;
 PORTD = 0xff;
}
void uart0_init(void)
{   UCSRC = (1<<URSEL) | 0x06;
	UCSRB = 0x00;
    UCSRA = 0x00;
    UBRRL= (F_CPU/BAUDRATE/16-1)%256;
    UBRRH= (F_CPU/BAUDRATE/16-1)/256;
    UCSRA = 0x00;
    UCSRB =(1<<RXEN)|(1<<TXEN)|(1<<RXCIE);
}
void put_c(unsigned char c) 
{
	while( !(UCSRA & (1<<UDRE)) );
	UDR=c;
}
void put_s(unsigned char *ptr)
{   uint i;
	for(i=0;i<4;i++)
	{
	put_c(ptr[i]);
	}
}
SIGNAL(SIG_UART_RECV)
{uchar i;
TCNT1 = 0x0000;
com_in_buf[0]=timer[0];
for(i=0;i<8;i++){
while( !(UCSRA & (1<<RXC)) ){display(data);}
 timer[i]=UDR;
 com_in_buf[1]=timer[0]&0X0F;
  if(com_in_buf[1]!=0x0F)
 {timer[0]=com_in_buf[0];
 break;}
 }
}
void init_devices(void)
{
 cli(); 
 port_init();
 uart0_init();
 MCUCR = 0x00;
 GICR  = 0x00;
 TIMSK = 0x00; 
    TCCR1A = 0x00;
	TCCR1B = 0x05;
	TCNT1 = 0x0000;
	TIFR |= 0x80;
	TIMSK |= 0x80;
 sei();
 
}
void process(uchar *p1,uchar*p2)
{p2[0]=p1[1]/16;
p2[1]=p1[1]-p2[0]*16;
p2[2]=p1[2]/16;
p2[3]=p1[2]-p2[2]*16;
p2[4]=p1[3]/16;
p2[5]=p1[3]-p2[4]*16;
//p2[6]=p1[0]/16;
//p2[7]=p1[0]-p2[6]*16;
}

int main(void)
{ //uchar j;
  PORTB=0X00;
  PORTD=0X00;
  delay_nms(500);
  init_devices();
  
  while(1)
  {  
   process(timer,data);
   display(data);
   
   }
}
INTERRUPT(SIG_OVERFLOW1)
{uchar i;   
 {for(i=0;i<8;i++){
	 
	 timer[i]=0xaa;}
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -