Embedded Programming Week

For this week, I decided to learn how to program my ATTinty44, which I made in week 4, using C. In th epast, I have programmed with Arduino, which is very straightforward, but was curious to learn the C behind the actual Arduino IDE.


Before actually starting with C, I made sure to understand the terms associated with this week's topic. My TA Ben was very helpful in explaining many of terms that confused me (exmaple, difference between microcontroller and microprocessor) during office hours. Below is a compilation of a summary of my notes:

Microprocessor: Environment that contains the program memory. The program memory is stored as commands (the program code/recipe) in the register and fetched one at a time. Commands are executed after being fetched. Example of microprocessor is computer's CPU.


What makes a microprocessor a microcontroller? A microcontroller is composed of a microprocessor and many other chips. A chip is a packaged IC or Integrated Circuit. Unlike microprocessors, microcontrollers have data memory, which means that they can read and write, but that is only valid when the microcontroller is connected to power.

EEPROM and FLASH : Similar to storage. Used to allow data to be stored for microprocessors even when power is off. Some FLASH memory is used for data memory. EEPROM reads/writes 1 bite at a time.

Bit/ word size: Size of data that a microprocessor can operate at. If word size is 1 digit, and I try to do the operation 46 +33, I won't do the whole operation (of finding 79) in one step, because this is a two- digit operation. If I have have an 8-bit microcontroller, I can do operations for any number from 0-(2^8 -1) in one step. 16 bits would be single operation for any number from 0- (2^16 -1), and so forth.

Comparator: A peripherel that takes the results from an analog to digital conversion and compares it to some value. The function of a comparator can basically be done in code (with a microprocessor), but a comparator does this for us without the need of going through a microprocessor loop.

Families: Families are similar to microcontroller brands. However, different families are incompatible to one another. If, for example, you design a system for an AVR, you cannot recycle the code and use it as is for a another family.

Wy pick one family over the other? - familiarity, pricing, one family could have peripherals/capibalities that the other doesn't.

Packages Materials and Geometry. How an IC is packaged.

Additional Notes If I wanted to read python code for AVR, I would need an interpretor that converts python code to a language the microprocessor understands. In the case of C, C is a compiled language, so there is a method for C code to be turned into an assembley language without the need of an interpretor.


Coding attiny44 with C: A wonderful place to begin learning how programming with C for avr families cwould be here . I have also created a summary below of important steps and terms.

Steps:

  • Download and install AVR toolchain that contain C libraries you would need for AVR environment. If you are using Mac, download Crosspack, if you are using Windows, download WinAVR
  • Make sure you have an in-system programmer. In lab we used a USBTiny. Also, have your PCB ready.
  • Open a text editor and create a new script. Make sure that the header includes the avr library.
  • Define inputs and output pins you will be using within the "int main(void) function. [ we will discuss how to do this in a bit]. Note that "main" function in C is only called once when a program starts. So, if you want to always check for a condition, make sure you have you code all indside a "While (1)". While(1) is equivalent to While(true), and can be broken (to exit the loop) by "break;". Save your code in a file, and give it the extension ".c"
  • Copy Niel's makefile (for Hello World) and save it to same program folder were your c code is. Make sure that you change the program name is chnaged to to the name of your c-code (but without the .c extension).
  • Launch terminal and type the following command. make -f [name of makefile]. This will create necessary hex file.
  • Then type, make -f [name of makefile] program-[programmer name]. In my case, I used a usbtiny, so I just wrote that there. This line uploads the code to the microcontroller.

  • Coding in C For Avr (Terms and More)


    Below is a summary of the terms you would use in your c code, what they correspond to, and how their values mean different things according to nature of I/O pin.



    Summary of terms

    Instead of reading or writing 1,0 in binary numbers according to pin location,(for example, pin 2 on a some port would be 00000100. This is because a port has 8 pins, with pin 7 as the leftmost pin, and pin 0 as the rightmost pin), we can simply use the function "_BV()" included in the C library for avr. To illustrate its use, say I would like to assign output to pin 7 on a port B, then I would type the line DDRB= _BV(PB7).

    Below is the code I developed for today's week. It is a simple code that turns the LED on my PCB on when the button is pressed.

    # include <avr/io.h> int main(void){ //Button on PA7 DDRB= _BV(PB2); // sets pin 2 on port B (LED) to output PORTA= _BV(PA7); // enable pull up resistor on button while (1){ if (_BV(PA7) & PINA) { PORTB= _BV(PB2); //turn LED on } else { PORTB= 0; //turn LED off } } }
    Code Uploading

    .