Hardware of KuttyPy

The board provides 32 terminals that are organized in to 4 groups, each having 8 terminals, namely A, B,C and D.  The individual terminals are marked as PA0 -- PA7 for port A, PB0 -- PB7 for port B etc. They can be controlled/monitored as a group or as individual pins. Each pin of the port can be configured as output or input, by setting/clearing the corresponding bit in the Data Direction Register.  For complete details refer to the ATMega32 datasheet.

To use the terminals PA0 to PA7, you need to access the registers DDRA, PORTA and PINA. Each pin can be made to act as an input or output

DDRA : Data direction register. Writing a 1 to any bit makes the corresponding pin an output. For example  setReg(DDRA, 255) makes all the pins as output.

PORTA: This is the data for the pins configured as outputs. Writing a 1 to a bit makes the corresponding pin HIGH. For pins configured as inputs writing a 1 connects the pin to the supply through an internal 10k resistor.

PINA: This is for reading the status of the input pins. data = getReg(PINA) gets you the status of voltage levels present on PA0 to PA7

The other ports can be used in a similar manner.

In addition to the simple I/O ports, the hardware provides features like Analog to Digital Converters, Timer/Counters etc.

Analog to Digital Converter
      Enabling the ADC of ATmega32 makes PA0 to PA7 to accept analog signals and convert them into digital data. The registers to control are the ADC status and control register and the ADC multiplex register.

The following lines code explains the usage of the ADC.  For more details, refer to the Atmega32 datasheet.

ADSP2 = 2
REFS1 = 7
REFS0 = 6
ADEN   = 7
ADIF   = 4
ADSC   = 6

setReg(ADMUX, (1 << ADLAR) |(1 << REFS1) | (1 << REFS0) | 0)
setReg(ADCSRA, 1 << ADEN | (1 << ADSC) | 1 << ADSP2)
print getReg(ADCH)

 The timer/counters along with set point registers and comparators are very powerful tools for implementing Pulse Width Modulated waveforms, frequency counters etc. The processor clock is given to a counter, after selected pre-scaling, and the counter is compared with registers that can be set by the user.

The control registers of the Timer/Counter0 is shown below.

The code given below generates a PWM signals on pin PB3

setReg( TCCR0 , (1 << WGM01) | (1 << WGM00) | (1 << COM01) | 1 )
setReg(OCR0 , 64)    # 256/4 = 64, means 25% duty cycle

By changing the value of OCR0, we can change the brightness of an LED connected to PB3.