Showing posts with label C Programming. Show all posts
Showing posts with label C Programming. Show all posts

15 November 2006

Installing SDCC on Fedora Core 6

I have upgraded my Linux to the latest version Fedora Core 6. This version come with linux kernel 2.6.18 and gcc v4.1.1. I don't hesitate to install the SDCC to work on it. After I downloaded the latest of SDCC source, sdcc-src-xxx-xxx.tar.bz2, I installed it with this command:
$tar -xvfz sdcc-src-xxx-xxx.tar.bz2
$cd sdcc
$./configure --prefix=/usr
$make
$make install
If it succeed, you should have these directories:
/usr/bin for execute files
/usr/share/sdcc/doc for documentation
/usr/share/sdcc/include for header files
/usr/share/sdcc/lib for libraries

I tested it with this simple code, I edited with gedit:

#include <8051.h>
void min() {
unsigned char var_1;
var_1 = 0xFF;
}

then saved it, let say test.c. To compile I use this command:
$sdcc test.c
and I found the test.ihx file in that directory which is the expected output.

16 September 2006

Distance Measurement Sensor

GP2Y0A21YK is General Purpose Type Distance Measuring Sensors from Sharp. (see the product page). Sharp Infrared (IR) radiation Distance Measuring Sensor use Infrared signal to measure object distance from 10 to 80 cm with analog output.

Features

  1. Less influence on the color of reflective objects, reflectivity

  2. Line-up of distance output/distance judgement type

    Distance output type (analog voltage) : GP2Y0A21YK

    Detecting distance : 10 to 80cm

    Distance judgement type : GP2Y0D21YK

    Judgement distance : 24cm

    (Adjustable within the range of 10 to 80cm [Optionally available])

  3. External control circuit is unnecessary

  4. Low cost

Applications

  1. TVs

  2. Personal computers

  3. Cars

  4. Copiers

  5. Robots

The output of this sensor is analog therefore it need Analog to Digital Converter (ADC) to interface the 8051. I use ads7841 as ADC (see this article).

The graph of Analog voltage output versus distance to reflective object are shown in Fig.1. When I plot Analog voltage output versus inverse number distance, I found the linear relation shown in Fig.2.

Fig. 1 Analog voltage output (V) versus distance to reflective object (cm).


Fig. 2 Analog voltage output (V) versus inverse number distance (1/cm).

The linear equation is y = 20.99x + 0.19, where y is voltage output and x invert distance. Notice that the voltage output I can trust are about 0.4 to 2.8 V.

This is an example code how to interface the distance sensor to 8051. I use SDCC as C Compiler.


#include <8051.h>

#include "ads7841.h"


void main()

{

float x,y; // Distance and Analog voltage output


y = analog(0); // Read volt out if the sensor connect to channel 0 of ADS7841


y = 0.00122*y; // Convert BCD to DEC by multiply voltage by 5/4096



if ( (y > 0.4) && (y < 2.8) ) {

x = (y-0.19)/20.99; // Solve the linear equation

x = 1/x; // Inverse back get distance in cm.

}

}



Source:
test_irsensor.c
characteristic.txt (For plot with Spreadsheet)

Related Links

GP2Y0A21YK/GP2Y0D21YK Datasheet
Analog to Digital (ADS7841) Interfacing

Servo Motor Control


Servos

Servos are DC motors with built in gearing and feedback control loop circuitry. And no motor drivers required. They are extremely popular with robot, RC plane, and RC boat builders. Most servo motors can rotate about 90 to 180 degrees. Some rotate through a full 360 degrees or more.

However, servos are unable to continually rotate, meaning they can't be used for driving wheels, unless they are modified (how to modify), but their precision positioning makes them ideal for robot legs and arms, rack and pinion steering, and sensor scanners to name a few. Since servos are fully self contained, the velocity and angle control loops are very easy to impliment, while prices remain very affordable. To use a servo, simply connect the black wire to ground, the red to a 4.8-6V source, and the yellow/white wire to a signal generator (such as from your microcontroller). Vary the square wave pulse width from 1-2 ms and your servo is now position/velocity controlled.


PWM

Pulse width modulation (PWM) is a powerful technique for controlling analog circuits with a processor's digital outputs. PWM is employed in a wide variety of applications, ranging from measurement and communications to power control and conversion. The general concept is to simply send an ordinary logic square wave to your servo at a specific wave length, and your servo goes to a particular angle (or velocity if your servo is modified). The wavelength directly maps to servo angle.The standard time vs angle is represented in this chart:

Figure: Pulse for controlling a Servo motor


Programmable Counter Array (PCA)

The PCA is a special modules in Philips P89V51RD2 which includes a special 16-bit Timer that has five 16-bit capture/compare modules associated with it. Each of the modules can be programmed to operate in one of four modes: rising and/or falling edge capture, software timer, high-speed output, or pulse width modulator. Each module has a pin associated with it in port 1.
Module 0 is connected to P1.3 (CEX0), module 1 to P1.4 (CEX1), etc. Registers CH and CL contain current value of the free running up counting 16-bit PCA timer. The PCA timer is a common time base for all five modules and can be programmed to run at: 1/6 the oscillator frequency, 1/2 the oscillator frequency, the Timer 0 overflow, or the input on the ECI pin (P1.2). The timer count source is determined from the CPS1 and CPS0 bits in the CMOD SFR.

In the CMOD SFR there are three additional bits associated with the PCA. They are CIDL which allows the PCA to stop during idle mode, WDTE which enables or disables the Watchdog function on module 4, and ECF which when set causes an interrupt and the PCA overflow flag CF (in the CCON SFR) to be set when the PCA timer overflows. The Watchdog timer function is implemented in module 4 of PCA. Here, we are interested only PWM mode.


8051 Pulse width modulator mode

All of the PCA modules can be used as PWM outputs. Output frequency depends on the source for the PCA timer. All of the modules will have the same frequency of output because they all share one and only PCA timer. The duty cycle of each module is independently variable using the module's capture register CCAPnL.When the value of the PCA CL SFR is less than the value in the module's CCAPnL SFR the output will be low, when it is equal to or greater than the output will be high. When CL overflows from FF to 00, CCAPnL is reloaded with the value in CCAPnH. this allows updating the PWM without glitches. The PWM and ECOM bits in the module's CCAPMn register must be set to enable the PWM mode. For more details see P89V51RD2 datasheet.

This is an example how to control servos with 8051 by using PWM. The schematic is shown below. I use P1.4 (CEX1) to control the left servo and P1.2 (CEX2) to control the right servo. Here, I use GWS servo motor model S03T STD. I need three states of duty cycle:

  • 20 ms to Stop the servo

  • 1 ms to Rotate Clockwise

  • 2 ms to Rotate Counter-clockwise


Calculation for duty cycle (for XTAL 18.432 MHz with 6 Clock/Machine cycle)

  • Initial PWM Period = 20mS (18.432MHz /6-Cycle Mode)

  • Initial PCA Count From Timer0 Overflow

  • 1 Cycle of Timer0 = (1/18.432MHz)x6 = 0.326 uS

  • Timer0 AutoReload = 240 Cycle = 78.125 uS

  • 1 Cycle PCA = [(1/18.432MHz)x6]x240 = 78.125 uS

  • Period 20mS of PCA = 20ms/78.125us = 256 (CL Reload)

  • CL (20mS) = 256 Cycle Auto Reload

  • Load CCAPxH (1.0mS) = 256-13 = 243 (243,244,...,255 = 13 Cycle)

  • Load CCAPxH (2.0mS) = 255-26 = 230 (230,231,...,255 = 26 Cycle)

Schematic: Control RC Servos motors with 8051 PWM


Datasheet

- P89V51RD2 [pdf]


Source Code (For SDCC)

- pwm_servos.h

- test_servos.c


Related Links

- What is a Servo?

- Tutorial on how to modify a servo for full 360 degree rotation

- Servo Tutorial for Robotics

- Pulse-width modulation (Wiki)

- Introduction to Pulse Width Modulation (PWM)

EEPROMs Interfacing


24LC512 is a 64K x 8 (512 Kbit) Serial Electrically Erasable PROM (EEPROMs), from Microchip Technology Inc. (see the product page). It has been developed for advanced, low-power applications such as personal communications and data acquisition. This device also has a page write capability of up to 128 bytes of data.

This device is capable of both random and sequential reads up to the 512K boundary. Functional address lines allow up to eight devices on the same bus, for up to 4 Mbit address space.

This is an example how to interface 24XXX EEPROMs with 8051. I use SDCC as C Compiler. My schematic is shown below.

Schematic: 8051 interface to 24XXX EEPROMs

Datasheet
- 24LC512 [pdf]

Source Code (For SDCC)
- 24xx512.h
- test_eeprom.c

Note: require lcd.h and i2c.h


Related Links
-24LC512 Products Page
-Interfacing I2C EEPROM (24LC256) with MCS-51 (KEIL C51)

10 September 2006

Real Time Clock Interfacing


DS1307, a 64 x 8, Serial, I2C Real-Time Clock, is a low-power, full binary-coded decimal (BCD) clock/calendar plus 56 bytes of NV SRAM. Address and data are transferred serially through an I2C, bidirectional bus. The clock/calendar provides seconds, minutes, hours, day, date, month, and year information. It a product of Maxim/Dallas Semiconductor, see the product page.

This is an example how to interface DS1307 with 8051. I use SDCC as C Compiler. My schematic is shown below.

Schematic: 8051 interface to DS1307

Datasheet
- DS1307 [pdf]

Source Code (For SDCC)
- ds1307.h
- test_ds1307.c

Note: require lcd.h and i2c.h


Related Links
- DS1307 Products Page
- Interfacing I2C RTC (DS1307) with MCS-51 (KEIL C51)

09 September 2006

I²C Bus Interfacing

I²C bus (Inter-Integrated Circuit) is a bidirectional, half duplex, two-wire, synchoronous bus, originally designed for interconnection over short distances within a piece of equipment. The I2C bus uses two lines called Serial Clock Line (SCL) and Serial Data Lines (SDA). Both lines are pulled high via a resistor (Rpu) as shown in Figure below. The bus is defined by Philips, see more details.

Three speed modes are specified: Standard; 100kbps [Bits per Second], Fast mode; 400kbps, High speed mode 3.4Mbps. I2C, due to its two-wire nature (one clock, one data) can only communicate half-duplex. The maximum bus capacitance is 400pF, which sets the maximum number of devices on the bus and the maximum line length.

The interface uses 8 bit long bytes, MSB (Most Significant Bit) first, with each device having a unique address. Any device may be a Transmitter or Receiver, and a Master or Slave. The Slave is any device addressed by the Master. A system may have more than one Master, although only one may me active at any time.

Data and clock are sent from the Master: valid while the clock line is high. The link may have multiple Masters and Slaves on the bus, but only one Master may be active at any one time. Slaves may receive or transmit data to the master.

This is an example how to interface I²C with 8051. I use SDCC as C Compiler. My schematic is shown below. I set P2.6 as SCL and P2.7 as SDA.

Schematic: I²C Bus

Datasheet
I²C-Bus Specification [pdf]

Source Code (For SDCC)
- i2c.h

Note: see DS1307 and 24LC512 for applications


Related Links
- Philips I²C Bus
- I2C (Inter-Integrated Circuit) Bus Technical Overview and Frequently Asked Questions (FAQ)
- I²C Bus and Access Bus
- http://www.i2c-bus.org/

08 September 2006

Analog-to-Digital Interfacing


The ADS7841 is a 4-channel, 12-bit sampling Analog-to-Digital Converter (ADC) with a synchronous serial interface. The resolution is programmable to either 8 bits or 12 bits. It is a product of Burr-Brown form Texas Instrument, see the product page.

This ADC give the 12-bit Binary Code Decimal (BCD) output value therefore the maximum value is 999 integer. If we want to convert this value to the real value, we must divide this value by 4096 (12-bit) and multiply by the maximum real ouput. Suppose the real maximum of output is 5V, the ratio should be 5/4096 = 0.00122. We can use 0.00122 multiply the output from ADS7841 to get the real ouput value.

This is an example how to interface ADS7841 with 8051. I use SDCC as C Compiler. My schematic is shown below. I still do not convert to the real value so my variables are integer.

Schematic: 8051 interface to ADS7841

Datasheet
- ADS7841 [pdf]

Source Code (For SDCC)
- ads7841.h
- test_ads7841.c

Note: require lcd.h


Related Links
- ADS7841 Products Page

07 September 2006

LCD interfacing


This is an example how to interface to the standard Hitachi-44780 LCD using an 8051 microcontroller and SDCC as C Compiler. I use a standard 16-character by 2-line LCD module, see schematic below. Here, I use 4-bit interfacing.


Schematic: 4-bit interfacing 16x2 LCD

Source Code (For SDCC)
- lcd.h
- test_lcd.c


Related Links
- How to control a HD44780-based Character-LCD: The Industry Standard Character LCD
- Yet Another 8051-to-LCD Interface
- Interfacing: Converting 8-bit LCD communication to 4-bit

05 September 2006

Introduction to SDCC: Small Device C Compiler

SDCC is a Freeware, retargettable, optimizing ANSI - C compiler that targets the Intel 8051, Maxim 80DS390, Zilog Z80 and the Motorola 68HC08 based MCUs. Work is in progress on supporting the Microchip PIC16 and PIC18 series. AVR and gbz80 ports are no longer maintained. The entire source code for the compiler is distributed under GPL.

Some of the features include:

  • ASXXXX and ASLINK, a Freeware, retargettable assembler and linker.

  • extensive MCU specific language extensions, allowing effective use of the underlying hardware.

  • a host of standard optimizations such as global sub expression elimination, loop optimizations (loop invariant, strength reduction of induction variables and loop reversing ), constant folding and propagation, copy propagation, dead code elimination and jump tables for 'switch' statements.

  • MCU specific optimisations, including a global register allocator.

  • adaptable MCU specific backend that should be well suited for other 8 bit MCUs

  • independent rule based peep hole optimizer.

  • a full range of data types: char (8 bits, 1 byte), short (16 bits, 2 bytes), int (16 bits, 2 bytes), long (32 bit, 4 bytes) and float (4 byte IEEE).

  • the ability to add inline assembler code anywhere in a function.

  • the ability to report on the complexity of a function to help decide what should be re-written in assembler.

  • a good selection of automated regression tests.

SDCC also comes with the source level debugger SDCDB, using the current version of Daniel's s51 simulator. (Currently not available on Win32 platforms).

SDCC was written by Sandeep Dutta and released under a GPL license. Since its initial release there have been numerous bug fixes and improvements. As of December 1999, the code was moved to SourceForge where all the "users turned developers" can access the same source tree. SDCC is constantly being updated with all the users' and developers' input.

SDCC Homepage
- http://sdcc.sourceforge.net/