Arduino Code to AVR C Programming - Page 1
Forum section: electronics / beginners

author: Gurpreet
author_rank: Contributor
timestamp: December 20, 2015, 11:29:00 pm
content: Hello People,I have an arduino code running on UNO and have some attiny 13A mcu's in hand. I want some information if i can open the arduino Sketch file(.ino) in atmel studio 7 and modify the pin configurations according to the Attiny to make it run. I am posting the Code below. I do have an Atmel ICE programmer to program attiny. I have very little knowledge about AVR C programming. If not sure of anything, please ask me as many questions as you can. Any help will be appreciated. Thanks.Below is how the Code works.I have an LDR which is placed on to an LCD and senses the brightness of an LCD. If the LCD shuts off, MCU is supposed to turn on a buzzer and latch on to it and should not turn off until I press the RESET button.Actual codeint LDR = 1; //analog pin to which LDR is connected, here we set it to 0 so it means A0int LDRValue = 0; //that’s a variable to store LDR valuesint speakerPin = 6;int light_sensitivity = 75; //This is the approx value of light surrounding your LDRvoid setup(){Serial.begin(9600); //start the serial monitor with 9600 baudpinMode(13, OUTPUT); //we mostly use 13 because there is already a built in yellow LED in arduino which shows output when 13 pin is enabledpinMode(11, INPUT); // Reset Button.pinMode(9,OUTPUT);}void loop(){digitalWrite(9,HIGH);LDRValue = analogRead(LDR); //reads the ldr’s value through LDRSerial.println(LDRValue); //prints the LDR values to serial monitordelay(10); //This is the speed by which LDR sends value to arduinoif (LDRValue > light_sensitivity){digitalWrite(13, LOW);}else{digitalWrite(13, HIGH);tone(speakerPin, 500);}if (digitalRead(11)){noTone(speakerPin);}}

author: Aodhan145
author_rank: Frequent Contributor
timestamp: December 20, 2015, 11:33:42 pm
content: You can put the arduino bootloader on the attiny13a but it is better if you learn how to program AVR's properly. I am learning how to do this now. My post may be helpful.https://www.eevblog.com/forum/beginners/trying-to-get-away-from-arduino/

author: Gurpreet
author_rank: Contributor
timestamp: December 20, 2015, 11:41:41 pm
content: Quote from: Aodhan145 on December 20, 2015, 11:33:42 pmYep that's true.. but this thing is kind of an emergency i would say.I've been following your post already, and have enrolled in the MOOC as suggested by one of the user.Uploading a bootloader onto an attiny13A might not work because of the memory constraints, that is what i think but completely unsure.

author: IconicPCB
author_rank: Super Contributor
timestamp: December 21, 2015, 12:21:44 am
content: You can down load Attiny13 core insta it in Youer Arduino IDE.Compile for ATtiny13 and download via ISP interface without a need for a boot loader on what is a small micro.You can use Your standard UNO and program it to work as an ISP programer

author: Gurpreet
author_rank: Contributor
timestamp: December 22, 2015, 04:54:20 am
content: Ok so, i have started to look up for some tutorials to actually start learning C for avr. I have written the following code#include <avr/io.h>#define F_CPU 1000000UL#define LED_1 PB0#define LED_2 PB1int main(void) {DDRB = 0b00000010;// PB0 is the input with a switch and PB1 Is defined as an Output(LED used as output on breadboard).while (1) {LED_2==LED_1; // assigning the value of pin 0 to pin1}return (0);}I am basically trying to copy the value at pin 0 and assign that value at pin1. Pin0 has a pull up resistor connected to it. When switch is open,DMM reads 5V and when the switch is pressed DMM reads 0V at pin0.But this code doesn't work. What is the mistake that I am doing in the program?Please advise guys.

author: Gurpreet
author_rank: Contributor
timestamp: December 22, 2015, 05:04:28 am
content: Quote from: Gurpreet on December 22, 2015, 04:54:20 amBy the way I am using Attiny13A MCU

author: Psi
author_rank: Super Contributor
timestamp: December 22, 2015, 05:20:45 am
content: Quote from: Gurpreet on December 22, 2015, 04:54:20 amMistake in red-Only use 1 equal sign for an assignment-you need to use PORTB and PINB for what you are trying toNote : Its important to understand that the mcu cannot read or write just one bit. It can only read or write all at once (a byte) So all 8 on the port you are using, in your case BAnother question is, Why are you calling it LED_1 if it's a switch and not an led?

author: Rick Law
author_rank: Super Contributor
timestamp: December 22, 2015, 05:21:54 am
content: Quote from: IconicPCB on December 21, 2015, 12:21:44 amFirst, your code:while (1) {LED_2==LED_1; // assigning the value of pin 0 to pin1}You probably mean = instead of ==.As to loading Arduino bootloader (and library). Keep in mind the ATTINY13 has only 1K of flash and 64 bytes of RAM. You will have a hard time putting the Arduino bootloader on it and do anything else useful.Also, functions like Serial.print or anything else, 1K flash and 64 ram is not going to do it. Even with the 8K version ATTINY85, I have having a hard time finding anything useful from the Arduino library given the resource limitation.

author: sleemanj
author_rank: Super Contributor
timestamp: December 22, 2015, 05:22:41 am
content: Quote from: Aodhan145 on December 20, 2015, 11:33:42 pmYou can program the tiny13 from the Arduino IDE (and write programs in Arduinoese) with an ISP programmer, using a tiny core such ascore13, or ATTinyCore (my work in progress fork), but you won't be putting a (proper) bootloader onto it, not if you want to have any room left to DO anything with it.Quote from: Rick Law on December 22, 2015, 05:21:54 amFWIW, my ATTinyCore fork above can run the ASCIITable example sketch with only slight change on a Tiny13 :-)

author: Gurpreet
author_rank: Contributor
timestamp: December 22, 2015, 05:30:30 am
content: Quote from: Rick Law on December 22, 2015, 05:21:54 amYep, I've tried using '=' instead of '==' , but then I am getting build error. I am using atmel studio 7.

author: Psi
author_rank: Super Contributor
timestamp: December 22, 2015, 05:39:14 am
content: Replace your line with this.Now you need to figure out what its doing and how it worksIf (PINB & 1){PORTB |= (1<<LED_2);} else {PORTB &= ~(1<<LED_2);}

author: Gurpreet
author_rank: Contributor
timestamp: December 22, 2015, 05:43:26 am
content: Quote from: Psi on December 22, 2015, 05:39:14 amThank you so much.. It works...

author: Gurpreet
author_rank: Contributor
timestamp: December 22, 2015, 05:51:57 am
content: Quote from: Psi on December 22, 2015, 05:20:45 amBecause i modified some other program to make this one which only involved LED's, so while modifying i forgot to change the name of PB1.Thanks for your help by the way!!

author: Mechatrommer
author_rank: Super Contributor
timestamp: December 22, 2015, 06:13:38 am
content: poor 1K flash tiny13a, whats left to program useful thing other than led blinking stuff if you smash in the bootloader somemore? why dont learn bare metal C if you have the programmer alrady in hand? its the easiest mcu on earth to program...

author: Gurpreet
author_rank: Contributor
timestamp: December 22, 2015, 06:26:18 am
content: Quote from: Mechatrommer on December 22, 2015, 06:13:38 amyea I'm trying to learn C for avr.

author: Mechatrommer
author_rank: Super Contributor
timestamp: December 22, 2015, 06:51:21 am
content: Quote from: Gurpreet on December 22, 2015, 06:26:18 ami usually find 1K is limiting, most of the program i developed for attiny13 or pic10f206 need to be trimmed or finely selected case by case to save spaces. google is your friend, key in programming c for attiny, or for avr, code example etc. i'm sure you can find many guide example, coupled with reading the attiny13 datasheet, you should be able to figure it out. good luck...

author: Landrew2390
author_rank: Regular Contributor
timestamp: December 23, 2015, 08:24:47 am
content: Mechatrommer is right on the money when he says to read the datasheet. All the Atmel datasheets I've seen spell out exactly what to do in order to make their products work. The only thing missing is a little bit of C/C++ coding on your part and if you can program using the "Arduino" language, you're already 90% of the way there.The only obstacles to the transition are learning the steps that happen automatically when you call a function like Serial.read(). I will say that making the transition was breathtaking for me. I learned about features of the AVR chip that I had no idea existed, but they were clearly laid out in the datasheet.