Principle and Interface Techniques of Microcontroller: - 8051 Microcontroller and Embedded Systems Using Assembly and C
Principle and Interface Techniques of Microcontroller: - 8051 Microcontroller and Embedded Systems Using Assembly and C
Techniques of Microcontroller
--8051 Microcontroller and Embedded Systems
Using Assembly and C
杭州 • 浙江大学 • 2015
Chapter 13
Real-world Interfacing
LCD, ADC, and DAC
Outline
§13-1 LCD and Keyboard Interfacing
§13-2 Interfacing to ADC
§13-3 Interfacing to DAC
- Send displayed
information or
instruction command
codes to the LCD
- Read the contents of
the LCD’s internal
registers
LCD Command Codes
Sending Codes and Data to LCDs w/ Time Delay
To send any of the commands to the LCD, make pin RS=0. For data,
make RS=1. Then send a high-to-low pulse to the E pin to enable the
internal latch of the LCD. This is shown in the code below.
COMMAND:
ACALL READY ;is LCD ready?
MOV P1,A ;issue command code
CLR P2.0 ;RS=0 for command
CLR P2.1 ;R/W=0 to write to LCD
SETB P2.2 ;E=1 for H-to-L pulse
CLR P2.2 ;E=0,latch in
RET
DATA_DISPLAY:
ACALL READY ;is LCD ready?
MOV P1,A ;issue data
SETB P2.0 ;RS=1 for data To read the command
CLR P2.1 ;R/W =0 to write to LCD register, we make R/W=1,
SETB P2.2 ;E=1 for H-to-L pulse
RS=0, and a H-to-L pulse
CLR P2.2 ;E=0,latch in
RET
for the E pin.
READY:
SETB P1.7 ;make P1.7 input port
CLR P2.0 ;RS=0 access command reg
If bit 7 (busy flag) is
SETB P2.1 ;R/W=1 read command reg high, the LCD is busy
and no information
;read command reg and check busy flag
should be issued to it.
BACK: SETB P2.2 ;E=1 for H-to-L pulse
CLR P2.2 ;E=0 H-to-L pulse
JB P1.7, BACK ;stay until busy flag=0
RET
END
One can put data at any location in the LCD and the
following shows address locations and how they are
accessed
压频变换型
由计数器、控制门及一个具有恒定时间的时钟
门控制信号组成,把输入的模拟电压转换成与
模拟电压成正比的脉冲信号。
ADC0804 Chip
ADC0804 IC is an analog-to-digital converter
It works with +5 volts and has a resolution of 8
bits
Conversion time is another major factor in
judging an ADC
Conversion time is defined as the time it takes the
ADC to convert the analog input to a digital (binary)
number
In ADC804 conversion time varies depending on the
1)中断方式
2) 查询方式
3)延时等待方式
§ 13.2.3 A/D转换与接口技术
⑴ 中断方式
用中断方式对8路模拟信号依次A/D转换一次,并把结果存入以30H为
首址的内RAM中,试编制程序。
ORG 0000H
LJMP STAT
ORG 0013H ;中断服务子程序入口地址
LJMP PINT1
ORG 0100H ;初始化程序首地址
STAT: MOV R1,#30H ;置数据区首址
MOV R7,#8 ;置转换通道数
SETB IT1 ;置边沿触发方式
SETB EX1 ;开外中断
SETB EA ;CPU开中断
MOV DPTR,#07FF8H ;置0809通道0地址
MOVX @DPTR,A ;启动0通道A/D
SJMP $ ;等待A/D中断
§ 13.2.3 A/D转换与接口技术
ORG 0200H
PINT1: PUSH ACC ;保护现场
PUSH PSW
MOVX A,@DPTR ;读A/D值
MOV @R1,A
INC DPTR ;修正通道地址
INC R1 ;修正数据区地址
MOVX @DPTR,A ;启动下一通道A/D
DJNZ R7,GORETI ;判8路采集完否?
CLR EX1 ;8路采集已完,关中断
GORETI: POP PSW ;恢复现场
POP ACC
RETI ;中断返回
§ 13.2.3 A/D转换与接口技术
⑵ 查询方式
工作在查询方式时,0809 EOC端可直接与80C51 P1口或P3口中任一端线相
连。设用P1.0直接与0809 EOC端相连,试用查询方式编制程序,对8路模拟信
号依次A/D转换一次,并把结果存入以40H为首址的内RAM中。
MAIN: MOV R1,#40H ;置数据区首址
MOV R7,#8 ;置通道数
SETB P1.0 ;置P1.0输入态
MOV DPTR,#07FF8H ;置0809通道0地址
LOOP: MOVX @DPTR,A ;启动A/D
JNB P1.0,$ ;查询A/D转换结束否?未完继续查询等待
MOVX A,@DPTR ;A/D已结束,读A/D值
MOV @R1,A ;存A/D值
INC DPTR ;修改通道地址
INC R1 ;修改数据区地址
DJNZ R7,LOOP ;判8路采集完否?未完继续
RET ;8路采集完毕,返回
§ 13.2.3 A/D转换与接口技术
⑶ 延时等待方式
工作在延时等待方式时,0809 EOC端可不必与80C51相连,是根据时钟频
率计算出A/D转换时间,略微延长后直接读A/D转换值。0809 EOC端开路,
fosc=6MHz,试用延时等待方式编制程序,对8路模拟信号依次A/D转换一
次,并把结果存入以50H为首址的内RAM中。
MAIN: MOV R1,#50H ;置数据区首址
MOV R7,#8 ;置通道数
MOV DPTR,#07FF8H;置0809通道0地址
LOOP: MOVX @DPTR,A ;启动A/D
MOV R6,#50
DJNZ R6,$ ;延时100µS:2µS×50=100µS
MOVX A,@DPTR ;读A/D值
MOV @R1,A
INC DPTR ;修正通道地址
INC R1 ;修正数据区地址
DJNZ R7,LOOP ;判8路采集完否?未完继续
RET ;8路采集完毕,返回
Modern MCU ADC
MSP430F4xx ADC structure
ADC program
//***********************************************************
// 功能:ADC12采样
// 入口:uchar i:ADC通道(1----12)
// 出口:uint ADC12MEM:AD采样值(12bit)
// 说明:1、使用外部参考电源Vr+=VeRef+,Vr-=AVss;
// 2、单通道单次转换模式;
// 3、单次转换地址为ADC12MEM0
// 4、采用ACLK时钟
//***********************************************************
ADC program (2)
uint ADC_sample( uchar i )
{
uint ADC_result;
ADC12CTL0 &= ~ENC;
ADC12CTL0 = ADC12ON + SHT0_2; //打开ADC12内核,设置采样周期4*16*t(aclk)
//定义ADC12MEM0为单次转换地址;采样信号来自采样定时器;单通道单次转换模式;内核
时钟源为MCLK
ADC12CTL1 = CSTARTADD_0 + SHP + CONSEQ_0 + ADC12SSEL_2;
ADC12MCTL0 = (i) + SREF_2 + EOS; //选择第i通道,参考电源Vr+=Veref+,Vr-=AVss;
ADC12CTL0 |= ENC + ADC12SC; //开始转换
while ( ( ADC12CTL1 & ADC12BUSY ) == 1 ); //ADC12BUSY?
ADC12CTL0 &= ~ENC;
ADC12CTL0 &= ~ADC12ON; //关闭ADC内核电源
ADC_result = ADCMEM[0]; //将ADC12MEMx给Result
_NOP();
return ADC_result;
}
§13-3 Interfacing to DAC
Digital-to-analog convert
0001 0010
Analog Signal
ideal
actual
DAC Devices
Figure 13-1
For Figure 13-1
Output saw tooth wave as following, amplitude
UREF/2=2.5V
7 machine cycles
5 machine cycles