AD CONVERSION METHOD IN REAL TIME

Asked 2 years ago, Updated 2 years ago, 28 views


with AD conversion value using PIC16F1827 You are trying to change the LED blinking speed.

Here is the code:
   ↓

#include<stdio.h>
# include <stdlib.h>
# include <xc.h>
#define_XTAL_FREQ4000000

// CONFIG1
# pragma config FOSC = INTOSC//Oscillator Selection (INTOSCoscillator: I/O function on CLKIN pin)
# pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
# pragma config PWRTE = ON // Power-up Timer Enabled (PWRT enabled)
# pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
# programa config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
# pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled)
# pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled)
# pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
# pragma config IESO = ON // Internal / External Switchover (Internal / External Switchover mode is enabled)
# pragma config FCMEN = ON // Fail-Safe Clock Monitor Enabled

// CONFIG2
# pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
# pragma config PLLEN = OFF // PLL Enable (4x PLL disabled)
# pragma config STVREN = ON // Stack Overflow / Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
# pragma config BORV = HI // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), high trip point selected.)
# pragma config LVP = ON // Low-Voltage Programming Enabled

void internal_osc();
voidio_int();
void adc_int();
int adc_read();
void wait1(charval);
int adc_tmp;

void main(void){
    internal_osc();
    io_int();
    adc_int();
    while(1){
        adc_tmp = adc_read();
        PORTB = 0x01;
        wait1(adc_tmp>>2);
        PORTB = 0x03;
        wait1(adc_tmp>>2);
        PORTB = 0x07;
        wait1(adc_tmp>>2);
        PORTB = 0x0f;
        wait1(adc_tmp>>2);
        PORTB = 0x00;
        wait1(adc_tmp>>2);
        PORTB = 0x0f;
        wait1(adc_tmp>>2);
        PORTB = 0x00;
        wait1(adc_tmp>>2);
        PORTB = 0x0f;
        wait1(adc_tmp>>2);
    }
}

void internal_osc(void){
    /*
     * SPLLEN = 0; 4 xPLL is disabled
     * IRCF = 1101; 4 MHz
     * SCS=10; Internal oscillator block
     * OSCCON = 0b01101010;
     */
    OSCCON = 0x6a;
}
voidio_int(void){
    /*
     * RB0 = 0; LED 0
     * RB1 = 0; LED1
     * RB2 = 0; LED2
     * RB3 = 0; LED3
     * RA0 = 1;AN0
     * TRISA = 0b00000001;
     * TRISB = 0b00000000;
     * ANSELA = 0b00000001;
     * ANSELB = 0b00000000;
     */
    TRISA = 0x01;
    TRISB = 0x00;
    ANSELA = 0x01;
    ANSELB = 0x00;
}
void adc_int(void){
    /*
     * CHS=00000;AN0
     * ADON = 1;
     * ADFM = 1; Right justified
     * ADCS=001; FOSC->1/8;
     * ->4MHz ad_scan2us
     * ADNREF = 0; VREF-is connected to AVSS
     * ADPREF = 00; VREF + is connected to AVDD
     * ADCON 0 = 0b00000001;
     * ADCON1 = 0b10010000;
     */
    ADCON 0 = 0x01;
    ADCON1 = 0x90;
}
intadc_read(void){
    __delay_us(20);
    ADCON 0 bits.GO_nDONE = 1;
    while(ADCON0bits.GO_nDONE);
    return(ADRESH<8)+ADRESL;
}
void wait1(charval){
    for(inti=0;i<=val;i++){
        __delay_ms(10);
    }
}

This code reads the AD translation value and

adc_tmp value until while returns to top I don't think I can change it.

Not dependent in while loop,
Change the adc_tmp value freely
I can't think of a way.

Can someone teach me?

c

2022-09-29 22:28

1 Answers

I solved myself.
By performing AD conversion during interrupt using timer interrupt
I was able to change the LED blinking speed independently of the main routine.
Thank you.

#include<stdio.h>
# include <stdlib.h>
# include <xc.h>
#define_XTAL_FREQ4000000

// CONFIG1
# pragma config FOSC = INTOSC//Oscillator Selection (INTOSCoscillator: I/O function on CLKIN pin)
# pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
# pragma config PWRTE = ON // Power-up Timer Enabled (PWRT enabled)
# pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
# programa config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
# pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled)
# pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled)
# pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
# pragma config IESO = ON // Internal / External Switchover (Internal / External Switchover mode is enabled)
# pragma config FCMEN = ON // Fail-Safe Clock Monitor Enabled

// CONFIG2
# pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
# pragma config PLLEN = OFF // PLL Enable (4x PLL disabled)
# pragma config STVREN = ON // Stack Overflow / Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
# pragma config BORV = HI // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), high trip point selected.)
# pragma config LVP = ON // Low-Voltage Programming Enabled

void internal_osc();
voidio_int();
void tmr0_int();
void adc_int();
int adc_read();
void wait1(charval);
char tmr0_cnt;
int adc_tmp;

void main(void){
    internal_osc();
    io_int();
    tmr0_int();
    adc_int();
    while(1){
        PORTB = 0x01;
        wait1(adc_tmp>>2);
        PORTB = 0x03;
        wait1(adc_tmp>>2);
        PORTB = 0x07;
        wait1(adc_tmp>>2);
        PORTB = 0x0f;
        wait1(adc_tmp>>2);
        PORTB = 0x00;
        wait1(adc_tmp>>2);
        PORTB = 0x0f;
        wait1(adc_tmp>>2);
        PORTB = 0x00;
        wait1(adc_tmp>>2);
        PORTB = 0x0f;
        wait1(adc_tmp>>2);
    }
}
void interrupt ISR (void) {
    INTCONbits.TMR0IF = 0;
    TMR0 = 130;
    adc_tmp = adc_read();
}
void internal_osc(void){
    /*
     * SPLLEN = 0; 4 xPLL is disabled
     * IRCF = 1101; 4 MHz
     * SCS=10; Internal oscillator block
     * OSCCON = 0b01101010;
     */
    OSCCON = 0x6a;
}
voidio_int(void){
    /*
     * RB0 = 0; LED 0
     * RB1 = 0; LED1
     * RB2 = 0; LED2
     * RB3 = 0; LED3
     * RA0 = 1;AN0
     * TRISA = 0b00000001;
     * TRISB = 0b00000000;
     * ANSELA = 0b00000001;
     * ANSELB = 0b00000000;
     */
    TRISA = 0x01;
    TRISB = 0x00;
    ANSELA = 0x01;
    ANSELB = 0x00;
}
void tmr0_int(void){
    /*
     * 1 msec interval_timer
     * FOSC - > 4 MHz
     * interval_time=(255-TMR0)*(FOSC/4)*PS;
     * PS=010; Prescaler->1/8
     * TMR0 = 130
     * OPTION_REG = 0b10000010;
     */
    OPTION_REG = 0x82;
    TMR0 = 130;
    INTCONbits.TMR0IE = 1;
    INTCONbits.GIE = 1;
}
void adc_int(void){
    /*
     * CHS=00000;AN0
     * ADON = 1;
     * ADFM = 1; Right justified
     * ADCS=001; FOSC->1/8;
     * ->4MHz ad_scan2us
     * ADNREF = 0; VREF-is connected to AVSS
     * ADPREF = 00; VREF + is connected to AVDD
     * ADCON 0 = 0b00000001;
     * ADCON1 = 0b10010000;
     */
    ADCON 0 = 0x01;
    ADCON1 = 0x90;
}
intadc_read(void){
    __delay_us(20);
    ADCON 0 bits.GO_nDONE = 1;
    while(ADCON0bits.GO_nDONE);
    return(ADRESH<8)+ADRESL;
}
void wait1(charval){
    for(inti=0;i<=val;i++){
        __delay_ms(10);
    }
}


2022-09-29 22:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.