Design by Q7b2064

VIEWS: 0 PAGES: 34

									Smart Appliance Communication Protocol
                    by
              Aaron Cohen
             Matt Kerchenfaut
              Natalie DiIorio
                  ECE 345
                Dave Crowe
              December 4, 2001
                  Project 4
I.   Abstract
     The goal of the Smart Appliance Communication System is to allow home appliances to send basic information to a
     center control. This system would allow the user to monitor the status of a given appliance and optimize power
     consumption if necessary. The main benefit of implementing this system would be to use the existing power lines
     for data transmission. This setup eliminates the need to install dedicated wiring and costs are drastically reduced.
II.   Table of Contents


      1. Introduction……………………………………………………..1
      2. Design Procedure……………………………………………….3
            a. Buffer, Filter, and Converter…………………………….3
            b. HC12…………………………………………………….5
      3. Design Details…………………………………………………..8
            a. Buffer, Filter, and Converter……………………………8
            b. HC12……………………………………………………10
      4. Design Verification…………………………………………….14
      5. Costs…………………………………………………………….16
      6. Conclusions……………………………………………………..17
      7. Appendix A: HC12 code……………………………………….18
      8. Appendix B: HC12 code……………………………………….25
III.     Introduction
         The ability to monitor and control the activity in a home has a variety of potential advantages. One may want to be

able to send and receive data for their home from a remote location. For example, a homeowner might want to turn on some

of the lights in their home before they enter, for security reasons. This person might also want the peace of mind that he or

she had turned off the oven before they went to work in the morning. In this case, it would be beneficial to implement a

system that could turn off the oven with just a simple phone call from work. If the home was powered by an alternative

energy source, the power that the house is drawing from the source might have to be limited to conserve energy. For

example, if a house powered by solar cells and back-up batteries had an electric oven running as a large cloud passed

overhead, some of the lights in the house might dim. This could be prevented if the oven were to shut down or reduce its

power for a few minutes until the cloud passes and the sunshine returned. In order for any of the above to be feasible, a good

communication system must be installed in order to send and receive data from the appliances in a home.

         The Smart Appliance Communication Protocol is a power line communication device that could be used for a wide

variety of applications. It can be installed in nearly any home due to the fact that it uses the existing power lines within a

home to transfer data. With a two-way communication feature, it could also be used to switch on and off appliances or

monitor the status of a device through the central control. It has the potential to obtain electrical or mechanical information

from any appliance equipped with the smart appliance technology by the use of sensors in the appliances. For instance, the

sensors on the appliances might measure voltage, current, temperature, light level, mechanical-motion, water level, etc. This

information could be obtained at the central control by simply choosing the appropriate appliance and corresponding data. It

could also be programmed to monitor certain appliances for specific conditions. One application could be a safety precaution

that could turn off an oven that became too hot until it had ample time to reduce its temperature. With this in mind, it could

also monitor current overload conditions that might not trip the circuit breaker, but could cause a fire. The system has a

variety of possible uses and could be very beneficial in any household.

         The smart appliance communication protocol works by performing the following tasks. (Please refer to Figure 1

found on the following page.) First, it will receive a signal from the user on the control end. This signal might be the user

requesting data from a certain appliance or an order to turn off a specific device. This signal could also be generated by the

micro-controller to monitor the status of a given appliance. Next, the control-side micro-controller will send out a 125kHz

TTL signal, encoded with the information needed, to the appliance’s micro-controller. To get to the micro-controller, the

signal will have to pass through a dual-stage buffer, the power line, a 125kHz resonant filter, and a circuit designed to convert

a sine-wave into a square wave, TTL signal. This converter circuit consists of an Op-Amp, a comparator, and a TTL inverter.

                                                                                                                             1
Once the reconstructed signal is received by the appliance’s micro-controller, it will obtain the data needed by selecting the

appropriate channel on an analog MUX. This data would include voltage values from 0 to +5V that would represent voltage,

temperature, current, etc. Once the data from the MUX is received, the information is encoded by the micro-controller and

sent back through the line to the control-side micro-controller. It is sent through with the same buffer, filter, and converter

circuit used previously. The information is then processed and printed out to a computer screen using ASCII characters. If

the user at the sending end wanted to turn off an appliance, a signal could be sent in the same manner, but the encoded

information would tell the receiving end micro-controller to open or close the contacts of a relay instead of selecting the

appropriate channel of the MUX. Here is the overall layout of our circuit:




                            Figure 1. Block diagram of Smart Appliance Communication Protocol.

         This project was separated into two major subprojects. The micro-controller and the encoding needed to transmit

data through the line were one major part of our project. We designed the rest of the circuit to pass only 125kHz

components, so the output signal of the micro-controller needed to send out a 125kHz signal. This eliminated the possibility

of using the RS232 part of the micro-controller. The micro-controller also had to send and receive data so that the amount of

error could be minimized. The control-side and appliance-side micro-controllers had to be synchronized so that a signal

would be sent one at a time. In addition, each micro-controller had to encode and decode the data accordingly. The other

major part of our project consisted of the buffer, filter and converter circuits. The input of the buffer was a square wave and

had to be sent through the power line and then reconstructed on the receiving side. Noise in the line also had to be

considered as well as signal attenuation caused by the internal resistance of the circuit.


                                                                                                                              2
IV. Design Procedures
         a. Buffer, Filter, and Converter:

Our original design can be represented by the figure shown below.

                              Sensors and Switches connected to the Appliance




                                                     Micro Controller



                                                     D/A                A/D



                                                Power        Circuit

                                                         B              F
                                                         U              I
                                                         F              L
                                                         F              T
                                                         E              E
                                                         R              R




                                                         Wall Outlet


                                  Figure 2. Original layout included in the design proposal.
Throughout the semester, there were several variations made to the original design. The modifications made to the project

proposal will be discussed along with justification for the individual choices made.

         We intended to use various voltage signals to be sent to an analog multiplexer in order to be read by a central

control. One of the voltage signals was to be obtained by using a temperature sensor. The original temperature sensor used

was the MTS102 by Motorola. There were several factors that led us to choose another part for our intended purpose. First

of all, the MTS102 has become obsolete and data sheets with design specifications were not available. This made connection

and design unreliable and inaccurate due to the necessity of several guesses made for implementation of the MTS102 in our

circuit. Secondly, the MTS102 required a constant current source, which added further complications. Therefore, we chose

to use the LM335 temperature sensor, which eliminated the difficulties associated with the original model. Also, the input

signal frequency was modified through the design process of our project. The intent in our final proposal was to use a high

frequency signal of approximately 100kHz. We used this value to determine inductor and capacitor values that would allow

                                                                                                                           3
a resonant frequency to be established in the filter and buffer sections of our circuit. The signal frequency used in the final

project was 125kHz. The motivation for this revision involves the consideration of the frequencies that could be used to

incorporate the micro-controller with the rest of the project. Specified frequencies could be used with the HC12 and 125kHz

was one of the allowed values. This value was approximately the value we planned for and was used in our final design. The

circuits could have been redesigned to be resonant closer to the new value, but testing proved that the circuit still performed

as expected and further changes were unnecessary.

         The buffer circuit did not undergo significant changes in design from the initial idea. The buffer was designed to

introduce our 125kHz signal into the line. The calculations were initially planned to function with a 100kHz signal, but were

not changed after testing proved the buffer functioned as expected with a 125 kHz signal.

         Our original idea was to use isolation transformers to separate our circuit from the wall outlet we would be using to

incorporate the power line into our circuit. Two isolation transformers were wrapped ensuring the turns ratio was equal on

both sides of each transformer in order to prevent any alteration of the circuit performance. In later stages of our project we

decided to abandon this approach and use a stage of two inverters, which would protect our circuit components from voltage

spikes in the line without modifying the signal we were attempting to transmit. The later design allowed for less error to be

introduced into our circuit. Transformers have many specific details such as leakage, additional resistance introduced and

unavoidable kinks formed when wrapping the transformer cores. These factors did not allow our signal to be unaffected

when introducing an isolation transformer in our circuit.

         The filter circuit serves as a way to recover our original 125 kHz signal after being sent via the wall outlet. The

main challenge in filter design was to prevent 60 Hz components from appearing at the output of the filter. The first filter

design was similar to the buffer circuit in that it focused around using a resonant frequency equal to 125 kHz. However, after

analyzing our filtered signal it could be observed that there was still a considerable amount of 60 Hz components present.

We overcame this problem by adding two high pass filters. These high pass filters consisted of a capacitor with a low value

of capacitance in order to introduce high impedance to the unwanted 60Hz component and low impedance to the high

frequency signal we were sending.

         A method of amplifying our filtered signal was not planned for in the original circuit. As we began testing the

buffer and filter circuits with the power line we began to realize that our signal was significantly attenuated during

transmission through the power line and filter/buffer circuits. Our signal was on the order of a few hundred millivolts so we

needed to add an op amp to regain amplitude for our signal. The initial choice for the op amp was the LM747. However, this

part did not appear in our final design due to improper functioning. The output of the op amp was not an amplified sine wave

as would be expected. The output resembled a triangular wave and we concluded that this op amp was not intended for use

with a high frequency signal. We chose the LF356 to replace the LM747 and after testing we determined that using our high
                                                                                                                            4
frequency signal would not produce undesired results with the new component. Our op amp served to introduce a gain of

approximately 25, which was adequate to be sent to the comparator.

         The comparator circuit component was not included in the project proposal. We had an initial assumption that we

would be sending a sine wave to the receiving end Analog to Digital converter. As this was not the case, we added the

comparator in order to convert our sine wave signal to a square waveform. The comparator compared our sine wave signal to

a specified DC offset value and corresponded signal values above the offset threshold to a value of 5 volts and signal values

below the threshold to a value of zero volts. The comparator was not tested against another alternative because it was a

valuable component, which also allowed us to account for noise in the signal introduced by transmission through the power

line. For example, any small variations due to disturbance in the line would be below the DC threshold value and would be

nullified to a value of zero by passing through the comparator. In addition, the DC offset value could be raised if noise or

disturbance in the line was higher at specific times in the lab. For these reasons the comparator LM339, which was chosen

was the optimal choice for our design purposes.

b. HC12
         One of the most challenging aspects of our project was to derive a method to transmit data. Originally, we had

decided to design our system to combine a 100KHz frequency signal with the 60Hz frequency signal from the wall outlet to

send data. After determining that our circuit worked for a frequency range from 100KHz to 175KHz we had to devise a

method of representing the data. There are four common ways to transmit data that are relatively easy to implement with the

micro-controller. The first is frequency modulation, which represents different bit values with different frequencies. For

example, 100KHz could represent a 0 and 200KHz could represent a 1.



 200KHz                                                                                   1




 100KHz                                                                                   0



                                              Figure 3: Frequency Modulation.

         We attempted to set the micro-controller to use frequency modulation but we were unsuccessful in getting it to use

frequencies that had values on the same order of magnitude. For serial communication, our micro-controller needed a base

frequency plus twice the base frequency. For example, if the base frequency were 125KHz then it would also need a 250KHz

to go through our circuit. Unfortunately, our circuit was not intended for passing signals with a frequency of 250KHz. The

                                                                                                                           5
second method for transmitting data is phase modulation. If the phase of the frequency changes then the bit received will be

the opposite of the last bit received. Otherwise, the value will be equal to the last bit received.



                                                                              (Current Bit) = (Last Bit)




                                                                             (Current Bit) = !(Last Bit)



Figure 4. Phase Modulation: If the phase stays the same then we would receive two identical bits in a row. However if the

phase changes then we received two complementary bits in a row.

         When examining phase modulation we determined it shared the same disadvantage as frequency modulation. When

the bit changes we see a pulse width that is equal to twice the base frequency’s pulse width or half the base frequency.

Therefore, when a bit changes our circuit would show no pulse at all. The third method of transmitting data is amplitude

modulation in which the amplitude of a waveform corresponds to a specific bit representation.




                 X            0            X           1            X            0



Figure 5. Amplitude Modulation: If the peak amplitude of the signal reaches a specific level it is known that a 1 has been

received. However, if the signal falls below the specific level it is concluded that a 0 has been received.

         After testing our circuit we found out that there was a great deal of attenuation of our signal when passing through

the line. This factor made it extremely difficult to determine differences in the amplitude of the waveform. The fourth

method of transmitting data is by using a pulse train, which involves sending a specific number of pulses to represent data

followed by a number of no pulses. This method wastes a considerable amount of bandwidth, however the result on the

receiving end is less susceptible to noise from the line.




                                                                                                                           6
                                                                            1




                                                                            0




 0        1        2       3        4        5        6       7

Figure 6. Pulse Train: A succession of 4 pulses represents a 1 whereas a single pulse represents a 0. The pulse train is then

followed by a series of no pulses so that the next pulse train can occur afterwards without affecting the number of pulses

currently being counted.

          Therefore, we chose to implement the pulse train as it appeared to be less susceptible to noise and would result in

less error.




                                                                                                                           7
  V.        Design Details
  a. Buffer, Filter, and Converter:
            The following section includes the details of the final circuit and the components used.
                                                                         Control Lines
  Voltage                                       Analog
                                                MUX                                                     Micro-
                                               LF13509                                                 Controller
Temperature


                                                Figure 7. Sending end of the overall circuit.

            The sending end of our overall circuit was necessary in order to model an appliance, which would be used to send

  data through the circuit. Voltage signals were used to model the status of an appliance and were derived from the function

  generator. We used a frequency of 125 kHz in order to correspond with both the circuit design and the micro-controller. The

  LM335 temperature sensor was used in conjunction with a potentiometer in order to calibrate a specific temperature to a

  precise voltage level. The analog multiplexer used was LF13509, which allowed a specific input signal to be read depending

  on what the receiving end was interested in retrieving from the inputs.

            SN7404 inverter chips were introduced into the design to ensure the inputs to the HC12 micro-controller would not

  be damaged from potential high voltage spikes, which may result from connections to the power grid. As a result, only a 0V

  or positive 5V level would result at the input ports to the micro-controller. The same reasoning was used for inserting an

  inverter at the output port of the micro-controller. Only one inverter was needed in the later arrangement because the op amp

  was connected using the inverting configuration so only one inversion was necessary to recover the original signal.

            The method used to calculate values for the components of the buffer circuit focused on the implementation of a

  resonant frequency at our signal frequency of 125kHz.




                                                          Figure 8. Buffer circuit.

            Xc = 1 / C         Equation (1)

            Choosing a value of C = 1000pF we were able to create a high impedance to be seen by the 60Hz signal existing

  from the power line and a low impedance to our high frequency signal. The impedance Xc seen by the 60Hz signal can be

  found to be 2.65M using Eq. (1), and by the same method the impedance seen by the 125kHz signal is 1.27k. Our buffer

  circuit allows a large impedance to create a large voltage drop for the power line, which allows two voltage signals with

  unequal voltages to be connected while avoiding KVL problems.




                                                                                                                              8
                                                         Figure 9. Filter circuit.

                                                          Figure 9. Filter circuit.

         The receiving end of the circuit begins with a filter setup, as shown above in Fig. 9, in order to retrieve the data

pertaining to a specific appliance. The design of the filter includes the same resonant circuit included in the buffer in order to

create a large Xc impedance at 60Hz to block these components from altering the data we intend to send. By using the

resonant Eq. (1) described in the previous section our signal of interest at 125kHz will be retrieved while only subjected to a

small impedance of the filter. In addition to the resonant L,C, R circuit the filter needed further design in order to reduce the

60Hz components we were still observing in our signal. Two 1000pF capacitors were added in parallel to the preliminary

filter design, which added additional impedances the 60Hz component could be attenuated through.


                                                      /\/\/\/\
                                                        /\/                                     Comparator
     Filter             /\/\/\/\/\                       LF
                                                            356

                            /
                                                 Figure 10. Receiving end of the overall circuit.

         The next stage included the LF356 op amp, which allowed us to amplify our signal. Following the op amp was the

comparator, which would convert our sinusoidal output waveform to a square waveform. The comparator chip used was the

LM339.
                                                                                  Micro-
                               Buffer                                            Controller


                                       Comparator


                                            Figure 11. Micro-controller connections
                                                                                                                            9
b. HC12
         The HC12 required software to control its functionality that was programmed in MINIDE using the Motorola HC12

assembly language. The two micro controllers were divided into separate senders and receivers. The sender consisted of a

program that was capable of reading the A/D port, encoding a data packet, and sending a data packet. The receiver was

capable of reading a data packet, deciphering it, outputting the house and appliance code to the PC via the serial port, and

outputting the deciphered data to PORTB, which was connected to light emitting diodes. A data packet consists of a start

code, house code, appliance code, and data.



 Start Code                     House Code                 Appliance Code              Data
 (8 bits)                       (4 bits)                   (4 bits)                    (8 bits)

                         Figure 12: Data Packet: An example representation of the bits in our data packet.

                  Main




                          Figure 13. Sender: This figure is a flowchart of main from the sender program.

         The sender program sits in a loop waiting for all bits to be sent before it creates another data packet for sending.

Once it has created another data packet for sending in memory it sets the flags that the interrupt handler uses to determine

weather or not it is sending bits for this interrupt.




                                                                                                                           10
                       Figure 14. Sender: This figure is a flowchart of the sender’s Interrupt Handler.

         The interrupt handler of the sender program gets called every 125KHz or every (1/(125000)) seconds. However,

when an interrupt occurs and there are bits to send, the handler will check to see if it has loaded the current pulse count for

the bit or if needs to load it from memory. Then depending on if the current pulse count is zero it will shut off the pulses

coming from the Pulse Width Modulator (PWM).




                                                                                                                           11
Figure 15. Flowchart for the receiver program. IntPCounter looks for pulses and Int125KhzTimer waits for a period of no
pulses.


         The receiver program works by waiting for an event to occur. Once the event occurs, which is a rising edge on the

input capture port, the current pulse counter in incremented and then an interrupt is set to occur 3 cycles (or 24 microseconds)

later. When this other interrupt occurs we then determine the bit corresponding to the number of pulses we have counted.

Afterwards, this bit is shifted into our FIFO. Next, our FIFO is checked for the start code. If the start code was found then

we decode the data packet and output the data to PORTB and the house and appliance code to the PC via the serial port.

                                                                                                                         12
         The data packet consisted of 24 bits sent consecutively in a pulse train. When we had read in the start code we knew

that we had read the entire data packet because our FIFO was 24bits deep. The start code was encoded as “11111110” and

the house code and appliance code were both selectable as “0xxx”, but “0111” was chosen for both. The data was encoded as

“00xxxxxx” to prevent the possibility of the start code occurring in the data packet more than once.




                                                                                                                      13
VI. Design Verification
         The overall project was divided into two sub-projects, which required the development of a method in order to test

each sub-project independent of the other. We then had to combine the two sub-projects together and test out the final,

completed project. A signal generator, pulse generator, multi-meter and an oscilloscope, were used to test the buffer, filter,

and converter sections of the circuit. The signal generator provided an oscillating 125kHz TTL square-wave for the input.

To simulate a single pulse, for the “zero bit”, we used the pulse generator. This would provide a single, 10us pulse, which

would simulate the start bit followed by zeros for the “zero bit”. Using the sources listed above, with an oscilloscope, each

stage of the filter, buffer, and converter circuit was tested. A multi-meter was used to measure the voltage across the buffer

and the filter to ensure that there was a 120V drop across it. This would ensure that the filter was functioning properly and

that the voltage sensitive components would not be damaged. For our final version of the sub-project, we obtained the

following data using the oscilloscope:




                         Figure 16. The input signal that tests the buffer, filter, and converter circuit




                 Figure 17. The output of the filter after the signal passed through the buffer and 120V line.




                                  Figure 18. Output of the Op-Amp, Input to the comparator
                                                                                                                         14
                                  Figure 19. Output of the comparator, Input to the inverter




                                Figure 20. Output of the inverter, Input to the micro-controller



         For the micro-controller, the error rate was the pertinent information tested and analyzed. Connecting the two

micro-controllers to the circuit, we were able to send and receive data from one micro-controller to another. In order to test

the reliability of the system we checked the error rate. We took a sample of 12568 characters received by the micro

controller. Then we found the total number of incorrect characters to be 273. By dividing the amount of errors by the total

number of characters we found a value of approximately 2.2% error. This was the total reliability of our project.




                                                                                                                         15
VII. Costs
The following are the approximate costs for the entire circuit.

Parts:

Description                Quantity           Cost per unit       Total cost per part

¼ W resistors (assorted)   25                 $0.10               $2.50

LM335 Temp Sensor          1                  $1.75               $1.75

25kohm potentiometer       1                  $0.65               $0.65

1mH inductor               8                  $0.80               $6.40

2.2uH inductor             4                  $0.35               $1.40

3.3uH inductor             4                  $0.40               $1.60

1000pF capacitor           8                  $0.50               $4.00

LF356 Op-Amp               2                  $1.73               $3.46

LM339 Voltage Comp         1                  $0.36               $0.36

SM7404 Hex inverter        2                  $0.49               $0.98

HC-12 micro-controller     2                  $100.00             $200.00

20-pin LED display DIP     1                  $3.13               $3.13

Various LED’s              10                 $0.15               $1.50

Total Cost:                                                       $227.73

Labor:

Aaron:             72 hours @ $36/hr                              $2592

Matt:              72 hours @ $28/hr                              $2016

Natalie:           72 hours @ $28/hr                              $2016

Total Labor:                                                      $6624

Total Cost of Project:                                            $6851.73




                                                                                        16
VIII. Conclusions
         The circuit that we built was designed to transmit a signal from one micro-controller representing an appliance to

another processing data at the receiving end of the circuit. This signal would be used to obtain data from the appliance side

or have the appliance perform a given task by opening or closing appropriate switches. Currently, our circuit can perform

one-way communication only, due to memory restrictions in the HC-12 micro-controller. This can be corrected by either

shortening the code downloaded onto the micro-controller, finding a more efficient algorithm for data transfer, or adding

extra memory using an EPROM. Currently, the error rate is fairly high at 2.2% error. Adjusting the filter so that it is

completely resonant at 125kHz may decrease the error rate. Testing concluded that the filter is completely resonant at

108kHz, but it works well at 125kHz. If we redesigned the filter and buffer circuits to better suit the new frequency of 125

kHz, more of the unwanted 60Hz component may have been eliminated. This would provide a much stronger output signal

with less error from noise in the line.

         In conclusion, the opportunities for power line based communications are extensive. Not only for home automation,

but also for any new system to be installed with the intention of using the existing power lines, the installation is extremely

simplified. Without the need to install dedicated wiring for a new system the cost is reduced and the need to alter existing

wiring is unnecessary. Although our data rates were not required to be high, it is possible for this application to be applied to

more complex systems where more data is transferred. Also, with more power electronics expertise and possible

implementation of fm modulation, the high frequency signal could be filtered out more accurately without the complications

due to 60 Hz components from the power line.




                                                                                                                          17
;; ======================================          APPENDIX A
; [SENDER]
; Smart Appliance (Group 4)
; uController Code version 0.5
; November 20, 2001
; ================================
; Purpose: This prog sends a data packet.
;
; ================================


; ================================
; Define Constants
; ================================
PORTA   equ $00     ;Port A Data
PORTB   equ $01     ;Port B Data
PORTE   equ $08     ;Port E Data
DDRA    equ $02     ;Port A Data Direction
DDRB    equ $03     ;Port B Data Direction
DDRE    equ $09     ;Port B Data Direction

ATDCTL2   equ   $062             ; A/D CTL
ATDCTL3   equ   $063
ATDCTL4   equ   $064
ATDCTL5   equ   $065

PORTAD0   equ   $70              ; A/D Ports 0 through 7
PORTAD1   equ   $72
PORTAD2   equ   $74
PORTAD3   equ   $76
PORTAD4   equ   $78
PORTAD5   equ   $7A
PORTAD6   equ   $7C
PORTAD7   equ   $7E

SC0BDH    equ   $C0              ; Baud Reg High
SC0BDL    equ   $C1          ; Baud Reg Low
SC0CR1    equ   $C2          ; Control Reg 1
SC0CR2    equ   $C3          ; Control Reg 2

SC0SR1    equ $C4                ; Status Reg
SC0SR2    equ     $C5                ; Status Reg (RAF) Receiver Active Flag

SC0DRH    equ          $C6               ; Data Register High
SC0DRL    equ          $C7           ; Data Register Low

; ATD Threshold
ATDTHRESH       equ              %11110000

COPCTL    equ $16            ;COP Control

; Interupt constants
irqmark equ     %11100000
intcr   equ     $001e

; PWM CH 11
PWCLK   equ            $0040
PWPOL   equ            $0041
PWEN    equ            $0042
PWPRES equ             $0043

                                                                               18
PWSCAL0 equ     $0044
PWSCNT0 equ     $0045
PWSCAL1 equ     $0046
PWSCNT1 equ     $0047
PWCNT0 equ      $0048
PWCNT1 equ      $0049
PWCNT2 equ      $004A
PWCNT3 equ      $004B
PWPER0 equ      $004C
PWPER1 equ      $004D
PWPER2 equ      $004E
PWPER3 equ      $004F
PWDTY0 equ      $0050
PWDTY1 equ      $0051
PWDTY2 equ      $0052
PWDTY3 equ      $0053
PWCTL   equ     $0054
PWTST   equ     $0055
PORTPP equ      $0056
PORTPD equ      $0057
; Timer CH 12
TIOS    equ     $0080
CFORC   equ     $0081
OC7M    equ     $0082
OC7D    equ     $0083
TCNT0   equ     $0084
TCNT1   equ     $0085
TSCR    equ     $0086
TQCR    equ     $0087
TCTL1   equ     $0088
TCTL2   equ     $0089
TCTL3   equ     $008A
TCTL4   equ     $008B
TMSK1   equ     $008C
TMSK2   equ     $008D
TFLG1   equ     $008E
TFLG2   equ     $008F
TC0A    equ     $0090
TC0B    equ     $0091
TC1A    equ     $0092
TC1B    equ     $0093
TC2A    equ     $0094
TC2B    equ     $0095
TC3A    equ     $0096
TC3B    equ     $0097
TC4A    equ     $0098
TC4B    equ     $0099
TC5A    equ     $009A
TC5B    equ     $009B
TC6A    equ     $009C
TC6B    equ     $009D
TC7A    equ     $009E
TC7B    equ     $009F
PACTL   equ     $00A0
PAFLG   equ     $00A1
PACNT0 equ      $00A2
PACNT1 equ      $00A3
TIMTST equ      $00AD
PORTT   equ     $00AE
DDRT    equ     $00AF
; ================================
; Set Interrupt Vector
                                     19
; ================================
;       ORG $FFEE
        ORG $0B2E
        dw   IntTimer

; ================================
; Initialize Microcontroller
; ================================
        ORG $0800

InitMicro:
        ; ==============================
        ; Setup the HC12's Stack Pointer
        ; ==============================
        lds #$0A00

        ; =================================================================
        ; The 68HC12, unlike the 68HC11, defaults with the COP turned on.
        ; The COP is a timer function that requires the software to
        ; periodically write a specific pair of values to a register. If
        ; this isn't done in time, the processor will reset every 1.04
        ; seconds or so.
        ;
        ; For this module, the COP is going to be disabled. The following
        ; line disables the COP by setting the COP Watchdog Rate to zero
        ; =================================================================
        clr     COPCTL          ; Store zero in COP Control Register

        ; ===============================
        ; Enable A/D Ports
        ; ===============================
        ldaa #%10000000
        staa ATDCTL2
        ldaa #%00000000
        staa ATDCTL3
        ldaa #%00000001
        staa ATDCTL4

        ; Select Channel 7
        ldaa #%01110111
        staa ATDCTL5
        ; ===================
        ; Init Interrupt Vars
        ; ===================
        ; Disable Sending
        clr bBitsLeft
        clr bTimerCount
        ; Enable Receiving

        ; ==============================
        ; Setup the Pulse Width Modulator
        ; ==============================
        ldaa #%00000000         ; 8MHz
        staa PWCLK
        ldaa #%00000000
        staa PWPOL
        ldaa #%00000000
        staa PWCTL
        ldaa #%00000001
        staa PWEN
                                ; 125KHz 3F, 1F
        ldaa #$00               ; 8us = (1/CLK)*(PWPER+1)
                                                                              20
        ;ldaa #$3F              ; Period = 3Fh
        staa PWPER0

        ldaa #$1F               ; Duty Cycle 50% = [(PWPER-PWDUTY)/(PWPER+1)]*100
        staa PWDTY0

        ; ==============================
        ; Setup Timer
        ; ==============================
        ldaa #%11111111         ; Output Compare
        staa TIOS

        ldaa #%10000000         ; Timer System Control Reg
        staa TSCR

        ldaa #%01010101
        staa TCTL1              ; Toggle Output Line
        staa TCTL2
        ;staa TCTL3             ; Rising Edges
        ;staa TCTL4

        ldaa #$00               ; Timer Count 0040
        staa TC7A
        staa TC0A

        ldaa #$20               ; Low Compare Value
        staa TC7B
        staa TC0B

        ldaa   #%00000001       ; Enable Timer Interrupts
        staa   TMSK1
        ldaa   #%00001000       ; Reset TCNT when time hits
        staa   TMSK2

        cli

        ; ==============================
        ; Setup Port Directions
        ;       Input = 0
        ;       Ouput = 1
        ; ==============================
        ldaa #%11111111
        staa DDRA

        ldaa #%11111111
        staa PORTA              ;; Select 11

Main:
        ; ==============================
        ; Generate a Data Packet
        ; ==============================
        ldaa bBitsLeft
        adda bTimerCount
        tsta
        bne Main
        ldab PORTAD7             ;; Get Data from A/D port 7
        jsr CreatePacket

        bra    Main

; ================================================;
; CreatePacket                                    ;
                                                                               21
; input:     Register B contains Data to send     ;
; purpose: To write the packet info into memory ;
;            for the Interrupt to send            ;
; ================================================;
CreatePacket
        ; Store Packet in MEM
        ldx #bPacketStart

       ldaa   #$04      ;   Start Code 1111 1110
       staa   1,X+      ;   1
       staa   1,X+      ;   1
       staa   1,X+      ;   1
       staa   1,X+      ;   1
       staa   1,X+      ;   1
       staa   1,X+      ;   1
       staa   1,X+      ;   1
       ldaa   #$02
       staa   1,X+      ; 0
       ldaa   #$02      ; House/App Code 0111 0111
       staa   1,X+      ; 0
       ldaa   #$04
       staa   1,X+      ; 1
       ldaa   #$04
       staa   1,X+      ; 1
       ldaa   #$04
       staa   1,X+      ; 1
       ldaa   #$02
       staa   1,X+      ; 0
       ldaa   #$04
       staa   1,X+      ; 1
       ldaa   #$04
       staa   1,X+      ; 1
       ldaa   #$04
       staa   1,X+      ; 1
       ldaa   #$02
       staa   1,X+      ; 0
       ldaa   #$02
       staa   1,X+      ; 0
       clra             ; Calc Bit 0
       aba
       anda   #$01
       lsrb
       rola
       inca
       staa   1,X+
       clra             ; Calc Bit 1
       aba
       anda   #$01
       lsrb
       rola
       inca
       staa   1,X+
       clra             ; Calc Bit 2
       aba
       anda   #$01
       lsrb
       rola
       inca
       staa   1,X+
       clra             ; Calc Bit 3
       aba
       anda   #$01
                                                      22
        lsrb
        rola
        inca
        staa 1,X+
        clra            ; Calc Bit 4
        aba
        anda #$01
        lsrb
        rola
        inca
        staa 1,X+
        clra            ; Calc Bit 5
        aba
        anda #$01
        lsrb
        rola
        inca
        staa 1,X+
        ; Disable Receive

        ; Init Send vars
        clr bPointer
        clr bTimerCount

        ; Start Send.
        ldaa #24
        staa bBitsLeft

         rts
; ================================================;
; TRANSMIT                                          ;
; input:      Register A contains Data to send      ;
; purpose: To transmit the character in A to the ;
;             PC via the serial port                ;
; ================================================;
TRANSMIT
         ldab SC0SR1
         andb #$40
         beq TRANSMIT
         staa SC0DRL
         rts
; ================================================;
; IntTimer                                          ;
; purpose: To transmit the data packet              ;
; ================================================;
IntTimer
         ldaa bBitsLeft           ; if (bBitsLeft == 0) then
         adda bTimerCount
         tsta                     ;   No Send
         beq ACK

        ldaa bTimerCount         ; if TimerCount == 0
        tsta                     ; Get Bit
        beq GetBit               ; TimerCount = 8

        ;;       GotBit:
        dec   bTimerCount        ; bTimerCount --;

        ldaa bPulseNum           ; if (PulseNum == 0) Then
        tsta                     ; Disable Pulse
        bne DecPulse

                                                               23
          ; ==============
          ; Turn Pulse oFF
          ; ==============
          ; Print off
          ;ldaa #$30
          ;jsr TRANSMIT

          ldaa #$00               ; 8us = (1/CLK)*(PWPER+1)
          staa PWPER0
          staa PWCNT0

          bra    ACK

GetBit:
          ldaa #$07
          staa bTimerCount

          dec    bBitsLeft

          ; Get Bit to Send

          ldx    #bPacketStart
          ldab   bPointer
          abx
          ldaa   0,X
          inc    bPointer
          staa   bPulseNum

          ; ==============
          ; turn Pulse On
          ; ==============
          ;ldaa #10
          ;jsr TRANSMIT
          ;ldaa #13
          ;jsr TRANSMIT

        ldaa #$3F                 ; 8us = (1/CLK)*(PWPER+1)
        staa PWPER0
        ldaa #$00
        staa PWCNT0
DecPulse:
        ; Print ON
        ;ldaa #$31
        ;jsr TRANSMIT

          dec    bPulseNum        ; PulseNum--

ACK:
          ; ACK Int
          ldaa #%00000001
          staa TFLG1
          rti


; =================
; Define Variables
; =================
bPacketStart    equ          *
bPacket         rmb          24
bPacketEnd      equ          *

bTimerCount            rmb   1    ; 0-7
                                                              24
bPulseNum   rmb   1   ; 1-4
bPointer    rmb   1   ; bPacket+#
bBitsLeft   rmb   1   ; 31-0




                                    25
APPENDIX B
; [Receiver]
; Smart Appliance (Group 4)
; uController Code version 0.50
; November 25, 2001
; ================================
; Purpose: This program receives data packets
;           on TC6.
; ================================


; ================================
; Define Constants
; ================================
PORTA   equ $00     ;Port A Data
PORTB   equ $01     ;Port B Data
PORTE   equ $08     ;Port E Data
DDRA    equ $02     ;Port A Data Direction
DDRB    equ $03     ;Port B Data Direction
DDRE    equ $09     ;Port B Data Direction

ATDCTL2   equ   $062             ; A/D CTL
ATDCTL3   equ   $063
ATDCTL4   equ   $064
ATDCTL5   equ   $065

PORTAD0   equ   $70              ; A/D Ports 0 through 7
PORTAD1   equ   $72
PORTAD2   equ   $74
PORTAD3   equ   $76
PORTAD4   equ   $78
PORTAD5   equ   $7A
PORTAD6   equ   $7C
PORTAD7   equ   $7E

SC0BDH    equ   $C0              ; Baud Reg High
SC0BDL    equ   $C1          ; Baud Reg Low
SC0CR1    equ   $C2          ; Control Reg 1
SC0CR2    equ   $C3          ; Control Reg 2

SC0SR1    equ $C4                ; Status Reg
SC0SR2    equ     $C5                ; Status Reg (RAF) Receiver Active Flag

SC0DRH    equ          $C6               ; Data Register High
SC0DRL    equ          $C7           ; Data Register Low

COPCTL    equ $16            ;COP Control

; PWM CH 11
PWCLK   equ            $0040
PWPOL   equ            $0041
PWEN    equ            $0042
PWPRES equ             $0043
PWSCAL0 equ            $0044
PWSCNT0 equ            $0045
PWSCAL1 equ            $0046
PWSCNT1 equ            $0047
PWCNT0 equ             $0048

PWCNT1    equ          $0049

                                                                               26
PWCNT2    equ     $004A
PWCNT3    equ     $004B

PWPER0    equ     $004C
PWPER1    equ     $004D
PWPER2    equ     $004E
PWPER3    equ     $004F

PWDTY0    equ     $0050
PWDTY1    equ     $0051
PWDTY2    equ     $0052
PWDTY3    equ     $0053

PWCTL     equ     $0054
PWTST     equ     $0055

PORTPP    equ     $0056
PORTPD    equ     $0057

; Timer   CH 12
TIOS      equ     $0080
CFORC     equ     $0081
OC7M      equ     $0082
OC7D      equ     $0083

TCNTH     equ     $0084
TCNTL     equ     $0085

TSCR      equ     $0086

TQCR      equ     $0087
TCTL1     equ     $0088
TCTL2     equ     $0089
TCTL3     equ     $008A
TCTL4     equ     $008B

TMSK1     equ     $008C
TMSK2     equ     $008D

TFLG1     equ     $008E
TFLG2     equ     $008F

TC0A      equ     $0090
TC0B      equ     $0091

TC1A      equ     $0092
TC1B      equ     $0093

TC2A      equ     $0094
TC2B      equ     $0095

TC3A      equ     $0096
TC3B      equ     $0097

TC4A      equ     $0098
TC4B      equ     $0099

TC5A      equ     $009A
TC5B      equ     $009B

TC6A      equ     $009C
TC6B      equ     $009D
                          27
TC7A     equ      $009E
TC7B     equ      $009F

PACTL    equ      $00A0

PAFLG    equ      $00A1

PACNT0   equ      $00A2
PACNT1   equ      $00A3

TIMTST   equ      $00AD

PORTT    equ      $00AE
DDRT     equ      $00AF

; ================================
; Set Interrupt Vector
; ================================
;       ORG $FFE4
        ORG $0b24
        dw   Int125KHzTimer

;        ORG   $FFE2
         ORG   $0b22
         dw    IntPCounter
;        dw    Int125KHzTimer

; ================================
; Initialize Microcontroller
; ================================
        ORG $0800

InitMicro:
        ; ==============================
        ; Setup the HC12's Stack Pointer
        ; ==============================
        lds #$0A00

         ; =================================================================
         ; The 68HC12, unlike the 68HC11, defaults with the COP turned on.
         ; The COP is a timer function that requires the software to
         ; periodically write a specific pair of values to a register. If
         ; this isn't done in time, the processor will reset every 1.04
         ; seconds or so.
         ;
         ; For this module, the COP is going to be disabled. The following
         ; line disables the COP by setting the COP Watchdog Rate to zero
         ; =================================================================
         clr     COPCTL          ; Store zero in COP Control Register

         ; ==============================
         ; Init Variables
         ; ==============================
         clr btLastPulse
         clr bPulseCount
         clr bCurrentByte
         clr bCurrentByte1
         clr bCurrentByte2

         ; ==============================
         ; Setup Timer
                                                                               28
        ; ==============================
        ldaa #%10111111         ; Output Compare
        staa TIOS

        ldaa #%10000000         ; Timer System Control Reg
        staa TSCR

        ldaa   #%01010101
        staa   TCTL1            ; Toggle Output Line
        staa   TCTL2
        staa   TCTL3            ; Rising Edges
        staa   TCTL4

        ldaa #$00               ; Timer Count 0040
        staa TC7A
        staa TC5A
        ;staa TC0A

        ldaa #$FF               ; Low Compare Value
        staa TC7B
        staa TC5B
        ;ldaa #$20
        ;staa TC0B

        ldaa #%01100000         ; Enable Timer Interrupts
        staa TMSK1

        ldaa #%00001000         ; TC7 Resets TCNT
        staa TMSK2

        cli

        ; ==============================
        ; Setup Port Directions
        ;       Input = 0
        ;       Ouput = 1
        ; ==============================
        ldaa #%11111111
        staa DDRA
        staa DDRB

Main:
         bra Main
; ================================================;
; TRANSMIT                                        ;
; input:      Register A contains Data to send    ;
; purpose: To transmit the character in A to the ;
;             PC via the serial port              ;
; ================================================;
TRANSMIT
         ldab SC0SR1
         andb #$40
         beq TRANSMIT
         staa SC0DRL
         rts
; =======================================;
; Int125KHzTimer                           ;
; purpose: To save the bit received.       ;
; =======================================;
Int125KHzTimer:
         psha
         pshb
                                                             29
           ldaa TFLG1
           anda #%01000000
           bne PCount

EQUAL
           ;; ==================
           ;; Equal Case
           ;; ==================
           ldaa bPulseCount
           tsta
           beq ACK125       ; No Pulse

           clr    bPulseCount

           clrb
           cmpa #$04
           blo ZeroBit

           ldab #$FF

ZeroBit:
           stab   PORTA
           lsrb
           rol    bCurrentByte
           rol    bCurrentByte1
           rol    bCurrentByte2

           ;; ====================================
           ;; Searching for Start Code
           ;; ====================================
           ldaa bCurrentByte2
           cmpa #%11111110
           bne ACK125
           ;; Start Code found / Data Packet Received

           ;; ======================================
           ;; Output Data to Port B LEDs

           ldaa bCurrentByte
           staa PORTB
           ;jsr TRANSMIT

           ;; ======================================
           ;; Output House+App Code to RS-232 Port
           ;; This prints a cute "w"
           ;; ======================================
           ldaa bCurrentByte1
           jsr TRANSMIT

ACK125:
           ldaa #$00
           staa PORTA

           ; ACK Int
           ldab #%00100000
           stab TFLG1
           pulb
           pula
           rti

; ===========================================================;
                                                                 30
; IntPCounter                                                ;
; purpose: To count up the number of pulses (rising edges). ;
; ===========================================================;
IntPCounter:
        psha
        pshb

PCount:
          inc   bPulseCount       ; pulses++;

          ldaa TCNTL
          adda #$C0               ; set 3 cycle delay
          staa TC5B

ACK:
          ldaa #$FF
          staa PORTA

          ; ACK Int
          ldab #%01100000
          stab TFLG1
          pulb
          pula
          rti

; ================================
; Define Variables (16 bytes Reserved)
; ================================
;bPacketStart   equ     *
;bPacket                rmb     24
;bPacketEnd     equ     *

btLastPulse        rmb        1
bPulseCount        rmb        1

bCurrentByte       rmb        1
bCurrentByte1      rmb        1
bCurrentByte2      rmb        1




                                                                 31

								
To top