龙空技术网

python定时器读取硬件ADC的值

理思录 99

前言:

眼前大家对“python1l”都比较注重,我们都需要了解一些“python1l”的相关资讯。那么小编同时在网络上网罗了一些关于“python1l””的相关知识,希望朋友们能喜欢,看官们一起来了解一下吧!

视频加载中...

python代码

import tkinterimport tkinter.messageboximport serialfrom PIL import Image,ImageTkfrom threading import Timerimport datetimeimport timenn=0COMPORT="COM3"class test:      def __init__(self):      self.t=Timer(0.5,self.func)      self.t.start()   def func(self):      global nn      nn=nn+1      #print(nn)      local_time=time.localtime(time.time())      week_index=local_time.tm_wday      week_list=["星期一","星期二","星期三","星期四","星期五","星期六","星期日"]      B8['text']=str(datetime.datetime.now().strftime("%H:%M:%S"))      B9['text']=week_list[week_index]+" ,第"+str(time.localtime()[7])+"天"      read_ADC()      self.t.cancel()      self.t=Timer(0.5,self.func)      self.t.start()left_space=50top_space=20line_space=30bt_width=250bt_height=30LED1_ON = b'\xfa\x01\x08\x01\x01\x01\x71\xba'LED1_OFF = b'\xfa\x01\x08\x01\x01\x00\xb1\x7b'LED2_ON = b'\xfa\x01\x08\x01\x02\x01\x81\xba'LED2_OFF = b'\xfa\x01\x08\x01\x02\x00\x41\x7b'LED1_READ = b'\xfa\x01\x07\x02\x01\x89\x8c'LED2_READ = b'\xfa\x01\x07\x02\x02\x88\xcc'ADC_READ = b'\xfa\x01\x07\x03\x00\xd9\x4c'top = tkinter.Tk()top.title("串口控制迷你硬件电路板  "+str(datetime.datetime.now().strftime("%Y-%m-%d")))top.geometry('800x600')top.iconbitmap('lsl.ico')top.resizable(width=False,height=False)#top.overrideredirect(1)img=Image.open("board.jpg").resize((600,600))photo = ImageTk.PhotoImage(img)tkinter.Label(master=top,image=photo).pack() def helloCallBack():   tkinter.messagebox.showinfo( "Hello Python", "Hello Runoob")def set_led1_on():   ser.write(LED1_ON)   B1['bg']='red'   B2['bg']='red'def set_led1_off():   ser.write(LED1_OFF)   B1['bg']='green'   B2['bg']='green'def set_led2_on():   ser.write(LED2_ON)   B3['bg']='red'   B4['bg']='red'def set_led2_off():   ser.write(LED2_OFF)   B3['bg']='green'   B4['bg']='green' def read_led1_status():   ser.write(LED1_READ)   xx=ser.read(8)   print(xx)   print(xx[5])   if xx[5]==1:      B5['bg']='red'   else:      B5['bg']='green'def read_led2_status():   ser.write(LED2_READ)   xx=ser.read(8)   if xx[5]==1:      B6['bg']='red'   else:      B6['bg']='green'def read_ADC():   ser.write(ADC_READ)   xx=ser.read(9)   aaa=xx[5]*4+xx[6]/64   B7['text']="ADC="+str(aaa)   ser=serial.Serial(COMPORT,9600,timeout=0.5)ser.close()ser.open()B1 = tkinter.Button(top, text ="点亮LED1", command = set_led1_on)B2 = tkinter.Button(top, text ="熄灭LED1", command = set_led1_off)B3 = tkinter.Button(top, text ="点亮LED2", command = set_led2_on)B4 = tkinter.Button(top, text ="熄灭LED2", command = set_led2_off)B5 = tkinter.Button(top, text ="查LED1状态", command = read_led1_status)B6 = tkinter.Button(top, text ="查LED2状态", command = read_led2_status)B7 = tkinter.Button(top, text ="读取ADC:", command = read_ADC)B8 = tkinter.Button(top, text ="日期时间")B9 = tkinter.Button(top, text ="星期,周,天") B1.place(x=left_space,y=top_space,width=bt_width,height=bt_height)B2.place(x=left_space,y=top_space+line_space*1,width=bt_width,height=bt_height)B3.place(x=left_space,y=top_space+line_space*2,width=bt_width,height=bt_height)B4.place(x=left_space,y=top_space+line_space*3,width=bt_width,height=bt_height)B5.place(x=left_space,y=top_space+line_space*4,width=bt_width,height=bt_height)B6.place(x=left_space,y=top_space+line_space*5,width=bt_width,height=bt_height)B7.place(x=left_space,y=top_space+line_space*6,width=bt_width,height=bt_height)B8.place(x=left_space,y=top_space+line_space*7,width=bt_width,height=bt_height)B9.place(x=left_space,y=top_space+line_space*8,width=bt_width,height=bt_height)t=test()top.mainloop()

ADC通讯协议:

ADC转换结果:

单片机程序:

__sfr __at(0x80) P0;__bit __at(0x80) P00;__bit __at(0x81) P01;__bit __at(0x82) P02;__bit __at(0x83) P03;__bit __at(0x84) P04;__bit __at(0x85) P05;__bit __at(0x86) P06;__bit __at(0x87) P07;__sfr __at(0x81) SP;__sfr __at(0x82) DPL;__sfr __at(0x83) DPH;__sfr __at(0x84) S4CON;__sfr __at(0x85) S4BUF;__sfr __at(0x87) PCON;__sfr __at(0x88) TCON;__bit __at(0x88) IT0;__bit __at(0x89) IE0;__bit __at(0x8a) IT1;__bit __at(0x8b) IE1;__bit __at(0x8c) TR0;__bit __at(0x8d) TF0;__bit __at(0x8e) TR1;__bit __at(0x8f) TF1;__sfr __at(0x89) TMOD;__sfr __at(0x8a) TL0;__sfr __at(0x8b) TL1;__sfr __at(0x8c) TH0;__sfr __at(0x8d) TH1;__sfr __at(0x8e) AUXR;__sfr __at(0x8f) INTCLKO;__sfr __at(0x90) P1;__bit __at(0x90) P10;__bit __at(0x91) P11;__bit __at(0x92) P12;__bit __at(0x93) P13;__bit __at(0x94) P14;__bit __at(0x95) P15;__bit __at(0x96) P16;__bit __at(0x97) P17;__sfr __at(0x91) P1M1;__sfr __at(0x92) P1M0;__sfr __at(0x93) P0M1;__sfr __at(0x94) P0M0;__sfr __at(0x95) P2M1;__sfr __at(0x96) P2M0;__sfr __at(0x98) SCON;__bit __at(0x98) RI;__bit __at(0x99) TI;__bit __at(0x9a) RB8;__bit __at(0x9b) TB8;__bit __at(0x9c) REN;__bit __at(0x9d) SM2;__bit __at(0x9e) SM1;__bit __at(0x9f) SM0;__sfr __at(0x99) SBUF;__sfr __at(0x9a) S2CON;__sfr __at(0x9b) S2BUF;__sfr __at(0x9d) IRCBAND;__sfr __at(0x9e) LIRTRIM;__sfr __at(0x9f) IRTRIM;__sfr __at(0xa0) P2;__bit __at(0xa0) P20;__bit __at(0xa1) P21;__bit __at(0xa2) P22;__bit __at(0xa3) P23;__bit __at(0xa4) P24;__bit __at(0xa5) P25;__bit __at(0xa6) P26;__bit __at(0xa7) P27;__sfr __at(0xa2) P_SW1;__sfr __at(0xa8) IE;__bit __at(0xa8) EX0;__bit __at(0xa9) ET0;__bit __at(0xaa) EX1;__bit __at(0xab) ET1;__bit __at(0xac) ES;__bit __at(0xad) EADC;__bit __at(0xae) ELVD;__bit __at(0xaf) EA;__sfr __at(0xa9) SADDR;__sfr __at(0xaa) WKTCL;__sfr __at(0xab) WKTCH;__sfr __at(0xae) TA;__sfr __at(0xaf) IE2;__sfr __at(0xb0) P3;__bit __at(0xb0) P30;__bit __at(0xb1) P31;__bit __at(0xb2) P32;__bit __at(0xb3) P33;__bit __at(0xb4) P34;__bit __at(0xb5) P35;__bit __at(0xb6) P36;__bit __at(0xb7) P37;__sfr __at(0xb1) P3M1;__sfr __at(0xb2) P3M0;__sfr __at(0xb3) P4M1;__sfr __at(0xb4) P4M0;__sfr __at(0xb5) IP2;__sfr __at(0xb6) IP2H;__sfr __at(0xb7) IPH;__sfr __at(0xb8) IP;__bit __at(0xb8) PX0;__bit __at(0xb9) PT0;__bit __at(0xba) PX1;__bit __at(0xbb) PT1;__bit __at(0xbc) PS;__bit __at(0xbd) PADC;__bit __at(0xbe) PLVD;__bit __at(0xbf) PPCA;__sfr __at(0xb9) SADEN;__sfr __at(0xba) P_SW2;__sfr __at(0xbc) ADC_CONTR;__sfr __at(0xbd) ADC_RES;__sfr __at(0xbe) ADC_RESL;__sfr __at(0xc0) P4;__bit __at(0xc0) P40;__bit __at(0xc1) P41;__bit __at(0xc2) P42;__bit __at(0xc3) P43;__bit __at(0xc4) P44;__bit __at(0xc5) P45;__bit __at(0xc6) P46;__bit __at(0xc7) P47;__sfr __at(0xc1) WDT_CONTR;__sfr __at(0xc2) IAP_DATA;__sfr __at(0xc3) IAP_ADDRH;__sfr __at(0xc4) IAP_ADDRL;__sfr __at(0xc5) IAP_CMD;__sfr __at(0xc6) IAP_TRIG;__sfr __at(0xc7) IAP_CONTR;__sfr __at(0xc8) P5;__bit __at(0xc8) P50;__bit __at(0xc9) P51;__bit __at(0xca) P52;__bit __at(0xcb) P53;__bit __at(0xcc) P54;__bit __at(0xcd) P55;__bit __at(0xce) P56;__bit __at(0xcf) P57;__sfr __at(0xc9) P5M1;__sfr __at(0xca) P5M0;__sfr __at(0xcb) P6M1;__sfr __at(0xcc) P6M0;__sfr __at(0xcd) SPSTAT;__sfr __at(0xce) SPCTL;__sfr __at(0xcf) SPDAT;__sfr __at(0xd0) PSW;__bit __at(0xd0) P;__bit __at(0xd1) F1;__bit __at(0xd2) OV;__bit __at(0xd3) RS0;__bit __at(0xd4) RS1;__bit __at(0xd5) F0;__bit __at(0xd6) AC;__bit __at(0xd7) CY;__sfr __at(0xd1) T4T3M;__sfr __at(0xd2) T4H;__sfr __at(0xd3) T4L;__sfr __at(0xd4) T3H;__sfr __at(0xd5) T3L;__sfr __at(0xd6) T2H;__sfr __at(0xd7) T2L;__sfr __at(0xd8) CCON;__bit __at(0xd8) CCF0;//0__bit __at(0xd9) CCF1;//1__bit __at(0xda) CCF2;//2__bit __at(0xde) CR;//6__bit __at(0xdf) CF;//7__sfr __at(0xd9) CMOD;__sfr __at(0xda) CCAPM0;__sfr __at(0xdb) CCAPM1;__sfr __at(0xdc) CCAPM2;__sfr __at(0xde) ADCCFG;__sfr __at(0xdf) IP3;__sfr __at(0xe0) ACC;__sfr __at(0xe1) P7M1;__sfr __at(0xe2) P7M0;__sfr __at(0xe3) DPS;__sfr __at(0xe4) DPL1;__sfr __at(0xe5) DPH1;__sfr __at(0xe6) CMPCR1;__sfr __at(0xe7) CMPCR2;__sfr __at(0xe8) P6;__bit __at(0xe8) P60;__bit __at(0xe9) P61;__bit __at(0xea) P62;__bit __at(0xeb) P63;__bit __at(0xec) P64;__bit __at(0xed) P65;__bit __at(0xee) P66;__bit __at(0xef) P67;__sfr __at(0xe9) CL;__sfr __at(0xea) CCAP0L;__sfr __at(0xeb) CCAP1L;__sfr __at(0xec) CCAP2L;__sfr __at(0xee) IP3H;__sfr __at(0xef) AUXINTIF;__sfr __at(0xf0) B;__sfr __at(0xf1) PWMSET;__sfr __at(0xf2) PCA_PWM0;__sfr __at(0xf3) PCA_PWM1;__sfr __at(0xf4) PCA_PWM2;__sfr __at(0xf5) IAP_TPS;__sfr __at(0xf6) PWMCFG01;__sfr __at(0xf7) PWMCFG23;__sfr __at(0xf8) P7;__bit __at(0xf8) P70;__bit __at(0xf9) P71;__bit __at(0xfa) P72;__bit __at(0xfb) P73;__bit __at(0xfc) P74;__bit __at(0xfd) P75;__bit __at(0xfe) P76;__bit __at(0xff) P77;__sfr __at(0xf9) CH;__sfr __at(0xfa) CCAP0H;__sfr __at(0xfb) CCAP1H;__sfr __at(0xfc) CCAP2H;__sfr __at(0xfe) PWMCFG45;__sfr __at(0xff) RSTCFG;/*__sfr16 __at(0xfea8) ADCTIM;__sfr16 __at(0xfea2) TM2PS;__sfr16 __at(0xfe88) I2CMSAUX;__sfr16 __at(0xfe80) I2CCFG;__sfr16 __at(0xfe81) I2CMSCR;__sfr16 __at(0xfe82) I2CMSST;__sfr16 __at(0xfe83) I2CCSLCR;__sfr16 __at(0xfe84) I2CSLST;__sfr16 __at(0xfe85) I2CSLADR;__sfr16 __at(0xfe86) I2CTxD;__sfr16 __at(0xfe87) I2CRxD;__sfr16 __at(0xfe31) P1IE;__sfr16 __at(0xfe33) P3IE;__sfr16 __at(0xfe29) P1DR;__sfr16 __at(0xfe2b) P3DR;__sfr16 __at(0xfe2d) P5DR;__sfr16 __at(0xfe21) P1SR;__sfr16 __at(0xfe23) P3SR;__sfr16 __at(0xfe25) P5SR;__sfr16 __at(0xfe19) P1NCS;__sfr16 __at(0xfe1b) P3NCS;__sfr16 __at(0xfe1d) P5NCS;__sfr16 __at(0xfe11) P1PU;__sfr16 __at(0xfe13) P3PU;__sfr16 __at(0xfe15) P5PU;__sfr16 __at(0xfe00) CKSEL;__sfr16 __at(0xfe01) CLKDIR;__sfr16 __at(0xfe02) HIRCCR;__sfr16 __at(0xfe03) XOSCCR;__sfr16 __at(0xfe04) IRC32KCR;__sfr16 __at(0xfe05) MCLKOCR;__sfr16 __at(0xfe06) IRCDB;*/__bit __at(0xb3) LED1;//P3_3__bit __at(0xb2) LED2;//P3_2__bit __at(0x91) BEEP;//P1_1__bit __at(0x92) SW2;//P1_2__bit __at(0x94) ENC_A;//P1_4__bit __at(0x93) ENC_B;//P1_3__bit __at(0xcc) DIP1;//P5_4__bit __at(0x97) DIP2;//P1_7__bit __at(0x96) DIP3;//P1_6__bit __at(0x95) DIP4;//P1_5#define TOU 0xfa#define MAX_LEN 19#define MAX_TIME_OUT 1000#define ADDRESS 1unsigned char crc_h;unsigned char crc_l;unsigned char flag_current_a;unsigned char flag_current_b;unsigned char flag_last_a;unsigned char flag_last_b;/* Table of CRC values for high–order byte */ const unsigned char auchCRCHi[] = { 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81,0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01,0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01,0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,0x40} ; /* Table of CRC values for low–order byte */ const char auchCRCLo[] = { 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4,0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD,0x1D, 0x1C, 0xDC, 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, 0x36, 0xF6, 0xF7,0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE,0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2,0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB,0x7B, 0x7A, 0xBA, 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, 0x50, 0x90, 0x91,0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88,0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80,0x40}; void CRC16 (unsigned char *puchMsg, unsigned int usDataLen ) /* The function returns the CRC as a unsigned short type */ { unsigned char uchCRCHi = 0xFF ; /* high byte of CRC initialized */ unsigned char uchCRCLo = 0xFF ; /* low byte of CRC initialized */ unsigned uIndex ; /* will index into CRC lookup table */ while (usDataLen--) /* pass through message buffer */ { uIndex = uchCRCLo ^ *puchMsg++ ; /* calculate the CRC */ uchCRCLo = uchCRCHi ^ auchCRCHi[uIndex] ; uchCRCHi = auchCRCLo[uIndex] ; } crc_h=uchCRCHi;crc_l=uchCRCLo;} //sdcc b2.c//packihx b2.ihx>b2.hexunsigned long n1;unsigned char n2;unsigned char rdata[20];unsigned char sdata[20];unsigned char i;unsigned char j;unsigned char flag_r_ok;unsigned long counter_times;unsigned char flag_led1;unsigned char flag_led2;unsigned char busy;unsigned long counter_ab;int CW_CCW[2][2][2][2]={{{{0,1},{-1,0}},{{-1,0},{0,1}}},{{{1,0},{0,-1}},{{0,-1},{1,0}}}};void delay(unsigned long int n){	unsigned long int i;	for(i=0;i<n;i++)	{			}}void UartInit(void)		//9600bps@11.0592MHz{	SCON = 0x50;		//8位数据,可变波特率	AUXR |= 0x01;		//串口1选择定时器2为波特率发生器	AUXR |= 0x04;		//定时器时钟1T模式	T2L = 0xE0;			//设置定时初始值	T2H = 0xFE;			//设置定时初始值	AUXR |= 0x10;		//定时器2开始计时	ES=1;	EA=1;	busy=0;}void uart_send(unsigned char dat){	while(busy);	busy=1;	SBUF=dat;}	void send_chars(unsigned char n){	for(j=0;j<n;j++)	{		uart_send(sdata[j]);	}}void uart_isr(void) __interrupt(4) __using(1){	if(TI)	{		TI=0;		busy=0;	}	if(RI)	{		RI=0;		rdata[i]=SBUF;		counter_times=0;		if(rdata[0]==TOU)		{			i++;			if((i>rdata[2]-1 || i>MAX_LEN-1)&& i>2)			{				i=0;				flag_r_ok=1;			}		}			}}void deal_cmd(void){	if(flag_r_ok==1)	{		flag_r_ok=0;		i=0;		if(rdata[0]==TOU && rdata[1]==ADDRESS && rdata[2]==0x08 && rdata[3]==0x01)		{			CRC16(rdata,6);			if(rdata[6]==crc_h && rdata[7]==crc_l)			{			if(rdata[4]==0x01 && rdata[5]==0x01)			{				LED1=0;				flag_led1=1;			}			if(rdata[4]==0x01 && rdata[5]==0x00)			{				LED1=1;				flag_led1=0;			}			if(rdata[4]==0x02 && rdata[5]==0x01)			{				LED2=0;				flag_led2=1;			}			if(rdata[4]==0x02 && rdata[5]==0x00)			{				LED2=1;				flag_led2=0;			}			}		}		if(rdata[0]==TOU && rdata[1]==ADDRESS && rdata[2]==0x07 && rdata[3]==0x02)		{			sdata[0]=0xfa;			sdata[1]=0x01;			sdata[2]=0x08;			sdata[3]=0x02;			CRC16(rdata,5);			if(rdata[5]==crc_h && rdata[6]==crc_l)			{			if(rdata[4]==0x01)			{				sdata[4]=0x01;				sdata[5]=flag_led1;				CRC16(sdata,6);				sdata[6]=crc_h;				sdata[7]=crc_l;				send_chars(8);			}			if(rdata[4]==0x02)			{				sdata[4]=0x02;				sdata[5]=flag_led2;				CRC16(sdata,6);				sdata[6]=crc_h;				sdata[7]=crc_l;				send_chars(8);			}			}		}		if(rdata[0]==TOU && rdata[1]==ADDRESS && rdata[2]==0x07 && rdata[3]==0x03 && rdata[4]==0x00)		{			sdata[0]=0xfa;			sdata[1]=0x01;			sdata[2]=0x09;			sdata[3]=0x03;			sdata[4]=0x00;			CRC16(rdata,5);			if(rdata[5]==crc_h && rdata[6]==crc_l)			{				ADC_CONTR|=0x40;				while(!(ADC_CONTR & 0x20));				ADC_CONTR &=~0x20;						sdata[5]=ADC_RES;				sdata[6]=ADC_RESL;				CRC16(sdata,7);				sdata[7]=crc_h;				sdata[8]=crc_l;				send_chars(9);						}		}				rdata[0]=0;		//send_chars(5);		//LED1=!LED1;	}}void main(void){P3M1=0;//0000 0000P3M0=0;//0000 0000P1M1=0;//0000 0000P1M0=2;//0000 0010   BEEP(P1_1 SET 0 1)  SW1(P1_2)BEEP=1;delay(90000);BEEP=0;delay(90000);BEEP=1;delay(90000);BEEP=0;delay(90000);BEEP=1;delay(90000);BEEP=0;delay(90000);BEEP=1;delay(90000);BEEP=0;delay(90000);UartInit();n1=0;n2=0;i=0;flag_r_ok=0;counter_times=0;sdata[0]=0x30;sdata[1]=0x31;sdata[2]=0x32;sdata[3]=0x33;sdata[4]=0x34;sdata[5]=0x35;sdata[6]=0x36;sdata[7]=0x37;sdata[8]=0x38;//init ADCADCCFG=0x0f;ADC_CONTR=0x80;busy=0;counter_ab=30000;while(1){	if(i>0)	{		counter_times++;		if(counter_times>MAX_TIME_OUT)		{			i=0;			rdata[0]=0;			counter_times=0;		}	}	flag_current_a=ENC_A;	flag_current_b=ENC_B;	if(flag_last_a==flag_current_a && flag_last_b==flag_current_b)	{		counter_ab=counter_ab;	}	else	{		counter_ab=counter_ab+CW_CCW[flag_current_a][flag_current_b][flag_last_a][flag_last_b];	}		flag_last_a=flag_current_a;	flag_last_b=flag_current_b;	/*	sdata[3]=counter_ab;	sdata[2]=counter_ab>>8;	sdata[1]=counter_ab>>16;	sdata[0]=counter_ab>>24;	send_chars(4);	*/	deal_cmd();}}

标签: #python1l