졸업작품/Encoder with Oscilloscope

Encoder with Oscilloscope

ElectricShock 2016. 1. 28. 17:57

http://www.instructables.com/id/How-to-Program-a-PIC-Microcontroller-Read-an-Encod/?ALLSTEPS


Encoder(인코더)를 돌렸을때 PIC18F4520에서 발생하는 출력값을 Oscillocope로 측적하는 실험을 하겠다.



MPLAB X IDE

Select Compiler: XC8

Chip : PIC18F4520

Encoder (Pololu Item # 2269)




출처 https://www.pololu.com/product/2269

 Color

Function 

 Red

 motor power (connects to one motor terminal)

 Black

 motor power (connects to the other motor terminal)

 Green

 encoder GND

 Blue

 encoder Vcc (3.5 – 20 V)

 Yellow

 encoder A output

 White

 encoder B output









/********************************************************************

How to program a PIC & read an encoder program

Last Revised: 1/3/14
Authors: Carson Miller
Written for: PIC18F4525 (Current Version)

*********************************************************************/

#define INPUT         1
#define OUTPUT     0

#define _XTAL_FREQ 4000000  //Used by the XC8 delay_ms(x) macro

// PIC18F25K22 Configuration Bit Settings

#include <xc.h>                             //Includes PIC hardware mapping
#include "GenericTypeDefs.h"      //Includes standard variable types
                                            //XC8 내에 기본적으로 제공해주는 Header이다.

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

// CONFIG1H
#pragma config OSC = INTIO7 

// Oscillator Selection bits (Internal oscillator block, CLKOUT function on RA6, port function on RA7)

//내부 오실레이터 선택비트

#pragma config FCMEN = OFF 

// Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)

#pragma config IESO = OFF 

// Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)

// CONFIG2L
#pragma config PWRT = OFF // 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 = 3 // Brown Out Reset Voltage bits (Minimum setting)

// CONFIG2H
#pragma config WDT = OFF // Watchdog Timer Enable bit (WDT enabled)
#pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
#pragma config CCP2MX = PORTC // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = ON // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
#pragma config LPT1OSC = OFF // Low-Power Timer1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = ON // MCLR Pin Enable bit (MCLR pin enabled; RE3 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 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 (000800-003FFFh) not code-protected)
#pragma config CP1 = OFF // Code Protection bit (Block 1 (004000-007FFFh) not code-protected)
#pragma config CP2 = OFF // Code Protection bit (Block 2 (008000-00BFFFh) not code-protected)

// CONFIG5H
#pragma config CPB = OFF // Boot Block Code Protection bit (Boot block (000000-0007FFh) 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 (000800-003FFFh) not write-protected)
#pragma config WRT1 = OFF // Write Protection bit (Block 1 (004000-007FFFh) not write-protected)
#pragma config WRT2 = OFF // Write Protection bit (Block 2 (008000-00BFFFh) not write-protected)

// CONFIG6H
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot Block (000000-0007FFh) 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 (000800-003FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (004000-007FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF // Table Read Protection bit (Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot Block (000000-0007FFh) not protected from table reads executed in other blocks)

//Dummy variable setup

UINT distance = 0;
CHAR direction = 0;
CHAR error = 0;

void configure(void)
{

//ADC Setup

ADCON1bits.PCFG = 1111; //Turns off all analog inputs (See datasheet p. 224)

//Oscillator Setup

OSCCONbits.IRCF = 110; //Sets oscillator to 4MHz

//Interrupt Setup

INTCONbits.GIE = 1; //Enables all unmasked or high priority interrupts (Depending on IPEN)
INTCONbits.PEIE = 1; //Enables all unmasked peripheral interrupts or low- priority interrupts (Depending on IPEN)
INTCONbits.INT0IF = 0; //Clears interrupt 0 flag bit (Must occur before enabling interrupt)
INTCONbits.INT0IE = 1; //Enables the INT0 external interrupt

INTCON2bits.INTEDG0 = 1; //Sets external interrupt 0 to interrupt on rising edge

RCONbits.IPEN = 0; //Disables priority levels on interrupts

TRISBbits.TRISB0 = INPUT; //Sets INT0 as input
TRISBbits.TRISB1 = INPUT; //Sets INT1 as input

}

void main()
{
configure();

while(1)

{
//Program Loop
}
}

//Main interrupt service routine (ISR)
void interrupt ISR()
{
//Check to see if it is interrupt 0
if (INTCONbits.INT0IF == 1)
{
distance++;
INTCONbits.INT0IF = 0; //Clears interrupt flag
}
else
error = 1;
}