Encoder

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 DigitalOut myled(LED1);
00004 DigitalOut Center(PA_9);
00005 DigitalIn Port0(PA_8);
00006 DigitalIn Port1(PA_10);
00007 
00008 PortOut C(PortC,0xFF);
00009 InterruptIn irqA(PA_1);
00010 InterruptIn irq0(PA_8);
00011 InterruptIn irq1(PA_10);
00012 
00013 int z=0,Zustand=0,Zustandalt=0;
00014 
00015 void isr(void)
00016 {
00017    myled=!myled;
00018 
00019 }
00020 
00021 void isrEnc(void)
00022 {
00023     Zustandalt=Zustand;
00024     if (Port0==0 && Port1==0) Zustand=0;
00025     if (Port0==1 && Port1==0) Zustand=1;
00026     if (Port0==1 && Port1==1) Zustand=2;
00027     if (Port0==0 && Port1==1) Zustand=3;
00028     if (Zustandalt==3 && Zustand==0) z++;
00029     if (Zustandalt==0 && Zustand == 3) z--;
00030     C=(z<<2)+Zustand;
00031 }
00032 
00033 int main() {
00034     Center=1;
00035  //   Port0.mode(PullDown);
00036  //   Port1.mode(PullDown);
00037     irqA.rise(&isr);
00038     irqA.mode(PullDown);
00039     irq0.rise(&isrEnc);
00040     irq0.fall(&isrEnc);
00041     irq0.mode(PullDown);
00042     irq1.rise(&isrEnc);
00043     irq1.fall(&isrEnc);
00044     irq1.mode(PullDown);
00045     
00046     RCC->AHBENR|=0b100;
00047     GPIOC->MODER=0x5555;
00048     //GPIOC->ODR=0x55;
00049     C=0;
00050     while(1) {
00051 
00052        /*
00053         myled = 1; // LED is ON
00054         wait(0.2); // 200 ms
00055         myled = 0; // LED is OFF
00056         wait(1.0); // 1 sec
00057         C=0;
00058         */
00059     }
00060 }