I'm stuck again
When I 1st started playing with all this. I was using the Arduino IDE and a Digispark Attiny85 board.
I worked out the timing to my satisfaction and was going to Port the code to the Attiny4. This is what I ended upo with in the Arduino IDE
#include <avr/wdt.h>
#include <avr/sleep.h>
#include <avr/interrupt.h>
#define adc_disable() (ADCSRA &= ~(1<<ADEN)) // disable ADC (before power-off)
//#define adc_enable() (ADCSRA |= (1<<ADEN)) // re-enable ADC
void setup()
{
// Power Saving setup
for (byte i = 0; i < 6; i++) {
pinMode(i, INPUT); // Set all ports as INPUT to save energy
digitalWrite (i, LOW); //
}
adc_disable(); // Disable Analog-to-Digital Converter
wdt_reset(); // Watchdog reset
wdt_enable(WDTO_4S); // Watchdog enable Options: 15MS, 30MS, 60MS, 120MS, 250MS, 500MS, 1S, 2S, 4S, 8S
WDTCR|= _BV(WDIE); // Interrupts watchdog enable
sei(); // enable interrupts
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // Sleep Mode: max
}
void loop()
{
pinMode(1, OUTPUT);
digitalWrite(1, HIGH); // 1 turn the LED on (HIGH is the voltage level
delay(200); // wait for 200ms
digitalWrite(1, LOW); // turn the LED off by making the voltage LOW
delay(100); // wait for 100ms
digitalWrite(1, HIGH); // 2 turn the LED on (HIGH is the voltage level
delay(200); // wait for 200ms
digitalWrite(1, LOW); // turn the LED off by making the voltage LOW
delay(100); // wait for 100ms
digitalWrite(1, HIGH); // 3 turn the LED on (HIGH is the voltage level
delay(200); // wait for 200ms
digitalWrite(1, LOW); // turn the LED off by making the voltage LOW
// adc_disable(); // Disable Analog-to-Digital Converter
sleep_enable();
sleep_cpu();
digitalWrite(1, HIGH); // 1 turn the LED on (HIGH is the voltage level
delay(200); // wait for 200ms
digitalWrite(1, LOW); // turn the LED off by making the voltage LOW
delay(100); // wait for 100ms
digitalWrite(1, HIGH); // 2 turn the LED on (HIGH is the voltage level
delay(200); // wait for 200ms
digitalWrite(1, LOW); // turn the LED off by making the voltage LOW
delay(100); // wait for 100ms
digitalWrite(1, HIGH); // 3 turn the LED on (HIGH is the voltage level
delay(200); // wait for 200ms
digitalWrite(1, LOW); // turn the LED off by making the voltage LOW
delay(100); // wait for 100ms
digitalWrite(1, HIGH); // 4 turn the LED on (HIGH is the voltage level
delay(200); // wait for 200ms
digitalWrite(1, LOW); // turn the LED off by making the voltage LOW
delay(100); // wait for 100ms
digitalWrite(1, HIGH); // 5 turn the LED on (HIGH is the voltage level
delay(200); // wait for 200ms
digitalWrite(1, LOW); // turn the LED off by making the voltage LOW
// adc_disable(); // Disable Analog-to-Digital Converter
sleep_enable();
sleep_cpu();
// adc_disable(); // Disable Analog-to-Digital Converter
sleep_enable();
sleep_cpu();
}
ISR (WDT_vect) {
WDTCR |= _BV(WDIE);
}
Page 53 shows this.
9 0x008 WDT Watchdog Time-out Interrupt
Instead of this.
ISR (WDT_vect) {
So I should put this ?
ISR (WDT) {
This is the jumbled mess I have so far.
/*
* 2 leds.c
*
* Created: 11/10/2020 11:27:18 AM
* Author : rascal
*/
#define F_CPU 500UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
#include <util/delay.h>
#define RED PB0
#define GREEN PB1
int main(void)
{
cli(); // Disable global interrupts before initialization
CCP=0xD8; // Unlock protected register
CLKMSR=0x01; // Change clock source to Low-power 128 kHz oscillator
CLKPSR=0b1000; // Clock division factor 256 (1000) = 500Hz
PRR |= (1<<PRTIM0); // Turn timer0 off
DDRB = 0b00000011; // Pins 4 & 6 inputs - Pins 1 & 3 outputs
PUEB = (1<<PUEB2)|(1<<PUEB3); // Enable Pullup Resistor Pins 4 & 6
ACSR = (1<<ACD); // Turn off Analog comparator
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
MCUSR = 0; // Clear MCU Status Register
WDTON = (0<<WDE)|(1<<WDIE) ; //set watchdog in interrupt mode
wdt_reset(); // Watchdog reset
WDP = 0b1001; // WDT prescale
// 0000=16ms, 0001=32ms, 0010=64ms, 0011=125ms, 0100=250ms, 0101=500ms
// 0110=1 sec, 0111=2 sec, 1000=4 sec, 1001=8sec
//wdt_enable(WDP=1001); // Watchdog enable Options: 15MS, 30MS, 60MS, 120MS, 250MS, 500MS, 1S, 2S, 4S, 8S
WDTCR|= _BV(WDIE); // Interrupts watchdog enable
sei(); // enable interrupts
SMCR = (1<<SE); //enable power-down sleep mode
sei(); //initialize global interrupts before beginning loop
/* Replace with your application code */
while (1)
{
// Blink PB0 (RED led) 4 times then sleep
PORTB |=(1<<RED); // 1 turn the LED on (HIGH is the voltage level
_delay_ms(200); // wait for 200ms
PORTB &=~(1<<RED); // turn the LED off by making the voltage LOW
_delay_ms(100); // wait for 100ms
PORTB |=(1<<RED); // 1 turn the LED on (HIGH is the voltage level
_delay_ms(200); // wait for 200ms
PORTB &=~(1<<RED); // turn the LED off by making the voltage LOW
_delay_ms(100); // wait for 100ms
PORTB |=(1<<RED); // 1 turn the LED on (HIGH is the voltage level
_delay_ms(200); // wait for 200ms
PORTB &=~(1<<RED); // turn the LED off by making the voltage LOW
sleep_enable();
sleep_cpu(); //sleep
// Blink PB0 (GREEN led) 5 times then sleep
PORTB |=(1<<GREEN); // 1 turn the LED on (HIGH is the voltage level
_delay_ms(200); // wait for 200ms
PORTB &=~(1<<GREEN); // turn the LED off by making the voltage LOW
_delay_ms(100); // wait for 100ms
PORTB |=(1<<GREEN); // 2 turn the LED on (HIGH is the voltage level
_delay_ms(200); // wait for 200ms
PORTB &=~(1<<GREEN); // turn the LED off by making the voltage LOW
_delay_ms(100); // wait for 100ms
PORTB |=(1<<GREEN); // 3 turn the LED on (HIGH is the voltage level
_delay_ms(200); // wait for 200ms
PORTB &=~(1<<GREEN); // turn the LED off by making the voltage LOW
_delay_ms(100); // wait for 100ms
PORTB |=(1<<GREEN); // 4 turn the LED on (HIGH is the voltage level
_delay_ms(200); // wait for 200ms
PORTB &=~(1<<GREEN); // turn the LED off by making the voltage LOW
_delay_ms(100); // wait for 100ms
PORTB |=(1<<GREEN); // 5 turn the LED on (HIGH is the voltage level
_delay_ms(200); // wait for 200ms
PORTB &=~(1<<GREEN); // turn the LED off by making the voltage LOW
sleep_enable(); // 2 sleep modes to extend the (pause) not blinking time
sleep_cpu();
sleep_enable(); // 2 sleep modes to extend the (pause) not blinking time
sleep_cpu();
}
// Watchdog Interrupt Service / is executed when watchdog timed out
ISR(WDT_vect) {
// f_wdt=1; // set global flag
WDTCR |= (1<<WDIE);
}
}
These are the build errors I'm getting in Atmelstudio 7
recipe for target 'main.o' failed
'MCUSR' undeclared (first use in this function)
'WDTON' undeclared (first use in this function)
'WDP' undeclared (first use in this function)
'WDTCR' undeclared (first use in this function)
static declaration of '__vector_8' follows non-static declaration
I'm stuck at setting the Watchdog timer Prescaler.
Because of the build errors I'm getting. I'm guessing this is an Arduino thing and not used in AtmelStudio gcc.
wdt_enable(WDTO_4S); // Watchdog enable Options: 15MS, 30MS, 60MS, 120MS, 250MS, 500MS, 1S, 2S, 4S, 8S
Samething here. Or is this a Attiny85 setting and is different on the Attiny4 ?
}
ISR (WDT_vect) {
WDTCR |= _BV(WDIE);
}
It seems 1 of my issues in understanding what to type for the setting I'm trying to use.
Like this. On page 50, the datasheet says
Bits 5,2:0 – WDPn: Watchdog Timer Prescaler [n=3:0]
The WDP[3:0] bits determine the Watchdog Timer prescaling when the Watchdog Timer is running. The
different prescaling values and their corresponding time-out periods are shown in the table below.
If your able to see the changes I have been trying in the Atmel studio code. I'm not understang what I need to plug, that will chose my wanted settings.
Or is there a #include I can use, that allow me to use the Arduino IDE in the AtmelStudio ?
I apologize for the messy code. I have been trying to keep myself to 1 change at a time, so I can keep track of what is really working and what is not.