You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

83 lines
2.0 KiB

#include <xc.h>
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
//#include <avr/sleep.h>
//#include <util/atomic.h>
//#include <avr/wdt.h>
//tests5 tested and works (though 950Hz). unplug cable to programmer to see pin 4 active.
//reference: https://www.avrfreaks.net/forum/sample-project-attiny10
//https://blog.podkalicki.com/attiny13-blinky-with-timer-compa/
//watchdog is too slow and inaccurate to get 6000Hz
ISR(TIM0_COMPA_vect)
{
// Toggle PB2 Hi/Low depending on current state
PINB = 1<<2;
//PORTB ^= _BV(LED_PIN); // toggle LED pin
}
/*
Delay in powerdown mode. Wake up by watchdog interrupt.
* //NOTE: see earlier code, e.g. tests3 in attiny10 elec projects 2020
*/
/*
void delay_power_down_wdt(uint8_t wdto)
{
wdt_reset();
wdt_enable(wdto);
WDTCSR |= (1<<WDIE);
//so far (with 128Khz clk) this sleep will be about 30-40 seconds.
//(however, I'll add the below to)
//adjust sleep speed here:
// 0110 is 1hz at 128KHz
//WDTCSR |= (0<< WDP3);
//WDTCSR |= (1<< WDP2);
//WDTCSR |= (1<< WDP1);
//WDTCSR |= (0<< WDP0);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
// Make sure interrups are enabled and the I flag is restored
NONATOMIC_BLOCK(NONATOMIC_RESTORESTATE)
{
sleep_cpu();
wdt_disable();
}
sleep_disable();
}
*/
int main(void)
{
//Write CCP (to enable changing clock)
//CCP = 0xD8;
//change CLK to 128KHz
//CLKMSR = 0b01;
TCCR0A |= _BV(WGM01); // set timer counter mode to CTC
// TCCR0B |= _BV(CS02)|_BV(CS00); // set prescaler to 1024 (CLK=1200000Hz/1024/256=4.57Hz, 0.22s)
TCCR0B |= _BV(CS00); // set prescaler to 1 (CLK=1MHz/1)
OCR0A = 167; // with one timer, ever 166.667hz we want to count once (3000hz)
TIMSK0 |= _BV(OCIE0A); // enable Timer CTC interrupt
// PB2 change to output
DDRB = 1<<2;
sei();
while(1)
{
//handled by interrupt
}
}