单片机设计小程序,多亏队友带在我本来的代码上修改了不少,在此表示由衷感谢!
以下是全部源程序,因为偷懒没有把各个模块的子程序封装成库,供大家学习!
临近期末,事多人忙,也愿大家好好努力,不愧初心!
单片机源程序如下:
- #include <reg52.h>
- #include <stdio.h>
- #include <string.h>
- #include <intrins.h>
- #define U unsigned char
- sbit scl=P1^1;
- sbit sda=P1^0;
- sbit key=P1^7;
- sbit red=P1^4;
- sbit green=P1^3;
- sbit blue=P1^5;
- U a[81]={1},st=1,sq=1;
- char page=0;
- U num=0,sw=1;
- U code order1[]="AT+RST\r\n";
- U code order2[]="AT+CWMODE=2\r\n";
- U code order3[]="AT+CWSAP=\"sPinAr\",\"22222222\",11,3\r\n";
- U code order4[]="AT+CIPMUX=1\r\n";
- U code order5[]="AT+CIPSERVER=1,8080\r\n";
- U code order6[]="AT+CIFSR\r\n";
- U code dt_pls[2][6]={
- 0x00,0x80,0x00,0x00,0x00,0X00, //dot
- 0x08,0x08,0x3E,0x08,0x08,0X00}; //plus
- U code alpha[26][6]={
- 0xFC,0x12,0x11,0x12,0xFC,0x00, //A
- 0xFF,0x89,0x89,0x89,0x76,0x00, //B
- 0x7E,0x81,0x81,0x81,0x42,0x00, //C
- 0xFF,0x81,0x81,0x81,0x7E,0x00, //D
- 0xFF,0x89,0x89,0x89,0x89,0x00, //E
- 0xFF,0x09,0x09,0x09,0x09,0x00, //F
- 0x7E,0x81,0x91,0x91,0x70,0x00, //G
- 0xFF,0x08,0x08,0x08,0xFF,0x00, //H
- 0x81,0x81,0xFF,0x81,0x81,0x00, //I
- 0x41,0x81,0x81,0xFF,0x01,0x00, //J
- 0xFF,0x18,0x24,0x42,0x81,0x00, //K
- 0xFF,0x80,0x80,0x80,0x80,0x00, //L
- 0xFF,0x06,0x0C,0x06,0xFF,0x00, //M
- 0xFF,0x08,0x10,0x20,0xFF,0x00, //N
- 0x7E,0x81,0x81,0x81,0x7E,0x00, //O
- 0xFF,0x11,0x11,0x11,0x0E,0x00, //P
- 0x7E,0x81,0xA1,0xA1,0xFE,0x00, //Q
- 0xFF,0x19,0x29,0x49,0x86,0x00, //R
- 0x46,0x89,0x89,0x89,0x72,0x00, //S
- 0x01,0x01,0xFF,0x01,0x01,0x00, //T
- 0x7F,0x80,0x80,0x80,0x7F,0x00, //U
- 0x07,0x38,0xC0,0x38,0x07,0x00, //V
- 0x1F,0xE0,0x18,0xE0,0x1F,0x00, //W
- 0x81,0x66,0x18,0x66,0x81,0x00, //X
- 0x03,0x0C,0xF0,0x0C,0x03,0x00, //Y
- 0xE1,0x91,0x89,0x85,0x83,0x00}; //Z
- U code bet[26][6]={
- 0x70,0x88,0x88,0x88,0xF0,0x00, //a
- 0xFF,0x88,0x88,0x88,0x70,0x00, //b
- 0x70,0x88,0x88,0x88,0x48,0x00, //c
- 0x70,0x88,0x88,0x88,0xFF,0x00, //d
- 0x70,0xA8,0xA8,0xA8,0xB0,0x00, //e
- 0x88,0x88,0xFE,0x09,0x09,0x00, //f
- 0x4C,0x92,0x92,0x92,0x7C,0x00, //g
- 0xFF,0x08,0x08,0x08,0xF0,0x00, //h
- 0x08,0x08,0xFA,0x80,0x40,0x00, //i
- 0x00,0x40,0x88,0x88,0xFA,0x00, //j
- 0xFF,0x20,0x50,0x88,0x88,0x00, //k
- 0x80,0x40,0xFF,0x92,0x4C,0x00, //l
- 0xF8,0x10,0xF8,0x10,0xF8,0x00, //m
- 0xF8,0x10,0x08,0x08,0xF0,0x00, //n
- 0x70,0x88,0x88,0x88,0x70,0x00, //o
- 0x08,0xFC,0x24,0x24,0x18,0x00, //p
- 0x18,0x24,0x24,0xFC,0x80,0x00, //q
- 0x08,0xFC,0x04,0x04,0x08,0x00, //r
- 0x48,0x94,0x94,0x94,0x64,0x00, //s
- 0x08,0x7C,0x88,0x88,0x40,0x00, //t
- 0x08,0x7C,0x80,0x80,0xFC,0x00, //u
- 0x1C,0x60,0x80,0x60,0x1C,0x00, //v
- 0x3C,0xC0,0x3C,0xC0,0x3C,0x00, //w
- 0x84,0x48,0x30,0x48,0x84,0x00, //x
- 0x84,0x98,0x60,0x18,0x04,0x00, //y
- 0xC4,0xA4,0x94,0x94,0x8C,0x00}; //z
- U digital[10][6]={
- 0x00,0x7E,0x91,0x89,0x7E,0x00, //0
- 0x00,0x80,0x82,0xFF,0x80,0x00, //1
- 0x00,0xC2,0xA1,0x91,0x8E,0x00, //2
- 0x00,0x42,0x89,0x89,0x76,0x00, //3
- 0x00,0x38,0x24,0x22,0xFF,0x00, //4
- 0x00,0x8F,0x89,0x89,0x71,0x00, //5
- 0x00,0x7E,0x89,0x89,0x71,0x00, //6
- 0x00,0x03,0xF1,0x09,0x07,0x00, //7
- 0x00,0x76,0x89,0x89,0x76,0x00, //8
- 0x00,0x4E,0x91,0x91,0x7E,0x00}; //9
- void time(){
- U i,j;
- for(i=200;i>0;i--)
- for(j=200;j>0;j--);
- }
- U scan(){
- U i;
- if(!key){
- time();
- while(!key);
- return 2;
- }
- if(sw && num>1){
- sw=0;
- for(i=1;i<81;i++)
- a[i]=0;
- return 3;
- }
- return 0;
- }
- void iic_init(){
- scl=1;_nop_();_nop_();
- sda=1;_nop_();_nop_();
- }
- void iic_sta(){
- sda=1;_nop_();_nop_();
- scl=1;_nop_();_nop_();
- sda=0;_nop_();_nop_();
- }
- void iic_rsp(){
- U i=0;
- scl=1;_nop_();_nop_();
- while(sda&&(i++<255));
- scl=0;_nop_();_nop_();
- }
- void iic_stp(){
- sda=0;_nop_();_nop_();
- scl=1;_nop_();_nop_();
- sda=1;_nop_();_nop_();
- }
- void iic_wr(U x){
- U i,temp;
- temp=x;
- for(i=0;i<8;i++){
- temp<<=1;
- scl=0;_nop_();_nop_();
- sda=CY;_nop_();_nop_();
- scl=1;_nop_();_nop_();
- }
- scl=0;_nop_();_nop_();
- sda=1;_nop_();_nop_();
- }
- void command(U x){
- iic_sta();
- iic_wr(0x78);iic_rsp(); //address of OLED
- iic_wr(0x00);iic_rsp(); //address of register
- iic_wr(x);iic_rsp();
- iic_stp();
- }
- void dat(U x){
- iic_sta();
- iic_wr(0x78);iic_rsp(); //address of OLED
- iic_wr(0x40);iic_rsp(); //address of register
- iic_wr(x);iic_rsp();
- iic_stp();
- }
- void OLED_SetPos(U x,U y){ //set_pos(place,page)
- command(0xb0+y);
- command(((x&0xf0)>>4)|0x10);
- command(x&0x0f);
- }
- void OLED_clear(){
- U i,j;
- for(i=0;i<8;i++){
- OLED_SetPos(0,i);
- for(j=0;j<128;j++){
- dat(0x00);
- }
- }
- }
- void OLED_Init(){
- command(0xae);command(0x20);command(0x10);command(0xb0);
- command(0xc8);command(0x00);command(0x10);command(0x40);
- command(0x81);command(0xff);command(0xa1);command(0xa6);
- command(0xa8);command(0x3f);command(0xa4);command(0xd3);
- command(0x00);command(0xd5);command(0xf0);command(0xd9);
- command(0x22);command(0xda);command(0x12);command(0xdb);
- command(0x20);command(0x8d);command(0x14);command(0xaf);
- OLED_clear();
- }
- void OLED_String(U page,U *str){
- U character,j;
- U count_character_num=0;
- while(*str){
- OLED_SetPos(count_character_num*6,page);
- character = *str;
- if(character>='0' && character<='9')
- for(j=0;j<6;j++)
- dat(digital[character-'0'][j]);
- else if(character=='.')
- for(j=0;j<6;j++)
- dat(dt_pls[0][j]);
- else if(character>='A' && character<='Z')
- for(j=0;j<6;j++)
- dat(alpha[character-'A'][j]);
- else if(character>='a' && character<='z')
- for(j=0;j<6;j++)
- dat(bet[character-'a'][j]);
- else if(character=='+')
- for(j=0;j<6;j++)
- dat(dt_pls[1][j]);
- else dat(0x00);
- str++;
- if(++count_character_num*6>120){
- count_character_num=0;
- if(++page==8){
- page=0;
- OLED_clear();
- }
- }
- }
- }
- void wificommand(U k[]){
- U i;
- ES=0;
- TI=1;
- puts(k);
- while(!TI);
- TI=0;
- ES=1;
- while(1){
- if(a[1])
- OLED_String(page,a);
- if(scan()==2){
- for(i=1;i<81;i++)
- a[i]=0;
- break;
- }
- }
- if((page=4-page)==0)
- OLED_clear();
- }
- void wifi_Init(){
- wificommand(order1);
- wificommand(order2);
- wificommand(order3);
- wificommand(order4);
- wificommand(order5);
- wificommand(order6);
- time();time();time();time();
- }
- void doublechat(){
- U i,l;
- while(1){
- if(a[3]=='+' && a[4]=='I' && a[5]=='P' && a[6]=='D' && sq){
- sq=0;
- l = (a[11]>='0'&&a[11]<='9')?(a[10]-'0')*10+(a[11]-'0'):a[10]-'0';
- if(a[12]=='2' && a[13]=='2' && a[14]=='2'){
- red=1;
- blue=0;
- green=0;
- }
- else if(a[12]=='3' && a[13]=='3' && a[14]=='3'){
- red=0;
- blue=1;
- green=0;
- }
- else if(a[12]=='4' && a[13]=='4' && a[14]=='4'){
- red=0;
- blue=0;
- green=1;
- }
- if(a[8]-'0'){
- if(l==1){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=0,1\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+12);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else if(l==2){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=0,2\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+12);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else if(l==3){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=0,3\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+12);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else if(l==4){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=0,4\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+12);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else if(l==5){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=0,5\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+12);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else if(l==6){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=0,6\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+12);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else if(l==7){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=0,7\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+12);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else if(l==8){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=0,8\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+12);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else if(l==9){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=0,9\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+12);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else if(l==10){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=0,10\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+13);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else if(l==11){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=0,11\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+13);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else if(l==12){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=0,12\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+13);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else if(l==13){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=0,13\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+13);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else if(l==14){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=0,14\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+13);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else if(l==15){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=0,15\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+13);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else if(l==16){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=0,16\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+13);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else if(l==17){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=0,17\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+13);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else if(l==18){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=0,18\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+13);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else if(l==19){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=0,19\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+13);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else{
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=0,20\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+13);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- }
- else{
- if(l==1){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=1,1\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+12);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else if(l==2){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=1,2\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+12);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else if(l==3){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=1,3\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+12);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else if(l==4){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=1,4\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+12);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else if(l==5){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=1,5\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+12);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else if(l==6){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=1,6\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+12);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else if(l==7){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=1,7\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+12);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else if(l==8){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=1,8\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+12);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else if(l==9){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=1,9\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+12);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else if(l==10){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=1,10\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+13);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else if(l==11){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=1,11\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+13);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else if(l==12){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=1,12\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+13);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else if(l==13){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=1,13\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+13);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else if(l==14){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=1,14\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+13);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else if(l==15){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=1,15\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+13);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else if(l==16){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=1,16\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+13);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else if(l==17){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=1,17\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+13);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else if(l==18){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=1,18\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+13);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else if(l==19){
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=1,19\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+13);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- else{
- st=0;
- ES=0;
- TI=1;
- printf("AT+CIPSEND=1,20\r\n");
- while(!TI);
- TI=0;
- time();
- TI=1;
- puts(a+13);
- while(!TI);
- TI=0;
- ES=1;
- time();
- st=1;
- }
- }
- }
- if(a[1])
- OLED_String(page,a);
- switch(scan()){
- case 3:
- sq=1;
- goto lop;
- case 2:
- for(i=1;i<81;i++)
- a[i]=0;
- OLED_clear();
- sq=page=0;
- }
- }
- lop:
- page+=2;
- if(page==8){
- time();time();time();time();time();time();
- time();time();time();time();time();time();
- OLED_clear();
- page=0;
- }
- }
- int main(){
- TMOD=0x21;
- TH1=TL1=0xfd;
- TH0=0x4c;
- TL0=0xd0;
- EA=ET0=ES=SM1=TR1=REN=1;
- SM0=TR0=0;
- iic_init();
- OLED_Init();
- wifi_Init();
- TR0=1;
- page=-2;
- OLED_clear();
- OLED_String(2,"Waiting For");
- OLED_String(4,"Connection...");
- while(1){
- doublechat();
- }
- }
- void tim() interrupt 1{
- TH0=0x4c;
- TL0=0xd0;
- num++;
- }
- void sep() interrupt 4{
- U i,p;
- RI=0;
- for(i=1;i<81;i++)
- if(a[i-1]&&!a[i]){
- if(st)a[i]=SBUF;
- else p=SBUF;
- num=0;
- sw=1;
- break;
- }
- }
复制代码
|