程序main.c:
#include <MEGA16.H>
#include "usart.h"
//#include <stdio.h>
#include "command.h"
void check_command(unsigned int intruction)
{
//unsigned int a;
usart_flag=0;
//a=compare_command(rec_buf);
switch(intruction)
{
case 1:usart_str_send("a\r\n");
break;
case 2:usart_str_send("b\r\n");
break;
case 3:usart_str_send("c\r\n");
break;
case 4:usart_str_send("d\r\n");
break;
case 5:usart_str_send("e\r\n");
break;
case 6:usart_str_send("f\r\n");
break;
default:
usart_str_send("abcdd\r\n");
break;
}
}
void main()
{
usart_init();
usart_str_send("Hello\r\n");
while(1)
{
if(usart_flag==1) check_command(compare_command(rec_buf));
}
}
程序usart.c:
#include <mega16.h>
//#include <stdio.h>
//#include "command.h"
#define BAUD 115200
#define FREQ 11059200
#define BUFF_SIZE 20
bit usart_flag=0;
unsigned char buf_num=0;
unsigned char rec_buf[BUFF_SIZE];
/***************UCSRA***************/
#define RXC 7//接收结束
#define TXC 6//发送结束
#define UDRE 5//数据寄存器空
#define FE 4//帧错误
#define DOR 3//数据溢出
#define PE 2//奇偶校验错误
#define U2X 1//倍速发送
#define MPCM 0//多处理器通信模式
/***************UCSRB***************/
#define RXCIE 7//接收结束使能
#define TXCIE 6//发送结束使能
#define UDRIE 5//数据寄存器空使能
#define RXEN 4//接收使能
#define TXEN 3//发送使能
#define UCSZ2 2//字符长度
#define RXB8 1//接收数据位8
#define TXB8 0//发送数据位8
/***************UCSRC***************/
#define URSEL 7//读UCSRC时为1,写UCSRC时为1
#define UMSEL 6//0为异步,1为同步
#define UPM1 5//校验位
#define UPM0 4//校验位
#define USBS 3//停止位
#define UCSZ1 2//字符长度
#define UCSZ0 1//字符长度
#define UCPOL 0//时钟极性
/***************UBRRH***************/
/*#define URSEL 7//读UBRRH时为0,写UBRRH时为0*/
void usart_init()
{
UCSRA=0x00;
UCSRB|=(1<<RXCIE)|(1<<RXEN)|(1<<TXEN);
UCSRC|=(1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);
UBRRH=(FREQ/(16*BAUD)-1)/256;
UBRRL=(FREQ/(16*BAUD)-1)%256;
#asm("sei");
}
void usart_char_send(unsigned char ch)
{
while(!(UCSRA&(1<<UDRE)));
UDR=ch;
}
void usart_str_send(flash unsigned char *str)
{
while(*str)
{
usart_char_send(*str);
str++;
}
}
interrupt[USART_RXC] receive_str()
{
rec_buf[buf_num]=UDR;
if((rec_buf[buf_num]=='\r')||(rec_buf[buf_num]=='\n'))
{
usart_flag=1;
rec_buf[buf_num]='\0';
buf_num=0;
}
else
{
buf_num++;
}
}
程序command.c:
#include "usart.h"
//#include <stdio.h>
#define command_size 6
flash unsigned char *commands[]=
{
"china",
"hunan",
"shaoyang",
"xinshao",
"taizhimiao",
"niutoucun"
};
/*unsigned int compare_str(unsigned char *str1,flash unsigned char *str2)
{
while((*str1!='\0')&&(*str2!='\0'))
{
usart_str_send("1");
if((*str1<='z')&&(*str1>='a'))
{
if(*str1-*str2==32)
{
str1++;
str2++;
}
}
else if((*str1<='Z')&&(*str1>='A'))
{
if(*str2-*str1==32)
{
str1++;
str2++;
}
}
else if(*str1==*str2)
{
str1++;
str2++;
}
else {usart_str_send("2");return 0;}
if((*str1=='\0')&&(*str2=='\0')) {usart_str_send("3");return 1;}
if(!(*str1&&*str2)) {usart_str_send("4");return 0;}
}
}*/
unsigned int compare_str(char *s1, flash char *s2)
{
//uchar i;
//uchar result;
unsigned char temp='a'-'A';
//char *Str1;Str2;
//Str1=s1;Str2=s2;
while((*s1!='\0')||(*s2!='\0'))
{
if(*s1!=*s2)
{
if((*s1<'A')||(*s1>'z')||(*s2<'A')||(*s2>'z')) //防止将26个大小号字母之外的
return 1; //字符判断为与26字母相同
if(((*s1>'Z')&&(*s1<'a'))||((*s2>'Z')&&(*s2<'a'))) //例如'!'与'A'
return 1;
if((*s1+temp)!=*s2)
{
if((*s1-temp)!=*s2)
return 1;
}
}
s1++;
s2++;
if((*s1=='\0')&&(*s2!='\0'))
return 1;
if((*s1!='\0')&&(*s2=='\0'))
return 1;
}
return 0;
}
unsigned int compare_command(unsigned char *str)
{
unsigned int i=0;
for(i=0;i<command_size;i++)
{
//usart_str_send("5");
if(compare_str(str,commands[i])==0)
{
//usart_str_send("6");
return i+1;
}
}
return 0;
}
该程序编译运行后,用SSCOM工具和STC-ISP的串口调试工具调试都会有问题,但是用超级终端调试运行却正常,这是什么原因?求大师问解答
|