完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
用的是sillicon lab的ADC0源程序,现在用仿真器单步运行发现信号采不进来,measurement和Result里面的值始终是0x00000000,信号源用的是方波信号(信号发生器),各种频率的都试过了,信号幅值也没有超过参考电压(2.43V),这个程序里里配置了一个sw1,觉得不影响就没接(***itSW1=P3^7),这个开关到底是干嘛用的,有关系吗?
***出来 //----------------------------------------------------------------------------- // Includes //----------------------------------------------------------------------------- #include #include //----------------------------------------------------------------------------- // 16-bit SFR Definitions for 'F12x //----------------------------------------------------------------------------- sfr16 ADC0 = 0xbe; // ADC0 data sfr16 RCAP2 = 0xca; // Timer2 capture/reload sfr16 RCAP3 = 0xca; // Timer3 capture/reload sfr16 TMR2 = 0xcc; // Timer2 sfr16 TMR3 = 0xcc; // Timer3 //----------------------------------------------------------------------------- // Global Constants //----------------------------------------------------------------------------- #define BAUDRATE 115200 // Baud rate of UART in bps #define SYSCLK 49000000 // Output of PLL derived from (INTCLK*2) #define SAMPLE_RATE 50000 // Sample frequency in Hz #define INT_DEC 256 // Integrate and decimate ratio #define SAR_CLK 2500000 // Desired SAR clock speed #define SAMPLE_DELAY 50 // Delay in ms before taking sample ***it LED = P1^6; // LED='1' means ON ***it SW1 = P3^7; // SW1='0' means switch pressed //----------------------------------------------------------------------------- // Function Prototypes //----------------------------------------------------------------------------- void OSCILLATOR_Init (void); void PORT_Init (void); void UART1_Init (void); void ADC0_Init (void); void TIMER3_Init (int counts); void ADC0_ISR (void); void Wait_MS (unsigned int ms); //----------------------------------------------------------------------------- // Global Variables //----------------------------------------------------------------------------- long Result; // ADC0 decimated value //----------------------------------------------------------------------------- // main() Routine //----------------------------------------------------------------------------- void main (void) { long measurement; // Measured voltage in mV WDTCN = 0xde; // Disable watchdog timer WDTCN = 0xad; OSCILLATOR_Init (); // Initialize oscillator PORT_Init (); // Initialize cros***ar and GPIO UART1_Init (); // Initialize UART1 TIMER3_Init (SYSCLK/SAMPLE_RATE); // Initialize Timer3 to overflow at // sample rate ADC0_Init (); // Init ADC SFRPAGE = ADC0_PAGE; AD0EN = 1; // Enable ADC EA = 1; // Enable global interrupts while (1) { EA = 0; // Disable interrupts // The 12-bit ADC value is averaged across INT_DEC measurements. The result is // then stored in Result, and is right-justified // The measured voltage applied to AIN 0.1 is then: // // Vref (mV) // measurement (mV) = --------------- * Result (bits) // (2^12)-1 (bits) measurement = Result * 2430 / 4095; EA = 1; // Re-enable interrupts SFRPAGE = UART1_PAGE; printf("AIN0.1 voltage: %ld mVn",measurement); SFRPAGE = CONFIG_PAGE; LED = ~SW1; // LED reflects state of switch Wait_MS(SAMPLE_DELAY); // Wait 50 milliseconds before taking // another sample } } //----------------------------------------------------------------------------- // Initialization Subroutines //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // OSCILLATOR_Init //----------------------------------------------------------------------------- // // Return Value : None // Parameters : None // // This function initializes the system clock to use the internal oscillator // at 24.5 MHz multiplied by two using the PLL. // //----------------------------------------------------------------------------- void OSCILLATOR_Init (void) { int loop; // Software timer char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page SFRPAGE = CONFIG_PAGE; // Set SFR page OSCICN = 0x83; // Set internal oscillator to run // at its maximum frequency CLKSEL = 0x00; // Select the internal osc. as // the SYSCLK source //Turn on the PLL and increase the system clock by a factor of M/N = 2 SFRPAGE = CONFIG_PAGE; PLL0CN = 0x00; // Set internal osc. as PLL source SFRPAGE = LEGACY_PAGE; FLSCL = 0x10; // Set FLASH read time for 50MHz clk // or less SFRPAGE = CONFIG_PAGE; PLL0CN |= 0x01; // Enable Power to PLL PLL0DIV = 0x01; // Set Pre-divide value to N (N = 1) PLL0FLT = 0x01; // Set the PLL filter register for // a reference clock from 19 - 30 MHz // and an output clock from 45 - 80 MHz PLL0MUL = 0x02; // Multiply SYSCLK by M (M = 2) for (loop=0; loop < 256; loop++); // Wait at least 5us PLL0CN |= 0x02; // Enable the PLL while(!(PLL0CN & 0x10)); // Wait until PLL frequency is locked CLKSEL = 0x02; // Select PLL as SYSCLK source SFRPAGE = SFRPAGE_SAVE; // Restore SFR page } //----------------------------------------------------------------------------- // PORT_Init //----------------------------------------------------------------------------- // // Return Value : None // Parameters : None // // This function configures the cros***ar and GPIO ports. // // P0.4 digital push-pull UART TX // P0.5 digital open-drain UART RX // P1.6 digital push-pull LED // AIN0.1 analog Analog input (no configuration necessary) //----------------------------------------------------------------------------- void PORT_Init (void) { char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page SFRPAGE = CONFIG_PAGE; // set SFR page XBR0 = 0x00; XBR1 = 0x00; XBR2 = 0x44; // Enable cros***ar and weak pull-up // Enable UART1 P0MDOUT |= 0x01; // Set TX1 pin to push-pull P1MDOUT |= 0x40; // Set P1.6(LED) to push-pull SFRPAGE = SFRPAGE_SAVE; // Restore SFR page } //----------------------------------------------------------------------------- // UART1_Init //----------------------------------------------------------------------------- // // Return Value : None // Parameters : None // // Configure the UART1 using Timer1, for // //----------------------------------------------------------------------------- void UART1_Init (void) { char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page SFRPAGE = UART1_PAGE; SCON1 = 0x10; // SCON1: mode 0, 8-bit UART, enable RX SFRPAGE = TIMER01_PAGE; TMOD &= ~0xF0; TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit reload if (SYSCLK/BAUDRATE/2/256 < 1) { TH1 = -(SYSCLK/BAUDRATE/2); CKCON |= 0x10; // T1M = 1; SCA1:0 = xx } else if (SYSCLK/BAUDRATE/2/256 < 4) { TH1 = -(SYSCLK/BAUDRATE/2/4); CKCON &= ~0x13; // Clear all T1 related bits CKCON |= 0x01; // T1M = 0; SCA1:0 = 01 } else if (SYSCLK/BAUDRATE/2/256 < 12) { TH1 = -(SYSCLK/BAUDRATE/2/12); CKCON &= ~0x13; // T1M = 0; SCA1:0 = 00 } else { TH1 = -(SYSCLK/BAUDRATE/2/48); CKCON &= ~0x13; // Clear all T1 related bits CKCON |= 0x02; // T1M = 0; SCA1:0 = 10 } TL1 = TH1; // Initialize Timer1 TR1 = 1; // Start Timer1 SFRPAGE = UART1_PAGE; TI1 = 1; // Indicate TX1 ready SFRPAGE = SFRPAGE_SAVE; // Restore SFR page } //----------------------------------------------------------------------------- // ADC0_Init //----------------------------------------------------------------------------- // // Return Value : None // Parameters : None // // Configure ADC0 to use Timer3 overflows as conversion source, to // generate an interrupt on conversion complete, and to use right-justified // output mode. Enables ADC end of conversion interrupt. Leaves ADC disabled. // //----------------------------------------------------------------------------- void ADC0_Init (void) { char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page SFRPAGE = ADC0_PAGE; ADC0CN = 0x04; // ADC0 disabled; normal tracking // mode; ADC0 conversions(ת»») are initiated // on overflow of Timer3; ADC0 data is // right-justified(ÓÒ¶ÔÆë) REF0CN = 0x07; // Enable temp sensor, on-chip VREF, // and VREF output buffer AMX0CF = 0x00; // AIN inputs are single-ended (default) AMX0SL = 0x01; // Select AIN0.1 pin as ADC mux input ADC0CF = (SYSCLK/SAR_CLK) << 3; // ADC conversion clock = 2.5MHz ADC0CF |= 0x00; // PGA gain = 1 (default) EIE2 |= 0x02; // enable ADC interrupts SFRPAGE = SFRPAGE_SAVE; // Restore SFR page } //----------------------------------------------------------------------------- // TIMER3_Init //----------------------------------------------------------------------------- // // Return Value : None // Parameters : // 1) int counts - calculated Timer overflow rate // range is postive range of integer: 0 to 32767 // // Configure Timer3 to auto-reload at interval specified by // interrupt generated) using SYSCLK as its time base. // //----------------------------------------------------------------------------- void TIMER3_Init (int counts) { char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page SFRPAGE = TMR3_PAGE; TMR3CN = 0x00; // Stop Timer3; Clear TF3; TMR3CF = 0x08; // use SYSCLK as timebase RCAP3 = -counts; // Init reload values TMR3 = RCAP3; // Set to reload immediately EIE2 &= ~0x01; // Disable Timer3 interrupts TR3 = 1; // start Timer3 SFRPAGE = SFRPAGE_SAVE; // Restore SFR page } //----------------------------------------------------------------------------- // Interrupt Service Routines //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // ADC0_ISR //----------------------------------------------------------------------------- // // Here we take the ADC0 sample, add it to a running total // decrement our local decimation counter // zero, we post the decimated result in the global variable // //----------------------------------------------------------------------------- void ADC0_ISR (void) interrupt 15 { static unsigned int_dec=INT_DEC; // Integrate/decimate counter // we post a new result when // int_dec = 0 static long accumulator=0L; // Here's where we integrate the // ADC samples AD0INT = 0; // Clear ADC conversion complete // indicator accumulator += ADC0; // Read ADC value and add to running // total int_dec--; // Update decimation counter if (int_dec == 0) // If zero, then post result { int_dec = INT_DEC; // Reset counter Result = accumulator >> 8; accumulator = 0L; // Reset accumulator } } //----------------------------------------------------------------------------- // Support Subroutines //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // Wait_MS //----------------------------------------------------------------------------- // // Return Value : None // Parameters: // 1) unsigned int ms - number of milliseconds of delay // range is full range of integer: 0 to 65335 // // This routine inserts a delay of // //----------------------------------------------------------------------------- void Wait_MS(unsigned int ms) { char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page SFRPAGE = TMR2_PAGE; TMR2CN = 0x00; // Stop Timer3; Clear TF3; TMR2CF = 0x00; // use SYSCLK/12 as timebase RCAP2 = -(SYSCLK/1000/12); // Timer 2 overflows at 1 kHz TMR2 = RCAP2; ET2 = 0; // Disable Timer 2 interrupts TR2 = 1; // Start Timer 2 while(ms) { TF2 = 0; // Clear flag to initialize while(!TF2); // Wait until timer overflows ms--; // Decrement ms } TR2 = 0; // Stop Timer 2 SFRPAGE = SFRPAGE_SAVE; // Restore SFRPAGE } //----------------------------------------------------------------------------- // End Of File //----------------------------------------------------------------------------- |
|
相关推荐 |
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
嵌入式学习-飞凌嵌入式ElfBoard ELF 1板卡-LCD显示图片编程示例之介绍mmap
72 浏览 0 评论
《DNESP32S3使用指南-IDF版_V1.6》第二章 常用的C语言知识点
509 浏览 0 评论
【RA-Eco-RA2E1-48PIN-V1.0开发板试用】(第三篇)ADC采集+PWM输出
546 浏览 0 评论
《DNK210使用指南 -CanMV版 V1.0》第四十五章 人脸识别实验
545 浏览 0 评论
1008 浏览 0 评论
【youyeetoo X1 windows 开发板体验】少儿AI智能STEAM积木平台
11763 浏览 31 评论
浏览过的版块 |
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-22 14:53 , Processed in 0.779944 second(s), Total 71, Slave 53 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号