336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

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;
}

Posted by ElectricShock
:
BLOG main image
잡동사니들(지극히 개인취향인...) (다른글에도 댓글 부탁해요♥) You May Leave English Messages on GuestBook. by ElectricShock

공지사항

카테고리

분류 전체보기 (782)
Programming(=프로그래밍) (3)
MiDi (2)
Animation (4)
Blender (3D Graphic Program.. (10)
Blendtuts.com (Series) (1)
Blender 기초 팁들 (2)
Processing (디지털미디어과) (2)
Music (1)
Books in the world (0)
Communication(CAN, UART, et.. (12)
MCU Examples (PIC 기반) (7)
Transistor (1)
Mikro C Pro (11)
Mikro Pascal (1)
Proton IDE (0)
Robot (0)
Swift 3D (1)
Dummies Series (1)
All about Hacking (0)
제2 외국어 (1)
PIC 해외서적들 (3)
AVR (25)
PIC (MikroC) (MPLAB) (4)
Assembly (2)
ARM (3)
Arduino (26)
PSpice (1)
Proteus ISIS (14)
CodeVision (2)
FPGA (15)
MPLAB (24)
PCB (the Procedure) (15)
3D Printer (5)
PICKIT3 (6)
Matlab (11)
RaspBerry PI (15)
BeagleBone (1)
Android Studio (17)
졸업작품 (172)
Korea History (0)
Issue(사회) (73)
Multimeter 리뷰 (1)
Oscilloscope (1)
A (34)
B (19)
J (6)
C (32)
P (12)
T (37)
H (12)
I (12)
M (44)
R (5)
E (5)
F (2)
D (9)
O (2)
L (7)
S (9)
W (2)
V (6)
G (14)
Visual C++ or Visual Studio (2)
Android App Development (0)

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백