完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
sfr16 ADC0 = 0xbd; // ADC0 result
void main() { // unsigned int j; Init_Device(); EA=1; TR2=1; ti0=1; while(1) { //P4=~P4; //for(j=0;j<50000;j++); } } void ADC0_ISR (void) interrupt 10 { static unsigned long accumulator = 0; // Accumulator for averaging static unsigned int measurements = 2048; // Measurement counter unsigned long result=0; unsigned long mV; // Measured voltage in mV unsigned int i,j; AD0INT = 0; // Clear ADC0 conv. complete flag accumulator += ADC0; measurements--; if(measurements==0) { measurements = 2048; result = accumulator / 2048; accumulator=0; // The 10-bit ADC value is averaged across 2048 measurements. // The measured voltage applied to P1.4 is then: // // Vref (mV) // measurement (mV) = --------------- * result (bits) // (2^10)-1 (bits) mV = result * 3300 / 1023; printf("P1.1 voltage: %ld mVn",mV); for(i=0;i<10000;i++) for(j=0;j<40;j++); P4=~P4; } // Toggle LED } 这是一个ADC的实例程序,printf那句在串口调试助手中为什么看不到结果,只能看到LED灯在闪烁 |
|
相关推荐
7个回答
|
|
|
|
|
|
//----------------------------------------------------------------------------- // Includes //----------------------------------------------------------------------------- #include "c8051F340.h" // SFR declarations #include //----------------------------------------------------------------------------- // 16-bit SFR Definitions for 'F34x //----------------------------------------------------------------------------- sfr16 TMR2RL = 0xca; // Timer2 reload value sfr16 TMR2 = 0xcc; // Timer2 counter sfr16 ADC0 = 0xbd; // ADC0 result //----------------------------------------------------------------------------- // Global CONSTANTS //----------------------------------------------------------------------------- #define SYSCLK 12000000 // SYSCLK frequency in Hz #define BAUDRATE 115200 // Baud rate of UART in bps ***it LED = P2^2; // LED='1' means ON //----------------------------------------------------------------------------- // Function PROTOTYPES //----------------------------------------------------------------------------- void SYSCLK_Init (void); void PORT_Init (void); void Timer2_Init(void); void ADC0_Init(void); void UART0_Init (void); //----------------------------------------------------------------------------- // MAIN Routine //----------------------------------------------------------------------------- void main (void) { PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer // enable) SYSCLK_Init (); // Initialize system clock to // 24.5MHz PORT_Init (); // Initialize cros***ar and GPIO Timer2_Init(); // Init Timer2 to generate // overflows to trigger ADC UART0_Init(); // Initialize UART0 for printf's ADC0_Init(); // Initialize ADC0 EA = 1; // enable global interrupts while (1) { // spin forever } } void SYSCLK_Init (void) { OSCICN = 0x83; // configure internal oscillator for // 12MHz / 1 RSTSRC = 0x04; // enable missing clock detector } void PORT_Init (void) { XBR0 = 0x01; // Enable UART0 XBR1 = 0xC0; // Enable cros***ar and weak pull-ups P0MDOUT |= 0x10; // Set TX pin to push-pull P2MDOUT |= 0x04; // enable LED as a push-pull output P1MDIN &= ~0x02; // set P1.1 as an analog input } void Timer2_Init (void) { TMR2CN = 0x00; // Stop Timer2; Clear TF2; // use SYSCLK as timebase, 16-bit // auto-reload CKCON |= 0x10; // select SYSCLK for timer 2 source TMR2RL = 65535 - (SYSCLK / 10000); // init reload value for 10uS TMR2 = 0xffff; // set to reload immediately TR2 = 1; // start Timer2 } void ADC0_Init (void) { ADC0CN = 0x02; // ADC0 disabled, normal tracking, // conversion triggered on TMR2 overflow REF0CN = 0x03; // Enable on-chip VREF and buffer AMX0P = 0x13; // ADC0 positive input = P1.1 AMX0N = 0x1F; // ADC0 negative input = GND // i.e., single ended mode ADC0CF = ((SYSCLK/3000000)-1)<<3; // set SAR clock to 3MHz ADC0CF |= 0x00; // right-justify results EIE1 |= 0x08; // enable ADC0 conversion complete int. AD0EN = 1; // enable ADC0 } void UART0_Init (void) { SCON0 = 0x10; // SCON0: 8-bit variable bit rate // level of STOP bit is ignored // RX enabled // ninth bits are zeros // clear RI0 and TI0 bits if (SYSCLK/BAUDRATE/2/256 < 1) { TH1 = -(SYSCLK/BAUDRATE/2); CKCON |= 0x08; // T1M = 1; SCA1:0 = xx } else if (SYSCLK/BAUDRATE/2/256 < 4) { TH1 = -(SYSCLK/BAUDRATE/2/4); CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 01 CKCON |= 0x01; } else if (SYSCLK/BAUDRATE/2/256 < 12) { TH1 = -(SYSCLK/BAUDRATE/2/12); CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 00 } else if (SYSCLK/BAUDRATE/2/256 < 48) { TH1 = -(SYSCLK/BAUDRATE/2/48); CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 10 CKCON |= 0x02; } else { while (1); // Error. Unsupported baud rate } TL1 = TH1; // init Timer1 TMOD &= ~0xf0; // TMOD: timer 1 in 8-bit autoreload TMOD |= 0x20; TR1 = 1; // START Timer1 TI0 = 1; // Indicate TX0 ready } void ADC0_ISR (void) interrupt 10 { static unsigned long accumulator = 0; // Accumulator for averaging static unsigned int measurements = 2048; // Measurement counter unsigned long result=0; unsigned long mV; // Measured voltage in mV AD0INT = 0; // Clear ADC0 conv. complete flag accumulator += ADC0; measurements--; if(measurements==0) { measurements = 2048; result = accumulator / 2048; accumulator=0; mV = result * 2440 / 1023; printf("P1.1 voltage: %ld mVn",mV); } LED=~LED; // Toggle LED } //----------------------------------------------------------------------------- // End Of File //----------------------------------------------------------------------------- |
|
|
|
|
|
|
|
找到原因了,我用VSPD模拟了一对串口,跟我的串口重复了
|
|
|
|
进来学学、、、、、、、
|
|
|
|
kankan
|
|
|
|
不错不错,学习咯~~
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
464 浏览 0 评论
478 浏览 1 评论
基于瑞萨FPB-RA4E2智能床头灯项目——1编译环境搭建与点亮驱动ws2812全彩LED
439 浏览 0 评论
嵌入式学习-飞凌嵌入式ElfBoard ELF 1板卡-LCD显示图片编程示例之介绍mmap
994 浏览 0 评论
《DNESP32S3使用指南-IDF版_V1.6》第二章 常用的C语言知识点
1056 浏览 0 评论
【youyeetoo X1 windows 开发板体验】少儿AI智能STEAM积木平台
11783 浏览 31 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-25 11:57 , Processed in 0.524436 second(s), Total 54, Slave 46 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号