新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > STC12C5A60S2的ADC+PWM功能

STC12C5A60S2的ADC+PWM功能

作者: 時間:2016-11-29 來源:網(wǎng)絡(luò) 收藏
/*------------------------------------------------寫入字符函數(shù)------------------------------------------------*/void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data) { if (y == 0) { LCD_Write_Com(0x80 + x); } else { LCD_Write_Com(0xC0 + x); } LCD_Write_Data( Data); }/*------------------------------------------------LCD初始化函數(shù)------------------------------------------------*/void LCD_Init(void) {LCD_Write_Com(0x38); /*顯示模式設(shè)置*/ DelayMs(5); LCD_Write_Com(0x38); DelayMs(5); LCD_Write_Com(0x38); DelayMs(5); LCD_Write_Com(0x38); LCD_Write_Com(0x08); /*顯示關(guān)閉*/ LCD_Write_Com(0x01); /*顯示清屏*/ LCD_Write_Com(0x06); /*顯示光標移動設(shè)置*/ DelayMs(5); LCD_Write_Com(0x0C); /*顯示開及光標設(shè)置*/}/*------------------------------------------------ADC初始化函數(shù)------------------------------------------------*/void InitADC(){P1ASF = 0x01;//使能P1口ADC功能ADC_RES = 0;ADC_CONTR = 0xc8;} /*------------------------------------------------主函數(shù)------------------------------------------------*/ void main(void) { LCD_Init(); LCD_Clear();//清屏LCD_Write_String(5,0,"526lab");//(列,行,數(shù)據(jù))LCD_Write_String(2,1,"Result:"); InitADC();IE=0xa0;//開中斷while (1) { //PWM初始化CCON=0;CMOD=0;//1MCCAP0H=CCAP0L=ADC_RES;CCAPM0=0x42;//開啟PWMCR=1;//計時開始 //調(diào)用顯示LCD_Write_Char(9,1,seg[ge]);LCD_Write_Char(10,1,.);LCD_Write_Char(11,1,seg[shifen]);LCD_Write_Char(12,1,seg[baifen]);LCD_Write_Char(13,1,V);}}/*------------------------------------------------ADC中斷處理函數(shù)------------------------------------------------*/void adc_isr() interrupt 5 using 1{unsigned int temp;temp=ADC_RES;result=temp*0.01953125*1000;ge=result/1000;shifen=result%1000/100;baifen=result%100/10;ADC_CONTR = 0xc8;//開啟轉(zhuǎn)換}12C5A60S2.h的頭文件//--------------------------------------------------------------------------------//新一代 1T 8051系列 單片機內(nèi)核特殊功能寄存器 C51 Core SFRs// 7 6 5 4 3 2 1 0 Reset Valuesfr ACC = 0xE0; //Accumulator 0000,0000sfr B = 0xF0; //B Register 0000,0000sfr PSW = 0xD0; //Program Status Word CY AC F0 RS1 RS0 OV F1 P 0000,0000//-----------------------------------sbit CY = PSW^7;sbit AC = PSW^6;sbit F0 = PSW^5;sbit RS1 = PSW^4;sbit RS0 = PSW^3;sbit OV = PSW^2;sbit P = PSW^0;//-----------------------------------sfr SP = 0x81; //Stack Pointer 0000,0111sfr DPL = 0x82; //Data Pointer Low Byte 0000,0000sfr DPH = 0x83; //Data Pointer High Byte 0000,0000//--------------------------------------------------------------------------------//新一代 1T 8051系列 單片機系統(tǒng)管理特殊功能寄存器// 7 6 5 4 3 2 1 0 Reset Valuesfr PCON = 0x87; //Power Control SMOD SMOD0 LVDF POF GF1 GF0 PD IDL 0001,0000// 7 6 5 4 3 2 1 0 Reset Valuesfr AUXR = 0x8E; //Auxiliary Register T0x12 T1x12 UART_M0x6 BRTR S2SMOD BRTx12 EXTRAM S1BRS 0000,0000//-----------------------------------sfr AUXR1 = 0xA2; //Auxiliary Register 1 - PCA_P4 SPI_P4 S2_P4 GF2 ADRJ - DPS 0000,0000/*PCA_P4:0, 缺省PCA 在P1 口1,PCA/PWM 從P1 口切換到P4 口: ECI 從P1.2 切換到P4.1 口,PCA0/PWM0 從P1.3 切換到P4.2 口PCA1/PWM1 從P1.4 切換到P4.3 口SPI_P4:0, 缺省SPI 在P1 口1,SPI 從P1 口切換到P4 口: SPICLK 從P1.7 切換到P4.3 口MISO 從P1.6 切換到P4.2 口MOSI 從P1.5 切換到P4.1 口SS 從P1.4 切換到P4.0 口S2_P4: 0, 缺省UART2 在P1 口1,UART2 從P1 口切換到P4 口: TxD2 從P1.3 切換到P4.3 口RxD2 從P1.2 切換到P4.2 口GF2: 通用標志位ADRJ:0, 10 位A/D 轉(zhuǎn)換結(jié)果的高8 位放在ADC_RES 寄存器, 低2 位放在ADC_RESL 寄存器1,10 位A/D 轉(zhuǎn)換結(jié)果的最高2 位放在ADC_RES 寄存器的低2 位, 低8 位放在ADC_RESL 寄存器DPS: 0, 使用缺省數(shù)據(jù)指針DPTR01,使用另一個數(shù)據(jù)指針DPTR1*///-----------------------------------sfr WAKE_CLKO = 0x8F; //附加的 SFR WAK1_CLKO/*7 6 5 4 3 2 1 0 Reset ValuePCAWAKEUP RXD_PIN_IE T1_PIN_IE T0_PIN_IE LVD_WAKE _ T1CLKO T0CLKO 0000,0000Bb7 - PCAWAKEUP : PCA 中斷可喚醒 powerdown。b6 - RXD_PIN_IE : 當(dāng) P3.0(RXD) 下降沿置位 RI 時可喚醒 powerdown(必須打開相應(yīng)中斷)。b5 - T1_PIN_IE : 當(dāng) T1 腳下降沿置位 T1 中斷標志時可喚醒 powerdown(必須打開相應(yīng)中斷)。b4 - T0_PIN_IE : 當(dāng) T0 腳下降沿置位 T0 中斷標志時可喚醒 powerdown(必須打開相應(yīng)中斷)。b3 - LVD_WAKE : 當(dāng) CMPIN 腳低電平置位 LVD 中斷標志時可喚醒 powerdown(必須打開相應(yīng)中斷)。b2 - b1 - T1CLKO : 允許 T1CKO(P3.5) 腳輸出 T1 溢出脈沖,F(xiàn)ck1 = 1/2 T1 溢出率b0 - T0CLKO : 允許 T0CKO(P3.4) 腳輸出 T0 溢出脈沖,F(xiàn)ck0 = 1/2 T1 溢出率*///-----------------------------------sfr CLK_DIV = 0x97; //Clock Divder - - - - - CLKS2 CLKS1 CLKS0 xxxx,x000//-----------------------------------sfr BUS_SPEED = 0xA1; //Stretch register - - ALES1 ALES0 - RWS2 RWS1 RWS0 xx10,x011/*ALES1 and ALES0:00 : The P0 address setup time and hold time to ALE negative edge is one clock cycle01 : The P0 address setup time and hold time to ALE negative edge is two clock cycles.10 : The P0 address setup time and hold time to ALE negative edge is three clock cycles. (default)11 : The P0 address setup time and hold time to ALE negative edge is four clock cycles.RWS2,RWS1,RWS0:000 : The MOVX read/write pulse is 1 clock cycle. 001 : The MOVX read/write pulse is 2 clock cycles.010 : The MOVX read/write pulse is 3 clock cycles.011 : The MOVX read/write pulse is 4 clock cycles. (default)100 : The MOVX read/write pulse is 5 clock cycles.101 : The MOVX read/write pulse is 6 clock cycles.110 : The MOVX read/write pulse is 7 clock cycles.111 : The MOVX read/write pulse is 8 clock cycles.*///--------------------------------------------------------------------------------//新一代 1T 8051系列 單片機中斷特殊功能寄存器//有的中斷控制、中斷標志位散布在其它特殊功能寄存器中,這些位在位地址中定義//其中有的位無位尋址能力,請參閱 新一代 1T 8051系列 單片機中文指南// 7 6 5 4 3 2 1 0 Reset Valuesfr IE = 0xA8; //中斷控制寄存器 EA ELVD EADC ES ET1 EX1 ET0 EX0 0x00,0000//-----------------------sbit EA = IE^7;sbit ELVD = IE^6; //低壓監(jiān)測中斷允許位sbit EADC = IE^5; //ADC 中斷允許位sbit ES = IE^4;sbit ET1 = IE^3;sbit EX1 = IE^2;sbit ET0 = IE^1;sbit EX0 = IE^0;//-----------------------sfr IE2 = 0xAF; //Auxiliary Interrupt - - - - - - ESPI ES2 0000,0000B//-----------------------// 7 6 5 4 3 2 1 0 Reset Valuesfr IP = 0xB8; //中斷優(yōu)先級低位 PPCA PLVD PADC PS PT1 PX1 PT0 PX0 0000,0000//--------sbit PPCA = IP^7; //PCA 模塊中斷優(yōu)先級sbit PLVD = IP^6; //低壓監(jiān)測中斷優(yōu)先級sbit PADC = IP^5; //ADC 中斷優(yōu)先級sbit PS = IP^4;sbit PT1 = IP^3;sbit PX1 = IP^2;sbit PT0 = IP^1;sbit PX0 = IP^0;//-----------------------// 7 6 5 4 3 2 1 0 Reset Valuesfr IPH = 0xB7; //中斷優(yōu)先級高位 PPCAH PLVDH PADCH PSH PT1H PX1H PT0H PX0H 0000,0000sfr IP2 = 0xB5; // - - - - - - PSPI PS2 xxxx,xx00sfr IPH2 = 0xB6; // - - - - - - PSPIH PS2H xxxx,xx00//-----------------------//新一代 1T 8051系列 單片機I/O 口特殊功能寄存器// 7 6 5 4 3 2 1 0 Reset Valuesfr P0 = 0x80; //8 bitPort0 P0.7 P0.6 P0.5 P0.4 P0.3 P0.2 P0.1 P0.0 1111,1111sfr P0M0 = 0x94; // 0000,0000sfr P0M1 = 0x93; // 0000,0000sfr P1 = 0x90; //8 bitPort1 P1.7 P1.6 P1.5 P1.4 P1.3 P1.2 P1.1 P1.0 1111,1111sfr P1M0 = 0x92; // 0000,0000sfr P1M1 = 0x91; // 0000,0000sfr P1ASF = 0x9D; //P1 analog special functionsfr P2 = 0xA0; //8 bitPort2 P2.7 P2.6 P2.5 P2.4 P2.3 P2.2 P2.1 P2.0 1111,1111sfr P2M0 = 0x96; // 0000,0000sfr P2M1 = 0x95; // 0000,0000sfr P3 = 0xB0; //8 bitPort3 P3.7 P3.6 P3.5 P3.4 P3.3 P3.2 P3.1 P3.0 1111,1111sfr P3M0 = 0xB2; // 0000,0000sfr P3M1 = 0xB1; // 0000,0000sfr P4 = 0xC0; //8 bitPort4 P4.7 P4.6 P4.5 P4.4 P4.3 P4.2 P4.1 P4.0 1111,1111sfr P4M0 = 0xB4; // 0000,0000sfr P4M1 = 0xB3; // 0000,0000// 7 6 5 4 3 2 1 0 Reset Valuesfr P4SW = 0xBB; //Port-4 switch - LVD_P4.6 ALE_P4.5 NA_P4.4 - - - - x000,xxxxsfr P5 = 0xC8; //8 bitPort5 - - - - P5.3 P5.2 P5.1 P5.0 xxxx,1111sfr P5M0 = 0xCA; // 0000,0000sfr P5M1 = 0xC9; // 0000,0000//--------------------------------------------------------------------------------//新一代 1T 8051系列 單片機定時器特殊功能寄存器// 7 6 5 4 3 2 1 0 Reset Valuesfr TCON = 0x88; //T0/T1 Control TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0 0000,0000//-----------------------------------sbit TF1 = TCON^7;sbit TR1 = TCON^6;sbit TF0 = TCON^5;sbit TR0 = TCON^4;sbit IE1 = TCON^3;sbit IT1 = TCON^2;sbit IE0 = TCON^1;sbit IT0 = TCON^0;//-----------------------------------sfr TMOD = 0x89; //T0/T1 Modes GATE1 C/T1 M1_1 M1_0 GATE0 C/T0 M0_1 M0_0 0000,0000sfr TL0 = 0x8A; //T0 Low Byte 0000,0000sfr TH0 = 0x8C; //T0 High Byte 0000,0000sfr TL1 = 0x8B; //T1 Low Byte 0000,0000sfr TH1 = 0x8D; //T1 High Byte 0000,0000//--------------------------------------------------------------------------------//新一代 1T 8051系列 單片機串行口特殊功能寄存器// 7 6 5 4 3 2 1 0 Reset Valuesfr SCON = 0x98; //Serial Control SM0/FE SM1 SM2 REN TB8 RB8 TI RI 0000,0000//-----------------------------------sbit SM0 = SCON^7; //SM0/FEsbit SM1 = SCON^6;sbit SM2 = SCON^5;sbit REN = SCON^4;sbit TB8 = SCON^3;sbit RB8 = SCON^2;sbit TI = SCON^1;sbit RI = SCON^0;

關(guān)鍵詞: STC12C5A60S2ADCPWM功

評論


技術(shù)專區(qū)

關(guān)閉