
I dunno, I was staring at the Slow Messenger and the power LED — your typical grasshopper green — was so bright and just staring at me and kind of giving me a headache. And I thought — that’s not about slow. And I thought about the drowsy white “sleep” pulse of my PowerBook and I was, like..I need something like that on this device. A slow, sleepy, drowsy pulse. And maybe the pulse will change at different times of the day or something.
Lucky for me I had attached the LED to one of the timer/comparator signals on the ATMega32 in the design.

That signal — PB3? Over there on the left side of the Atmega32? That has an “alternate” use as an output for the timer/comparator circuit. That means that it will output a digital signal if you tell it to based on an internal counter equalling (comparing to) a value you specify, or when a timer is done counting up or down. So, by knowing things like how fast the chip is running (8 megahertz in my circuit) you can start to derive how often this signal will get turned on and off. Pretty soon you’re doing pulse-width modulation, something this MCU has built-in facilities for. With PWM you can make an LED think that the voltage being supplied to it is different values. If you change the PWM (and the apparent voltage to the LED) you can get it to dim to varying degrees. Or do a gooey, drozy pulse.
Here’s the code that will do this on an Atmega32. I’m having trouble getting it to work on the Atmega324 (which appears to be a replacement for the Atmega32), but I’ll get that straightened out soon enough..
#include <stdlib.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include "a2d.h"
#include "uart.h"
#include "avrlibdefs.h"
#include "avrlibtypes.h"
/* define CPU frequency in Mhz here if not defined in Makefile */
#ifndef F_CPU
#define F_CPU 8000000UL
#endif
static char mPwrLedLevel = 0;
static void incr_pwr_led(void);
ISR(SIG_OVERFLOW0) {
incr_pwr_led();
}
ISR(TIMER0_COMP_vect) {
incr_pwr_led();
}
static char aDirection;
static void
incr_pwr_led(void)
{
//TIMSK = 0×03;
if(mPwrLedLevel == 0×20) {
aDirection = 1;
}
if(mPwrLedLevel == 0xE0) {
aDirection = 0;
}
if(aDirection == 1) mPwrLedLevel++;
if(aDirection == 0) mPwrLedLevel--;
OCR0 = mPwrLedLevel;
}
int main(void)
{
sbi(DDRB, PB3);
PORTB |= (1<<PORTB3);
for(int j=0; j<30; j++)
_delay_ms(32);
cbi(PORTB, PB3);
for(int j=0; j<30; j++)
_delay_ms(32);
sbi(PORTB, PB3);
for(int j=0; j<30; j++)
_delay_ms(32);
sbi(PORTB, PB3);
for(int j=0; j<50; j++)
_delay_ms(32);
TCCR0 |= (1<<WGM00) | (1<<COM01) | (1<<CS02);
// TCCR0A |= (1<<CS02) | (0<<CS01) | (0<<CS00);
// TCCR0A |= (0<<WGM02) | (1<<WGM00);
// TCCR0A |= (1<<COM0A1) | (0<<COM0A0);
TCNT0 = 0×00;
sbi(TIMSK, TOIE0);
sei();
OCR0 = 0×10;
int j=0×00;
while(1==1) {
_delay_ms(32);
}
} Continue reading





