E2UART编程(β板实操)task3猜数字大小(传送字符串)

E2UART编程(β板实操)task3猜数字大小(传送字符串)_第1张图片
E2UART编程(β板实操)task3猜数字大小(传送字符串)_第2张图片

#include
#include "derivative.h" /* include peripheral declarations */
void UART1_Init()
{
	SIM_SCGC4 |= (1<<11);//使能UART1时钟
	SIM_SCGC5 |= (1<<13);//使能PORTE时钟
	PORTE_PCR0 = 0x0300;//设置PTE0为UART
	PORTE_PCR1 = 0x0300;//设置PTE1为UART
	UART1_C1 = 0;//不使用奇偶校验
	UART1_C2 = (1<<2)+(1<<3);
	UART1_BDH = 0x00;
	UART1_BDL = 0x44;//设置波特率为9600
	
}
unsigned char UART_GetChar(void)
{
	while(!(UART1_S1&(1<<5)));
	return UART1_D;
	
}
void UART1_PutChar(unsigned char data)
{
	while(!(UART1_S1&(1<<7)));
	UART1_D=data;
}
void UART1_SendString(char*pt){
	while((*pt)!=0){
		UART1_PutChar(*pt);
		pt++;
	}
}
unsigned char dtoa(unsigned char c1)
{
return (c1>=10)?(c1+'A'-10):(c1+'0');	
}
void UART1_SendHex(unsigned char c1){
	UART1_PutChar(dtoa(c1&(0xF0)>>4));
	UART1_PutChar(dtoa(c1&(0x0F)));
}


int main(void)
{   unsigned char rand_number=rand()%10;
	unsigned char counter =0;
	unsigned  char input;
	UART1_Init();
		
	for(;;) {
		input=UART_GetChar();
	   	counter++;
	   	UART1_PutChar(input);
	   	if((input-'0')>rand_number)
	   	{
	   		UART1_SendString("Large\r\n");
	   	}
	   	else if((input-'0')<rand_number)
	   {
	   		UART1_SendString("Small\r\n");
	   }
	   	else{
	   		UART1_PutChar(0x21);//给爷来个!
	   		UART1_SendString("Bingo\r\nYou take ");
	   		UART1_PutChar(counter+'0');
	   		UART1_SendString(" times to get it.\r\n");
	   	}
	   	
	}
	
	return 0;
}

在这里要注意单引号和双引号的区别
’ ’ 引起的一个字符代表一个整数,整数值对应于该字符在编译器采用的字符集中的序列值;
” ”引起的字符串代表的是一个指向无名数组起始字符的指针。
同时妖姬住counter=0ascii是0所以要输出整形要+‘0’

你可能感兴趣的:(arm)