Getting Started

(lily) #1

Chapter 4: C Types, Operators, and Expressions


Cylon Eye Speed and Polarity Control

se to control the
polari y polarity I ean that we will
LED which will be off, or all the
LEDs off and the sweep LED on. We will control the polarity with the switch


this ample we will use the ~ bitwise operator to invert the LEDs on port D.

Open PortIO.c in Personal Notepad and save it as CylonEyes.c in a new directory
CylonEyes. Make the following changes to the main() function


// Cyl
#include <av
#inclu

int main (vo
{
ize the scroll delay_count
unsigned long delay_count = 10000;

a variable for the speed increase
unsigned long increase = 0;

ase = PINB;

polarity
127)

e -= 127;
ity = 1;

In this example we will use port B to input data that we will u
Cylon eye movement rate and the LED ty. B m
set either all the LEDs on except the sweep


connected to the port B pin 7, leaving the lower pins to allow us to set the speed
increase factor from 0 to 127.


In ex


onEyes.c
r/io.h>
de <avr/delay.h>

id)

// declare and initial

// declare

// declare a variable for the polarity
unsigned char polarity = 0;

// Init port pins
DDRB = 0x00; // set port B for input
DDRD = 0xFF; // set port D for output

while(1)
{
// read the switches
incre

// set the
if(increase >
{
increas
polar
Free download pdf