Author Topic: Arduino Code to AVR C Programming  (Read 4971 times)

0 Members and 1 Guest are viewing this topic.

Offline GurpreetTopic starter

  • Contributor
  • Posts: 42
  • Country: ca
Arduino Code to AVR C Programming
« on: December 20, 2015, 11:29:00 pm »
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 code
int LDR = 1;     //analog pin to which LDR is connected, here we set it to 0 so it means A0
int LDRValue = 0;      //that’s a variable to store LDR values
int speakerPin = 6;
int light_sensitivity = 75;    //This is the approx value of light surrounding your LDR
 
void setup()
  {
    Serial.begin(9600);          //start the serial monitor with 9600 baud
    pinMode(13, OUTPUT);     //we mostly use 13 because there is already a built in yellow LED in arduino which shows output when 13 pin is enabled
    pinMode(11, INPUT); // Reset Button.
    pinMode(9,OUTPUT);
  }
 
void loop()
  {
    digitalWrite(9,HIGH);
    LDRValue = analogRead(LDR);      //reads the ldr’s value through LDR
    Serial.println(LDRValue);       //prints the LDR values to serial monitor
    delay(10);        //This is the speed by which LDR sends value to arduino
 
    if (LDRValue > light_sensitivity)
      {
        digitalWrite(13, LOW);
      }
    else
      {
        digitalWrite(13, HIGH);
        tone(speakerPin, 500);
      }

     if (digitalRead(11))
      {
        noTone(speakerPin);
      }
  }
 

Offline Aodhan145

  • Frequent Contributor
  • **
  • Posts: 403
  • Country: 00
Re: Arduino Code to AVR C Programming
« Reply #1 on: December 20, 2015, 11:33:42 pm »
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/
 

Offline GurpreetTopic starter

  • Contributor
  • Posts: 42
  • Country: ca
Re: Arduino Code to AVR C Programming
« Reply #2 on: December 20, 2015, 11:41:41 pm »
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/

Yep 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.
 

Offline IconicPCB

  • Super Contributor
  • ***
  • Posts: 1534
  • Country: au
Re: Arduino Code to AVR C Programming
« Reply #3 on: December 21, 2015, 12:21:44 am »
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
 
 

Offline GurpreetTopic starter

  • Contributor
  • Posts: 42
  • Country: ca
Re: Arduino Code to AVR C Programming
« Reply #4 on: December 22, 2015, 04:54:20 am »
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 PB1

int 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.
 

Offline GurpreetTopic starter

  • Contributor
  • Posts: 42
  • Country: ca
Re: Arduino Code to AVR C Programming
« Reply #5 on: December 22, 2015, 05:04:28 am »
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 PB1

int 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.

By the way I am using Attiny13A MCU
 

Online Psi

  • Super Contributor
  • ***
  • Posts: 9944
  • Country: nz
Re: Arduino Code to AVR C Programming
« Reply #6 on: December 22, 2015, 05:20:45 am »
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 PB1

int 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.

Mistake in red

-Only use 1 equal sign for an assignment
-you need to use PORTB and PINB for what you are trying to

Note : 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 B

Another question is, Why are you calling it LED_1 if it's a switch and not an led?
« Last Edit: December 22, 2015, 05:31:54 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline Rick Law

  • Super Contributor
  • ***
  • Posts: 3441
  • Country: us
Re: Arduino Code to AVR C Programming
« Reply #7 on: December 22, 2015, 05:21:54 am »
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

First, 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.
 

Offline sleemanj

  • Super Contributor
  • ***
  • Posts: 3024
  • Country: nz
  • Professional tightwad.
    • The electronics hobby components I sell.
Re: Arduino Code to AVR C Programming
« Reply #8 on: December 22, 2015, 05:22:41 am »
You can put the arduino bootloader on the attiny13a


You can program the tiny13 from the Arduino IDE (and write programs in Arduinoese) with an ISP programmer, using a tiny core such as core13, 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.

Also, functions like Serial.print or anything else, 1K flash and 64 ram is not going to do it. 

FWIW, my ATTinyCore fork above can run the ASCIITable example sketch with only slight change on a Tiny13 :-)
« Last Edit: December 22, 2015, 05:24:50 am by sleemanj »
~~~
EEVBlog Members - get yourself 10% discount off all my electronic components for sale just use the Buy Direct links and use Coupon Code "eevblog" during checkout.  Shipping from New Zealand, international orders welcome :-)
 

Offline GurpreetTopic starter

  • Contributor
  • Posts: 42
  • Country: ca
Re: Arduino Code to AVR C Programming
« Reply #9 on: December 22, 2015, 05:30:30 am »
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

First, 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.

Yep, I've tried using '=' instead of '==' , but then I am getting build error. I am using atmel studio 7.
 

Online Psi

  • Super Contributor
  • ***
  • Posts: 9944
  • Country: nz
Re: Arduino Code to AVR C Programming
« Reply #10 on: December 22, 2015, 05:39:14 am »
Replace your line with this.
Now you need to figure out what its doing and how it works

If (PINB & 1)
{
   PORTB |= (1<<LED_2);
} else {
   PORTB &= ~(1<<LED_2);
}
« Last Edit: December 22, 2015, 05:42:24 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline GurpreetTopic starter

  • Contributor
  • Posts: 42
  • Country: ca
Re: Arduino Code to AVR C Programming
« Reply #11 on: December 22, 2015, 05:43:26 am »
Replace your line with this.
Now you need to figure out what its doing any how it works

If (PINB & 1)
{
   PORTB |= (1<<LED_2);
} else {
   PORTB &= ~(1<<LED_2);
}

Thank you so much.. It works...

 

Offline GurpreetTopic starter

  • Contributor
  • Posts: 42
  • Country: ca
Re: Arduino Code to AVR C Programming
« Reply #12 on: December 22, 2015, 05:51:57 am »


Another question is, Why are you calling it LED_1 if it's a switch and not an led?
Because 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!!
 

Online Mechatrommer

  • Super Contributor
  • ***
  • Posts: 11631
  • Country: my
  • reassessing directives...
Re: Arduino Code to AVR C Programming
« Reply #13 on: December 22, 2015, 06:13:38 am »
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...
Nature: Evolution and the Illusion of Randomness (Stephen L. Talbott): Its now indisputable that... organisms “expertise” contextualizes its genome, and its nonsense to say that these powers are under the control of the genome being contextualized - Barbara McClintock
 

Offline GurpreetTopic starter

  • Contributor
  • Posts: 42
  • Country: ca
Re: Arduino Code to AVR C Programming
« Reply #14 on: December 22, 2015, 06:26:18 am »
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...
yea I'm trying to learn C for avr.
 

Online Mechatrommer

  • Super Contributor
  • ***
  • Posts: 11631
  • Country: my
  • reassessing directives...
Re: Arduino Code to AVR C Programming
« Reply #15 on: December 22, 2015, 06:51:21 am »
yea I'm trying to learn C for avr.
i 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...
Nature: Evolution and the Illusion of Randomness (Stephen L. Talbott): Its now indisputable that... organisms “expertise” contextualizes its genome, and its nonsense to say that these powers are under the control of the genome being contextualized - Barbara McClintock
 

Offline Landrew2390

  • Regular Contributor
  • *
  • Posts: 68
  • Country: us
Re: Arduino Code to AVR C Programming
« Reply #16 on: December 23, 2015, 08:24:47 am »
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.
Oh look, a new hobby . . .
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf