制作出來的實物圖如下:
IMG_2962[1].jpg (1.24 MB, 下載次數(shù): 76)
下載附件
2018-11-24 14:51 上傳
IMG_2964[1].jpg (1.37 MB, 下載次數(shù): 77)
下載附件
2018-11-24 14:52 上傳
IMG_2966[1].jpg (1.39 MB, 下載次數(shù): 70)
下載附件
2018-11-24 14:53 上傳
用STC15W408as+LCD1602做了個串口收發(fā),可以設(shè)置波特率、設(shè)定發(fā)送的字節(jié)個數(shù)、設(shè)定要發(fā)送的字節(jié)、及收發(fā)模式,進行串口調(diào)試時,可以不用電腦了,很方便不是么。
單片機源程序如下:
- #include "STC15W408AS.H"
- #include "Lcd.h"
- #include "key.h"
- #include "E2prom.h"
- #include "Serive_usart.h"
- uchar b_value=0;
- uchar Ser_state=0;
- bit byte_bit=0;
- uchar Data_Len=1;
- uchar Menu_Count=0;
- uchar KEY_Value=0, Arr_point=0, old_apoint=0;
- uchar Mu4_L=0, Mu4_H=0;
- uchar idata Send_Buffer[10]={0};
- uchar idata Rec_Buffer[10]={0}; /*設(shè)立接收數(shù)組*/
- uchar R_Count=0;
- bit R_Flag=0;
- uint R_outtime=0;
- bit R_okFlag=0;
- uchar code tab1[]={" Serial_Receive "};
- uchar code tab2[]={"BAND = "};
- uchar code tab4[]={"[+][-] [En]"};
- uchar code tab5[6][5]= {{"1200 "},
- {"2400 "},
- {"4800 "},
- {"9600 "},
- {"14400"},
- {"19200"}};
- uchar code tab6[5][10]= {{"1:Set_Baud"}, /*開機選擇畫面0-選擇波特率*/
- {"2:Set_Len "},
- {"3:Set_SeBy"}, /*1-選擇要發(fā)送的字節(jié)*/
- {"4:Set_Mode"}, /*2-選擇模式*/
- {"5: Start "}}; /*3-是否開始*/
- uchar code tab7[2][7]= {{"Send "},
- {"Receive"}};
- uchar code tab8[13]= {"Set Send Len="};
- uchar code tab9[16]= {"S=0 R=1 sta= 0 "};
- uchar code tab10[16]={"[<][>][+][^][En]"};
- uchar code tab11[8]={"NO. : 0x"};
- uchar code tab12[16]={"[S][R] [En]"};
- uchar code tab13[7]={"[S][En]"};
- uchar code tab14[4]={"[En]"};
- uchar code tab15[16]={" "};
- uchar code tab16[12]={" "};
- void Mu1(void);
- void Mu2(void);
- void Mu3(void);
- void Mu4(void);
- void Mu5(void);
- void Mu6(void);
- void Data_ind(uchar point);
- void Usart_Ser();
- void main()
- {
- key_Init();
- init_lcd();
- Usart_Init();
- Clear_display();
- delay(1000);
- chrt_disp(tab1,0x80,16);
- Disp_Init_Draw();
- delay (100);
- Arr_point=Band_Init();
- old_apoint=Arr_point;
- delay(100);
- Clear_display();
- while(1)
- {
- switch(Menu_Count)
- {
- case 0:
- {
- Mu1();
- }
- break;
- case 1:
- {
- Mu2();
- }
- break;
- case 2:
- {
- Mu3();
- }
- break;
- case 3:
- {
- Mu4();
- }
- break;
- case 4:
- {
- Mu5();
- }
- break;
- case 5:
- {
- Mu6();
- }
- break;
- default:
- {
- Menu_Count=0;
- }
- }
- }
- }
- void Mu1(void)
- {
- uchar Mscr1_Flag=0;
- chrt_disp(tab6[0+b_value],0x80,10);
- chrt_disp(tab4,0xc0,16);
- do
- {
- KEY_Value=Key_Scan();
- if(KEY_Value == 0x3c)
- {
- Mscr1_Flag=1;
- b_value++;
- if(b_value==5)
- {
- b_value=0;
- }
- }
- else if(KEY_Value == 0x5c)
- {
- Mscr1_Flag=1;
- if(b_value==0)
- {
- b_value=4;
- }
- else
- {
- b_value--;
- }
- }
- else
- {
- if(Mscr1_Flag)
- {
- chrt_disp(tab6[0+b_value],0x80,10);
- Mscr1_Flag=0;
- }
- }
- }while(KEY_Value != 0x78);
- Menu_Count=b_value+1;
- Clear_display();
- delay(100);
- }
- void Mu2(void)
- {
- uchar M2scr_Flag=0;
- chrt_disp(tab2,0x80,16);
- chrt_disp(tab5[0+Arr_point],0x87,5);
- chrt_disp(tab4,0xc0,16);
- do
- {
- KEY_Value=Key_Scan();
- if(KEY_Value == 0x3c)
- {
- M2scr_Flag=1;
- Arr_point++;
- if(Arr_point==6)
- {
- Arr_point=0;
- }
- }
- else if(KEY_Value==0x5c)
- {
- M2scr_Flag=1;
- if(Arr_point==0)
- {
- Arr_point=5;
- }
- else
- {
- Arr_point--;
- }
- }
- else
- {
- if(M2scr_Flag)
- {
- M2scr_Flag=0;
- chrt_disp(tab5[0+Arr_point],0x87,5);
- }
- }
- }while(KEY_Value != 0x78);
- if(old_apoint != Arr_point)
- {
- IapEraseSector(IAP_ADDRESS);
- IapProgramByte(IAP_ADDRESS, Arr_point);
- }
- Set_Baud(Arr_point);
- Menu_Count=0;
- Clear_display();
- delay(100);
- }
- void Mu3(void)
- {
- uchar Scr_Flag=0;
- uchar GB_point=0;
- chrt_disp(tab8,0x80,13);
- Mum_disp(0x8e,Data_Len ,1);
- chrt_disp(tab4,0xc0,16);
- do
- {
- KEY_Value=Key_Scan();
- if(KEY_Value == 0x3c) /*按下加鍵*/
- {
- Scr_Flag=1;
- Data_Len++;
- if(Data_Len==0x0a)
- {
- Data_Len=1;
- }
- }
- else if(KEY_Value == 0x5c) /*按下減鍵*/
- {
- Scr_Flag=1;
- if(Data_Len==1)
- {
- Data_Len=9;
- }
- else
- {
- Data_Len--;
- }
- }
- else
- {
- ;
- }
- if(Scr_Flag)
- {
- Scr_Flag=0;
- Mum_disp(0x8e,Data_Len,1);
- }
- }while(KEY_Value != 0x78); /*取得要發(fā)送字節(jié)長度值*/
- Menu_Count=0;
- Clear_display();
- delay(100);
- }
- void Mu4(void)
- {
- uchar temp4=1, M4scr=0, M4scr1=0, temp41, temp42;
- temp41=Send_Buffer[temp4-1]>>4; /*取得高4位*/
- temp42=Send_Buffer[temp4-1]&0x0f; /*取得低4位*/
- Data_ind(temp4-1);
- chrt_disp(tab11,0x83,8);
- Mum_disp(0x86, temp4,1);
- Mum_disp(0x8b, Mu4_H,0);
- Mum_disp(0x8c, Mu4_L,0);
- chrt_disp(tab10,0xc0,16);
- do
- {
- KEY_Value=Key_Scan();
- if(KEY_Value==0x3c)
- {
- M4scr=1;
- if(temp4==1)
- {
- temp4 = Data_Len;
- }
- else
- {
- temp4--;
- }
- }
- else if(KEY_Value==0x5c)
- {
- M4scr=1;
- temp4++;
- if(temp4>Data_Len)
- {
- temp4=1;
- }
-
- }
- else if(KEY_Value==0x6c)
- {
- M4scr1=1;
- if(byte_bit)
- {
- temp41++;
- if(temp41>=16)
- {
- temp41=0;
- }
- }
- else
- {
- temp42++;
- if(temp42>=16)
- {
- temp42=0;
- }
- }
- }
- else if(KEY_Value==0x74)
- {
- byte_bit = !byte_bit;
- }
- else
- {
- if(M4scr)
- {
- temp41=Send_Buffer[temp4-1]>>4;
- temp42=Send_Buffer[temp4-1]&0x0f;
- Mum_disp(0x86, temp4,1);
- Data_ind(temp4-1);
- Mum_disp(0x8b, Mu4_H,0);
- Mum_disp(0x8c, Mu4_L,0);
- M4scr=0;
- }
- if(M4scr1)
- {
- M4scr1=0;
- Send_Buffer[temp4-1]=(temp41<<4) | temp42;
- Data_ind(temp4-1);
- Mum_disp(0x86, temp4,1);
- Mum_disp(0x8b, Mu4_H,0);
- Mum_disp(0x8c, Mu4_L,0);
-
- }
- }
- }while(KEY_Value != 0x78); /*不但要回車,還要輸入字節(jié)個數(shù)等于設(shè)定個數(shù)*/
- Menu_Count=0;
- Clear_display();
- delay(100);
- }
- void Data_ind(uchar point)
- {
- Mu4_H=Send_Buffer[point]>>4;
- if(Mu4_H >= 0x0a)
- {
- Mu4_H += 0x37;
- }
- else
- {
- Mu4_H += 0x30;
- }
- Mu4_L=Send_Buffer[point] & 0x0f;
- if(Mu4_L >= 0x0a)
- {
- Mu4_L += 0x37;
- }
- else
- {
- Mu4_L += 0x30;
- }
- }
- void Mu5(void)
- {
- uchar M5scr=0;
- chrt_disp(tab9,0x80,16);
- chrt_disp(tab12,0xc0,16);
- do
- {
- KEY_Value=Key_Scan();
- if(KEY_Value==0x3c)
- {
- M5scr=1;
- Ser_state=0;
- }
- else if(KEY_Value==0x5c)
- {
- M5scr=1;
- Ser_state=1;
- }
- else
- {
- if(M5scr)
- {
- M5scr=0;
- Mum_disp(0x8d, Ser_state,1);
- }
- }
- }while(KEY_Value != 0x78);
- Menu_Count=0;
- Clear_display();
- delay(100);
- }
- void Mu6(void)
- {
- uchar i, temp1;
- ES=1;
- if(Ser_state)
- {
- chrt_disp(tab14,0xcc,16);
- do
- {
- if(R_Flag)
- {
- R_outtime--;
- if(R_outtime ==0)
- {
- R_Flag=0;
- R_okFlag=1;
- }
- }
- KEY_Value=Key_Scan();
- if(R_okFlag) /*如果有接收的數(shù)據(jù)*/
- {
- // Clear_display();
- // delay(10);
- chrt_disp(tab16,0xc0,12);
- chrt_disp(tab15,0x80,16);
- for(i=0; i<R_Count; i++)
- {
- if(i>=8)
- {
-
- temp1=Rec_Buffer[i] >> 4;
- if(temp1 > 9)
- {
- temp1+=0x37;
- }
- else
- {
- temp1+=0x30;
- }
- Mum_disp(0xc0+(i-8)*2, temp1,0);
- temp1=Rec_Buffer[i] & 0x0f;
- if(temp1 > 9)
- {
- temp1+=0x37;
- }
- else
- {
- temp1+=0x30;
- }
- Mum_disp(0xc0+(i-8)*2+1, temp1,0);
- //R_okFlag=0;
- }
- else
- {
- temp1=Rec_Buffer[i] >> 4;
- if(temp1 > 9)
- {
- temp1+=0x37;
- }
- else
- {
- temp1+=0x30;
- }
- Mum_disp(0x80+2*i, temp1,0);
- temp1=Rec_Buffer[i] & 0x0f;
- if(temp1 > 9)
- {
- temp1+=0x37;
- }
- else
- {
- temp1+=0x30;
- }
- Mum_disp(0x80+2*i+1, temp1,0);
- //R_okFlag=0;
- }
- }
- R_Count=0;
- R_okFlag=0;
- }
- }while(KEY_Value != 0x78);
- }
- else
- {
- chrt_disp(tab13,0xc9,7);
- do
- {
- KEY_Value=Key_Scan();
- if(KEY_Value==0x74)
- {
- for(i=0;i<Data_Len;i++)
- {
- Send_Byte(Send_Buffer[i]);
- }
- }
- }while(KEY_Value != 0x78);
- }
- ES=0;
- Menu_Count=0;
- Clear_display();
- delay(100);
- }
- void Usart_Ser() interrupt 4 using 1
- {
- if(RI)
- {
- RI=0;
- Rec_Buffer[R_Count]=SBUF;
- R_Count++;
- if(!R_Flag)
- {
- R_Flag=1;
- }
- R_outtime=1000;
- }
- if(TI)
- {
- TI=0;
- }
- }
復制代碼
|