C code for micro servo sg90

Go To Last Post
18 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi, I am new to AVRfreaks.com, but thought I could receive some help here. I have an assignment to change 2 NeoPixels from red to orange to yellow to green to blue to indigo and to violet using a POT. I have successfully found code to use the analog signal to change the colors of the neopixels through digital input based off of the position of the POT. I can put that code in here if that helps too. But the other part of the assignment have code that allows the servo motor track the position of the potentiometer and indicate the color on the NeoPixels. What I mean by the servo matching the colors of the LED, I refer to this attached picture. I am working with Atmel Studio 7 and with an atmega328p micro controller. Let me know if I can make anything more clear. 

Here is the code for the potentiometer and the NeoPixels, (which compiles and works as expected)

 

void startConversion()
{
    ADCSRA |= (1 << ADSC);
}

void setupADC()
{
    ADMUX = (1 << REFS0) | (1 << MUX0) | (1 << MUX2);
    ADCSRA = (1 << ADEN) | (1 << ADIE) | (1 << ADPS0) | (1 << ADPS1) | (1 << ADPS2);
    DIDR0 = (1 << ADC5D);

    startConversion();
}

ISR(ADC_vect)
{
    analogIn = ADC << 2;
    startConversion();
}

int main()
{
    uint8_t rainbowColor = 0;
    uint8_t inputValue = 0;
    setupADC();
    
    while (1)
    {
        inputValue = analogIn;
        if((0 < inputValue) & (inputValue < 36)){
            rainbowColor = 0;
        }
        else if ((37 < inputValue) & (inputValue < 73)){
            rainbowColor = 1;
        }
        else if ((74 < inputValue) & (inputValue < 110)){
            rainbowColor = 2;
        }
        else if ((111 < inputValue) & (inputValue < 147)){
            rainbowColor = 3;
        }
        else if ((148 < inputValue) & (inputValue < 184)){
            rainbowColor = 4;
        }
        else if ((185 < inputValue) & (inputValue < 221)){
            rainbowColor = 5;
        }
        else if ((222 < inputValue) & (inputValue < 255)){
            rainbowColor = 6;
        }
        else{
            rainbowColor = rainbowColor;
        }
        
        
        for(uint8_t i = 0; i < LED_COUNT; i++) //i is for each LED in the array
            colors[i] = rainbow[rainbowColor];
        led_strip_write(colors, LED_COUNT);
        _delay_ms(100);

    }
}

 

Thoughts of the code for the servo motor? Let me know if you need anything else.

Thanks so much.

Attachment(s): 

Ethan

Last Edited: Mon. Apr 6, 2020 - 09:50 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ethandude27 wrote:
Thoughts of the code for the servo motor?

It's your assignment - so what have you thought?

 

What have you tried?

 

Where are you stuck?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Why do all the schools insist on using Studio instead of Arduino?   Arduino makes the hardware easy so that the programmer can concentrate on the application, not the CPU's inner details.   What they don't tell you in school is that there are dozens of CPU families in use and when you switch to a different CPU, you lose all the relevance that studying all of the technical details of the old CPU gives you.

 

   For example, your servo uses PWM for its arm positioning.  With Arduino, you use the analogWrite(uint8_t myPWMdataPin, uint8_t myValue) function to make the PWM signal for the servo.  You don't have to care that the details for making PWM are completely different between an AVR and an ARM CPU.  You just use analogWrite(5,194); to get a 75% duty cycle on Arduino data pin 5.  

 

In Arduino your whole program would have three main lines:

void loop() {

        myADCvalue = analogRead( pin);   // pot setting 0-1023

        setNeoPixelColor (NeoColorRGBtable[ myADCvalue / (1023/number_of_colors)] );  // three bytes [red, grn, blue] for each color  R,O,Y,G,B,ect...

        analogWrite(servoPin, myADCvalue/4);    // PWM 0-255

}

That's it.  Pick up your check and go home for the day.  But everyone insists on doing it the hard way.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

 

It's your assignment - so what have you thought?

 

What have you tried?

 

Where are you stuck?

 

You are correct, this is my assignment, I am not looking for just the answer, but more of an explanation and like what to look for/understand. To be honest, this is where I am stuck. I do not understand necessarily where to start and what to look for. For this assignment, the professor decided not to give us all the code for the project in hope that we would research and look else where to find the answers. So, I decided to come here to learn more about what I could find and use. We are learning about C code and how we can change it and see the changes in hardware. XD

Ethan

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Simonetta wrote:

Why do all the schools insist on using Studio instead of Arduino?   Arduino makes the hardware easy so that the programmer can concentrate on the application, not the CPU's inner details.   What they don't tell you in school is that there are dozens of CPU families in use and when you switch to a different CPU, you lose all the relevance that studying all of the technical details of the old CPU gives you.

 

Yes, I understand what you are saying. For the class I am in, I believe my professor is focused on teaching us the inner details. However, I am not sure how in-depth he plans on going, but more helping the class have a base knowledge. So does that determine why we are using Atmel Studio instead of Arduino? Not to sure, I would have to ask him. But thanks for the help so far. Quick question for you, how did you figure out that 3 main lines of code for Arduino with all the correct values and order of the code? And where would I implement it in my code I already have if it were to work in Atmel Studio?

Ethan

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You are correct, this is my assignment, I am not looking for just the answer, but more of an explanation and like what to look for/understand. To be honest, this is where I am stuck. I do not understand necessarily where to start and what to look for. For this assignment, the professor decided not to give us all the code for the project in hope that we would research and look else where to find the answers. So, I decided to come here to learn more about what I could find and use. We are learning about C code and how we can change it and see the changes in hardware. XD

Ethan

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Future people began to use calculators from an early age.
Eventually no one could calculate with the pen.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

kabasan wrote:
in hope that we would research and look else where to find the answers.

 

Take a look at "Hobby Servo Tutorial" from Sparkfun.com (Link).

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ethandude27 wrote:
explanation and like what to look for/understand

Well you've mentioned use of a Servo - so start by googling that!

 

If you include "AVR" in your search, you will get AVR-related results

 

A good tip is to include "Arduino" in your search - even if you won't be using Arduino in your final project, there will (almost) certainly find relevant projects & tutorials. And most Arduinos are AVR-based.

 

 

EDIT: Other Internet search engines are available.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Tue. Apr 7, 2020 - 08:25 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Simonetta wrote:
Arduino makes the hardware easy so that the programmer can concentrate on the application, not the CPU's inner details

There is a good point there.

 

However, with Atmel Studio (or 'C' or C++ in any other similar IDE) you really aren't dealing with the CPU's inner details at all.

All of that is taken care of by the compiler.

 

But it does require that you have a good understanding of how to handle the peripherals in the microcontroller - GPIO, Timers, PWM, UART, etc

Which teaches you how to read a datasheet, deal with registers, apply what you learn, etc, etc.

 

And that is very much a transferable skill!

 

Just takes a look at the recent interminable threads from  ansh11 / muke12 / sky33 to see what happens when you lack those skills!

 

EDIT

 

typos

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ethandude27 wrote:
Thoughts of the code for the servo motor? Let me know if you need anything else.
Ummm.. the rest of the code perhaps ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok, so some basics of servo's, they get there position info from a pulse width that varies from 1ms min to 2ms max corresponding to a 0-180 degree position, the pulse repetition rate is generally once ever 20ms. 

So you will need to learn how to setup a timer to produce the 1-2ms pulse every 20ms.  (hint, T1 works well for this, but other timers may work too, read up on fast pwm mode)

You have only a few positions needed, so precision is not needed in this application, you just need 6-8 positions (however many colors) for this project, so should be easy to do.

 

I know your teacher probably does not want you to use arduino, but you should be able to find examples and can use it to prototype what you need.  Then write your own c code to do the same thing.

 

Sounds like a fun project, good luck.

 

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

 Ummm.. the rest of the code perhaps ?

Here is the whole code I am using. Most of it was given to me from my professor. Also, I did find code for the servo motor that i used in another project. However, it was for the Attiny85. I was told that the Atmega328p and the Attiny85 are pretty similar. Good news, i am able to get my code to compile, however, at first, i received an error that TIMSK is undeclared. I went in and changed it to TIMSK0 since I found out that there are TIMSK0, 1, and 2. Then i was able to successfully compile. My issue at the moment is to understand where to hook up my load to a certain pin and also to make sure my code is correct. I have come to understand that sometimes I can get something to compile or build, but that does not guarantee that it will work correctly in the hardware itself.

 

my code

// This is AVR code for driving the RGB LED strips from Pololu.

// This version can control any AVR pin.  It uses "lds" and "sts" instructions,
// which means it has to write to the entire port register every time it toggles
// the I/O line, but that should be safe because interrupts are disabled.
//
// It allows complete control over the color of an arbitrary number of LEDs.
// This implementation disables interrupts while it does bit-banging with
// inline assembly.

// This line specifies the frequency your AVR is running at.
// This code supports 20 MHz, 16 MHz and 8MHz
#define F_CPU 16000000

// These lines specify what pin the LED strip is on.
// You will either need to attach the LED strip's data line to PH3
// (pin 6 on the Arduino Mega 2560) or change these
// lines to specify a different pin.
#define LED_STRIP_PORT PORTD
#define LED_STRIP_DDR  DDRD
#define LED_STRIP_PIN  6
#define LED_COUNT 2
#define max_speed 237
#define min_speed 130
#define enable_speed 125
#define cycles_before_update 12
#define enable_cycles 200
#define timer_overflows 10 //this gives desired servo control signal period of about 20 ms
#define smooth_speed 2



#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdint.h>

// The rgb_color struct represents the color for an 8-bit RGB LED.
// Examples:
//   Black:      (rgb_color){ 0, 0, 0 }
//   Pure red:   (rgb_color){ 255, 0, 0 }
//   Pure green: (rgb_color){ 0, 255, 0 }
//   Pure blue:  (rgb_color){ 0, 0, 255 }
//   White:      (rgb_color){ 255, 255, 255}
// ---DWF Changed the order here, to fit our neopixel order.
typedef struct rgb_color
{
    uint8_t green, red, blue;
} rgb_color;

//global variable
volatile uint8_t tot_overflow;
volatile int flag = 1, flag2 = 1;
int i = 11;
int analogInservo = 0;
int speedOut = min_speed;
int speedSP = min_speed;

rgb_color colors[LED_COUNT];
uint8_t analogIn = 0;
rgb_color rainbow[7] = {
    [0] = { 0x00, 0x1A, 0x00}, // red [0]
    [1] = { 0x0D, 0x1A, 0x00}, // orange [1]
    [2] = { 0x1A, 0x1A, 0x00}, //yellow [2]
    [3] = { 0x1A, 0x00, 0x00}, //green [3]
    [4] = { 0x00, 0x00, 0x1A}, //blue [4]
    [5] = { 0x0A, 0x0A, 0x1A}, //indigo [5]
    [6] = { 0x05, 0x0F, 0x1A}, //violet [6]    
        
};

// led_strip_write sends a series of colors to the LED strip, updating the LEDs.
// The colors parameter should point to an array of rgb_color structs that hold
// the colors to send.
// The count parameter is the number of colors to send.
// This function takes about 1.1 ms to update 30 LEDs.
// Interrupts must be disabled during that time, so any interrupt-based library
// can be negatively affected by this function.
// Timing details at 20 MHz:
//   0 pulse  = 400 ns
//   1 pulse  = 850 ns
//   "period" = 1300 ns
// Timing details at 16 MHz:
//   0 pulse  = 375 ns
//   1 pulse  = 812.5 ns
//   "period" = 1500 ns
void __attribute__((noinline)) led_strip_write(rgb_color * colors, uint16_t count)
{
    // Set the pin to be an output driving low.
    LED_STRIP_PORT &= ~(1 << LED_STRIP_PIN);
    LED_STRIP_DDR |= (1 << LED_STRIP_PIN);

    cli();   // Disable interrupts temporarily because we don't want our pulse timing to be messed up.
    while (count--)
    {
        uint8_t portValue = LED_STRIP_PORT;
        // Send a color to the LED strip.
        // The assembly below also increments the 'colors' pointer,
        // it will be pointing to the next color at the end of this loop.
        asm volatile (
        "ld __tmp_reg__, %a0+\n"
        "ld __tmp_reg__, %a0\n"
        "rcall send_led_strip_byte%=\n"  // Send red component.
        "ld __tmp_reg__, -%a0\n"
        "rcall send_led_strip_byte%=\n"  // Send green component.
        "ld __tmp_reg__, %a0+\n"
        "ld __tmp_reg__, %a0+\n"
        "ld __tmp_reg__, %a0+\n"
        "rcall send_led_strip_byte%=\n"  // Send blue component.
        "rjmp led_strip_asm_end%=\n"     // Jump past the assembly subroutines.

        // send_led_strip_byte subroutine:  Sends a byte to the LED strip.
        "send_led_strip_byte%=:\n"
        "rcall send_led_strip_bit%=\n"  // Send most-significant bit (bit 7).
        "rcall send_led_strip_bit%=\n"
        "rcall send_led_strip_bit%=\n"
        "rcall send_led_strip_bit%=\n"
        "rcall send_led_strip_bit%=\n"
        "rcall send_led_strip_bit%=\n"
        "rcall send_led_strip_bit%=\n"
        "rcall send_led_strip_bit%=\n"  // Send least-significant bit (bit 0).
        "ret\n"

        // send_led_strip_bit subroutine:  Sends single bit to the LED strip by driving the data line
        // high for some time.  The amount of time the line is high depends on whether the bit is 0 or 1,
        // but this function always takes the same time (2 us).
        "send_led_strip_bit%=:\n"
        #if F_CPU == 8000000
        "rol __tmp_reg__\n"                      // Rotate left through carry.
        #endif
        "sts %2, %4\n"                           // Drive the line high.

        #if F_CPU != 8000000
        "rol __tmp_reg__\n"                      // Rotate left through carry.
        #endif

        #if F_CPU == 16000000
        "nop\n" "nop\n"
        #elif F_CPU == 20000000
        "nop\n" "nop\n" "nop\n" "nop\n"
        #elif F_CPU != 8000000
        #error "Unsupported F_CPU"
        #endif

        // If the bit to send is 0, drive the line low now.
        "brcs .+4\n" "sts %2, %3\n"

        #if F_CPU == 8000000
        "nop\n" "nop\n"
        #elif F_CPU == 16000000
        "nop\n" "nop\n" "nop\n" "nop\n" "nop\n"
        #elif F_CPU == 20000000
        "nop\n" "nop\n" "nop\n" "nop\n" "nop\n"
        "nop\n" "nop\n"
        #endif

        // If the bit to send is 1, drive the line low now.
        "brcc .+4\n" "sts %2, %3\n"

        "ret\n"
        "led_strip_asm_end%=: "
        : "=b" (colors)
        : "0" (colors),           // %a0 points to the next color to display
        "" (&LED_STRIP_PORT),   // %2 is the port register (e.g. PORTH)
        "r" ((uint8_t)(portValue & ~(1 << LED_STRIP_PIN))),  // %3
        "r" ((uint8_t)(portValue | (1 << LED_STRIP_PIN)))    // %4
        );

        // Uncomment the line below to temporarily enable interrupts between each color.
        //sei(); asm volatile("nop\n"); cli();
    }
    sei();          // Re-enable interrupts now that we are done.
    _delay_us(80);  // Send the reset signal.
}
// new ADC Code

void startConversion()
{
    ADCSRA |= (1 << ADSC);
}

void setupADC()
{
    ADMUX = (1 << REFS0) | (1 << MUX0) | (1 << MUX2);
    ADCSRA = (1 << ADEN) | (1 << ADIE) | (1 << ADPS0) | (1 << ADPS1) | (1 << ADPS2);
    DIDR0 = (1 << ADC5D);

    startConversion();
}

ISR(ADC_vect)
{
    analogIn = ADC << 2;
    startConversion();
}

//servo timer
void timer0_init()
{
    // set up timer with prescaler = 64 (for attiny85)
    TCCR0B |= (1 << CS01)|(1 << CS00);
    
    // initialize counter
    TCNT0 = 0;

    // enable overflow interrupt
    TIMSK0 |= (1 << TOIE0);
    
    // enable global interrupts
    sei();
    
    // initialize overflow counter variable
    tot_overflow = 0;
}

ISR(TIMER0_OVF_vect)
{
    // keep a track of number of overflows
    tot_overflow++;
}

void adc_init()
{
    // ADC Enable and prescaler of 128
    ADCSRA = (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
}

uint16_t adc_read(uint8_t ch)
{
    // select the corresponding channel 0~7
    // ANDing with ’7? will always keep the value
    // of ‘ch’ between 0 and 7
    ch &= 0b00000111;  // AND operation with 7
    ADMUX = (ADMUX & 0xF8)|ch; // clears the bottom 3 bits before ORing
    
    // start single convertion
    // write ’1? to ADSC
    ADCSRA |= (1<<ADSC);
    
    // wait for conversion to complete
    // ADSC becomes ’0? again
    // till then, run loop continuously
    while(ADCSRA & (1<<ADSC));
    
    return (ADC);
}

void updateSP(){
    //get sensor imput, convert to delay...this should be configured for min and max period desired
    analogInservo = adc_read(2);
    speedSP = (int)(((float)(analogInservo-500))/5.0+134.0); //again, this looks weird because of the setup im using
    if(speedSP > max_speed) speedSP = max_speed;
    else if(speedSP < min_speed) speedSP = min_speed;
}

void enableMotor(){
    unsigned int p = 0;
    while(p<enable_cycles){
        
        if (tot_overflow >= timer_overflows)  // NOTE: '>=' is used (skipping may occur)
        {
            if(flag){
                PORTB ^= (1 << 0);    // toggles the pin (on)
                flag = 0;
            }
            // check if the timer count reaches 124
            if (TCNT0 >= enable_speed)
            {
                PORTB ^= (1 << 0);    // toggles the pin (off)
                TCNT0 = 0;            // reset counter
                tot_overflow = 0;
                flag = 1;
                p++;
            }
        }
    }
}

void setupStuff(){
    // connect led to pin PB0
    DDRB |= (1 << 0);
    //analog read init on PB2
    DDRB |= (0 << 4);
    // initialize timer
    timer0_init();
    //adc setup
    adc_init();
}

int main()
{
    uint8_t rainbowColor = 0;
    uint8_t inputValue = 0;
    setupADC();
    
    while (1)
    {
        inputValue = analogIn;
        if((0 < inputValue) & (inputValue < 36)){
            rainbowColor = 0;
        }
        else if ((37 < inputValue) & (inputValue < 73)){
            rainbowColor = 1;
        }
        else if ((74 < inputValue) & (inputValue < 110)){
            rainbowColor = 2;
        }
        else if ((111 < inputValue) & (inputValue < 147)){
            rainbowColor = 3;
        }
        else if ((148 < inputValue) & (inputValue < 184)){
            rainbowColor = 4;
        }
        else if ((185 < inputValue) & (inputValue < 221)){
            rainbowColor = 5;
        }
        else if ((222 < inputValue) & (inputValue < 255)){
            rainbowColor = 6;
        }
        else{
            rainbowColor = rainbowColor;
        }
        
        
        for(uint8_t i = 0; i < LED_COUNT; i++) //i is for each LED in the array
            colors[i] = rainbow[rainbowColor];
        led_strip_write(colors, LED_COUNT);
        _delay_ms(100);

    }
    
    setupStuff();
    enableMotor();
    // loop forever
    while(1)
    {
        if (tot_overflow >= timer_overflows)  // NOTE: '>=' is used
        {
            if(flag){
                PORTB ^= (1 << 0);    // toggles the pin (on)
                flag = 0;
            }
            // check if the timer count reaches desired speed
            if (TCNT0 >= speedOut)
            {
                PORTB ^= (1 << 0);    // toggles the pin (off)
                TCNT0 = 0;            // reset counter
                tot_overflow = 0;
                flag = 1;

                //this is for smoothing as the SP changes... smoothing can be removed if u really want...
                if(abs(speedOut-speedSP)<=smooth_speed) ; //have reached value (close enough anyway
                else if(speedOut > speedSP) speedOut-=smooth_speed;
                else speedOut+=smooth_speed;
                i++;

                //update once every so often
                if(i >= cycles_before_update){
                    i=0;
                    updateSP();
                }
            }
        }
    }
}

 

Ethan

Last Edited: Wed. Apr 8, 2020 - 04:38 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You've pulled some random code in there and mixed it together. Not a good strategy for success. For starters, you've got the adc setup for interrupts, then you have a function for polling. 

 

Do yourself a favour - solve one problem at a time. IE - write some code that does just the servo control. Get that working, then combine with the other code.

 

As well, when you post code here, use the <> button on the toolbar.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
when you post code here, use the <> button on the toolbar.

For illustrated instructions on how to properly post source code, see Tip #1 in my signature (below).

 

Note that this is not just a weird thing specific to this forum - it will be similar on any forum:

 

https://www.avrfreaks.net/commen...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If I were you I'd try to get out of the habit of "oh it's a 'general variable' so int makes sense then".

 

While never wise, that might work for PC code - it doesn't for micros.

 

(having said that I do see some (sensible) uint8_t and uint16_t use in there too - so what's with the "int" thing? Just a case of "this variable type doesn't matter so int will do"?)

Last Edited: Wed. Apr 8, 2020 - 09:09 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Kartman wrote:

You've pulled some random code in there and mixed it together. Not a good strategy for success. 

 

I'm afraid it's another example of trying to write code without any design going into what's needed.

 

I'm intrigued by the two 'while (1)' loops in main.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brian Fairchild wrote:
trying to write code

No, not writing code, tryint to mash two different codes together with out understanding how either works!

 

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"