//----------------------------------------------------------------------------- // Includes //----------------------------------------------------------------------------- #include // SFR declarations #include //----------------------------------------------------------------------------- // 16-bit SFR Definitions for 'F00x //----------------------------------------------------------------------------- sfr16 DP = 0x82; // data pointer sfr16 TMR3RL = 0x92; // Timer3 reload value sfr16 TMR3 = 0x94; // Timer3 counter sfr16 ADC0 = 0xbe; // ADC0 data sfr16 ADC0GT = 0xc4; // ADC0 greater than window sfr16 ADC0LT = 0xc6; // ADC0 less than window sfr16 RCAP2 = 0xca; // Timer2 capture/reload sfr16 T2 = 0xcc; // Timer2 sfr16 DAC0 = 0xd2; // DAC0 data sfr16 DAC1 = 0xd5; // DAC1 data //----------------------------------------------------------------------------- // Function PROTOTYPES //----------------------------------------------------------------------------- void SYSCLK_Init (void); //----------------------------------------------------------------------------- // MAIN Routine //----------------------------------------------------------------------------- void main (void){ WDTCN = 0xde; // disable watchdog timer WDTCN = 0xad; SYSCLK_Init (); // initialize oscillator REF0CN = 0x03; // Reference Control Register //---------------------------------------------------------------------------- // DAC Configuration //---------------------------------------------------------------------------- DAC0CN = 0x80; // DAC0 Control Register DAC1CN = 0x80; // DAC1 Control Register while(1) { DAC0L = 0xAB; // DAC0 Low Byte Register DAC0H = 0x0C; // DAC0 High Byte Register DAC1L = 0xAB; // DAC1 Low Byte Register DAC1H = 0x0C; // DAC1 High Byte Register } } //----------------------------------------------------------------------------- // Initialization Subroutines //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // Oscillator Configuration //----------------------------------------------------------------------------- void SYSCLK_Init (void) { int i; // delay counter OSCXCN = 0x67; // start external oscillator with // 18.432MHz crystal for (i=0; i < 256; i++) ; // XTLVLD blanking interval (>1ms) while (!(OSCXCN & 0x80)) ; // Wait for crystal osc. to settle OSCICN = 0x88; // select external oscillator as SYSCLK // source and enable missing clock // detector } C8051F005的AD驅(qū)動(dòng)程序(C語(yǔ)言)//----------------------------------------------------------------------------- // Includes //----------------------------------------------------------------------------- #include #include #include //----------------------------------------------------------------------------- // Function PROTOTYPES //----------------------------------------------------------------------------- #define uchar unsigned char #define uint unsigned int void SYSCLK_Init (void); void delaynus(unsigned int q) ; //N us延時(shí)函數(shù) void PORT_Init (void); void SPI0_Init (void); void LCD_Init(void); void SendSPIByte(unsigned char ch); void delaynms (unsigned int j); void writecom(unsigned char com); void writedata(unsigned char d); void writechar(unsigned char ua); void lcd_Net(void); void Write_COM(uchar ins); void SendSPIByte(uchar disdata); void lcden(datad); void LCD_set_xy( unsigned char x, unsigned char y ); void lcd_xhl(void); void LCD_write_string(unsigned char n); void lcd_adc(void); void UART0_Init (void); void ADC (void); sbit lcdcs=P3^0; //----------------------------------------------------------------------------- // Global CONSTANTS //----------------------------------------------------------------------------- #define BAUDRATE 115200 // Baud rate of UART in bps #define SYSCLK 11059200 // SYSCLK frequency in Hz #define SAMPLE_RATE 50000 // Sample frequency in Hz #define INT_DEC 4096 // integrate and decimate ratio sfr16 DP = 0x82; // data pointer sfr16 TMR3RL = 0x92; // Timer3 reload value sfr16 TMR3 = 0x94; // Timer3 counter sfr16 ADC0 = 0xbe; // ADC0 data sfr16 ADC0GT = 0xc4; // ADC0 greater than window sfr16 ADC0LT = 0xc6; // ADC0 less than window sfr16 RCAP2 = 0xca; // Timer2 capture/reload sfr16 T2 = 0xcc; // Timer2 sfr16 DAC0 = 0xd2; // DAC0 data sfr16 DAC1 = 0xd5; // DAC1 data
unsigned char xdata ADC1Data[4]; unsigned int xdata ad_arr[50]; unsigned char datad ,comd,kk,sdf,ppca; unsigned char virt_port,v,b,m; unsigned char lcd_data_count; unsigned char *lcdpoint; unsigned char qqq; unsigned char data8; unsigned int i; unsigned int AD_MAXT; unsigned int AD_MINT; long AD_VAL,AD_VAL_TEST; unsigned char YYY; long result; long fff; long hhh; int j,w,z; // temperature in hundredths of a unsigned int fff_int, fff_frac; unsigned char p=0,a=0,s,d,f,g=0; //----------------------------------------------------------------------------- // MAIN Routine //----------------------------------------------------------------------------- void main(void){ WDTCN = 0xde; // disable watchdog timer WDTCN = 0xad; SYSCLK_Init (); PORT_Init (); UART0_Init() ; // initialize crossbar and GPIO SPI0_Init (); LCD_Init() ; delaynms(100); Write_COM(0x00); delaynms(100); delaynms(100); Write_COM(0x01); delaynms(100); delaynus (100); LCD_set_xy(0X00,0); delaynus (200); lcd_adc(); delaynus (200); LCD_write_string(5); delaynms (100); while (1) { delaynms (100); ADC (); } } //----------------------------------------------------------------------------- // Initialization Subroutines //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // ADC //----------------------------------------------------------------------------- void ADC (void) { AMX0CF = 0x60; // AMUX Configuration Register AMX0SL = 0x00; // AMUX Channel Select Register ADC0CN = 0x00; // ADC Control Register REF0CN = 0x03; ADC0CN |= 0x80; AD_VAL=0; AD_VAL_TEST = 0; for( YYY = 0;YYY<50;YYY++) { ad_arr[YYY]=0; } YYY=0; ADCINT = 0; for(YYY= 0;YYY<50;YYY++) //AD_TIMES=50 { ADBUSY = 1; while (!ADCINT); ADCINT = 0; _nop_(); _nop_(); _nop_(); //*AD_POINTER = ADC0 ad_arr[YYY]=ADC0; } for(YYY=0;YYY<50;YYY++)// for(i = 0;i { AD_VAL_TEST += ad_arr[YYY]; } AD_VAL = (int)(AD_VAL_TEST/50); _nop_(); _nop_(); _nop_(); _nop_(); hhh=AD_VAL*5; hhh=hhh*5; hhh=hhh*100; hhh=hhh/4096; ADC1Data[0]=hhh/1000; //將A/D轉(zhuǎn)化值存到顯示數(shù)組 ADC1Data[1]=(hhh-1000*ADC1Data[0])/100; ADC1Data[2]=(hhh-1000*ADC1Data[0]-100*ADC1Data[1])/10; ADC1Data[3]=hhh-1000*ADC1Data[0]-100*ADC1Data[1]-10*ADC1Data[2]; delaynus (100); LCD_set_xy(0X05,0); delaynus (200); for(qqq=0;qqq<4;qqq++) { data8=ADC1Data[qqq]; if(data8>=0x0a) data8 += 0x37; else data8 += 0x30; delaynms (200); writechar(data8); } AD_VAL=0; } //----------------------------------------------------------------------------- // PORT_Init //----------------------------------------------------------------------------- void PORT_Init (void) { XBR0 = 0x27; // XBAR0: Initial Reset Value XBR1 = 0x00; // XBAR1: Initial Reset Value XBR2 = 0x5c; // XBAR2: Initial Reset Value PRT0CF = 0x14; // Output configuration for P0 PRT1CF = 0x10; // Output configuration for P3 PRT3CF = 0x01; // Output configuration for P3 } //----------------------------------------------------------------------------- // SYSCLK_Init //----------------------------------------------------------------------------- void SYSCLK_Init (void) { // delaynms counter OSCXCN = 0x67; // start external oscillator with for (i=0; i < 256; i++) ; // XTLVLD blanking interval (>1ms) while (!(OSCXCN & 0x80)) ; // Wait for crystal osc. to settle OSCICN = 0x88; // select external oscillator as SYSCLK } //----------------------------------------------------------------------------- // SPI0_Init //----------------------------------------------------------------------------- void SPI0_Init (void) { SPI0CFG = 0x07; // data sampled on 1st SCK rising edge SPI0CFG|=0xC0; //CKPOL =1; SPI0CN = 0x03; // Master mode; SPI enabled; flags SPI0CKR = SYSCLK/2/2000000-1; // SPI clock <= 8MHz (limited by // EEPROM spec.) } //----------------------------------------------------------------------------- // UART0_Init //----------------------------------------------------------------------------- void UART0_Init (void) { SCON = 0x50; // SCON: mode 1, 8-bit UART, enable RX TMOD = 0x20; // TMOD: timer 1, mode 2, 8-bit reload TH1 = -(SYSCLK/BAUDRATE/16); // set Timer1 reload value for baudrate TR1 = 1; // start Timer1 CKCON |= 0x10; // Timer1 uses SYSCLK as time base PCON |= 0x80; // SMOD = 1 TI = 1; // Indicate TX ready } //----------------------------------------------------------------------------- // LCD_Init //----------------------------------------------------------------------------- void LCD_Init(void) //向LCD送命令 { // unsigned int xdata x; delaynms(100); datad=0x00; SendSPIByte(datad); delaynms(10); Write_COM(0x30); delaynms(10); Write_COM(0x30); delaynms(10); Write_COM(0x30); delaynms(10) ; Write_COM(0x28); delaynms(100); virt_port=0; SendSPIByte(virt_port); lcden(virt_port); Write_COM(0x01); delaynms(100); Write_COM(0x06); delaynms(10) ; Write_COM(0x0C); delaynms(500) ; } //----------------------------------------------------------------------------- // SendSPIByte //----------------------------------------------------------------------------- void SendSPIByte(unsigned char ch) { lcdcs=1; delaynus(100); SPIF = 0; SPI0DAT = ch; while (SPIF == 0); delaynus(100); lcdcs=0; delaynus(100); _nop_(); // 等待寫(xiě)結(jié)束 } //----------------------------------------------------------------------------- // lcden //----------------------------------------------------------------------------- void lcden(datad) { datad|=0x08; SendSPIByte(datad); datad&=0xf7; SendSPIByte(datad); } //----------------------------------------------------------------------------- // delaynms //----------------------------------------------------------------------------- void delaynms (unsigned int uu) { unsigned int oo,ll; for (oo=0;oo { for(ll=0;ll<1140;ll++); } } //----------------------------------------------------------------------------- // writechar //----------------------------------------------------------------------------- void writechar(unsigned char ua) { uint j; uchar t,x; for(j=0;j<500;j++); datad|=0x02; SendSPIByte(datad); datad|=ua&0xf0; SendSPIByte(datad); datad|=0x08; SendSPIByte(datad); for(x=0;x<3;x++); datad&=0xf7; SendSPIByte(datad); for(x=0;x<3;x++); datad&=0x07; delaynus(100); SendSPIByte(virt_port); t|=ua&0x0f; datad|=t<<4; SendSPIByte(datad); for(x=0;x<3;x++); datad|=0x08; SendSPIByte(datad); for(x=0;x<3;x++); datad&=0xf7; SendSPIByte(datad); for(x=0;x<3;x++); datad=0x00; t=0x00; SendSPIByte(datad); } //----------------------------------------------------------------------------- // Write_COM //----------------------------------------------------------------------------- void Write_COM(uchar ins) { uchar t; uint j; for(j=0;j<5000;j++); //用延時(shí)代替查詢(xún) virt_port|=ins&0xf0; SendSPIByte(virt_port); //LCDE=1; virt_port|=0x08; SendSPIByte(virt_port); for(i=3;i>0;i--); //LCDE=0; virt_port&=~0x08; SendSPIByte(virt_port); virt_port&=0x07; SendSPIByte(virt_port); t=ins<<4; virt_port|=t&0xf0; SendSPIByte(virt_port); //LCDE=1; virt_port|=0x08; SendSPIByte(virt_port); for(i=3;i>0;i--); //LCDE=0; virt_port&=~0x08; SendSPIByte(virt_port); virt_port=0; SendSPIByte(virt_port); } //----------------------------------------------------------------------------- // LCD_set_xy //----------------------------------------------------------------------------- void LCD_set_xy( unsigned char x, unsigned char y ) { unsigned char address; if (y == 0) address = 0x80 + x; else address = 0xc0 + x; Write_COM(address); } //----------------------------------------------------------------------------- // LCD_write_string //----------------------------------------------------------------------------- void LCD_write_string(unsigned char n) { unsigned char data1; for(n;n>0;n--) { data1=*lcdpoint; writechar(data1); delaynms(100); lcdpoint++; delaynus(10); } } //----------------------------------------------------------------------------- // delaynus //----------------------------------------------------------------------------- void delaynus(unsigned int q) //N us延時(shí)函數(shù) { for (i=0;i { _nop_(); } } //----------------------------------------------------------------------------- // lcd_adc //----------------------------------------------------------------------------- void lcd_adc(void) { unsigned char xdata ADC0Data[5]=; lcdpoint=&ADC0Data; } 利用C8051F005的DA,AD通過(guò)HD7279實(shí)現(xiàn)數(shù)據(jù)處理類(lèi)問(wèn)題(C語(yǔ)言)//----------------------------------------------------------------------------- // Includes //----------------------------------------------------------------------------- #include #include #include //----------------------------------------------------------------------------- // Function PROTOTYPES //----------------------------------------------------------------------------- #define uchar unsigned char #define uint unsigned int void SYSCLK_Init (void); void PORT_Init (void); void DAC (void); void ADC (void); void long_delay(void); // 長(zhǎng)延時(shí) void short_delay(void); // 短暫延時(shí) void delay10ms(unsigned char); // 延時(shí)10MS void delaynus(unsigned int q) ; //N us延時(shí)函數(shù) void delaynms (unsigned int j); void write7279(unsigned char, unsigned char); // 寫(xiě)入到HD7279 unsigned char read7279(unsigned char command); void send_byte(unsigned char); // 發(fā)送一個(gè)字節(jié) unsigned char receive_byte(void); void DA_dispdata(void); void AD_dispdata(void); void dispStep(); void dispReset(); //----------------------------------------------------------------------------- // Global CONSTANTS //----------------------------------------------------------------------------- #define BAUDRATE 115200 // Baud rate of UART in bps #define SYSCLK 11059200 // SYSCLK frequency in Hz #define SAMPLE_RATE 50000 // Sample frequency in Hz #define INT_DEC 4096 // integrate and decimate ratio sfr16 DP = 0x82; // data pointer sfr16 TMR3RL = 0x92; // Timer3 reload value sfr16 TMR3 = 0x94; // Timer3 counter sfr16 ADC0 = 0xbe; // ADC0 data sfr16 ADC0GT = 0xc4; // ADC0 greater than window sfr16 ADC0LT = 0xc6; // ADC0 less than window sfr16 RCAP2 = 0xca; // Timer2 capture/reload sfr16 T2 = 0xcc; // Timer2 sfr16 DAC0 = 0xd2; // DAC0 data sfr16 DAC1 = 0xd5; // DAC1 data sbit cs=P3^2; // cs at P1.4 sbit clk=P3^3; // clk 連接于 P1.5 sbit dat=P3^1; // dat 連接于 P1.2 sbit key=P1^0; // key 連接于 P1.3 unsigned int xdata ad_arr[50]; unsigned char datad ,comd,kk,sdf,ppca; unsigned char virt_port,v,b,m; unsigned int tmr,ig,is,ib,iq,rg,rs,rb,rq; unsigned char qqq; unsigned char data8; unsigned int i; unsigned int AD_MAXT; unsigned int AD_MINT; long AD_VAL,AD_VAL_TEST; unsigned char YYY; long result; long fff; long hhh,ddd; int j,w,z; // temperature in hundredths of a unsigned int fff_int, fff_frac; unsigned char p=0,a=0,s,d,f,g=0; data unsigned char digit[5]; data unsigned char key_number, e, k; unsigned long cnter; unsigned long CurValue; unsigned char CurStepLen; unsigned long Decvalue0; float Decvalue1,Decvalue2,Decvalue3; /****** HD7279A 指令 ******/ #define CMD_RESET 0xa4 #define CMD_TEST 0xbf #define DECODE0 0x80 #define DECODE1 0xc8 #define UNDECODE 0x90 #define RLC 0xa3 #define RRC 0xa2 #define RL 0xa1 #define RC 0xa0 #define HIDE 0x98 #define FLASH 0x88 #define SEGON 0xe0 #define SEGOFF 0xc0 #define CMD_READ 0x15 /***Main***/ void main(void) { WDTCN = 0xde; // disable watchdog timer WDTCN = 0xad; SYSCLK_Init (); PORT_Init (); cnter=0; Decvalue0 = 0; CurStepLen = 1; for (tmr=0;tmr<0x2000;tmr++); // 上電延時(shí) send_byte(CMD_RESET); write7279(FLASH,0XFF); write7279(HIDE,0x0f); while (1) { key_number=0xff; write7279(FLASH,0xf0); // 第1、2兩位設(shè)為閃爍顯示 write7279(UNDECODE ,0X08); // 在第1位顯示下劃線(xiàn)'_' write7279(UNDECODE+1,0x08); // 在第2位顯示下劃線(xiàn)'_' write7279(UNDECODE+2,0X08); write7279(UNDECODE+3,0X08); do { if (!key) // 如果有鍵按下 IE=0x81; { key_number=read7279(CMD_READ); // 讀出鍵碼 if (key_number < 0x0a) //0~9輸入 { digit[cnter] = key_number; Decvalue0 = Decvalue0*10+digit[cnter]; send_byte(RLC); // delay10ms(12); for(e=0;e<=cnter;e++) { write7279(DECODE1+3-e,digit[e]&0x0f);//delay10ms(5) } if (cnter==3) { DA_dispdata(); cnter=0; } else } } if (key_number==0x0a) //ADD { Decvalue0 =Decvalue0 + CurStepLen; DA_dispdata(); } if (key_number==0x0b) //Dec { if (Decvalue0 > CurStepLen) { Decvalue0 =Decvalue0 - CurStepLen; DA_dispdata(); } } if (key_number==0x0C) //Clear { send_byte(CMD_RESET); write7279(HIDE,0x0f); Decvalue0 = 0; cnter=0; dispReset(); } if (key_number==0x0d) //Ok { write7279(FLASH,0xff); DAC(); // 清除閃爍設(shè)置 } if (key_number==0x0E) // Change Step Len { write7279(FLASH,0xf0); if ( CurStepLen ==1) { CurStepLen =10; } else{ CurStepLen =1;} dispStep(); delay10ms(100); write7279(FLASH,0xff);// DAData = CurValue; } if (key_number==0x0F) //Set Current Value { write7279(HIDE,0x0f); Decvalue0 =0; cnter=0; write7279(FLASH,0xf0); // 第1、2兩位設(shè)為閃爍顯示 for (e=0;e<4;e++) write7279(UNDECODE+e,0X08); // 在第1位顯示下劃線(xiàn)'_' // dispStep(); // DAData = CurValue; } key_number = 0xff; while (!key); IE=0x00; } while(1); // 等待按鍵放開(kāi) } } /***DAC***/ void DAC (void) { REF0CN = 0x03; // Reference Control Register DAC0CN = 0x80; // DAC0 Control Register DAC0L = 0xFF; // DAC0 Low Byte Register DAC0H = 0x0F; %2 C8051F005的中文數(shù)據(jù)簡(jiǎn)介 模擬外設(shè) 8051 兼容的微控制器內(nèi)核 100KSPS ADC 12 位 流水線(xiàn)指令結(jié)構(gòu) 70%的指令的執(zhí)行時(shí)間為一個(gè)或兩 1LSB INL 個(gè)系統(tǒng)時(shí)鐘周期 無(wú)失碼 時(shí)鐘頻率為 25MHz時(shí) 速度可達(dá) 25MIPS 可編程轉(zhuǎn)換速率 增強(qiáng)的中斷系統(tǒng) 最大 100ksps 可有最多 22個(gè)中斷源 可編程為單端輸入或差分輸入 8個(gè)外部輸入 存儲(chǔ)器 16 8 4 2 1 0.5 可編程放大器增益 2304字節(jié)數(shù)據(jù)存儲(chǔ)器 數(shù)據(jù)相關(guān)窗口中斷發(fā)生器 32K 字節(jié)閃速存儲(chǔ)器 可以在系統(tǒng)編程扇區(qū)大小為 內(nèi)置溫度傳感器 3°C 512字節(jié) 兩個(gè) 12 位 DAC 數(shù)字外設(shè) 電壓輸出 32個(gè) I/O口線(xiàn) 所有口線(xiàn)均容許 5V電壓 2 TM TM TM 10微秒建立時(shí)間 C /SMBus SPI 可同時(shí)使用的硬件 I 及 UART串 兩個(gè)比較器 口 16個(gè)可編程滯回電壓值 16 位可編程的計(jì)數(shù)器/定時(shí)器陣列 帶 5 個(gè)捕獲/比較可用于產(chǎn)生中斷或復(fù)位 模塊 每一個(gè)都可配置為 8位 PWM 基準(zhǔn)源 4個(gè)通用 16位計(jì)數(shù)器/定時(shí)器 2.4V 15ppm/°C 雙向復(fù)位 專(zhuān)用的看門(mén)狗定時(shí)器外部基準(zhǔn)輸入 時(shí)鐘源 VDD 監(jiān)視器和節(jié)電降壓檢測(cè)器 內(nèi)部可編程振蕩器 2-16MHz 片內(nèi) JTAG 仿真 外部振蕩器 晶體 RC C 或外部時(shí)鐘 片內(nèi) JTAG 仿真電路提供全速 非侵入式的電路內(nèi) 節(jié)電模式時(shí)使用 可在運(yùn)行中切換時(shí)鐘源 仿真 供電電壓………………………………2.7V - 3.6V 典型工作電流 支持?jǐn)帱c(diǎn) 單步 觀察點(diǎn) 12mA @ 25MHz 觀察/修改存儲(chǔ)器和寄存器 多種節(jié)電休眠和停機(jī)模式 比使用仿真芯片 目標(biāo)仿真頭和仿真插座的仿真系 64 腳 TQFP 封裝 -40°C - +85°C 統(tǒng)有更好的性能 溫度范圍
|