Toggling a pin in C on the Arduino uno

It has been a while since I have posted, mainly because I have been trying to get to a stopping point, where I could just spend a few days writing. I switched over the arduino uno, mainly because of the issues I have outlined (I loose connecting to the computer as soon as the sparkfun micro gets up and running). I decided to start from scratch and work on the basics and then went up from there. So the first thing I started with was trying to blink an led on the arduino uno. The code is very similar to sparkfun micro. In order to upload my code onto arduino uno I used this link to get me started and then combined it with arduino sketch output. I ended up with the following script

avr-gcc -Os -DF_CPU=16000000UL -mmcu=atmega328p -c -o blink.o blink.c
avr-gcc -mmcu=atmega328p blink.o -o blink
avr-objcopy -O ihex -R .eeprom blink blink.hex

#flashing the Arduino:
#avrdude -V -F -C /etc/avrdude/avrdude.conf -p atmega328p -P /dev/ttyUSB0 -c stk500v1 -b 57600 -U flash:w:blink.hex
avrdude -C/home/ashwini/Downloads/arduino-1.8.3/hardware/tools/avr/etc/avrdude.conf -v -patmega328p -carduino -P/dev/ttyACM0 -b115200 -D -Uflash:w:blink.hex:i

I was able to compile and upload to the uno using the software above. Information from Balau’s website was instrumental in getting me started on the uno. The C code for blinking an led was:

#include <avr/io.h>
#include <util/delay.h>

int main (void)
{
 /* set PORTB for output*/
 DDRB = 0xFF;

while (1)
 {
 /* set PORTB.5 high */
 PORTB = 0x20;

_delay_ms(200);

/* set PORTB.5 low */
 PORTB = 0x00;

_delay_ms(200);
 }

return 1;
}

This allowed me to blink an led. I have stepped away from using my own header file because it became quite a pain in the neck to carry around those header files and plus I need to figure out how to link up the header files (which needs to happen as my projects get more complex). Looking at the arduino website, the led pin seems to be on what is defined as D13. After looking at the schematic , the Pin 13 is found on PB5. This can be found here:Screenshot from 2017-09-02 14:13:03

As you can see D13 or SCK is also found on PB5. Once you toggle that pin correctly you should be able to see led toggle.

 

Here is the list of the useful links I used:
https://github.com/dataplayer/Blinking-LED-on-Arduino

https://balau82.wordpress.com/2011/03/29/programming-arduino-uno-in-pure-c/

https://www.arduino.cc/en/Tutorial/Blink

My code can be found on github.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s