| 1 | # Reading and writing special function registers |
| 2 | |
| 3 | Features of microcontrollers ranging from I/O operations to ADC reading and I2C communications are all configured by toggling and reading bits in variables called Special Function Registers (SFR) |
| 4 | |
| 5 | For example, in the ATMEGA32 microcontroller, the DDRB register (Data direction Register for B pins PB0 - PB7) , is used to set which pins on Port B are output, and which are input. This can be done by writing an 8-bit number(0 - 255) to DDRB , where each bit controls one of the pins from PB0 to PB7. |
| 6 | |
| 7 | DDRB = 255 (binary 11111111) implies all pins will be output type. DDRB = 160 (101000000) implies PB7 and PB5 will be output type, and rest will be input. |
| 8 | |
| 9 | Similarly, PORTB register is used to turn an output HIGH(5V), or LOW(0V/Ground) |
| 10 | |
| 11 | |
| 12 |  |
| 13 | |
| 14 | PB5 and PB7 set to Output type (DDRB=160), and made HIGH(PORTB=160). |
| 15 | |
| 16 | ### Test this with a simple code to turn on an LED connected to PB3 |
| 17 | |
| 18 | === "Python Code" |
| 19 | ```python |
| 20 | from kuttyPy import * # automatically connects to an available device. |
| 21 | setReg('DDRB',8) # PB3 made output. 0b00001000 = 8 |
| 22 | setReg('PORTB', 8) #PB3 to 5 Volt supply. RED LED turns on |
| 23 | ``` |
| 24 | |
| 25 | === "C equivalent" |
| 26 | ```C hl_lines="1" |
| 27 | /* Compile with AVR-GCC for atmega32, and upload with avrdude/kuttypy-gui */ |
| 28 | #include <avr/io.h> |
| 29 | int main (void) { |
| 30 | DDRB = 8; // Data Direction Register for port B |
| 31 | PORTB = 8; // PB3 HIGH |
| 32 | } |
| 33 | return 0; |
| 34 | } |
| 35 | ``` |
| 36 | |
| 37 | ## Python Functions for basic register access |
| 38 | |
| 39 | ### setting and reading register values |
| 40 | |
| 41 | === "setReg" |
| 42 | ```python hl_lines="1" |
| 43 | |
| 44 | def setReg(Register Name, Value) |
| 45 | writes a value to a REGISTER defined in the datasheet of the processor |
| 46 | |
| 47 | Register Name : a string representing the register. e.g. 'DDRB', 'ADMUX' etc |
| 48 | Value: an integer between 0 and 255 , because atmega32 is an 8 bit microcontroller. |
| 49 | return: True if success |
| 50 | |
| 51 | ``` |
| 52 | |
| 53 | |
| 54 | === "getReg" |
| 55 | ```python hl_lines="1" |
| 56 | |
| 57 | def getReg(Register Name) |
| 58 | reads a value frm a REGISTER defined in the datasheet of the processor |
| 59 | |
| 60 | Register Name : a string representing the register. e.g. 'PINB', 'ADCL', 'ADCH' etc |
| 61 | return: Contents of the register. an integer between 0 and 255 |
| 62 | |
| 63 | ``` |
| 64 | |
| 65 | |
| 66 | === "Example" |
| 67 | ```python hl_lines="1" |
| 68 | # Read value from Analog to Digital converter(ADC) channel 0 (PA0) |
| 69 | from kuttyPy import * |
| 70 | setReg('ADMUX', (1<<6) | 0) #REF_AVCC | Channel 0 |
| 71 | setReg('ADCSRA', 196) #Set ADC conversion speeds, and enable it. Refer to the ATMEGA32 datasheet for details. |
| 72 | cl = getReg('ADCL') #LSB 8 bits |
| 73 | ch = getReg('ADCH') # MSB 2 bits |
| 74 | print( (ch<<8)|cl ) # Combine the two to make a 10 bit number, and print to the screen |
| 75 | ``` |
| 76 | |
| 77 | You can also twiddle bits on various registers inside the "Registers" tab to watch |
| 78 | changes in real time. In the animation below, the ADC is being continuously read. |
| 79 | |
| 80 |  |
| 81 | |
| 82 | === "readADC" |
| 83 | ```python hl_lines="1" |
| 84 | def readADC(channel) |
| 85 | reads a voltage value from the specified channel, and returns it |
| 86 | |
| 87 | channel : 0 to 7 |
| 88 | return: 10 bit number( an integer between 0 and 1023 ) |
| 89 | |
| 90 | ``` |
| 91 | |
| 92 | === "data logger example with matplotlib" |
| 93 | ```python hl_lines="1" |
| 94 | # Read values from Analog to Digital converter(ADC) |
| 95 | # channel 5 (PA5), and plot them |
| 96 | import time |
| 97 | from kuttyPy import * |
| 98 | from matplotlib import pyplot as plt |
| 99 | |
| 100 | setReg('ADMUX', (1<<6) | 5) #REF_AVCC | Channel 5 |
| 101 | for a in range(50): |
| 102 | setReg('ADCSRA', 196) |
| 103 | cl = getReg('ADCL') |
| 104 | ch = getReg('ADCH') |
| 105 | plt.scatter(a, (ch<<8)|cl ,s=5) |
| 106 | plt.pause(0.01) #Wait 10 mS |
| 107 | ``` |
| 108 | |
| 109 | ## Embedded iPython Console |
| 110 | |
| 111 | In the scripting tab, there is an option to launch the ipython console. This |
| 112 | is a quick way to access the python functions from the GUI itself. |
| 113 | The kuttyPy instance in this window is called `kp` , so you can |
| 114 | use functions such as `kp.setReg` , `kp.MPU6050_init` etc. |
| 115 | TAB based autocompletion and inline help is also available. |
| 116 | |
| 117 | !!! tip "Screenshot" |
| 118 |  |
| 119 | |
| 120 | |
| 121 | ## Total list of defined registers for ATMEGA32 |
| 122 | |
| 123 | ```python |
| 124 | UBRRL = 0X29, |
| 125 | UCSRB = 0X2A, |
| 126 | UCSRA = 0X2B, |
| 127 | UDR = 0X2C, |
| 128 | SPCR = 0X2D, |
| 129 | SPSR = 0X2E, |
| 130 | SPDR = 0X2F, |
| 131 | PIND = 0x30, # Port D input |
| 132 | DDRD = 0x31, # Port D direction |
| 133 | PORTD = 0x32, # Port D output |
| 134 | PINC = 0x33, |
| 135 | DDRC = 0x34, |
| 136 | PORTC = 0x35, |
| 137 | PINB = 0x36, |
| 138 | DDRB = 0x37, |
| 139 | PORTB = 0x38, |
| 140 | PINA = 0x39, |
| 141 | DDRA = 0x3A, |
| 142 | PORTA = 0x3B, |
| 143 | EECR = 0X3C, |
| 144 | EEDR = 0X3D, |
| 145 | EEARL = 0X3E, |
| 146 | EEARH = 0X3F, |
| 147 | |
| 148 | TWBR = 0X20, |
| 149 | TWSR = 0X21, |
| 150 | TWAR = 0X22, |
| 151 | TWDR = 0X23, |
| 152 | |
| 153 | ADCL = 0X24, # ADC data |
| 154 | ADCH = 0X25, |
| 155 | ADCSRA = 0X26, # ADC status/control |
| 156 | ADMUX = 0X27, # ADC channel, reference |
| 157 | ACSR = 0X28, |
| 158 | |
| 159 | |
| 160 | |
| 161 | OCR2 = 0X43, # Timer/Counter 2 Output Compare Reg |
| 162 | TCNT2 = 0X44 , # Counter2 |
| 163 | TCCR2 = 0x45, # Timer/Counter 2 control reg |
| 164 | ICR1L = 0X46, |
| 165 | ICR1H = 0X47, |
| 166 | OCR1BL = 0X48, |
| 167 | OCR1BH = 0X49, |
| 168 | OCR1AL = 0X4A, |
| 169 | OCR1AH = 0X4B, |
| 170 | TCNT1L = 0X4C, |
| 171 | TCNT1H = 0X4D, |
| 172 | TCCR1B = 0X4E, |
| 173 | TCCR1A = 0x4F, |
| 174 | SFIOR = 0X50, |
| 175 | |
| 176 | TCNT0 = 0x52 , # Timer/ Counter 0 |
| 177 | TCCR0 = 0x53, |
| 178 | MCUCSR = 0X54, |
| 179 | MCUCR = 0X55, |
| 180 | TWCR = 0X56, |
| 181 | SPMCR = 0X57, |
| 182 | TIFR = 0X58, |
| 183 | TIMSK = 0X59, |
| 184 | GIFR = 0X5A, |
| 185 | GICR = 0X5B, |
| 186 | OCR0 = 0x5C, |
| 187 | SPL = 0X5D, |
| 188 | SPH = 0x5E, |
| 189 | SREG = 0X5F |
| 190 | |
| 191 | ``` |