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

📄 wxjtest1.c

📁 almegal128串口编程
💻 C
字号:
/*
    Title:    AVR-GCC test program #1 for the STK200 eva board
    Author:   Volker Oth
    Date:     4/1999
    Purpose:  Flashes the LEDs on Port B with a hard coded delay loop
    needed
    Software: AVR-GCC
    needed
    Hardware: ATS90S8515/8535/2313/mega on STK200/STK300 board
    Note:     To contact me, mail to
                  volkeroth@gmx.de
              You might find more AVR related stuff at my homepage:
                  http://members.xoom.com/volkeroth
*/
#include <io.h>
#include <interrupt.h>
#include <signal.h>
#include <sig-avr.h>Z
#include <fs/uromfs.h>

#define F_CPU            14745600      /* 4Mhz */
#define UART_BAUD_RATE       9600      /* 9600 baud */


#define UART_BAUD_SELECT (F_CPU/(UART_BAUD_RATE*16l)-1)


typedef unsigned char  u08;
typedef          char  s08;
typedef unsigned short u16;
typedef          short s16;


/* uart globals */
static volatile u08 *uart_data_ptr;
static volatile u08 uart_counter;

unsigned char led,seriel_data,seriel_re_time,trans_data,
		seriel_tx_time,trans_cpl=0;
u16 rate;
void uart_init(u16 band);
u08 seriel_re_buf[42]={0};
u08 seriel_tx_buf[42]={"Equipment Is Ready,Enter Command"};
u08 seriel_trans_ok,xi,size=40;
/*prog_char message_to_kandshake[8][40]=
                  {
                   {"The eqiupment is ready."},
                   {"please enter the optional commands."},
                   {"please enter a new parameter!"},
                   {"gain_set:XX  freq_set:XX  outp_set:XX"},
                   {"work_status_check:"},
                   {"speed of comminication set:"},
                   {"maximum level of output:"},
                   {"command errer!"},
                   {"please enter a correct command."}
                  };*/

//SIGNAL(SIG_OVERFLOW0)        /* signal handler for tcnt0 overflow interrupt */
//{
 //   outp(~led, PORTB);       /* invert the output since a zero means: LED on */
 //   led <<= 1;               /* move to next LED */
 //   if (!led)                /* overflow: start with Port B0 again */
 //       led = 1;
 //   outp(0x40, TCNT0);          /* reset counter to get this interrupt again 
                                 //定时器初值*/
//}
                         



void uart_init(u16 baud)
         /* initialize uart */
{
    /* enable RxD/TxD and ints */
    outp((1<<RXCIE)|(1<<TXCIE)|(1<<RXEN)|(1<<TXEN),UCSR0B);
    outp((1<<RXCIE)|(1<<TXCIE)|(1<<RXEN)|(1<<TXEN),UCSR1B);     
    /*outp(1<<RXC,UCSR1A);*/     
    /* set baud rate */
    outp((u08)UART_BAUD_SELECT, UBRR0L);
    outp((u08)UART_BAUD_SELECT, UBRR1L);
    baud=UART_BAUD_SELECT;
    UBRR0H=(u08)baud>>8;
    UBRR1H=(u08)baud>>8;
    //outp((u08)baud,UBRR0L);         
}

SIGNAL(SIG_UART0_TRANS)      
/* signal handler for uart txd ready interrupt */
{
    seriel_tx_time++;
    //led=~led;
    /*outp(led,PORTB);*/
    if (uart_counter>0)
    {
    	uart_counter--;
    	trans_data=(u08)seriel_tx_buf[seriel_tx_time];
        outp(trans_data, UDR0);       /* write byte to data buffer */
    }
    else
    {
    	trans_cpl=1;
    	//outp(0<<TXEN,UCSR0B);
    }
}

SIGNAL(SIG_UART1_TRANS)      
/* signal handler for uart txd ready interrupt */
{
    seriel_tx_time++;
    //led=~led;
    /*outp(led,PORTB);*/
    if (uart_counter>0)
    {
    	uart_counter--;
    	trans_data=(u08)seriel_tx_buf[seriel_tx_time];
        outp(trans_data, UDR1);       /* write byte to data buffer */
    }
    else
    {
    	trans_cpl=1;
    	//outp(0<<TXEN,UCSR0B);
    }
}



SIGNAL(SIG_UART0_RECV)      
/* signal handler for receive complete interrupt */
{

    seriel_data = inp(UDR0);        /* read byte for UART data buffer */
    seriel_re_buf[seriel_re_time]=seriel_data;
    //outp(~led, PORTB);     /*output received byte to PortB (LEDs) */
    if((seriel_re_time>0)&&(seriel_re_time<=30)&&(!(seriel_data==0x0d))&&(!(seriel_data==0x20)))
    {
    	seriel_re_time++;
    }
    else
    {
    	seriel_trans_ok=1;
    	for(xi=0;xi<=seriel_re_time;xi++)
    	{
    		seriel_tx_buf[xi]=seriel_re_buf[xi];
    	}
    	size=seriel_re_time;
    	seriel_re_time=0;
    }
}

SIGNAL(SIG_UART1_RECV)      
/* signal handler for receive complete interrupt */
{

    seriel_data = inp(UDR1);        /* read byte for UART data buffer */
    seriel_re_buf[seriel_re_time]=seriel_data;
    //outp(~led, PORTB);     /*output received byte to PortB (LEDs) */
    if((seriel_re_time>0)&&(seriel_re_time<=30)&&(!(seriel_data==0x0d))&&(!(seriel_data==0x20)))
    {
    	seriel_re_time++;
    }
    else
    {
    	seriel_trans_ok=1;
    	for(xi=0;xi<=seriel_re_time;xi++)
    	{
    		seriel_tx_buf[xi]=seriel_re_buf[xi];
    	}
    	size=seriel_re_time;
    	seriel_re_time=0;
    }
}


void uart_send(void)
/* send buffer <buf> to uart */
{   
//    if (!uart_counter) 
//    {
        /* write first byte to data buffer */
        seriel_tx_time  =0;    
        uart_counter=size;
        //uart_data_ptr=seriel_tx_buf;                                                                                       
      outp(seriel_tx_buf[0],UDR0);                                                                                       
      // outp(seriel_tx_buf[0],UDR1);
        
//    }
}




int main(void)
{
u08 i,j;
    outp(0xff,DDRB);         /* use all pins on PortB for output */

//    outp((1<<TOIE0), TIMSK); /* enable TCNT0 overflow 中断允许*/
//    outp(0x40,   TCNT0);        /* reset TCNT0 计数器初值*/
//    outp(6,   TCCR0);        /* count with cpu clock/256
                               // 即:每256个时钟周期计数器加1*/
    //rate=(u16)UART_BAUD_RATE;
    seriel_trans_ok=0;
    seriel_re_time=0;
    led = ~led;
    outp(led,PORTB);                 /* init variable representing the LED state */
    uart_init(UART_BAUD_RATE);             /*串行口初始化*/
    sei();
    uart_send();                   /* enable interrupts 打开全局中断*/
   /* for(i=0;i<9;i++)
    {
        while(!trans_cpl);
        trans_cpl=0;
    	for(j=0;j<40;j++)
    	{
    		seriel_tx_buf[j]=message_to_kandshake[i][j];
    	}
    	size=40;
    	uart_send();
    }*/
    /*uart_send();*/                   
    for (;;)
    {
    	if(seriel_trans_ok)
    	{
    	        seriel_trans_ok=0;    		
    	        outp(~led,PORTB);
    		uart_send();
    	}
    }              /* loop forever */
}

⌨️ 快捷键说明

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