Project 21 - An Rotary Display (CleO IO )

<< Click to Display Table of Contents >>

Navigation:  Projects >

Project 21 - An Rotary Display (CleO IO )

This project uses MikroElectronica Rotary Click to implement a Rotary Switch. CleO is used to display the current selection of the switch. The Mikroe Rotary Click is connected the CleO IO hardware.

 

 

Component(s)

 

This project uses the following component(s) -

 

MikroE Rotary Click

CleO IO

 

 

Wiring Diagram

 

 

 

Project 21-Rotary Schematic

 

 

Code

 

The Setup() routine initializes the CleO50 display and configures the GPIO pins used by the Rotary Click. ENCA, ENCB, and SW are inputs used for detecting the rotation and switch press. Additionally, a Chip Select output pin is required for driving the 74HC595 shift register. Here is code snippet -

 

void setup()

{

CleO.begin();

 pinMode(PIN_ENCA, INPUT);

 pinMode(PIN_ENCB, INPUT);

 pinMode(PIN_SW, INPUT);

 pinMode(PIN_CS, OUTPUT);

 unsel();

 Led_State = Led_Init[0]; /* Initial state is one LED ON */

 light_leds(0x0001); /* Update the shift register */

}

 

The loop() routine polls the ENCA, ENCB and SW pins for any level changes. The relative phase between ENCA and ENCB indicates the direction of rotation. The display is updated if there is a change in the status of the LEDs. The below code snippet explains how this is done -

 

void loop()

{

 uint8_t ENCA, ENCB, SW;

 /* Encoder processing */

 ENCA = digitalRead(PIN_ENCA);

 ENCB = digitalRead(PIN_ENCB);

 

 /* If logic state on pin A or pin B change, rotate the LEDs */

 if ((ENCA_bit != ENCA) || (ENCB_bit != ENCB)) {

   if (ENCB_bit == 1 || ENCA == 1) {

       Led_State = (Led_State << 1) | (Led_State >> 15);

   }

   if (ENCA_bit == 1 || ENCB == 1) {

       Led_State = (Led_State >> 1) | (Led_State << 15);

   }

   ENCA_bit = ENCA; /* Save state */              

   ENCB_bit = ENCB;                      

   light_leds(Led_State);

 }

 /* Switch processing */

 SW = digitalRead(PIN_SW);

 if (SW && (SW != SW_bit)) {

   Switch_state++;

   if (Switch_state == 4) Switch_state = 0;

   Led_State = Led_Init[Switch_state];

   light_leds(Led_State);

 }

 SW_bit = SW;

 update_cleo(Led_State); /* Finally update the CleO display */

}

 

On the CleO50 display, a mirror of the LED display on the click is shown. A large orange ring is drawn surrounded by 16 small blue circles. The color of the blue circle changes as the switch is rotated. Here is the code snippet -

 

void Update_Display(uint16_t  led_state)

{

 if (led_state == Prev_LED_State) return; /* Only update if there's a change */

 

 Prev_LED_State = led_state;

 int cx = 400;

 int cy = 240;

 CleO.Start();

 /* Draw a big circle */

 CleO.CircleExt(cx, cy, (cx - (BDR + DIA + WDTH * 2)) / 2 , (cx - (BDR + DIA)) / 2, DARK_ORANGE, MM, 0, 0);

 byte i = 0;

 /* Draw 16 smaller discs surrounding it */

 for (float a = 90.0; i < DETENTS; a -= 360.0 / DETENTS, i++) {

 

 /* If bit is set, update the LED else don't */

   if (Z_BitIsSet(led_state, (0x0001 << i) ) ) {

     CleO.CircleExt(cx, cy, 0, DIA / 2, FORESTGREEN, MM, a, (cx - (BDR + DIA)) / 2 + DIA / 2);

   }

   else {

     CleO.CircleExt(cx, cy, 0, DIA / 2, LIGHT_BLUE, MM, a, (cx - (BDR + DIA)) / 2 + DIA / 2);

   }

 }

 CleO.SetBackgroundGradient(0, 0, cx * 2, cy * 2, DARK_BLUE, DARK_RED);

 CleO.Show();

}

 

 

Output

 

 

Project 21 RotaryDisplay