Embedded Programming

Embedded Programming


TASK
- Read a microcontroller data sheet
-Program your board to do something, with as many different programming languages
and programming environments as possible



To make sure that the board is working, I decided to use Arduino to program my board

I need to make the Arduino as ISPto program the Echo-hello board




First, I connect the Arduino board to the USB hub, press on File .. Example select Arduino as ISP



Upload the code




After the uploading is done, I connect the Arduino and my PCB with wires by following this picture.





What is the connection mean?





After the connection I go totool menu and chose the -Processor,Board and Clock- ATtiny24/44/84, ATtiny44,External 20Mhz.
then press theBurn Bootloader button





Then I chose the blink example to blink the LED in my board,and we have to define the right blink
in my board I definePin 3 .










To program our board



In order to program our board using low-level programming, we need 3 things which are:

- AVR libraries - which I can download like I did before when I programmed the ISP.
- A makefile and the make tool.
- A C code

After I make sure that my board is working, I go to Data sheet of Attiny44,

The datasheet is the biography and manual of any electronic component. They explain exactly what a component does and how to use it.
Before you start doing anything or even designing any circuit, looking at the datasheet is crucial.
It holds important information like Power supply requirements, Pins Configurations and descriptions, Electrical ratings and Schematic
of the IC circuit
.


How to read a datasheet?









What I learned by reading the datasheet : :


There are three hardware register memory locations: DDRxn, PORTxn and PINxn

-DDRx - Data Direction Register
-PORTx - Pin Output Register
-PINx - Pin Input Register

Lower case x is the number letter for the port and n represents the number.
DDRx pins set the direction of the pin. If logic 1 is written on DDRxn, Pxn is configured as an output pin.
Conversely,if DDRxn is written logic 0, Pxn is configured as input pin.

One particularity, is that if the data direction register is configured for input and PORTxn is written logic one
, an internal pull-up resistor in PORTxn is activated.

When DDRx bits are set to logic one (as output), the PORTx register will control if that pin is set to low or high.


So, after reading the data sheet I can program my file .I write my program using C programming languageto blink the LED light when the button is pressed.






After that I make the make file

But the question is: what is the make file?
The make fileis a special file, containing shell commands, that you create and name makefile.The makefile contains a list of rules.
These rules tell the system what commands you want to be executed. Most times, these rules are commands
to compile or recompile a series of files.

In the make file we define the microcontroller and crystal we will use, specify the .C file with it`s input and output
we use gcc to compile it to .hex
Avuirdude we use it to upload the file.

To create my makefile, I used Neil's example, then edited it to have the following:

 
     PROJECT=shefa // the name of the file
SOURCES=$(PROJECT).c
MMCU=attiny44      // define the microcontroller
F_CPU = 20000000 // define the crystal 

CFLAGS=-mmcu=$(MMCU) -Wall -Os -DF_CPU=$(F_CPU)

$(PROJECT).hex: $(PROJECT).out
	avr-objcopy -O ihex $(PROJECT).out $(PROJECT).c.hex;\
	avr-size --mcu=$(MMCU) --format=avr $(PROJECT).out  // the size of the file
 
$(PROJECT).out: $(SOURCES)
	avr-gcc $(CFLAGS) -I./ -o $(PROJECT).out $(SOURCES) // to compile the file 
 

program-fabISP: $(PROJECT).hex
	avrdude -p t44 -P usb -c usbtiny -U flash:w:$(PROJECT).c.hex // to upload the file
    
     program-fabISP-fuses: $(PROJECT).hex
	        avrdude -p t44 -P usb -c usbtiny -U lfuse:w:0x1F:m         // to upload the settings of the fuses 
     
     
     
     
     

How to set the fuses?



To understand how to set the fuses, I went through this tutorial.
The fuses are 3 bytes of permanent storage in the chip called 'fuse low byte', 'fuse high byte' and 'fuse extended byte'
. These bytes are called fuses and can be reprogrammed as many times as you want and determines the behaviour of the chip.





After I connected the FabISP to my HelloBoard, I have to install the necessary software for AVR Programming.
Because I use Ubuntu, I just have to follow the steps:

Open Terminal and type:

sudo apt-get install flex byacc bison gcc libusb-dev avrdude

Then type:
sudo apt-get install gcc-avr
- type "y" when asked to do so by your system

type:
sudo apt-get install avr-libc

Then :
sudo apt-get install libc6-dev

After installing all the libraries, I used the make command to create the .hex and .out file that are needed to program the board.

After that, we used make program-shefa to upload the program to the board. This is mentioned in the makefile.






Here is the log of my commands:
 
     ubuntu@ubuntu:~$ cd Desktop
ubuntu@ubuntu:~/Desktop$ ls
examples.desktop  nadine                                   shifa
files             Screenshot from 2018-03-20 15-44-12.png  ubiquity.desktop
ubuntu@ubuntu:~/Desktop$ cd shifa
ubuntu@ubuntu:~/Desktop/shifa$ ls
Makefile  SH.M.J.ISP.c.ino
ubuntu@ubuntu:~/Desktop/shifa$ ls
Makefile  SH.M.J.ISP.c
ubuntu@ubuntu:~/Desktop/shifa$ lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 006: ID 138a:0011 Validity Sensors, Inc. VFS5011 Fingerprint Reader
Bus 001 Device 005: ID 04f2:b5c0 Chicony Electronics Co., Ltd 
Bus 001 Device 004: ID 0cf3:e500 Atheros Communications, Inc. 
Bus 001 Device 045: ID 045e:0797 Microsoft Corp. Optical Mouse 200
Bus 001 Device 044: ID 1781:0c9f Multiple Vendors USBtiny
Bus 001 Device 002: ID 058f:6387 Alcor Micro Corp. Flash Drive
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
ubuntu@ubuntu:~/Desktop/shifa$ lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 006: ID 138a:0011 Validity Sensors, Inc. VFS5011 Fingerprint Reader
Bus 001 Device 005: ID 04f2:b5c0 Chicony Electronics Co., Ltd 
Bus 001 Device 004: ID 0cf3:e500 Atheros Communications, Inc. 
Bus 001 Device 045: ID 045e:0797 Microsoft Corp. Optical Mouse 200
Bus 001 Device 048: ID 1781:0c9f Multiple Vendors USBtiny
Bus 001 Device 002: ID 058f:6387 Alcor Micro Corp. Flash Drive
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
ubuntu@ubuntu:~/Desktop/shifa$ make
make: *** No rule to make target 'Sh.M.JISP.c', needed by 'Sh.M.JISP.out'.  Stop.
ubuntu@ubuntu:~/Desktop/shifa$ cd ..
ubuntu@ubuntu:~/Desktop$ cd files
ubuntu@ubuntu:~/Desktop/files$ make
make: *** No rule to make target 'Sh.M.JISP.c', needed by 'Sh.M.JISP.out'.  Stop.
ubuntu@ubuntu:~/Desktop/files$ ls
Makefile  Q_BOARD.c  qusai
ubuntu@ubuntu:~/Desktop/files$ make
make: *** No rule to make target 'Sh.M.JISP.c', needed by 'Sh.M.JISP.out'.  Stop.
ubuntu@ubuntu:~/Desktop/files$ make
avr-gcc -mmcu=attiny44 -Wall -Os -DF_CPU=20000000 -I./ -o Q_BOARD.out Q_BOARD.c
avr-objcopy -O ihex Q_BOARD.out Q_BOARD.c.hex;\
avr-size --mcu=attiny44 --format=avr Q_BOARD.out
AVR Memory Usage
----------------
Device: attiny44

Program:     104 bytes (2.5% Full)
(.text + .data + .bootloader)

Data:          0 bytes (0.0% Full)
(.data + .bss + .noinit)


ubuntu@ubuntu:~/Desktop/files$ make program-fabISP
avr-objcopy -O ihex Q_BOARD.out Q_BOARD.c.hex;\
avr-size --mcu=attiny44 --format=avr Q_BOARD.out
AVR Memory Usage
----------------
Device: attiny44

Program:     104 bytes (2.5% Full)
(.text + .data + .bootloader)

Data:          0 bytes (0.0% Full)
(.data + .bss + .noinit)


avrdude -p t44 -P usb -c usbtiny -U flash:w:Q_BOARD.c.hex

avrdude: initialization failed, rc=-1
         Double check connections and try again, or use -F to override
         this check.


avrdude done.  Thank you.

Makefile:17: recipe for target 'program-fabISP' failed
make: *** [program-fabISP] Error 1
ubuntu@ubuntu:~/Desktop/files$  cd..
cd..: command not found
ubuntu@ubuntu:~/Desktop/files$ cd ..
ubuntu@ubuntu:~/Desktop$ cd shifa
ubuntu@ubuntu:~/Desktop/shifa$ make
make: *** No rule to make target 'shefa.c', needed by 'shefa.out'.  Stop.
ubuntu@ubuntu:~/Desktop/shifa$ make
avr-gcc -mmcu=attiny44 -Wall -Os -DF_CPU=20000000 -I./ -o shefa.out shefa.c
shefa.c: In function ‘main’:
shefa.c:14:7: warning: implicit declaration of function ‘switchGreenLED’ [-Wimplicit-function-declaration]
       switchGreenLED();
       ^
shefa.c: At top level:
shefa.c:22:7: warning: conflicting types for ‘switchGreenLED’
  void switchGreenLED() {
       ^
shefa.c:14:7: note: previous implicit declaration of ‘switchGreenLED’ was here
       switchGreenLED();
       ^
avr-objcopy -O ihex shefa.out shefa.c.hex;\
avr-size --mcu=attiny44 --format=avr shefa.out
AVR Memory Usage
----------------
Device: attiny44

Program:      96 bytes (2.3% Full)
(.text + .data + .bootloader)

Data:          0 bytes (0.0% Full)
(.data + .bss + .noinit)


ubuntu@ubuntu:~/Desktop/shifa$ make program-fabISP
avr-objcopy -O ihex shefa.out shefa.c.hex;\
avr-size --mcu=attiny44 --format=avr shefa.out
AVR Memory Usage
----------------
Device: attiny44

Program:      96 bytes (2.3% Full)
(.text + .data + .bootloader)

Data:          0 bytes (0.0% Full)
(.data + .bss + .noinit)


avrdude -p t44 -P usb -c usbtiny -U flash:w:shefa.c.hex

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e9207 (probably t44)
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "shefa.c.hex"
avrdude: input file shefa.c.hex auto detected as Intel Hex
avrdude: writing flash (96 bytes):

Writing | ################################################## | 100% 0.13s

avrdude: 96 bytes of flash written
avrdude: verifying flash memory against shefa.c.hex:
avrdude: load data flash data from input file shefa.c.hex:
avrdude: input file shefa.c.hex auto detected as Intel Hex
avrdude: input file shefa.c.hex contains 96 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.15s

avrdude: verifying ...
avrdude: 96 bytes of flash verified

avrdude: safemode: Fuses OK (E:FF, H:DF, L:FE)

avrdude done.  Thank you.

ubuntu@ubuntu:~/Desktop/shifa$ 






Problem :

The problem I had a problem with uploading the code and I didn't know why. I spent a couple of hours before I decided to power my board from another source that is not the FabISP and it worked. So appparently the problem was the FabISP didn't have sufficeint power to supply the board.




Download files
C Code
Make file



I tried in the next code to use the button to change the LED blink speed:

	 
	 #include < avr/io.h >
#include < util/delay.h >           // for _delay_ms()

#define led_pin PA2               // pin for LED
#define button_pin PA3           // pin for button

#define blink_fast 100             // delay for fast blink
#define blink_slow 2000             // delay for slow blink

int main(void)
{

  DDRA |= (1 << led_pin);         // set LED pin as output
  PORTB |= (1 << button_pin);     // set pullup resistor for button pin

  while(1)
  {
    if(PINA & (1 << button_pin))  // if button is not pressed
    {
      PORTA |= (1 << led_pin);    // turn LED on
      _delay_ms(blink_slow);     // wait 500 milliseconds

      //LED off
      PORTA &= ~(1 << led_pin);   // turn LED off
      _delay_ms(blink_slow);     // wait 500 milliseconds
    } else {
      PORTA |= (1 << led_pin);    // turn LED on
      _delay_ms(blink_fast);     // wait 500 milliseconds

      //LED off
      PORTA &= ~(1 << led_pin);   // turn LED off
      _delay_ms(blink_fast);     // wait 500 milliseconds
    }
  }
}
 	 
	 




And here's a screenshot of my work.





My terminal log :

	 ubuntu@ubuntu:~$ cd Desktop 
ubuntu@ubuntu:~/Desktop$ cd shefa
ubuntu@ubuntu:~/Desktop/shefa$ ls
makefile  shefa.c
ubuntu@ubuntu:~/Desktop/shefa$ make
avr-gcc -mmcu=attiny44 -Wall -Os -DF_CPU=20000000 -I./ -o shefa.out shefa.c
avr-objcopy -O ihex shefa.out shefa.c.hex;\
avr-size --mcu=attiny44 --format=avr shefa.out
AVR Memory Usage
----------------
Device: attiny44

Program:     144 bytes (3.5% Full)
(.text + .data + .bootloader)

Data:          0 bytes (0.0% Full)
(.data + .bss + .noinit)


ubuntu@ubuntu:~/Desktop/shefa$ make program-shefa
avr-objcopy -O ihex shefa.out shefa.c.hex;\
avr-size --mcu=attiny44 --format=avr shefa.out
AVR Memory Usage
----------------
Device: attiny44

Program:     144 bytes (3.5% Full)
(.text + .data + .bootloader)

Data:          0 bytes (0.0% Full)
(.data + .bss + .noinit)


avrdude -p t44 -P usb -c usbtiny -U flash:w:shefa.c.hex

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e9207 (probably t44)
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "shefa.c.hex"
avrdude: input file shefa.c.hex auto detected as Intel Hex
avrdude: writing flash (144 bytes):

Writing | ################################################## | 100% 0.20s

avrdude: 144 bytes of flash written
avrdude: verifying flash memory against shefa.c.hex:
avrdude: load data flash data from input file shefa.c.hex:
avrdude: input file shefa.c.hex auto detected as Intel Hex
avrdude: input file shefa.c.hex contains 144 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.22s

avrdude: verifying ...
avrdude: 144 bytes of flash verified

avrdude: safemode: Fuses OK (E:FF, H:DF, L:FE)

avrdude done.  Thank you.

ubuntu@ubuntu:~/Desktop/shefa$ 


	 
	 
	 
	 



I also try this code using Arduino language

 
	 int ledPin = 13; //define pin 13 as LED 

void setup() {
pinMode(ledPin, OUTPUT); //Pin mode is output

Serial.begin(9600); //Start Seial 
}

void loop() {

int photocell = analogRead(A0); // Read from analoge A0


Serial.println(photocell);
if (photocell> 300)   // compare statment  (if statement)    
  digitalWrite (ledPin,1); // LED will be on


if (photocell < 300) // compare statment  (if statement)  
  digitalWrite(ledPin,0); // LED will be off


delay(200);
 
}

Then using Ubuntu to program it by following the previous steps.




Download files
LED blink speed
hex file
Make file
Arduino language