完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
你好,我想在AN7上读一个LM35DZ,但是结果总是0。有人能帮我吗?感谢这是我的密码:
以上来自于百度翻译 以下为原文 Hello, I'm trying to read a LM35DZ at the AN7, but the result is always 0. Can anyone help me? Thanks This is my code: /* * File: main.c * Author: NOT-PDI * * Created on 8 de Junho de 2017, 11:37 */ #include #include #include #include "usart.h" // PIC18F13K22 Configuration Bit Settings // 'C' source line config statements #include //definindo frequência do cristal #define _XTAL_FREQ 32000000 // #pragma config statements should precede project file includes. // Use project enums instead of #define for ON and OFF. // CONFIG1H #pragma config FOSC = HS // Oscillator Selection bits (HS oscillator) #pragma config PLLEN = ON // 4 X PLL Enable bit (Oscillator multiplied by 4) #pragma config PCLKEN = ON // Primary Clock Enable bit (Primary clock enabled) #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor disabled) #pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled) // CONFIG2L #pragma config PWRTEN = ON // Power-up Timer Enable bit (PWRT disabled) #pragma config BOREN = SBORDIS // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled)) #pragma config BORV = 22 // Brown Out Reset Voltage bits (VBOR set to 1.9 V nominal) // CONFIG2H #pragma config WDTEN = ON // Watchdog Timer Enable bit (WDT is always enabled. SWDTEN bit has no effect.) #pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768) // CONFIG3H #pragma config HFOFST = OFF // HFINTOSC Fast Start-up bit (HFINTOSC starts clocking the CPU without waiting for the oscillator to stablize.) #pragma config MCLRE = ON // MCLR Pin Enable bit (MCLR pin enabled, RA3 input pin disabled) // CONFIG4L #pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset) #pragma config LVP = OFF // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled) #pragma config BBSIZ = OFF // Boot Block Size Select bit (512W boot block size) #pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode)) // CONFIG5L #pragma config CP0 = OFF // Code Protection bit (Block 0 not code-protected) #pragma config CP1 = OFF // Code Protection bit (Block 1 not code-protected) // CONFIG5H #pragma config CPB = OFF // Boot Block Code Protection bit (Boot block not code-protected) #pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM not code-protected) // CONFIG6L #pragma config WRT0 = OFF // Write Protection bit (Block 0 not write-protected) #pragma config WRT1 = OFF // Write Protection bit (Block 1 not write-protected) // CONFIG6H #pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers not write-protected) #pragma config WRTB = OFF // Boot Block Write Protection bit (Boot block not write-protected) #pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM not write-protected) // CONFIG7L #pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 not protected from table reads executed in other blocks) #pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 not protected from table reads executed in other blocks) // CONFIG7H #pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot block not protected from table reads executed in other blocks) //prototipos de funções void TMR0Isr(); void leituraTemperatura(); void leituraNivel(); void verificaTanque(); void inicializa_RS232(long velocidade, int modo); //variáveis globais char flagTMR0; bit idTemp; int cont1s, nivel, cont200ms, tanque, contTemp; unsigned int vTemp[20], auxTemp, temp1, temp2, temp3; void interrupt vInt(){ if (INTCONbits.TMR0IF) TMR0Isr(); } /* * */ int main(int argc, char** argv) { // Configura RS232 // 9 bits // Processador 8 MHz x 4 (PLL) = 32 MHz // 19200 bps inicializa_RS232(9600,1); //configura o timer em 50ms TMR0H = 0x3C; TMR0L = 0xB0; T0CON = 0b10000010; //interrupções INTCON = 0b11100000; INTCON2 = 0b10000000; INTCON3 = 0b00000000; // PIE1 = 0b00000000; // PIE2 = 0b00000000; // PIR1 = 0b00000000; // PIR2 = 0b00000000; //oscilador // OSCCON = 0b01100010; // OSCCON2 = 0b00000000; // // OSCTUNE = 0b00000000; //configuração de portas de entrada e saida TRISC = 0b00001111; LATC = 0b00000000; TRISB = 0b01110000; LATB = 0b00000000; WPUB = 0b01110000; //resistores de pull-UP TRISA = 0b00000100; LATA = 0b00000000; WPUA = 0b00000000; //conversor A/D ADCON0 = 0b00000000; ADCON1 = 0b00000000; ADCON2 = 0b10100100; ANSEL = 0b10000000; ANSELH = 0b00000000; CCP1CON = 0b00000000; // CM1CON0 = 0b000000000; // CM2CON0 = 0b000000000; // // SRCON0 = 0b00000000; // SRCON1 = 0b00000000; // // VREFCON0 = 0b00000000; //inicialização de variáveis flagTMR0 = 0; cont1s = 20; cont200ms = 4; nivel = 0; LATCbits.LATC4 = 0 ; while (1){ CLRWDT(); if (flagTMR0){ if (cont200ms) cont200ms--; if (cont1s) cont1s--; // LATCbits.LATC4 = !LATCbits.LATC4; flagTMR0 = 0; leituraTemperatura(); } CLRWDT(); if (!cont1s){ if (idTemp){ auxTemp = auxTemp / 20; cont1s = 20; if (auxTemp > 10000) auxTemp = 0; temp1 = 0x6; temp2 = auxTemp >> 8; temp3 = auxTemp & 0xFF; while(!TXSTAbits.TRMT); TXREG = temp1; while(!TXSTAbits.TRMT); TXREG = temp2; while(!TXSTAbits.TRMT); TXREG = temp3; auxTemp = 0; idTemp = 0; } LATCbits.LATC4 = !LATCbits.LATC4; } CLRWDT(); if (!cont200ms){ cont200ms = 4; } } return (EXIT_SUCCESS); } void TMR0Isr(){ TMR0H = 0x3C; TMR0L = 0xB0; flagTMR0 = 1; // LATCbits.LATC4 = !LATCbits.LATC4; INTCONbits.TMR0IF = 0; } void inicializa_RS232(long velocidade, int modo) {////por padrão é usado o modo 8 bits e sem paridade, mas se necessario ajuste aqui a configuração desejada. //verifique datasheet para ver a porcentagem de erro e se a velocidade é possivel para o cristal utilizado. RCSTA = 0X90; //habilita porta serial,recepção de 8 bit em modo continuo,assincrono. int valor; if (modo == 1) {//modo = 1 ,modo alta velocidade TXSTA = 0X24; //modo assincrono,trasmissao 8 bits. valor = (int) ((((_XTAL_FREQ*4) / velocidade) - 16) / 16); //calculo do valor do gerador de baud rate } else {//modo = 0 ,modo baixa velocidade TXSTA = 0X20; //modo assincrono,trasmissao 8 bits. valor = (int) ((((_XTAL_FREQ*4) / velocidade) - 64) / 64); //calculo do valor do gerador de baud rate } SPBRG = 0x40; SPBRGH = 3; RCIE = 0; //habilita interrupção de recepção TXIE = 0; BAUDCON = 0x18; } void verificaTanque(){ CLRWDT(); if (!PORTBbits.RB4) tanque = 220; else if (!PORTBbits.RB5) tanque = 120; else if (!PORTBbits.RB6) tanque = 65; else tanque = 25; CLRWDT(); } void leituraTemperatura(){ unsigned long tempH, tempL, temperatura; tempH = 0; tempL = 0; temperatura = 0; ADCON0 = 0b00011101; PIR1bits.ADIF = 0; ADCON0bits.GO = 1; while (!PIR1bits.ADIF); tempH = ADRESH; tempH = tempH << 8; tempL = ADRESL; temperatura = tempH + tempL; temperatura = temperatura * 5000; temperatura = temperatura / 4096; vTemp[contTemp] = temperatura; auxTemp += vTemp[contTemp]; contTemp++; if (contTemp == 20){ idTemp = 1; contTemp = 0; } } void leituraNivel(){ int auxNivel, nivelH, nivelL; //LATCbits.LATC4 = 1; //lendo o primeiro nível auxNivel = nivelH = nivelL = 0; ADCON0 = 0b00001001; PIR1bits.ADIF = 0; ADCON0bits.GO = 1; while (!PIR1bits.ADIF); nivelH = ADRESH; nivelH = nivelH << 8; nivelL = ADRESL; auxNivel = nivelH + nivelL; if (auxNivel > 600) nivel = 1; //lendo o segundo nível auxNivel = nivelH = nivelL = 0; ADCON0 = 0b00010001; PIR1bits.ADIF = 0; ADCON0bits.GO = 1; while (!PIR1bits.ADIF); nivelH = ADRESH; nivelH = nivelH << 8; nivelL = ADRESL; auxNivel = nivelH + nivelL; if (auxNivel > 500) nivel = 2; //lendo o terceiro nível auxNivel = nivelH = nivelL = 0; ADCON0 = 0b00010101; PIR1bits.ADIF = 0; ADCON0bits.GO = 1; while (!PIR1bits.ADIF); nivelH = ADRESH; nivelH = nivelH << 8; nivelL = ADRESL; auxNivel = nivelH + nivelL; if (auxNivel > 400) nivel = 3; //lendo o quarto nível auxNivel = nivelH = nivelL = 0; ADCON0 = 0b00011001; PIR1bits.ADIF = 0; ADCON0bits.GO = 1; while (!PIR1bits.ADIF); nivelH = ADRESH; nivelH = nivelH << 8; nivelL = ADRESL; auxNivel = nivelH + nivelL; if (auxNivel > 350) nivel = 4; // LATCbits.LATC4 = 0; } |
|
相关推荐
1个回答
|
|
|
|
|
|
只有小组成员才能发言,加入小组>>
5237 浏览 9 评论
2026 浏览 8 评论
1950 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3201 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2253 浏览 5 评论
772浏览 1评论
660浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
590浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
672浏览 0评论
572浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-21 07:56 , Processed in 1.302714 second(s), Total 75, Slave 59 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号