?? mcs51-項目開發經典.c
字號:
// 串口UART操作相關程序
//=================================================================================================
//函數功能:串口發射1個字節
void uart_byte_out(uchar uart_data_out)
{ SBUF = uart_data_out;
while(TI==0);
TI=0;
}
//-------------------------------------------------------------------------------------------------
//函數功能:串口發射字符數組。通常將要發送的字符數組定義在CODE代碼區。
void uart_string_out(char * char_array)
{ uchar i;
for(i=0; i<strlen(char_array) ;i++)
{
uart_byte_out(char_array[i]);
}
}
//-------------------------------------------------------------------------------------------------
//函數功能:串口輸出數據十進制到PC機屏幕上
//形參:范圍 0-65535 ;例如:data_to_out=12345(或0x3039),則計算機屏幕就顯示12345
void uart_data_decimalist_out(uint data_to_out)
{ bit entrance;
uchar ge,shi,bai,qian,wan;
wan = (data_to_out/10000) ;//拆分萬位,并轉化為ASKII碼
qian = (data_to_out%10000)/1000;//拆分千位,并轉化為ASKII碼
bai = (data_to_out%1000)/100 ;//拆分百位,并轉化為ASKII碼
shi = (data_to_out%100)/10 ;//拆分十位,并轉化為ASKII碼
ge = (data_to_out%10) ;//拆分個位,并轉化為ASKII碼
entrance=1;//開放個、十、百、千、萬的判斷傳輸入口
if(wan && entrance)
{uart_byte_out(change_hex_to_askii(wan));
uart_byte_out(change_hex_to_askii(qian));
uart_byte_out(change_hex_to_askii(bai));
uart_byte_out(change_hex_to_askii(shi));
uart_byte_out(change_hex_to_askii(ge));
entrance=0;//如果萬位不為0,則不再判斷其它位
}
else if(qian && entrance)
{uart_byte_out(change_hex_to_askii(qian));
uart_byte_out(change_hex_to_askii(bai));
uart_byte_out(change_hex_to_askii(shi));
uart_byte_out(change_hex_to_askii(ge));
entrance=0;//如果千位不為0,則不再判斷其它位
}
else if(bai && entrance)
{uart_byte_out(change_hex_to_askii(bai));
uart_byte_out(change_hex_to_askii(shi));
uart_byte_out(change_hex_to_askii(ge));
entrance=0;//如果百位不為0,則不再判斷其它位
}
else if(shi && entrance)
{uart_byte_out(change_hex_to_askii(shi));
uart_byte_out(change_hex_to_askii(ge));
entrance=0;//如果十位不為0,則不再判斷其它位
}
else
{uart_byte_out(change_hex_to_askii(ge));
}
}
//-------------------------------------------------------------------------------------------------
// 函數功能:串口顯示的回車換行
// 形參:newline_number 表示一共換幾行
void uart_newline(uchar newline_number)
{ uchar i;
for(i=0;i<newline_number;i++)
{
uart_byte_out(0x0d);
uart_byte_out(0x0a);
}
}
//-------------------------------------------------------------------------------------------------
// 函數功能:串口顯示的空格
// 形參:space_number 表示空幾個格
void uart_space(uchar space_number)
{ uchar i;
for(i=0;i<space_number;i++)
{
uart_byte_out(0x20);
}
}
//=================================================================================================
// 數制轉換相關程序
//=================================================================================================
uchar change_bcd_to_hex(uchar shu)//轉換BCD碼成十進制數
{ uchar shu_h;
uchar shu_l;
shu_l=shu&0x0f;
shu_h=shu&0xf0;
shu_h=shu_h>>4;
return (shu_h*10+shu_l);
}
//-------------------------------------------------------------------------------------------------
uchar change_hex_to_bcd(uchar shu)//轉換十進制數成BCD碼
{ uchar shu_h;
uchar shu_l;
shu_l=shu%10;
shu_h=shu/10;
return ((shu_h<<4)|shu_l);
}
//-------------------------------------------------------------------------------------------------
uchar change_askii_to_hex(uchar askii)//轉換ASKII碼成HEX,實參范圍:30-39、41-46
{ if(askii<=0x39) return(askii-0x30);
else return(askii-0x37);
}
//-------------------------------------------------------------------------------------------------
uchar change_hex_to_askii(uchar data_hex)//HEX轉換成ASKII,實參范圍:0-9、A-F
{ if(data_hex<=0x09) return(data_hex+0x30);
else return(data_hex+0x37);
}
//=================================================================================================
// 函數功能:將格式為"data1,data2,...datan#" 的字符串中的data數據全部取出,并存儲于temporary_data[]
//=================================================================================================
void get_data(uchar * how_much_data)
{
uchar i;
uchar j=0;
uchar k=0;
uchar how_much_temp_askii=0;
uchar temp_askii[5];
uint return_data;
for(i=0; i<strlen(how_much_data); i++)
{
if( (how_much_data[i] != ',') && (how_much_data[i] != '#') )
{
temp_askii[j] = how_much_data[i];
j=j+1;
how_much_temp_askii=how_much_temp_askii+1;
}
else
{j=0;
switch (how_much_temp_askii)
{case 1:{return_data = change_askii_to_hex(temp_askii[0]);
break;
}
case 2:{return_data = change_askii_to_hex(temp_askii[0])*10+
change_askii_to_hex(temp_askii[1]);
break;
}
case 3:{return_data = change_askii_to_hex(temp_askii[0])*100+
change_askii_to_hex(temp_askii[1])*10 +
change_askii_to_hex(temp_askii[2]);
break;
}
case 4:{return_data = change_askii_to_hex(temp_askii[0])*1000+
change_askii_to_hex(temp_askii[1])*100 +
change_askii_to_hex(temp_askii[2])*10 +
change_askii_to_hex(temp_askii[3]);
break;
}
case 5:{return_data = change_askii_to_hex(temp_askii[0])*10000+
change_askii_to_hex(temp_askii[1])*1000 +
change_askii_to_hex(temp_askii[2])*100 +
change_askii_to_hex(temp_askii[3])*10 +
change_askii_to_hex(temp_askii[4]);
break;
}
default:break;
}
temporary_data[k] = return_data;
k=k+1;
how_much_temp_askii=0;
}
}
}
//=================================================================================================
// 函數功能:字符串匹配函數
//=================================================================================================
char * strstr(char * haystack, char * needle)
{ char *ptr1, *ptr2;
weizhi=0;
// Protect against NULL pointer
if (*needle == 0) return(haystack);
for( ; *haystack; haystack++ ,weizhi++)
{// Look for needle in haystack. If there is a
// match then this will continue all the way
// until ptr1 reaches the NULL at the end of needle
for(ptr1 = needle, ptr2 = haystack; *ptr1 && (*ptr1 == *ptr2); ++ptr1, ++ptr2);
// If there is a match then return pointer to needle in haystack
if(*ptr1 == 0) return(haystack);
}
return NULL;// no matching string found
}
//=================================================================================================
// 串口UART中斷服務程序
// 可以自動接收格式為"@xxx...xxx#"的字符串,長度小于等于30
//=================================================================================================
void UART_interrupt (void) interrupt 4
{ RI=0;
if( SBUF == '@' ) //判斷頭
{uart_buffer_address = 0;
uart_buffer[uart_buffer_address] = SBUF;
uart_buffer_address++;
uart_receive_start = 1; //置uart0接收啟動標志
return;
}
if( uart_receive_start == 1 ) //接收中間數據
{uart_buffer[uart_buffer_address] = SBUF;
uart_buffer_address++; //每接收1字節緩沖區地址加1
}
if( (uart_receive_start == 1)&&(SBUF == '#') ) //判斷尾
{uart_buffer[uart_buffer_address] = 0;//置字符串結束符號 \0,為字符串處理函數提供標準格式
uart_buffer_address = 0; //清緩沖區地址指針
uart_receive_start = 0; //清uart0接收啟動標志
uart_receive_ok = 1; //置已接收完整串口命令標志
return;
}
if(uart_buffer_address>39) //uart0接收緩沖區地址溢出故障處理:
{uart_buffer_address = 0; //清緩沖區地址指針
uart_receive_start = 0; //清uart0接收啟動標志
uart_receive_ok = 0; //清已接收完整串口命令標志
}
}
//=================================================================================================
// T1中斷服務程序
//=================================================================================================
void T1_interrupt(void) interrupt 3
{
uchar key_value;
TH1=0x4c;//11.0592M晶振時T1定時時間長度為50毫秒
TL1=0x00;
t1_delay_time++;//在需要延時的地方清空并判斷該變量
key_value=0xff;
//=================================================================================================
// 按住放手發射
//=================================================================================================
/*if(KEY_7279==0)
{key_press_counter++;
if(key_press_counter==4)
{key_value_previous=HD7279_GetKey();
}
}
else
{if(key_press_counter>4)
{key_value_main=key_value_previous;
}
key_press_counter=0;
}
*/
//=================================================================================================
//0.5秒延時發送
//=================================================================================================
if(KEY_7279==0)
{ key_press_timer++;
if(key_press_counter==0)
{key_value_previous=HD7279_GetKey();
}
if(key_press_timer>10)
{key_value=HD7279_GetKey();
if(key_value!=key_value_previous)
{key_value_main=0xff;
key_value=0xff;
key_value_previous=0xff;
key_press_timer=0;
key_press_counter=0;
}
else
{key_value_main=key_value_previous;
key_press_timer=0;
key_press_counter=0;
}
}
}
//=================================================================================================
//老師給通用延時
//=================================================================================================
/* if(!KEY_7279)//如果有按鍵觸發,則取出該鍵鍵值
{key_value=HD7279_GetKey();
}
if(key_value!=0xff)//有鍵觸發
{
if(key_press_counter==0)//如果上一次T1中斷沒發現按鍵觸發,而本次T1中斷發現了,做如下處理
{key_value_previous=key_value;//保存本次鍵值
key_press_counter+=1;//按鍵計數器加1
}
else//如果再次T1中斷時key_press_counter不為0且鍵值與上次鍵值相等,則按鍵完成了50毫秒延時消抖
{if(key_value_previous==key_value)
{key_value_main=key_value;}//key_value_main中存儲的鍵值在主程序中使用和清除
}
}
else//不管上次是否有鍵觸發,只要本次無鍵觸發就清空按鍵計數器,即如果按鍵時間不能持續50毫秒以上則重新判斷
{key_press_counter=0;
}
*/
}
//=================================================================================================
// end of the file
//=================================================================================================
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -