Intelligent Lighting Control using AVR Microcontroller

May 12th, 2012 | by | technology

May
12

This dissertation is an effort to learn embedded system development and embedded programming using AVR microcontroller.

This dissertation will look at the working of Analog to Digital conversion (ADC) and the usage of Pulse Width Modulation (PWM) for Digital to Analog conversion (DAC). This dissertation will also cover the features of AVR microcontroller and the usage of timers and ADC conversion in AVR microcontroller.

This dissertation will combine the Analog to Digital conversion (ADC) and Pulse Width Modulation (PWM) to create an intelligent lighting, which will be automatically controlled based on the external ambient lighting. So, at dusk, when the external sunlight gradually decreases, the intelligent light will start, and it brightness will start gradually increasing to its maximum. Similarly, at dawn, the first appearance of light in the sky before sunrise, the intelligent light will slowly start dimming and will automatically switch off in the morning.

#define F_CPU 8000000UL

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

// initialize adc
void adcInit()
{
  // AREF = AVcc
  ADMUX = (1<<REFS0);

  // ADC Enable and prescaler of 128
  // 8000000/128 = 62500
  ADCSRA = (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
}

// read adc value
uint16_t adcRead(uint8_t ch)
{
  // select the corresponding channel 0~7
  // ANDing with '7' will always keep the value
  // of 'ch' between 0 and 7
  ch &= 0b00000111;  // AND operation with 7
  ADMUX = (ADMUX & 0xF8)|ch;   // clears the bottom 3 bits before ORing

  // start single conversion
  // write '1' to ADSC
  ADCSRA |= (1<<ADSC);

  // wait for conversion to complete
  // till then, run loop continuously
  // The loop does nothing while ADIF is set to 0,
  // it exits as soon as ADIF is set to one,
  // i.e. conversion is complete.
  while(!(ADCSRA & (1<<ADIF)));

  //Clear ADIF by writing one to it
  ADCSRA|=(1<<ADIF);

  return (ADC);
}

void pwmInit()
{
   /*
   TCCR0 - Timer Counter Control Register (TIMER0)
   -----------------------------------------------
   BITS DESCRIPTION

   NO:   NAME   DESCRIPTION
   --------------------------
   BIT 7 : FOC0   Force Output Compare [Not used in this example]
   BIT 6 : WGM00  Wave form generation mode  [SET to 1]
   BIT 5 : COM01  Compare Output Mode    [SET to 1]
   BIT 4 : COM00  Compare Output Mode    [SET to 0]

   BIT 3 : WGM01  Wave form generation mode  [SET to 1]
   BIT 2 : CS02   Clock Select         [SET to 0]
   BIT 1 : CS01   Clock Select         [SET to 0]
   BIT 0 : CS00   Clock Select         [SET to 1]

   The above settings are for
   --------------------------

   Timer Clock = CPU Clock (No Prescalling)
   Mode    = Fast PWM
   PWM Output  = Non Inverted

   */

   TCCR0|=(1<<WGM00)|(1<<WGM01)|(1<<COM01)|(1<<CS00);

   //Set OC0 PIN as output. It is  PB3 on ATmega16 ATmega32

   DDRB|=(1<<PB3);
}

/******************************************************************
Sets the duty cycle of output. 

Arguments
---------
duty: Between 0 - 255

0 = 0%

255 = 100%

The Function sets the duty cycle of pwm output generated on OC0 PIN
The average voltage on this output pin will be

         duty
 Vout=  ------ x 5v
         255 

This can be used to control the brightness of LED or Speed of Motor.
*********************************************************************/

void setPWMOutput(uint8_t duty)
{
   OCR0=duty;
}

int main()
{
  uint16_t adcResult;
  uint8_t pwmBrightness;

  // initialize adc and pwm
  adcInit();
  pwmInit();

  _delay_ms(50);

  while(1)
  {
    adcResult = adcRead(0);    // read adc value at PA0

    // Mapping the adc value in the range of 0-1024 (2^10 bits)
    // to 0-255, which can be sent to 8 bit OCR0 register.
    // So the adcResult is divided by 4, to make it in the
    // range of 0-255.
    // After that the value is subtracted from 255, so that if
    // the adc value is less, we want to send high value to the
    // OCR0, for increasing brightness, and if the adc value is
    // high, we want to send lower values for the PWM signal
    // generation, to reduce the brightness of the LED.
    pwmBrightness = (uint8_t)(255 - (adcResult/4));

    //Send the pwm value to the OCR0, to control the LED brightness
    setPWMOutput(pwmBrightness);
   }
}

The presentation and the project report can be downloaded from the previous post: Seminar on Embedded System Development using AVR Microcontroller.

No Comments »

Firefox New Download Manager Toolbar Interface

April 22nd, 2012 | by | technology

Apr
22

Firefox has released a new, easy and improved interface to the download manager. Instead of popping out a new download window, now a toolbar button will show the remaining time, and the toolbar window will automatically disappear when the download is complete. Also, when you click on that toolbar button, a new tool tip like window, will open, which will show each file details.

Firefox  New Download Manager Toolbar Interface

Firefox Download Manager Popup Window

No Comments »

Google Reader’s UI Issue Fixed

February 22nd, 2012 | by | technology

Feb
22

In my last post, I have showed, how the new Google Reader’s user interface is so unfriendly, wasting more than 40% screen space. I came across, some user defined CSS to solve the Google user interface issues. I have modified the CSS by Ingmar Hupp to provide more reading space for news items. I am using Stylish addon for Firefox, to load the user CSS.

Google Reader's UI Layout Issue Fixed
Google Reader’s Fixed GUI

vs

Google Reader's Stupid GUI
Google Reader’s Stupid GUI

I am not a CSS expert, and this CSS has lot of issues, and it interferes with GMail and other Google products. So, I just disable it, when I am not using the Google Reader. The idea here is to show Google: how their GUI needs to be user friendly. Google Reader is basically a reader for RSS feeds, and any reader’s first priority should be to use the screen real estate efficiently.

No Comments »

How to remove Visual Source Safe binding from Visual Studio projects?

January 19th, 2012 | by | technology

Jan
19

Just wrote this script to remove the VSS source safe bindings from old visual studio projects, so that I can move to GIT repository. I just run these commands using the Cygwin shell, from the solution root folder, and it removes all traces of the source control of the Jurassic era.

find . -type f -name *.dsp -print0 | xargs -0 -r perl -p -i.vssbak -e 's/^.*Scc_ProjName.*$//g'
find . -type f -name *.dsp -print0 | xargs -0 -r perl -p -i.vssbak -e 's/^.*Scc_LocalPath.*$//g'
find . -type f -name *.dsw -print0 | xargs -0 -r sed -i '/begin.source.code.control/,/end.source.code.control/d'
find . -type f -name *.sln -print0 | xargs -0 -r sed -i '/GlobalSection(SourceCodeControl)/,/EndGlobalSection/d'
find . -type f -name *.*proj -print0 | xargs -0 -r perl -p -i.vssbak -e 's/^.*SccProjectName.*$//g'
find . -type f -name *.*proj -print0 | xargs -0 -r perl -p -i.vssbak -e 's/^.*SccLocalPath.*$//g'
find . -type f -name *.*proj -print0 | xargs -0 -r perl -p -i.vssbak -e 's/^.*SccProvider.*$//g'
find . -type f -name *.vssbak -print0 | xargs -0 -r rm -f
find . -type f -name *.*scc -print0 | xargs -0 -r rm -f

Now its easy, to create a GIT repository on top of it.

git init

No Comments »

Flashing STM32 MCU using ST-Link Command Line Utility

January 18th, 2012 | by | technology

Jan
18

You can directly download your compiled HEX file from the Keil µVision 4 IDE or any other IDE to your STM32 value discovery kit or any other STM32 microcontroller, using the command line ST-Link utility. That means, that you don’t need to manually search the HEX file and use the ST-Link Utility GUI to flash the MCU. Just a press of download button in the IDE, will be enough to load your new program to the STM32 MCU and reset it also.

You need to go to the Utilities tab in the project options (ALT + F7) and select the “Use External Tool for Flash programming“. Browse to the path of the “ST-LINK_CLI.exe” program and give the command line arguments as: ‘-c SWD -p "$H@H.hex" -Rst -Run‘.

	Flashing STM32 MCU using ST-Link Command Line Utility

No Comments »

Digital Frame implementation using STM32 Cortex M3 processor and FatFs

January 9th, 2012 | by | technology

Jan
09

My first try at implementing Digital Frame using STM32 Cortex M3 processor. Using Keil IDE and STM32VLDISCOVERY as ST-Link for programming. The prototype device is HY-Smart STM32 board. The FAT 32 file system on the SanDisk 8GB card is read using FatFs library. Luckily, most of code is just copied from the sample code provided with the board.

Download code: Digital Frame using STM32 Cortex M3 and FatFs

2 Comments »