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

📄 serial.c

📁 用超级终端通过串口模拟的一个小Shell.已经实现的命令:cls
💻 C
字号:
//ICC-AVR application builder : 2006-3-8 15:48:09
// Target : M16
// Crystal: 7.3728Mhz

#include <iom16v.h>
#include <macros.h>
#define uchar unsigned char
#define KeyBufLen 10

uchar keyBuf[KeyBufLen];
uchar keyCnt;
uchar adEnable;


void ASCIISend(unsigned int ascVal)
{
 	UDR=ascVal;
	while(!(UCSRA&0X40));
	UCSRA|=0x40;
}


void sEnter(void)
{
	ASCIISend(13);
	ASCIISend(10);
}

void Enter(void)
{
	ASCIISend(13);
	ASCIISend(10);
	ASCIISend(62);
}

void cls(void)
{
 uchar tmp;
	  	for(tmp=0;tmp<24;tmp++)
	 	{
		 	sEnter();	
		}
	 	for(tmp=0;tmp<24;tmp++)
	 	{
		 	ASCIISend(27);
			ASCIISend(91);
			ASCIISend(65);	
		}
	 	ASCIISend(62);
}



void startAD0(void)
{
 	 ADMUX=0x40;
	 ADCSR|=BIT(ADSC);

}
void analyzeCMD(void)
{
 
 	 if(keyBuf[0]=='c' && keyBuf[1]=='l' && keyBuf[2]=='s' && keyCnt==3)
	 {
	  cls();
	 }
	 else
	 if(keyBuf[0]=='a' && keyBuf[1]=='d' && keyBuf[2]=='0' && keyCnt==3)
	 {
	  	adEnable=1;
		sEnter();
	 }
	 else
	 if(keyBuf[0]=='l' && keyBuf[1]=='o' && keyBuf[2]=='n' && keyCnt==3)
	 {
	  	DDRD=0x04;
		PORTD=0x04;
		Enter();
	 }
	 else
	 if(keyBuf[0]=='l' && keyBuf[1]=='o' && keyBuf[2]=='f' && keyBuf[3]=='f' && keyCnt==4)
	 {
	  	DDRD=0x00;
		PORTD=0x00;
		Enter();
	 }
	 else
	 {
	  	 sEnter();
	 	 ASCIISend(101);//e
	 	 ASCIISend(114);//r
	 	 ASCIISend(114);//r
	 	 ASCIISend(111);//o
	 	 ASCIISend(114);//r
	 	 ASCIISend(33); //!
		 Enter();
	 }
	 keyCnt=0;
}

void delay_1ms(void)//1ms延时函数
{
  unsigned int i;
  for (i=0;i<150;i++)
  {
  } 
}
void delay_nms(unsigned int n)//延时n毫秒
{
  unsigned int i;
  for (i=0;i<n;i++)//执行n次1毫秒延时
  delay_1ms(); 
}

 


void port_init(void)
{
 PORTA = 0x00;
 DDRA  = 0x00;
 PORTB = 0x00;
 DDRB  = 0x00;
 PORTC = 0x00; //m103 output only
 DDRC  = 0x00;
 PORTD = 0x00;
 DDRD  = 0x00;
}

//ADC initialize
// Conversion time: 104uS
void adc_init(void)
{
 ADCSR = 0x00; //disable adc
 ADMUX = 0x40; //select adc input 0
 	   	 	   //0x40 select AVCC as a reference voltage.
 ACSR  = 0x80;
 ADCSR = 0x8B;
}


//UART0 initialize
// desired baud rate: 115200
// actual: baud rate:115200 (0.0%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
 UCSRB = 0x00; //disable while setting baud rate
 UCSRA = 0x00;
 UCSRC = BIT(URSEL) | 0x06;
 UBRRL = 0x03; //set baud rate lo 
 UBRRH = 0x00; //set baud rate hi
 UCSRB = 0x98; //Enable Rx INT,Disable Tx INT.
 	   	 	   //0xd8 to enable Rx&Tx INT;
}

#pragma interrupt_handler uart0_rx_isr:12
void uart0_rx_isr(void)
{
 //uart has received a character in UDR
 uchar tmpUDR;
 tmpUDR=UDR;
 keyBuf[keyCnt]=tmpUDR;
 
	if(tmpUDR!=13)
	{
	 UDR=tmpUDR;
	 keyCnt++;
	 if(tmpUDR==3)
	 {
	  adEnable=0;
	  keyCnt=0;
	  sEnter();
	  ASCIISend(62);
	 }
	}
	else
	if(keyCnt==0)
	{
	 sEnter();
	 if(!adEnable)ASCIISend(62);
	}
	else
	{
	 analyzeCMD();
	}
}



//#pragma interrupt_handler uart0_tx_isr:14
//void uart0_tx_isr(void)
//{
 
 //character has been transmitted
//}


#pragma  interrupt_handler  ad_handler:15
void ad_handler(void)
{      
       uchar i,j,k,l; 
       int  data_temp;
	   MCUCR=0x10;
       data_temp=ADC;
       data_temp=(int)(data_temp*5.00/1024*1000);
	   l=data_temp/1000+0x30;
	   k=(data_temp%1000)/100+0x30;
	   j=(data_temp%100)/10+0x30;
	   i=data_temp%10+0x30;
	   
		ASCIISend(l);
		ASCIISend(46);//'.'
		ASCIISend(k);
	    ASCIISend(j);
		ASCIISend(i);
		ASCIISend(32);//' '
		ASCIISend(86);//'V'	
		ASCIISend(13);

}









//call this routine to initialize all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 port_init();
 uart0_init();
 adc_init();
 keyCnt=0;
 MCUCR = 0x10;
 GICR  = 0x00;
 TIMSK = 0x00; //timer interrupt sources
 SEI(); //re-enable interrupts
 //all peripherals are now initialized
}


void main(void)
{
init_devices();

while(1)
{
 if(adEnable==1)
 {
 
  MCUCR=0x50;
   asm("SLEEP");
  
 }
}
}

⌨️ 快捷键说明

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