ECE 4510/5530 Microcontroller Applications Week 10bazuinb/ECE4510/Week10_3.pdfECE 4510/5530...
Transcript of ECE 4510/5530 Microcontroller Applications Week 10bazuinb/ECE4510/Week10_3.pdfECE 4510/5530...
ECE 4510/5530Microcontroller Applications
Week 10
Dr. Bradley J. BazuinAssociate Professor
Department of Electrical and Computer EngineeringCollege of Engineering and Applied Sciences
ECE 2510 2
Lab 9 & 10 Elements
• Inter-Integrated Circuit (I2C) Interface– SCL and SDA connections
• Calendar– MCP7940M-I/P Real-Time Clock/Calendar
• PING
Lab 8 Note: ADC Dynamic Range
ECE 4510/5530
3
The True Range of and ADC
• ADC values are readily defined for and N-bit converter
• But what is really being measured?– Voltage guard bands!– you don’t want to go all the way to 0.0V or 2.5V– Example: Temperature – indoor/outdoor -30°C to + 50 °C or -22 °F to 122 °F– With our device, the measurement range is -0.3V to + 0.5V
• Unipolar ADC range: decimal 0 to 13,107 or hex 0x0000 to 0x3333• Bipolar ADC range: decimal 28,836 to 39,322 or hex 0x70A4 to 0x999A• We have lost over two bits of measurement precision!• Only 20% of the measurement range is being used!ECE
4510/55304
NUnipolarAInV
2DC_integer5.2_ 12
DC_integer5.2_ NBipolarAInV
Tailoring ADCs to tasks: Options
1) Adjust the internal gain. – The device we are using could use an internal gain of 4. – The new measurement range would be -0.625 to 0.625 V
2) Provide external gain.– Provide an analog op amp circuit so that the signal range nearly
fills the ADC range (you still may only use 80% of the total)
3) Use a different Vref for the ADC– A possible solution, but normally applying gain is preferred.– The “noise” in the ADC has a defined voltage level. Measuring
signals smaller than the noise voltage is not a good idea.
ECE 4510/5530
5
Analog sensor signals are often buffered, had gain applied, filtered, or otherwise preconditioned before the ADC.
DAC outputs may also be “conditioned”.
Lab 9: Real-Time Clock/Calendar
ECE 4510/5530
6
I2C Real-Time Clock/Calendar MCP7940M
Features• Real-Time Clock/Calendar (RTCC):
– Hours, Minutes, Seconds, Day of Week, Day, Month and Year
– Dual alarm with single output
• On-Chip Digital Trimming/Calibration:– Range -127 to +127 ppm, Resolution 1 ppm
• Programmable Open-Drain Output Control:– CLKOUT with 4 selectable frequencies– Alarm output
• 64 Bytes SRAM• Low-Power CMOS Technology:
– Dynamic Current: 400 �A max write
• 100 kHz and 400 kHz Compatibility• ESD Protection >4,000V• Packages include 8-Lead SOIC, TSSOP,
2x3 TDFN, MSOP and PDIP• Temperature Ranges:
– Industrial (I): -40°C to +85°C
Typical Application Schematic
MCP7940M Address Map Detail
Same as textbook
Matches clock
structure
Matches clock
structure
Extra bits
MCP7940M Control Register
• Bit 7 is the OUT bit. This sets the logic level on the MFP when not using this as a square wave output. (prefer high for potential interrupt signals = 1)
• Bit 6 is the SQWE bit. Setting this bit enables the divided output from the crystal oscillator.(we wish to use the signal as an alarm interrupt = 0)
• Bits 5:4 determine which alarms are active. (set according to requirement)– 00 – No Alarms are active– 01 – Alarm 0 is active– 10 – Alarm 1 is active– 11 – Both Alarms are active
• Bit 3 is the EXTOSC enable bit. Setting this bit will allow an external 32.768 kHz signal to drive the RTCC registers, eliminating the need for an external crystal. (set to 0)
• Bit 2:0 sets the internal divider for the 32.768 kHz oscillator to be driven to the MFP. The duty cycle is 50%. The output is responsive to the Calibration register. The following frequencies are available: (typically 000)
– 000 – 1 Hz– 001 – 4.096 kHz– 010 – 8.192 kHz– 011 – 32.768 kHz– 1xx enables the Cal output function. Cal output appears on MFP if SQWE is set (64 Hz Nominal). See Section
4.2.1 “Calibration” for more details.
Initial test: 0x40 (1 Hz output)Alarms: 0x80, 0x90, 0xA0 or 0xB0(high output going low alarm condition)
MCP7940M Alarms
• 0x07h – Is the Control register.– Bits 5:4 determine which alarms are active.
• 00 – No Alarms are active• 01 – Alarm 0 is active• 10 – Alarm 1 is active• 11 – Both Alarms are active
• Locations 0x0Dh and 0x14h have additional bits for alarm configuration.– ALMxPOL: This bit specifies the level that the MFP will drive when the alarm is
triggered. • ALM2POL is a copy of ALM1POL. The default state of the MFP when used for alarms is
the inverse of ALM1POL.– ALMxIF: This is the Alarm Interrupt Fag. This bit is set in hardware if the alarm
was triggered. The bit is cleared in software.– ALMxC2:0: These Configuration bits determine the alarm match. The logic will trigger the
alarm based on one of the following match conditions• 000 – Seconds match• 001 – Minutes match• 010 – Hours match (takes into account 12/24 hour)• 011 – Matches the current day, interrupt at 12.00.00 a.m.• 100 – Date• 111 – Seconds, Minutes, Hour, Day, Date, Month
ALMxPOL = 0ALMxC2 = 111
MCP7940M I2C Data Transfer
• MCP7940M supports both 100 kHz and 400 kHz data transfer.
• The device address (ID) of the MCP7940M is 1101111.
Lalith’s Example Code
• I2C routines– i2c.h and i2c.c– // Function prototypes
• int I2Cinit (char baud_divisor, char dev_id);
• int I2Cstart (char);• int I2Crestart (char);• int I2Cwritec (char data);• int I2Cwritestr (char *,int);• char I2Creadc_ack (void);• char I2Creadc_nack (void);• int I2Creadstr (char *,int);
– // uses “special definitions” for• I2C_STOP IBCR • I2C_TX_ACK IBCR • I2C_TX_NACK IBCR • I2C_ACK_CHK(x)
• Real Time Clock/Calendar– rtic.h and rtic.c– // Function prototypes
• int RTICinit (void);• int RTICwrite (char,char);• char RTICread (char);• int RTICpwm (void);
– //global declaration externs
Day of Week from Month/Day/Year
• The Day of the Week can be determined based on knowing the day, month and year.
• Microchip Application Note AN1303– “Software Real-Time Clock and Calendar Using PIC16F1827”– http://ww1.microchip.com/downloads/en/AppNotes/01303A.pdf
• Add a century offset (6 for 2000-2099)• Add the two digits of the year (13 for 2013)• Add the number of leap years since 2000 ( int(13/4) = 3)• Add a month offset based on a table of values (March = 3)• Add the Date (22 March 2013)• Take the modulo 7 of the sum (6 + 13 + 3 + 3 + 22 = 47 5)• With Sunday as 0 compute the day …. (5 Friday)
ECE 4510/5530
14
Lab 10: see PING Notes
ECE 4510/5530
15