打印
[PIC®/AVR®/dsPIC®产品]

PIC18F452 AD采样

[复制链接]
58|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
代码

/*
* File:   MAIN.c
* Author: Administrator
*
* Created on May 24, 2024, 10:32 AM
*/
#include <xc.h>

//?????
//CONFIG1L
#pragma config OSC = XT          // VREG Sleep Enable bit (Ultra low-power regulator is Disabled (Controlled by REGSLP bit)),?????????
#pragma config OSCS = OFF     // LF-INTOSC Low-power Enable bit (LF-INTOSC in High-power mode during Sleep),???? LF-INTOSC ???????
#pragma config PWRT = OFF         // SOSC Power Selection and mode Configuration bits (Digital (SCLKI) mode),SOSC ??????????,?? ? SCLKI?????? RC0 ? RC1 ? I/O ????
#pragma config BOR = ON           // Extended Instruction Set (Disabled)???????????????????????? ??????

// CONFIG1H
//#pragma config BORV = 20            // Oscillator (HS oscillator (Medium power, 4 MHz - 16 MHz))?HS????????
#pragma config WDT = OFF           // PLL x4 Enable bit (Enabled),??PLL???????4?????32MHz
#pragma config WDTPS = 128           // Fail-Safe Clock Monitor (Disabled)????????????
#pragma config CCP2MUX = ON            // Internal External Oscillator Switch Over Mode (Disabled)???????

// CONFIG2L
#pragma config STVR = ON         // Power Up Timer (Disabled)??????????
#pragma config LVP = ON      // Brown Out Detect (Enabled in hardware, SBOREN disabled)????????????
#pragma config BORV = 3             // Brown-out Reset Voltage bits (1.8V)??????????1.8v
#pragma config CP0 = OFF     // BORMV Power level (ZPBORMV instead of BORMV is selected)???ZPBORMV???BORMV

// CONFIG2H
#pragma config CP1 = OFF      // Watchdog Timer (WDT disabled in hardware; SWDTEN bit disabled),???????
//#pragma config WDTEN = SWDTDIS      // Watchdog Timer (WDT enabled in hardware; SWDTEN bit disabled),?????WDT???SWDTEN?
#pragma config CP2 = OFF         // Watchdog Postscaler (1:256)???????1:256?????1.024s

// CONFIG3H
#pragma config CP3 = OFF    // ECAN Mux bit (ECAN TX and RX pins are located on RB2 and RB3, respectively)?1 = CANTX ? CANRX ?????? RB2 ? RB3 ?
#pragma config CPB = OFF   // MSSP address masking (7 Bit address masking mode)?1 = ?? 7 ???????
#pragma config CPD = OFF      // Master Clear Enable (MCLR Enabled, RE3 Disabled)???MCLR????

// CONFIG4L


// CONFIG6L
#pragma config WRT0 = OFF       // Table Write Protect 00800-01FFF (Disabled)?????
#pragma config WRT1 = OFF       // Table Write Protect 02000-03FFF (Disabled)?????
#pragma config WRT2 = OFF       // Table Write Protect 04000-05FFF (Disabled)?????
#pragma config WRT3 = OFF       // Table Write Protect 06000-07FFF (Disabled)?????

// CONFIG6H
#pragma config WRTC = OFF       // Config. Write Protect (Disabled)???????????
#pragma config WRTB = OFF       // Table Write Protect Boot (Disabled)?????????
#pragma config WRTD = OFF       // Data EE Write Protect (Disabled)???EEPROM?????

// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protect 00800-01FFF (Disabled)??????
#pragma config EBTR1 = OFF      // Table Read Protect 02000-03FFF (Disabled)??????
#pragma config EBTR2 = OFF      // Table Read Protect 04000-05FFF (Disabled)??????
#pragma config EBTR3 = OFF      // Table Read Protect 06000-07FFF (Disabled)??????

// CONFIG7H
#pragma config EBTRB = OFF      // Table Read Protect Boot (Disabled)?????????

//void TMR1_ISR(void);
//void delay(void);
unsigned char j,flag;
unsigned char n;
//#pragma interrupt chk_isr
unsigned    long    i;
//#pragma code    My_Hiprio_Int=0x08

//#pragma code
int main(int argc, char** argv) {
    PORTD=0;
    TRISD=0;
    TRISAbits.RA0=1;
    ADCON0=0X81;
    ADCON1=0X0E;
    INTCONbits.TMR0IF=0;
    T0CON=0XC7;

    while(1)
    {

        while(INTCONbits.TMR0IF==0);
        INTCONbits.TMR0IF=0;
        ADCON0bits.GO=1;
        while(ADCON0bits.DONE==1);
        PORTD=ADRESH;

    }
    return (0);
}

运行效果图





————————————————

                            版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原文链接:https://blog.csdn.net/chenbingjy/article/details/139248599

使用特权

评论回复
沙发
598330983| | 2025-2-23 11:18 | 只看该作者
#include <pic18f452.h>
#include <stdio.h>

// 配置位设置
#pragma config OSC = HS         // 使用外部高速晶振
#pragma config WDT = OFF        // 关闭看门狗定时器
#pragma config LVP = OFF        // 关闭低电压编程

// 定义AD转换结果变量
unsigned int adc_result = 0;

// 初始化AD模块
void ADC_Init() {
    ADCON1 = 0x0E;  // 配置AN0为模拟输入,其他引脚为数字I/O
    ADCON2 = 0x92;  // 右对齐结果,TAD = 12 TOSC,ACQT = 12 TAD
    ADCON0 = 0x01;  // 使能AD模块,选择AN0通道
}

// 读取AD转换结果
unsigned int ADC_Read() {
    ADCON0bits.GO = 1;  // 启动AD转换
    while (ADCON0bits.GO);  // 等待转换完成
    return ((unsigned int)ADRESH << 8) | ADRESL;  // 返回10位AD结果
}

// 主程序
void main() {
    // 初始化
    TRISA = 0xFF;  // 设置PORTA为输入
    TRISB = 0x00;  // 设置PORTB为输出
    ADC_Init();    // 初始化AD模块

    while (1) {
        adc_result = ADC_Read();  // 读取AD转换结果
        PORTB = adc_result >> 2;  // 将AD结果的高8位输出到PORTB(10位结果右移2位)
        __delay_ms(100);          // 延时100ms
    }
}

使用特权

评论回复
板凳
598330983| | 2025-2-23 11:19 | 只看该作者
ADCON1配置:

ADCON1 = 0x0E;:将AN0配置为模拟输入,其他引脚配置为数字I/O。

参考电压选择VDD和VSS。

ADCON2配置:

ADCON2 = 0x92;:

右对齐AD转换结果。

TAD(AD时钟周期)设置为12 TOSC。

ACQT(采样时间)设置为12 TAD。

ADCON0配置:

ADCON0 = 0x01;:使能AD模块,并选择AN0通道。

ADC_Read函数:

启动AD转换并等待转换完成。

返回10位的AD转换结果。

主程序:

初始化PORTA和PORTB。

循环读取AD转换结果,并将结果的高8位输出到PORTB。

使用特权

评论回复
地板
dongnanxibei| | 2025-2-23 13:18 | 只看该作者
寄存器掌握好了,写代码还是挺好看的。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

26

主题

50

帖子

0

粉丝