• No results found

On-Chip EEPROM

In document PIC for Beginners (Page 94-98)

accesses this location to display the stored data. New data is however changed using the push switches, and updated in EEPROM, so that next time device starts the updated data is read.

Notice the Edata command at the end of program. If you are programming the microcontroller using ICPROG, when program is loaded into it, notice that apart from program buffer, the EEPROM buffer con-tains 64 (= 100 decimal) hexadecimal value written. This data is written at the time of programming, now in our program, we read the EEPROM contents using ERead command. This command expects the address to read. Since x is a byte sized variable ERead will automatically read only one byte from the specified loca-tion. This program when run will display 100, which is read from EEPROM.

This program by default contains 100 in byte 0 of EEPROM. Run the program and 100 is displayed. Now press SW3, or SW4 to increase or decrease the value. When a new value is selected press SW5 this will write the new value into EEPROM, byte 0, effectively updating the previous value of 100. Now power off the board and restart it, this time the newly selected value is displayed.

As previously said if the data stored is not byte sized, it will occupy more bytes, so be careful about the ad-dress while reading it back. EEPROM is also used to act as data logger device, where sequential data record is stored in EEPROM to be retrieved later.

Lets develop a small application that records the environment temperature and after every minute stores the current temperature into EEPROM. The data can later be retrieved and viewed using Switches.

'EEProm

Print At 1,1,"EEPROM 0:", Dec x End

Print At 1,1,"EEPROM 0:", DEC3 x If SW3=0 Then

ADCON1 = %10000010 ' Set PORTA analog and right justify

Print At 2,1,DEC2 c," C"

EWrite n,[raw]

Print At 2,8,"Log:", DEC3 n n=n+2

Print At 2,1,DEC2 c," C"

Print At 1,8,"Log:", DEC3 k GoTo AA

rr:

Print Cls

Print At 1,1,"Temp Logger"

Return

This program, although not very advanced, but shows many important procedures. Whenever the processor starts it starts logging the temperature, into EEPROM. Instead of storing the floating point number it stores the raw data obtained from ADC. Mean while the program scans for switches, if SW5 is pressed it halts data logging and branches to a subroutine to show/review data, now using SW3 or SW4 you can view the stored data, converted into temp. again when SW5 is pressed, review is cancelled and program enters into data logging again starting over where it halted.

There is lot to improve, you can think yourself, like a procedure to clear the log, know exactly how many logs are there etc.

Data loggers are a big market in microcontroller applications. For example, consider a shipment of frozen food to some country, the temperature data logger will keep on recording the temperature every hour. On arrival the company can inspect the logged temperatures to know if temperature increased beyond certain level, or if it remained within safe limits. Similarly a medical equipment recording blood pressure of a pa-tient can store the recorded values, so that a review can be made about how blood pressure fluctuated.

Write a temperature logger, and display the review as graphic data on GLCD

T

his chapter will discuss, CCP which stands for Capture, Compare and Pulse width modulation is one of the most complicated modules in PIC microcontrollers. I will go through this module only briefly, as not overburden the beginner. In fact this module does three functions, or it has three modes. There are two such modules present in PIC18F452.

In Capture Mode, the peripheral allows timing of duration of an event. This circuit gives insight into the current state of some register which constantly changes its value. In this case, it is the timer TMR1 register.

Thus with this mode we can measure for how long a pin remained in logic 1 or 0.

The Compare Mode compares values contained in two registers at some point. One of them is the timer TMR1 register. This circuit also allows the user to trigger an external event when a predetermined amount of time has expired. Thus if you set a compare register to some value, when Timer1 reaches that value, a capture event takes place and an interrupt signal is fired indicating that a predefined time period has been elapsed.

PWM - Pulse Width Modulation can generate signals of varying frequency and duty cycle. The ratio be-tween ON and OFF states of the pulse determines the amount of energy transferred to device. This method is useful in controlling the speed of motors, or brightness of LEDs etc.

Now consider an example of ultrasonic range finder. The project consists of a few driving transistors, and standard 40KHz ultrasonic transducers. The microcontroller uses its CCP module in capture mode. So the value of timer1 is noted and an impulse is given on Tx transducer, when the impulse is returned the CCP module stores the new value of Timer1 in CCP register, subtracting the previous value from current value

gives you the time to echo. From this you can easily calculate distance. (see projects section).

If we talk about CCP1, it is the same as CCP2. The central point in CCP module is CCPR register. This is a 16 bit register and consists of a H and L parts. This register contains the values captured or compared with

Timer1. Remember when Capture is initiated, Timer1, is not initialized. And at the time of capture, what-ever the value of Timer1 register is, is copied into this register.

Pulse Width Modulation

Pulse width modulation is a technique where digital data is used to control the energy transfer to a device.

Chapter 15

In document PIC for Beginners (Page 94-98)