This is a section of code that's giving me problems in a project and I can't work out why.
The purpose is to use the rand function to generate a number between 1 and 100 then use this number to randomly alter the delay time that an led is on.
#include <util/delay.h>
#include <avr/io.h>
#include <stdlib.h>
#define led_port PORTB
#define led_DDR DDRB
#define led_pin PINB0
int main()
{
led_DDR |= 1 << led_pin; //set to output
while (1)
{
uint8_t ran_num = 0; //set the random number variable and clear it at each loop
ran_num = rand() % 100; //get a random number between 1 and 100 and put it into ran_num variable
led_port |= 1 << led_pin; //turn on the LED
_delay_ms(ran_num); //random number put into the delay function
led_port &= ~ 1 << led_pin; //turn the led off
_delay_ms(100); //slight delay before looping again
}
return 0;
}
I'm using Proteus 8 and have simplified my project to an LED to resistor to ground from PB01 as an indicator.
The code compiles no problem but when I run it chaos ensues with Proteus throwing a stream of literally thousands of identical warnings.
PC=0x003A [AVR CPU] LPM Accessed outside program memory
I changed Proteus to several different AVR MCU's, but it always gets the same response.
I have checked that I'm getting an integer between 1 and 100 from rand() and that if I change the _delay_ms() to a number rather than the ran_num it works fine.
An internet search turned up nothing.
What am I missing?
The purpose is to use the rand function to generate a number between 1 and 100 then use this number to randomly alter the delay time that an led is on.
#include <util/delay.h>
#include <avr/io.h>
#include <stdlib.h>
#define led_port PORTB
#define led_DDR DDRB
#define led_pin PINB0
int main()
{
led_DDR |= 1 << led_pin; //set to output
while (1)
{
uint8_t ran_num = 0; //set the random number variable and clear it at each loop
ran_num = rand() % 100; //get a random number between 1 and 100 and put it into ran_num variable
led_port |= 1 << led_pin; //turn on the LED
_delay_ms(ran_num); //random number put into the delay function
led_port &= ~ 1 << led_pin; //turn the led off
_delay_ms(100); //slight delay before looping again
}
return 0;
}
I'm using Proteus 8 and have simplified my project to an LED to resistor to ground from PB01 as an indicator.
The code compiles no problem but when I run it chaos ensues with Proteus throwing a stream of literally thousands of identical warnings.
PC=0x003A [AVR CPU] LPM Accessed outside program memory
I changed Proteus to several different AVR MCU's, but it always gets the same response.
I have checked that I'm getting an integer between 1 and 100 from rand() and that if I change the _delay_ms() to a number rather than the ran_num it works fine.
An internet search turned up nothing.
What am I missing?