Posts Tagged micrcontroller

8051-Programmable Counter Array

Hello people…its been quite some time since i ve posted anything here. Well let me fix that…. this is a post on the programmable counter array functionality in 8051. To be clear, the 8051 core as such does not have this functionality but if you have an 8051 board which is not too old, you should have it. Referring the data sheet might be helpful. For the description below i ll be using AT89c51ed2. But you can extend the ideas to other boards too.

A few words on programmable counter array…its there to expand the timing capabilities of 8051. It requires comparatively low software overhead and is more accurate. It has an array of 5 counter/capture modules. They can be programmed in any of the following modes:

  • Rising or falling edge timer
  • Software timer
  • High speed output
  • PWM

If you ve read my previous post on controlling the servo motor using 8051..i wrote about one way PWM can be achieved. Here i ll demonstrate a much simpler approach. I ll be using the module 0 here to generate a PWM wave. We ll set the frequency as 100kHz and the duty cycle to 20%. The output will be available on Pin1.3/CEX0. I ll be using the Keil simulator to see the output.

The modules that i mentioned above require a timer of their own to function properly, this is achieved by what is called a PCA timer. Its a common base timer for all the modules. This timer is itself sourced by :

  • Freq (clk-peripheral) / 2
  • Freq(clk-peripheral) / 6
  • Timer 0 overflow
  • Input on ECI pin (Pin 1.2)

A better way to say this is….lets assume that we have programmed it to use Timer 0 overflow as a source. This means every time timer0 overflows the PCA timer increments by 1.

Okay then lets get started with it.

Since the output frequency is 100kHz, Freq(clk-peripheral)/ 6  seems to be a good enough choice .To program that we need the CPS1 and the CPS0 bits in the CMOD register.

MOV 0D9H, #00H

Next we ll fix the contents of CCAPM0 register. This is where we tell the microcontroller that we are going to use it in the PWM and enable the comparison.

MOV 0DAH, #42H

The next step is loading the CH & CL registers with appropriate values such that CH/CL overflows at the rate of 100kHz.

The oscillator frequency here is 33MHz. The F(clk-peripheral) is (Oscillator / 2) in this case..i.e 16.5MHz. Further more the clock to PCA is F(clk-peripheral)/6….i.e. 2.75MHz. As a result the number of increments the CH/CL register should go through to get a frequency of 100kHz is 27.5 ~ 28. So CH can be initialized with 0xFF and CL with 0xE4. Everytime the timer overflows an interrupt is raised. Thus in the interrupt service routine reload the CH/CL registers with the above values.

Once we have taken care of the frequency, the next step is setting the duty cycle. How this works is,  we store a value in the CCAPL0 SFR. This value is compared with the value in CL. As long as the count in CL is less than the value in CCAPLo, the output remains LOW, it then toggles to HIGH when the CL count exceeds the value in CCAPL0.

Finally start the timer and enable the PCA interrupt. After debugging and running the program, the plot the output.

Here is the final code for 100kHz and 20 % duty cycle. I ve loaded the CH/CL with values other than what I mentioned above since 28 increments was giving me a frequency of around 80kHz. After experimenting a bit the value of 235 in CL worked out perfectly.

, , , , , , , , ,

Leave a comment

Arduino 101 – Blinking LED

HI….If you are reading this page, i assume that you either have an Arduino microcontroller board or are looking to get one. Well ..for those of you who are new to microcontrollers and are looking to get ones board for themselves, let me tell you that Arduino is the perfect way to begin. It uses the Atmega microcontroller and is very easy to use. Also there are tons of Arduino projects posted on the internet which you can and should check out on your time.

Ok…so lets begin..Some knowledge of any of the programming languages out there would make things easier for you to understand but its not the end of the world if you don’t.

In this post i ll tell you how to get an LED blinking with the help of your Arduino board.

In  order to do this you ll require the following items:

  • An Arduino board.
  • USB cable
  • Resistor
  • LED
  • Some single strand wires
  • Bread board

The circuit for this implementation is quite simple.

Connect the pin3 to the anode of the led. The cathode ( the leg towards the blunt side when you look from the top ) is connected to one end of the resistor. The other end of the resistor is connected to the GND pin on the board.

Now that you have all the components, the next step is to download the arduino software from this link:

Once you are done with it, start the application. The interface is really simple to understand. There is a text editor, where you type in your code, and a few button above that let you verify and upload your program to the board.

The following figure shows you the code that we have used here.

Lets go through the code line by line.

int led =3;

This line declares an integer variable led and we assign to it the digital pin number that we are going to use. (In this case the 3rd digital pin)

void setup(){

pinMode(led, OUTPUT);


This “setup” is a function/method which runs, only once, whenever you load a new program onto your board or whenever you “power on” the board before moving to the loop() method. Also it returns nothing (“void”). In this method we define all the stuff necessary for a program to run properly eg. configuring the pins as inputs/outputs, setting the baud rate etc. (Here in the setup() method we define our int variable “led” as an “Output” using the pinMode() method,  since  well…it is gonna be our output pin)

Next we move on to the loop() function.

This is where you write the code that you want your microcontroller to follow. The loop function repeats itself as long as the board has power (that s why the “loop”). As mentioned earlier the loop() function executes after the setup() function.

In this example we want our LED to blink continuously…so here s the code that makes it happen:

void loop (){






Remember that the variable “led” is used to denote the 3rd digital pin here. So the first line tells the microcontroller to set the voltage on that pin to “High” value or 5V. This action forward biases the LED and it starts emitting light.

Next is the delay() statement. The argument to this function is the number of milliseconds it must wait before executing the next statement. Here the microcontroller waits for 500 ms before setting the voltage of the pin to “Low” or 0V which turns off the diode. This is followed by another delay of 500ms.

And then it again goes to digitalWrite(led, HIGH); And so the cycle continues.

Once we are done typing in the code, we ll need to verify it. This is done by clicking on the button which has a tick mark on it (refer to the figure). If everything s all right we need to upload the code to the board, but before doing that make sure that you have selected the correct board and the port. In order to check which COM port the board is using go to Start -> Right click on Computer ->  manage -> Device manager. You will see “COM” in the list. See the one that your board is using and then in the Arduino window go to Tools and select your board and the COM port that you just verified.

Now for the last step….click on the Upload button (the one with the arrow, next to the verify button).

The result is … we have an LED that stays on for half a second and turns off for the next half repeatedly…hence a blinking LED.

, , , ,

Leave a comment