PIC16F1827 ブレッドボード配線５　AD変換

# AD変換
<iframe width="560" height="315" src="https://www.youtube.com/embed/E7vYdfniRI8?si=6aCWFyPUcqO2CE2o" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
I2Cで、LCDに文字列を表示できるようになったので、AD変換の値を表示させます。
RA0/AN0（１７番ピン）にVRを接続します。

![IMG_4974[1].JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3693934/4ee90e18-c559-b14f-977b-d02968a6f136.jpeg)
LCDの2段目の"４５３"がADCの値。

:::note info
# AD変換モジュールの初期化
```C:ADC初期化関数
void ADC_INIT(void)
{
    uint8_t buf;
    
    //1.Configure PortA
    buf=TRISA;
    TRISA=buf|0x01;
    ANSELA|=0x01;
  
    //2.Configure the ADC module
    ADCON1bits.ADFM=1;      //Right justified.（右詰めでデータを格納)
    ADCON1bits.ADCS=0b101;  //FOSC/16  Select ADC conversion clock 
    ADCON1bits.ADNREF=0;    //Vref-:AVss Configure voltage referrence （リファレンス電源：ネガティブ）
    ADCON1bits.ADPREF=0b00; //Vref+ is connected to Vdd.（リファレンス電源：ポジティブ）
    ADCON0bits.CHS=0x00;    //RA0:Select ADC input channel（ADCチャンネル指定）
    
    //Turn on ADC module
    ADCON0bits.ADON=1; //ADCモジュール稼働開始
    PIR1bits.ADIF=0;
    PIE1bits.ADIE=1; //ADC割り込み許可
    
    //ADC struct data initialize(ユーザー定義データ初期化）
    ADC.Completed=false;   //main内ADC関連処理開始フラグ
    
  
}
```

:::


:::note info 
ADCのトリガーは、Timer2の周期割込み内で、セットします。
```C:Timer2の割り込みとADC完了割込みのコード
    //Timer2割り込み処理ADCトリガー
    if(PIE1bits.TMR2IE == 1 && PIR1bits.TMR2IF)
    {
        PIR1bits.TMR2IF=0;
        ADCON0bits.ADGO=1;
       // while(ADCON0bits.ADGO);
    }
    
    //AD変換完了割り込み
    if(PIE1bits.ADIE==1 && PIR1bits.ADIF)
    {
        PIR1bits.ADIF = 0;
        ADC.data[0]=ADRESH;
        ADC.data[0]<<=8;
        ADC.data[0]|=ADRESL;
        PIE1bits.ADIE = 0;//一旦、ADC割り込みを停止。
        PIE1bits.TMR2IE=0;//一旦、Timer2割り込みを停止。
        ADC.Completed = true;//メインルーチンで処理開始
    }
```

PIC16F1827は、オートコンバージョントリガー機能がないので、Timer2の割り込み処理内で、`ADGO`ビットをセットしてAD変換を開始し、完了したらADC割り込み処理で変換値を取り出し、メインルーチンでその値を表示させています。
PIC16Fの割り込みベクタは１つなので、Timer2割り込み、ADC割り込み処理を同じ割込み関数内に記述し、
if文で割込み判定を行っています。
:::

```C:Peripheral.h

#ifndef PERIPHERAL_H
#define	PERIPHERAL_H

#include <xc.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#define _XTAL_FREQ 16000000

#ifdef	__cplusplus
extern "C" {
#endif

//**************************************************//    
//   USART 
//**************************************************//    
//#define BAUDRATE 19200
#define BAUDRATE 115200  
#define SET_SPBRG_DATA  (((unsigned int)((_XTAL_FREQ/16)/BAUDRATE))-1)
#define SET_SPBRG_DATA1 (((unsigned int)((_XTAL_FREQ/4)/BAUDRATE))-1)    
extern void USART_INIT(void);
extern void putch(uint8_t byte);
extern uint8_t getch(void);
#define rxLength 30
typedef struct {
    uint8_t length;
    uint8_t rxBuf[30];
    bool rxCompleted;
}_usart;    
extern _usart usart;

#define rxBuffer_length 30
typedef struct{
    uint8_t ch;
    uint8_t Buffer[rxBuffer_length];
    uint8_t index;
    uint8_t Completed;
}_rx;
extern _rx rx;

//************************************************//
//Timer0 8bits timer
//************************************************//
#define T0_UP 1
#define T0_STOP 0
typedef struct{
    bool up;
    uint16_t cnt;
}_tm0;    
extern _tm0 tm0;

//************************************************//
//Timer1
//************************************************//
#define T1_UP 1
#define T1_STOP 0
typedef struct{
    bool up;
    uint16_t cnt;
}_tm;    
extern _tm tm1;

//************************************************//
//ADC
//************************************************//
#define ADC_Sampling_Num 10    
typedef struct{
    uint8_t Ch;
    uint16_t val;
    bool    Completed;
    uint16_t data[ADC_Sampling_Num+1];
    uint8_t index;
    uint16_t RMS;
    int32_t cal;
}_ADC;
extern _ADC ADC;

extern void Timer0_INIT(void);
extern void Timer2_INIT(void);
extern void Interrupt_START(void);
extern void ADC_INIT(void);

#ifdef	__cplusplus
}
#endif

#endif	/* MYLIB_H */


```

```C:Peripheral.c
#include <pic16f1827.h>

#include "Peripheral.h"

/*-----------------------------------------
 *USART
 *-----------------------------------------*/
_usart usart;
_rx rx;
void USART_INIT(void)
{
    uint8_t buf;
    unsigned int brg;
    //SPBRG value set
    brg = SET_SPBRG_DATA1;
    SP1BRGL =(unsigned char) brg;
    SP1BRGH =(unsigned char) (brg>>8); 
    //TX-------------------------------------
    TXREG=0x00;
    //TXSTA SYNC:1 BRGH:1 TXEN:1 TX9:0
    buf =TXSTA & 0x83;
    TXSTA = buf | 0x24;
    //RX-------------------------------------
    //RCSTA  SPEN:1 RX9:0 CREN:1
    buf =RCSTA & 0x2F;
    RCSTA =buf | 0x90;
    //BAUR RATE CONTROL REGISTER
    BAUDCON = 0x08;
    //if interrupt is desired, TXIE set(PIE1)
    PIE1bits.TXIE=0;//disable
    //if interrupts are desired, RCIE set(PIE1)
    PIE1bits.RCIE=1;//enable
    
    usart.length=0;
    usart.rxCompleted=false;
}
void putch(uint8_t byte)
{
    while(!PIR1bits.TXIF)continue;
    TXREG=byte;
}

uint8_t getch(void)
{
    while(!PIR1bits.RCIF){};
    return RCREG;
}



/*---------------------------------------------------
 Timer0     8bits　Timer　
 ---------------------------------------------------*/
_tm0 tm0;
void Timer0_INIT(void)
{
    OPTION_REGbits.TMR0CS = 0;  //ClockSource:Fosc/4
    OPTION_REGbits.PSA = 0;     //assingned to the Timer0
    OPTION_REGbits.PS = 0b111;  //1:256
    TMR0=0x63;  //Fosc:32Mhz interval time 10ms
    INTCONbits.T0IE = 1;
    tm0.cnt=0;
}


/*---------------------------------------------------
 Timer1
 ---------------------------------------------------*/
 _tm tm1;
void Timer1_INIT(void)
{
    TMR1L=0x0B;//16MHz 1ms PSC:1:8
    TMR1H=0xFE;
    T1CON=0x31;    
    T1GCON=0x00;
    PIE1bits.TMR1IE=1;
    tm1.up=T0_STOP;
    tm1.cnt=0x0000;
}

/*-------------------------------------------
Timer2
--------------------------------------------*/
void Timer2_INIT(void)
{
    T2CONbits.T2OUTPS=0b0000;//post sc 1:16
    T2CONbits.T2CKPS=0b11;//PSC 1:16
    T2CONbits.TMR2ON=1;
    PR2=0xFA;//1ms matching.
    TMR2=0;
    PIR1bits.TMR2IF=0;
    PIE1bits.TMR2IE=1;
}

/*---------------------------------------------------
 Timer4
 ---------------------------------------------------*/
void Timer4_INIT(void)
{
    T4CONbits.T4OUTPS=0b1111;//1:16 Postscaler
    TMR4=0;
    T4CONbits.T4CKPS=0b01;//prescle1:4
    PR4=0x4B;
    PIE3bits.TMR4IE=1;
    PIR3bits.TMR4IF=0;
    T4CONbits.TMR4ON=1;
}

/*---------------------------------------------------
 Timer6
 ---------------------------------------------------*/
void Timer6_INIT(void)
{
    T6CONbits.T6OUTPS=0b0000;//1:16 Postscaler
    T6CONbits.T6CKPS=0b11;//prescle1:16
    T6CONbits.TMR6ON=1;
    PR6=0xFA;
    TMR6=0;
    PIR3bits.TMR6IF=0;
    PIE3bits.TMR6IE=1;
}
//interrupt start
void Interrupt_START(void)
{
    INTCONbits.PEIE=1;
    INTCONbits.GIE=1;
}
/*---------------------------------------------------
 AD converter
 -------------------------------------------------------*/
_ADC ADC;
void ADC_INIT(void)
{
    uint8_t buf;
    
    //1.Configure PortA
    buf=TRISA;
    TRISA=buf|0x01;
    ANSELA|=0x01;
  
    //2.Configure the ADC module
    ADCON1bits.ADFM=1;      //Right justified.
    ADCON1bits.ADCS=0b101;  //FOSC/16  Select ADC conversion clock
    ADCON1bits.ADNREF=0;    //Vref-:AVss Configure voltage referrence 
    ADCON1bits.ADPREF=0b00; //Vref+ is connected to Vdd.
    ADCON0bits.CHS=0x00;    //RA0:Select ADC input channel
    
    //Turn on ADC module
    ADCON0bits.ADON=1;
    PIR1bits.ADIF=0;
    PIE1bits.ADIE=1;
    
    //ADC struct data initialize
    ADC.index=0;
    ADC.cal=0;
    ADC.Completed=false;
    
  
}

/*----------------------------------------------*/
//PWM
/*-----------------------------------------------*/

//RB0
void CCP1_PWM_Init(void)
{
    CCP1CONbits.CCP1M=0b1100;//PWMmode
    CCPTMRSbits.C1TSEL0=0b00;//Timer2 in PWM mode
    CCPR1L=0x00;
    TRISB0=0;
    APFCON0bits.CCP1SEL=1;//RB0
}

//RA7
void CCP2_PWM_Init(void)
{
    CCP2CONbits.CCP2M=0b1100;//PWMmode
    CCPTMRSbits.C2TSEL0=0b00;//Timer2 in PWM mode
    CCPR2L=0x00;
    TRISA7=0;
    APFCON0bits.CCP2SEL=1;//RA7
}

//RA3
void CCP3_PWM_Init(void)
{
    CCP3CONbits.CCP3M=0b1100;//PWMmode
    CCPTMRSbits.C3TSEL0=0b00;//Timer2in PWM mode
    CCPR3L=0x00;
    TRISA3=0;
}

//RA4
void CCP4_PWM_Init(void)
{
    CCP4CONbits.CCP4M=0b1100;//PWMmode
    CCPTMRSbits.C4TSEL0=0b00;//Timer2in PWM mode
    CCPR4L=0x00;
    TRISA4=0;
}

```

```C:interrupt.h
#ifndef INTERRUPT_H
#define	INTERRUPT_H

#ifdef	__cplusplus
extern "C" {
#endif
    extern void __interrupt()isr();
#ifdef	__cplusplus
}
#endif

#endif	/* INTERRUPT_H */
```

```C:interrupt.c
#include <pic16f1827.h>
#include "Peripheral.h"
#include "Interrupt.h"

void __interrupt()isr()
{
    uint8_t ch;
    
    //USART受信割り込み処理
    if(PIE1bits.RCIE==1 &&  PIR1bits.RCIF)
    {
        PIR1bits.RCIF=0;
        ch = getch();
        usart.rxBuf[usart.length++]=ch;
        if(usart.length>=rxLength)
        {
            usart.length=0;
        }
        if(ch==0x0A)
        {//CR,LF受信
            usart.rxCompleted=true;
            usart.rxBuf[usart.length-2]=0x00;
            PIE1bits.RCIE=0;
        }
    }
    
    //Timer0割り込み処理
    if(INTCONbits.TMR0IE==1 && INTCONbits.TMR0IF )
    {
        INTCONbits.TMR0IF=0;
        tm0.cnt++;
        if(tm0.cnt==50)
        {
            tm0.cnt=0;
            INTCONbits.TMR0IE=0;
            tm0.up=true;
        }
        TMR0=0x63;
    }
    
    //Timer2割り込み処理ADCトリガー
    if(PIE1bits.TMR2IE == 1 && PIR1bits.TMR2IF)
    {
        PIR1bits.TMR2IF=0;
        ADCON0bits.ADGO=1;
       // while(ADCON0bits.ADGO);
    }
    
    //AD変換完了割り込み
    if(PIE1bits.ADIE==1 && PIR1bits.ADIF)
    {
        PIR1bits.ADIF = 0;
        ADC.data[0]=ADRESH;
        ADC.data[0]<<=8;
        ADC.data[0]|=ADRESL;
        PIE1bits.ADIE = 0;
        PIE1bits.TMR2IE=0;
        ADC.Completed = true;
    }

}


```

```main.c

// PIC16F1827 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1
//#pragma config FOSC = HS      // Oscillator Selection (HS Oscillator, High-speed crystal/resonator connected between OSC1 and OSC2 pins)
#pragma config FOSC = INTOSC    // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = ON       // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#pragma 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 Enable (Fail-Safe Clock Monitor is enabled)

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

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>
#include <stdlib.h>
#include <stdio.h>
#include "Interrupt.h"
#include "Peripheral.h"
#include "I2C_MSSP1.h"
#include "LCDdriver.h"

#define _XTAL_FREQ  16000000

void Oscillator_Init(void);
void Port_Init(void);

char txt[20];

void main(void)
{    
    uint8_t val=0x55;
    uint8_t counter=0x00;
    uint8_t length=0;
    //Basic Hard Initialize
    Oscillator_Init();
    Port_Init(); 
    
    //Peripheral初期化
    Timer0_INIT();
    USART_INIT();
    I2C1_Reg_Init();//I2C初期化
    Timer2_INIT();  //1khzサンプリング
    ADC_INIT();     //ADC初期化
    
    //IOex:MCP23017初期化
    I2C1_b2Write(0x4C,0x0A,0x00);//コンフィグ
    I2C1_b2Write(0x4C,0x00,0x00);//PortA IOdirection
    I2C1_b2Write(0x4C,0x01,0x00);//PortB IOdirection
    
    //LCD SC1602　初期化
    LCD_Init(LcdDeviceAdd);//Fclock=100kHzで初期化。
    SSP1ADD=0x09;//Fosc=16MHz Fclock=400kHz
    LCD_Printf(LcdDeviceAdd,"PIC16F1827",10,0x80);
    
    //割り込み許可
    Interrupt_START();
    
    while(1)
    {
        //USART interrupt processing
        if(usart.rxCompleted)
        {
            usart.rxCompleted=false;
            printf("%s\n",usart.rxBuf);
            usart.length=0;
            PIE1bits.RCIE=1;
        }
        
        //Timer0 interrupt processing
        if(tm0.up)
        {
            tm0.up=false;
            LATBbits.LATB0=~LATBbits.LATB0;
            I2C1_b2Write(0x4C,0x14,val);//IOex:PortA
            I2C1_b2Write(0x4C,0x15,val);//IOex:PortB
            
            if(val==0x55)
                val=0xAA;
            else
                val=0x55;
            
            counter++;
            length=sprintf(txt,"Count:%3d",counter);
            LCD_Printf(LcdDeviceAdd,txt,length,0xC0);
            INTCONbits.TMR0IE=1;
        }   
        
        if(ADC.Completed)
        {
            ADC.Completed=false;
            
            length=sprintf(txt,"%4d",ADC.data[0]);
            LCD_Printf(LcdDeviceAdd,txt,length,0xCC);
                        
            PIE1bits.ADIE = 1;
            PIE1bits.TMR2IE=1;
        }
    }
    return;
}

void Oscillator_Init(void)
{
    OSCCONbits.SPLLEN=0;
    OSCCONbits.IRCF=0b1111;//16Mhz
    OSCCONbits.SCS=0b10;//InternalOscillator
}

void Port_Init(void)
{
    TRISA=0x00;
    ANSELA=0x00;
    TRISB=0x04;     //RX:RB2
    ANSELB=0x00;
    PORTA=0x00;
    PORTB=0x00;  
    //Altanative Pin Selective
    APFCON0bits.RXDTSEL=1;  //RX:RB2
    APFCON1bits.TXCKSEL=1;  //TX:RB5
    //I2Cpin
    TRISBbits.TRISB1=1;
    TRISBbits.TRISB4=1;
}


```