Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Help me understand PWM and ADCH
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Help me understand PWM and ADCH

by soggybag on Sat Aug 21, 2010 12:25 pm

I've written a small program (below), which controls the brightness of an LED with a pot. The program below works. I have a few questions about how it works.

I'm using the attiny13. I have my LED connected to PIN 5 with a 150ohm resistor to GND. I have a 10K pot connected to +5V and GND with the wiper connected to pin 3.

For some reason the at the lowest value the LED isn't completely off. I'm wondering about ADCH. Where is this value coming from and what is the range of it?

OCR0Asets the PWM value. I remember using this with another program and it accepted a value of 0 - 255, uint8_t.

Any illumination, clarification, suggestions and comments welcome.

Code: Select all | TOGGLE FULL SIZE
/*
* changing the brightness with PWM
* control PWM value with a 10K pot
* Confirmed working 082110
* LED PIN 5
* POT PIN 3
*/

#include <avr/io.h>
#include <util/delay.h>

#define F_CPU 9600000UL

int main(void) {
   
   TCCR0A |= (1<<COM0A1) | (1<<WGM00);    // PWM Phase correct mode 1
   TCCR0B |= (1<<CS00);         // --no prescale
   TCNT0  = 0x00;            // Timer/Counter Register
   OCR0A  = 0x12;            // Output Compare Register A

   DDRB   |= (1<<PORTB0);   // Set PB0 (Pin 5) as output

   ADMUX |=  ( 1 << ADLAR ) |          // AD result store in (more significant bit in ADCH)
              ( 1 << MUX1  );              // Choose AD input AD2 (BP 4, Pin 3)

   while( 1 ) {
      // Start A to D conversion
      /**/
      ADCSRA |= ( 1 << ADEN );          // Analog-Digital enable bit
          ADCSRA |= ( 1 << ADSC );          // Discard first conversion
            while ( ADCSRA & ( 1 << ADSC ) ); // wait until conversion is done
          ADCSRA |= ( 1 << ADSC );          // start single conversion
             while ( ADCSRA & ( 1 << ADSC ) )  // wait until conversion is done
          ADCSRA &= ~( 1 << ADEN );         // shut down the ADC
      
      OCR0A = ADCH;         // Set PWM at pin 5 to the ADCH read from pin 3
   }
   return 1;
}

soggybag
 
Posts: 35
Joined: Sat Mar 01, 2008 3:28 am

Re: Help me understand PWM and ADCH

by chatham on Sun Sep 26, 2010 3:38 pm

Maybe there's some noise on your ADC line?

My suggestions would be to isolate where the problem is in the program further. It's either the case where the ADC in isn't getting a clean "0V" reading, or the PWM out isn't outputting a proper 0V signal. So,
A - test the output side of things (just have your code be: OCR0A = 0;) to make sure the PWM is sending a good 0V signal, then
B - using a piece of wire, directly ground pin 3, and/or
C - get the multimeter out, and test to make sure that your pot actually is bringing the voltage all the way down to zero on pin 3.

Hope it helps!
OpenChord.org - Open Source kits to let you play Guitar Hero with a real guitar, or build your own Wii/PS3/USB controllers.

chatham
 
Posts: 144
Joined: Thu Feb 04, 2010 2:30 am

Please be positive and constructive with your questions and comments.